Note that there are some explanatory texts on larger screens.

plurals
  1. POAllegro 5 C++ Program crashing related to image
    text
    copied!<p>I am building a side shooter by following some tutorials online in order to get used to Allegro 5. The guy who made the tutorials does not use classes for the sake of "simplicity" even though in the end the code is a mess. He uses structs and then functions within the main cc file instead of including member functions. The only thing I really differed on in this regard was instead of an initialize function in the main file, I use a constructor for the structs.</p> <p>When I drew with primitives the program compiled and ran fine. I then added an image and some basic sprite animation for the SpaceShip struct and it also worked fine. The issue came with adding an image to the Comet struct. With the SpaceShip struct I could include the bitmap pointer as a parameter to the constructor, but I cannot do that with Comet because I declare an array of them and you cannot pass parameters while declaring arrays.</p> <pre><code>#define ALLEGRO_STATICLINK #include &lt;allegro5/allegro.h&gt; #include &lt;allegro5/allegro_primitives.h&gt; #include &lt;allegro5/allegro_font.h&gt; #include &lt;allegro5/allegro_ttf.h&gt; #include &lt;allegro5/allegro_image.h&gt; #include &lt;allegro5/allegro_native_dialog.h&gt; #include "objects.h" //GLOBALS enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE}; bool keys[5] = {false, false, false, false}; //Function Prototypes void DrawShip(SpaceShip &amp;ship); void DrawBullet(Bullet bullet[], int size); void FireBullet(Bullet bullet[], int size, SpaceShip &amp;ship); void UpdateBullet(Bullet bullet[], int size); void DrawComet(Comet comets[], int size); void StartComet(Comet comets[], int size); void UpdateComet(Comet comets[], int size); void CollideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize, SpaceShip &amp;ship); void CollideComet(Comet comets[], int cSize, SpaceShip &amp;ship); void RestartGame(SpaceShip &amp;ship, Comet comets[], Bullet bullet[], bool &amp;GameOver); int main(void) { //Primitive Variables bool done = false;//Ends Game Loop bool redraw = true;//Allows for only rendering on framerate bool isGameOver = false;//Game state indicator const int FPS = 60; //Allegro Variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; ALLEGRO_BITMAP *shipImage; ALLEGRO_BITMAP *cometImage; //Object Variables Bullet bullets[NUM_BULLETS]; //Initialize Functions if(!al_init()) { return -1; } display = al_create_display(width, height); if(!display) { return -1; } al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); shipImage = al_load_bitmap("STRIP-1.png");//Load ship image and call ship constructor SpaceShip ship(shipImage); cometImage = al_load_bitmap("asteroid.png");//Load comet image and call comet constructor Comet comets[NUM_COMETS]; //Make array of comets for(int i; i &lt; NUM_COMETS; i++) { comets[i].image = cometImage; }//Assign pictures to comets event_queue = al_create_event_queue();//Create Queue timer = al_create_timer(1.0/FPS);//Create timer al_convert_mask_to_alpha(shipImage, al_map_rgb(255,255,255));//Trans Ship al_convert_mask_to_alpha(cometImage, al_map_rgb(0,0,0));//Trans Comet srand(time(NULL)); //Seed based on time for generator font18 = al_load_font("arial.ttf", 18, 0); //Register Event Sources al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); //Start Timer al_start_timer(timer); //Game Loop while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &amp;ev); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; if(keys[UP]) ship.MoveShipUp(); else if(keys[DOWN]) ship.MoveShipDown(); else ship.ResetShip(); if(keys[LEFT]) ship.MoveShipLeft(); else if(keys[RIGHT]) ship.MoveShipRight(); if(!isGameOver) { UpdateBullet(bullets, NUM_BULLETS); StartComet(comets, NUM_COMETS); UpdateComet(comets, NUM_COMETS); CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship); CollideComet(comets, NUM_COMETS, ship); if(ship.lives &lt;= 0) isGameOver = true; }//Only handle enemies and bullets while in game state } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; FireBullet(bullets, NUM_BULLETS, ship); break; case ALLEGRO_KEY_ENTER: if(isGameOver) RestartGame(ship, comets, bullets, isGameOver); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if(redraw &amp;&amp; al_is_event_queue_empty(event_queue)) { redraw = false; if(!isGameOver) { DrawShip(ship); DrawBullet(bullets, NUM_BULLETS); DrawComet(comets, NUM_COMETS); al_draw_textf(font18, al_map_rgb(255,255,255), 5, 5, 0, "Lives: %i Score: %i", ship.lives, ship.score); } else { al_draw_textf(font18, al_map_rgb(0,255,255), width/2, height/2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score is %i Press Enter to Play Again", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_bitmap(cometImage); al_destroy_bitmap(shipImage); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_font(font18); al_destroy_display(display); return 0; } //End main </code></pre> <p>^That is my main and the various declarations, the following is the draw function for the comet in which, if I remove the part that actually draws the image, the program runs fine and just draws the magenta boxes instead.</p> <pre><code>void DrawComet(Comet comets[], int size) { for(int i = 0; i &lt; size; i++) { if(comets[i].live) { int fx = (comets[i].curFrame % comets[i].animationColumns) * comets[i].frameWidth; int fy = ((comets[i].curFrame / comets[i].animationColumns) * comets[i].frameHeight) + comets[i].startRow; al_draw_bitmap_region(comets[i].image, fx, fy, comets[i].frameWidth, comets[i].frameHeight, comets[i].x - comets[i].frameWidth/2, comets[i].y - comets[i].frameHeight/2, 0); al_draw_filled_rectangle(comets[i].x - comets[i].boundx, comets[i].y - comets[i].boundy, comets[i].x + comets[i].boundx, comets[i].y + comets[i].boundy, al_map_rgba(255,0,255,100)); } } }//End DrawComet </code></pre> <p>I apologize for the length but it seems like the issue could be in any number of places. Finally, the Comet struct has the following values,</p> <pre><code>struct Comet { Comet(); int ID; int x; int y; bool live; int speed; int boundx; int boundy; int maxFrame, curFrame; int frameCount, frameDelay; int frameWidth, frameHeight; int animationColumns; int animationDirection; int startRow; ALLEGRO_BITMAP *image; }; </code></pre> <p>The structs and constructors exist in an object.h file included in the project, the main and other function is in learning_allegro.cc, the two image files exist in the same directory as all my source files and are spelled correctly. And I am using Dev C++ as my IDE. Again I apologize for the length of all of this but I have yet to find an answer to this issue in my searches.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload