Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5 - Assertion failed

This thread is locked; no one can reply to it. rss feed Print
Allegro 5 - Assertion failed
DJLad16
Member #14,857
January 2013

After leaving my game on for about 1-2 minutes, I get "-abort() has been called" and in the console window it says "Assertion failed: bitmap, file allegro-git/src/bitmap.c, line 315". I think that it may have something to do with the coin collide functions, but I'm not 100%.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_native_dialog.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_image.h> 7#include <allegro5/allegro_audio.h> 8#include<allegro5/allegro_acodec.h> 9#include <cstdlib> 10#include <ctime> 11#include "objects.h" 12 13#define ScreenWidth 800 14#define ScreenHeight 600 15 16//Prototpyes 17void initPlayer(Player &player); 18 19void initCoin(Coin &coin1); 20void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image); 21bool coinCollide(Coin &coin1,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount); 22 23void initCoin2(Coin &coin2); 24void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image); 25bool coinCollide2(Coin &coin2,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount); 26 27void initArrow(Arrow arrow[], int size); 28void drawArrow(Arrow arrow[], int size); 29void fireArrow(Arrow arrow[], int size, Player &player, ALLEGRO_SAMPLE *bowShot); 30void updateArrow(Arrow arrow[], int size); 31 32bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight); 33 34void cameraUpdate(float *cameraPosition,Player &player, float width, float height, ALLEGRO_BITMAP *bg); 35 36//Global Variables 37enum direction {LEFT, RIGHT}; 38int dir = LEFT; 39int arrowCount = 10; 40//const int NUM_ARROW = 10; 41const int NUM_ENEMY = 10; 42int groundHeight = 545; 43static bool fired = false; 44bool coinLive = false; 45 46int main() 47{ 48 srand(static_cast<unsigned int>(time(0))); 49 50 al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE); 51 ALLEGRO_DISPLAY *display; 52 53 if(!al_init()) 54 { 55 al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize allegro", NULL, NULL); 56 return -1; 57 } 58 59 display = al_create_display(ScreenWidth, ScreenHeight); 60 61 if(!display) 62 { 63 al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize the display", NULL, NULL); 64 return -1; 65 } 66 67 Player player; 68 Coin coin1; 69 Coin coin2; 70 Arrow arrow[10]; 71 bool done = false, active = false, draw = true; 72 bool jump = false; 73 int sourceX = 32, sourceY = 0; 74 int coinCount = 0; 75 const float FPS = 60.0; 76 float jumpSpeed = 16.5; 77 float velX = 0, velY = 0; 78 const float gravity = 1; 79 float cameraPosition[2] = {0,0}; 80 int cameraX = 0; 81 82 al_init_image_addon(); 83 al_init_primitives_addon(); 84 al_install_keyboard(); 85 al_init_font_addon(); 86 al_init_ttf_addon(); 87 al_install_audio(); 88 al_init_acodec_addon(); 89 90 initPlayer(player); 91 initArrow(arrow, 10); 92 initCoin(coin1); 93 initCoin2(coin2); 94 95 ALLEGRO_KEYBOARD_STATE keystate; 96 ALLEGRO_TRANSFORM camera; 97 ALLEGRO_SAMPLE *coinCollect = al_load_sample("coin collect sound.wav"); 98 ALLEGRO_SAMPLE *bowShot = al_load_sample("bow sound effect.wav"); 99 ALLEGRO_SAMPLE *bgSong = al_load_sample("Background song.wav"); 100 ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(bgSong); 101 ALLEGRO_BITMAP *character = al_load_bitmap("spritesheet(Bow & left + right).png"); 102 ALLEGRO_BITMAP *background = al_load_bitmap("ayers rock.png"); 103 ALLEGRO_BITMAP *coin = al_load_bitmap("coin2.png"); 104 ALLEGRO_BITMAP *ground = al_load_bitmap("Ground.png"); 105 ALLEGRO_BITMAP *platform = al_load_bitmap("platform(long).png"); 106 ALLEGRO_BITMAP *platform2 = al_load_bitmap("platform(short).png"); 107 ALLEGRO_FONT *numArrow = al_load_font("JUNGBN__.TTF", 23, 0); 108 ALLEGRO_FONT *numCoin = al_load_font("JUNGBN__.TTF", 23, 0); 109 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS); 110 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 111 112 al_reserve_samples(3); 113 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP); 114 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer()); 115 al_set_window_title(display, "Australian Outback"); 116 al_register_event_source(event_queue, al_get_keyboard_event_source()); 117 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 118 al_register_event_source(event_queue, al_get_display_event_source(display)); 119 120 al_play_sample_instance(songInstance); 121 122 al_start_timer(timer); 123 while(!done) 124 { 125 ALLEGRO_EVENT event; 126 al_wait_for_event(event_queue, &event); 127 al_get_keyboard_state(&keystate); 128 129 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE)) 130 { 131 done = true; 132 } 133 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 134 { 135 done = true; 136 } 137 138 139 else if(event.type == ALLEGRO_EVENT_TIMER) 140 { 141 active = true; 142 updateArrow(arrow, 10); 143 144 if(al_key_down(&keystate, ALLEGRO_KEY_D)) 145 { 146 velX = player.speed; 147 cameraX += player.speed; 148 dir = RIGHT; 149 150 } 151 else if(al_key_down(&keystate, ALLEGRO_KEY_A)) 152 { 153 velX = -player.speed; 154 cameraX += player.speed; 155 dir = LEFT; 156 } 157 else 158 { 159 velX = 0; 160 active = false; 161 } 162 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump) 163 { 164 velY = -jumpSpeed; 165 jump = false; 166 } 167 if(active) 168 { 169 sourceX += al_get_bitmap_width(character) / 3; 170 } 171 else 172 { 173 sourceX = 32; 174 } 175 if(sourceX >= al_get_bitmap_width(character)) 176 { 177 sourceX = 0; 178 } 179 sourceY = dir; 180 draw = true; 181 if(!jump) 182 velY += gravity; 183 else 184 velY = 0; 185 player.x += velX; 186 player.y += velY; 187 188 jump = (player.y >= groundHeight); 189 190 if(jump) 191 { 192 player.y = groundHeight; 193 } 194 195 cameraUpdate(cameraPosition, player, 32, 32, platform); 196 al_identity_transform(&camera); 197 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 198 al_use_transform(&camera); 199 200 201 202 } 203 204 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE)) 205 { 206 fireArrow(arrow, 10, player, bowShot); 207 } 208 else 209 { 210 fired = false; 211 } 212 if(collision(player, 360, 447, 351, 30, 32, 32)) 213 { 214 groundHeight = 435; 215 player.y = groundHeight; 216 217 } 218 else if(collision(player, 70, 377, 214, 38, 32, 32)) 219 { 220 groundHeight = 365; 221 player.y = groundHeight; 222 } 223 else 224 { 225 groundHeight = 545; 226 } 227 228 if(coinCollide(coin1, player, coin1.x, coin1.y, coin1.width, coin1.height, 32, 32, coinLive, coinCount)) 229 { 230 coin1.live = false; 231 ++coinCount; 232 coin1.x = 0; 233 coin1.y = 0; 234 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 235 } 236 if(coinCollide2(coin2, player, coin2.x, coin2.y, coin2.width, coin1.height, 32, 32, coinLive, coinCount)) 237 { 238 coin2.live = false; 239 ++coinCount; 240 coin2.x = 0; 241 coin2.y = 0; 242 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 243 } 244 245 if(draw && al_is_event_queue_empty(event_queue)) 246 { 247 draw = false; 248 al_draw_bitmap(platform, 360, 447, NULL); 249 al_draw_bitmap (platform2, 70, 377, NULL); 250 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, NULL); 251 drawArrow(arrow,10); 252 drawCoin(coin1, coin); 253 drawCoin2(coin2, coin); 254 al_draw_textf(numArrow, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrowCount); 255 al_draw_textf(numCoin, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount); 256 al_flip_display(); 257 al_clear_to_color(al_map_rgb(255, 255, 255)); 258 al_draw_bitmap(background, 0,0, 0); 259 al_draw_bitmap(ground, 0, 549, 0); 260 261 262 } 263 } 264 265 al_destroy_bitmap(character); 266 al_destroy_bitmap(ground); 267 al_destroy_bitmap(coin); 268 al_destroy_bitmap(platform); 269 al_destroy_sample(coinCollect); 270 al_destroy_sample(bowShot); 271 al_destroy_sample(bgSong); 272 al_destroy_sample_instance(songInstance); 273 al_destroy_font(numArrow); 274 al_destroy_display(display); 275 al_destroy_event_queue(event_queue); 276 al_destroy_timer(timer); 277 return 0; 278} 279void initPlayer(Player &player) 280{ 281 player.x = 10; 282 player.y = 545; 283 player.ID = PLAYER; 284 player.lives = 3; 285 player.speed = 5; 286 player.score = 0; 287} 288 289void initCoin(Coin &coin1) 290{ 291 coin1.ID = COIN; 292 coin1.width = 16; 293 coin1.height = 16; 294 coin1.live = true; 295 coin1.x = 550; 296 coin1.y = 440; 297} 298void initCoin2(Coin &coin2) 299{ 300 coin2.ID = COIN; 301 coin2.width = 16; 302 coin2.height = 16; 303 coin2.live = true; 304 coin2.x = 120; 305 coin2.y = 365; 306} 307void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image) 308{ 309 if(coin1.live) 310 { 311 al_draw_bitmap(image, coin1.x, coin1.y, NULL); 312 } 313} 314void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image) 315{ 316 if(coin2.live) 317 { 318 al_draw_bitmap(image, coin2.x, coin2.y, NULL); 319 } 320} 321bool coinCollide(Coin &coin1,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount) 322{ 323 if((player.x > cX + cWidth) || (player.y > cY + cHeight) || 324 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin1.live)) 325 { 326 return false; 327 } 328 return true; 329} 330bool coinCollide2(Coin &coin1,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount) 331{ 332 if((player.x > cX + cWidth) || (player.y > cY + cHeight) || 333 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin1.live)) 334 { 335 return false; 336 } 337 return true; 338} 339 340void initArrow(Arrow arrow[], int size) 341{ 342 for(int i = 0; i < size; i++) 343 { 344 arrow[i].ID = ARROW; 345 arrow[i].speed = 4.0; 346 arrow[i].live = false; 347 } 348} 349void drawArrow(Arrow arrow[], int size) 350{ 351 for(int i = 0; i < size; i++) 352 { 353 ALLEGRO_BITMAP *bArrow = al_load_bitmap("Arrow.png"); 354 if(arrow[i].live) 355 { 356 al_draw_bitmap(bArrow, arrow[i].x, arrow[i].y, NULL); 357 } 358 } 359} 360void fireArrow(Arrow arrow[], int size, Player &player, ALLEGRO_SAMPLE *bowShot) 361{ 362 for(int i = 0; i < size; i++) 363 { 364 if(!arrow[i].live && !fired) 365 { 366 --arrowCount; 367 --size; 368 arrow[i].live = true; 369 arrow[i].x = player.x + 17; 370 arrow[i].y = player.y + 22; 371 fired = true; 372 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); 373 } 374 } 375} 376void updateArrow(Arrow arrow[], int size) 377{ 378 for(int i = 0; i < size; i++) 379 { 380 if(arrow[i].live) 381 { 382 arrow[i].x += arrow[i].speed; 383 if(arrow[i].x >= ScreenWidth - 5 && size <= 0) 384 { 385 arrow[i].live = false; 386 } 387 } 388 } 389} 390 391bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight) 392{ 393 if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) || 394 (ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10)) 395 { 396 return false; 397 } 398 return true; 399} 400 401void cameraUpdate(float *cameraPosition, Player &player, float width, float height, ALLEGRO_BITMAP *bg) 402{ 403 cameraPosition[0] = -(al_get_bitmap_width(bg)) + (player.x + width / 2); 404 //cameraPosition[1] = -(al_get_bitmap_height(bg) / 2) + (player.y + height / 2); 405 406 if(cameraPosition[0] < 0) 407 cameraPosition[0] = 0; 408 /*if(cameraPosition[1] < 0) 409 cameraPosition[1] = 0;*/ 410 411}

Thomas Fjellstrom
Member #476
June 2000
avatar

That error typically (always?) means you are passing a pointer set to NULL to a function. Check the return values from al_load_bitmap to make sure all of your bitmaps loaded.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

DJLad16
Member #14,857
January 2013

Thanks for the feedback, but I changed the NULL to a 0 and it didn't work. I also tried ALLEGRO_FLIP_ HORIZONTAL but that didn't work either.

Thomas Fjellstrom
Member #476
June 2000
avatar

DJLad16 said:

Thanks for the feedback, but I changed the NULL to a 0 and it didn't work.

That is not what I meant. Your al_load_bitmap calls are likely failing, and returning NULL. You want to check for that as the drawing calls will assert and die if they get a NULL rather than a valid pointer to a bitmap.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

DJLad16
Member #14,857
January 2013

I think its glitching out somehow. I removed the the draw coin functions so now I just draw normally. Got the same error, still line 315, so I fill line 315 with a bunch of whitespace. Same error, same line?

torhu
Member #2,727
September 2002
avatar

Try to gently massage the computer while singing a lullaby in a soft, gentle voice. Then give it a kiss and pat the hard drive for a couple of minutes. I bet it'll stop messing up your game then ;)

Fishcake
Member #8,704
June 2007
avatar

DJLad16 said:

I think its glitching out somehow. I removed the the draw coin functions so now I just draw normally. Got the same error, still line 315, so I fill line 315 with a bunch of whitespace. Same error, same line?

That's because it's referring to the line 315 in allegro-git/src/bitmap.c, not your source code. If you open the mentioned file, line 315 should look like this:

ASSERT(bitmap);

Which means that somewhere in your code, you are passing a NULL bitmap into that particular function. If you're using a decent IDE, you should be able to look at the stack trace and identify which line in your code that is triggering the assert. Otherwise, you will have to assert that each of the bitmap you loaded is not NULL (NULL is returned if the bitmap failed to load, most probably because it's missing). For example:

ALLEGRO_BITMAP *coin = al_load_bitmap("coin2.png");
ASSERT(coin);
// etc etc..

Then you'll be able to know which variable is NULL. :)

Cassio Renan
Member #14,189
April 2012
avatar

try this:

#SelectExpand
307void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image) 308{ 309 if(coin1.live) 310 { 311 if(image == NULL) 312 al_draw_filled_circle(coin1.x - 10, coin1.y - 10, 20, al_map_rgb(255,0,255)); 313 else 314 al_draw_bitmap(image, coin1.x, coin1.y, NULL); 315 } 316} 317void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image) 318{ 319 if(coin2.live) 320 { 321 if(image == NULL) 322 al_draw_filled_circle(coin2.x - 10, coin2.y - 10, 20, al_map_rgb(255,0,255)); 323 else 324 al_draw_bitmap(image, coin2.x, coin2.y, NULL); 325 } 326}

If it draws a circle instead of your coin, your coin's ALLEGRO_BITMAP* is NULL. Try checking if the file exists, and if it is named correctly.

Thomas Fjellstrom
Member #476
June 2000
avatar

Or just check for your bitmaps being NULL right after loading them.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

DJLad16
Member #14,857
January 2013

Thanks Cassio :D I think your code, worked. I left for about 7 mins and got no errors

Thomas Fjellstrom
Member #476
June 2000
avatar

Sure, but you're probably not drawing any images. You're going to have to handle errors from al_load_bitmap properly.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Cassio Renan
Member #14,189
April 2012
avatar

DJLad16, mind me, that's not a sollution. I was merely making you check for failures in your coin drawing in a way it would be visually more obvious. The true sollution, as Thomas said, is checking if your bitmaps are NULL after creation.

If the code worked, either of two things are happening:

1) Your coin bitmap could not be created properly. Maybe the file is in the wrong format, or in the wrong path. The sollution is to check your bitmap for validity after creating.

2) The bitmap is created properly, but it is destroyed somewhere before drawing. The sollution is to check for validity before drawing.

For both, you have to act accordingly after checking(Either by finishing the application, or by doing something to make it obvious the error is happening(Such as drawing a circle where the coin should be)).

Go to: