Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help, First-chance exception at 0x5abeb70e in Game.exe: 0xC0000005: Access viola

This thread is locked; no one can reply to it. rss feed Print
Help, First-chance exception at 0x5abeb70e in Game.exe: 0xC0000005: Access viola
1thatonedude1
Member #15,130
May 2013

I cant get the images to load, it ran when i just drew primitives but when i tried loading an image this error happened and i cant figure it out why, iv already tried running it as an admin. any ideas?

Heres the main

#SelectExpand
1 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_primitives.h> 4#include <allegro5\allegro_font.h> 5#include <allegro5\allegro_ttf.h> 6#include <allegro5\allegro_image.h> 7#include "objects.h" 8 9//Globals 10const int WIDTH = 800; 11const int HEIGHT = 400; 12const int NUM_BULLETS = 5; 13const int NUM_COMETS = 10; 14enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE}; 15bool keys[5] = {false, false, false, false, false}; 16 17//prodotypes 18 19void InitShip(SpaceShip &ship, ALLEGRO_BITMAP *image); 20void DrawShip(SpaceShip &ship); 21void MoveShipUp(SpaceShip &ship); 22void MoveShipDown(SpaceShip &ship); 23void MoveShipLeft(SpaceShip &ship); 24void MoveShipRight(SpaceShip &ship); 25 26void InitBullet(Bullet bullet[], int size); 27void DrawBullet(Bullet bullet[], int size); 28void FireBullet(Bullet bullet[], int size, SpaceShip &ship); 29void UpdateBullet(Bullet bullet[], int size); 30void CollideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize, SpaceShip &ship); 31 32void InitComet(Comet comets[], int size); 33void DrawComet(Comet comets[], int size); 34void StartComet(Comet comets[], int size); 35void UpdateComet(Comet comets[], int size); 36void CollideComet(Comet comets[], int size, SpaceShip &ship); 37 38int main(void) 39{ 40 bool done = false; 41 bool redraw = true; 42 bool isGameOver = false; 43 44 45 const int FPS = 60; 46 47 SpaceShip ship; 48 Bullet bullets[NUM_BULLETS]; 49 Comet comets[NUM_COMETS]; 50 51 ALLEGRO_DISPLAY *display = NULL; 52 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 53 ALLEGRO_TIMER *timer = NULL; 54 ALLEGRO_FONT *font18 = NULL; 55 ALLEGRO_FONT *font20 = NULL; 56 ALLEGRO_BITMAP *shipImage; 57 58 if( !al_init()) 59 return -1; 60 61 display = al_create_display(WIDTH,HEIGHT); 62 timer = al_create_timer(1.0 / FPS); 63 64 shipImage = al_load_bitmap("ship.png"); 65 al_convert_mask_to_alpha(shipImage, al_map_rgb(255, 0, 255)); 66 67 srand(time(NULL)); 68 69 if( !display ) 70 return -1; 71 72 al_init_primitives_addon(); 73 al_install_keyboard(); 74 al_init_font_addon(); 75 al_init_ttf_addon(); 76 al_init_image_addon(); 77 78 event_queue = al_create_event_queue(); 79 80 InitShip(ship, shipImage); 81 InitBullet(bullets, NUM_BULLETS); 82 InitComet(comets, NUM_COMETS); 83 84 font18 = al_load_font("impact.ttf",65,0); 85 font20 = al_load_font("impact.ttf",20,0); 86 87 al_register_event_source(event_queue, al_get_keyboard_event_source()); 88 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 89 al_register_event_source(event_queue, al_get_display_event_source(display)); 90 91 al_start_timer(timer); 92 while(!done) 93 { 94 ALLEGRO_EVENT ev; 95 al_wait_for_event(event_queue, &ev); 96 97 if(ev.type == ALLEGRO_EVENT_TIMER) 98 { 99 redraw = true; 100 if(keys[UP]) 101 MoveShipUp(ship); 102 if(keys[DOWN]) 103 MoveShipDown(ship); 104 if(keys[LEFT]) 105 MoveShipLeft(ship); 106 if(keys[RIGHT]) 107 MoveShipRight(ship); 108 109 if(!isGameOver) 110 { 111 112 UpdateBullet(bullets, NUM_BULLETS); 113 StartComet(comets, NUM_COMETS); 114 UpdateComet(comets, NUM_COMETS); 115 CollideBullet(bullets,NUM_BULLETS,comets,NUM_COMETS, ship); 116 CollideComet(comets, NUM_COMETS,ship); 117 118 al_draw_textf(font20,al_map_rgb(255,0,0), 5, 5, 0, "Lives: %i Score: %i", ship.lives, ship.score); 119 120 if(ship.lives <= 0) 121 { 122 isGameOver = true; 123 } 124 } 125 126 } 127 128 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 129 { 130 done = true; 131 } 132 133 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 134 { 135 switch(ev.keyboard.keycode) 136 { 137 case ALLEGRO_KEY_ESCAPE: 138 done = true; 139 break; 140 141 case ALLEGRO_KEY_UP: 142 keys[UP] = true; 143 break; 144 145 case ALLEGRO_KEY_DOWN: 146 keys[DOWN] = true; 147 break; 148 149 case ALLEGRO_KEY_LEFT: 150 keys[LEFT] = true; 151 break; 152 153 case ALLEGRO_KEY_RIGHT: 154 keys[RIGHT] = true; 155 break; 156 157 case ALLEGRO_KEY_SPACE: 158 keys[SPACE] = true; 159 FireBullet(bullets, NUM_BULLETS, ship); 160 break; 161 } 162 163 } 164 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 165 { 166 switch(ev.keyboard.keycode) 167 { 168 169 case ALLEGRO_KEY_ESCAPE: 170 done = false; 171 break; 172 173 case ALLEGRO_KEY_UP: 174 keys[UP] = false; 175 break; 176 177 case ALLEGRO_KEY_DOWN: 178 keys[DOWN] = false; 179 break; 180 181 case ALLEGRO_KEY_LEFT: 182 keys[LEFT] = false; 183 break; 184 185 case ALLEGRO_KEY_RIGHT: 186 keys[RIGHT] = false; 187 break; 188 189 case ALLEGRO_KEY_SPACE: 190 keys[SPACE] = false; 191 break; 192 } 193 194 } 195 if(redraw && al_is_event_queue_empty(event_queue)) 196 { 197 198 redraw = false; 199 200 if(!isGameOver) 201 { 202 DrawShip(ship); 203 DrawBullet(bullets, NUM_BULLETS); 204 DrawComet(comets, NUM_COMETS); 205 } 206 207 else 208 { 209 al_draw_textf(font18,al_map_rgb(220,1,6), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over! Final Score: %i", ship.score); 210 } 211 212 al_flip_display(); 213 al_clear_to_color(al_map_rgb(0,0,0)); 214 } 215 } 216 217 al_destroy_event_queue(event_queue); 218 al_destroy_timer(timer); 219 al_destroy_bitmap(shipImage); 220 al_destroy_font(font20); 221 al_destroy_font(font18); 222 al_destroy_display(display); 223 224 return 0; 225} 226 227void InitShip(SpaceShip &ship, ALLEGRO_BITMAP *image) 228{ 229 ship.x = 20; 230 ship.y = HEIGHT / 2; 231 ship.ID = PLAYER; 232 ship.lives = 3; 233 ship.speed = 6; 234 ship.boundx = 6; 235 ship.boundy = 7; 236 ship.score = 0; 237 238 ship.maxFrame = 3; 239 ship.curFrame = 0; 240 ship.frameDelay = 50; 241 ship.frameHeight = 46; 242 ship.frameWidth = 41; 243 ship.animationColumns = 1; 244 ship.animationDirection = 1; 245 ship.animationRow = 1; 246 247 ship.image = image; 248 249} 250 251void DrawShip(SpaceShip &ship) 252{ 253 al_draw_filled_rectangle(ship.x,ship.y - 9,ship.x + 10, ship.y - 7 ,al_map_rgb(255,0,0)); 254 al_draw_filled_rectangle(ship.x,ship.y + 9,ship.x + 10, ship.y + 7 ,al_map_rgb(255,0,0)); 255 256 al_draw_filled_triangle(ship.x - 12, ship.y - 17, ship.x + 12, ship.y,ship.x -12,ship.y + 17, al_map_rgb(0,255,0)); 257 al_draw_filled_rectangle(ship.x - 12, ship.y - 2, ship.x + 15, ship.y + 2, al_map_rgb(0,0,255)); 258 259} 260 261void MoveShipUp(SpaceShip &ship) 262{ 263 ship.y -= ship.speed; 264 if(ship.y < 0) 265 ship.y = 0; 266} 267void MoveShipDown(SpaceShip &ship) 268{ 269 ship.y += ship.speed; 270 if(ship.y > HEIGHT) 271 ship.y = HEIGHT; 272} 273void MoveShipLeft(SpaceShip &ship) 274{ 275 ship.x -= ship.speed; 276 if(ship.x < 0) 277 ship.x = 0; 278} 279void MoveShipRight(SpaceShip &ship) 280{ 281 ship.x += ship.speed; 282 if(ship.x > 300) 283 ship.x = 300; 284} 285 286void InitBullet(Bullet bullet[], int size) 287{ 288 for(int i = 0;i < size;i++) 289 { 290 bullet[i].ID = BULLET; 291 bullet[i].speed = 10; 292 bullet[i].live = false; 293 } 294} 295void DrawBullet(Bullet bullet[], int size) 296{ 297 for(int i = 0;i < size;i++) 298 { 299 if(bullet[i].live) 300 { 301 al_draw_filled_circle(bullet[i].x, bullet[i].y, 2, al_map_rgb(255, 255, 255)); 302 } 303 } 304} 305void FireBullet(Bullet bullet[], int size, SpaceShip &ship) 306{ 307 for(int i = 0;i < size;i++) 308 { 309 if( !bullet[i].live ) 310 { 311 bullet[i].x = ship.x + 17; 312 bullet[i].y = ship.y; 313 bullet[i].live = true; 314 break; 315 } 316 } 317} 318void UpdateBullet(Bullet bullet[], int size) 319{ 320 for(int i = 0;i < size;i++) 321 { 322 if(bullet[i].live) 323 { 324 bullet[i].x += bullet[i].speed; 325 if(bullet[i].x > WIDTH) 326 { 327 bullet[i].live = false; 328 } 329 } 330 } 331} 332void CollideBullet(Bullet bullet[], int bSize, Comet comets[], int cSize, SpaceShip &ship) 333{ 334 for(int i = 0;i < bSize; i++) 335 { 336 if(bullet[i].live) 337 { 338 for(int j = 0;j< cSize; j++) 339 { 340 if(comets[j].live) 341 { 342 if(bullet[i].x > (comets[j].x - comets[j].boundx) && 343 bullet[i].x < (comets[j].x + comets[j].boundx) && 344 bullet[i].y > (comets[j].y - comets[j].boundy) && 345 bullet[i].y < (comets[j].y + comets[j].boundy)) 346 { 347 bullet[i].live = false; 348 comets[j].live = false; 349 ship.score++; 350 } 351 } 352 } 353 } 354 } 355} 356void InitComet(Comet comets[], int size) 357{ 358 for(int i = 0; i < size; i++) 359 { 360 comets[i].ID = ENEMY; 361 comets[i].live = false; 362 comets[i].speed = 5; 363 comets[i].boundx = 18; 364 comets[i].boundy = 18; 365 } 366} 367void DrawComet(Comet comets[], int size) 368{ 369 for(int i = 0; i < size; i++) 370 { 371 if(comets[i].live) 372 { 373 al_draw_filled_circle(comets[i].x, comets[i].y, 20, al_map_rgb(255,0,0)); 374 } 375 } 376} 377void StartComet(Comet comets[], int size) 378{ 379 for(int i = 0; i < size; i++) 380 { 381 if(!comets[i].live) 382 { 383 if(rand() % 500 == 0) 384 { 385 comets[i].live = true; 386 comets[i].x = WIDTH; 387 comets[i].y = 30 + rand() % (HEIGHT - 60); 388 break; 389 } 390 } 391 } 392} 393void UpdateComet(Comet comets[], int size) 394{ 395 for(int i = 0; i < size; i++) 396 { 397 if(comets[i].live) 398 { 399 comets[i].x -= comets[i].speed; 400 } 401 } 402} 403void CollideComet(Comet comets[], int size, SpaceShip &ship) 404{ 405 for(int i = 0;i < size ;i++) 406 { 407 if(comets[i].live) 408 { 409 if((comets[i].x - comets[i].boundx) < (ship.x + ship.boundx) && 410 (comets[i].x + comets[i].boundx) > (ship.x - ship.boundx) && 411 (comets[i].y - comets[i].boundy) < (ship.y + ship.boundy) && 412 (comets[i].y + comets[i].boundy) > (ship.y - ship.boundy)) 413 { 414 ship.lives--; 415 comets[i].live = false; 416 } 417 else if(comets[i].x < 0) 418 { 419 comets[i].live = false; 420 ship.lives--; 421 } 422 } 423 } 424}

Objects.h

#SelectExpand
1enum IDS{PLAYER, BULLET, ENEMY}; 2 3struct SpaceShip 4{ 5 int ID; 6 int x; 7 int y; 8 int lives; 9 int speed; 10 int boundx; 11 int boundy; 12 int score; 13 14 int maxFrame; 15 int curFrame; 16 int frameDelay; 17 int frameWidth; 18 int frameHeight; 19 int animationColumns; 20 int animationDirection; 21 22 int animationRow; 23 24 ALLEGRO_BITMAP *image; 25}; 26 27struct Bullet 28{ 29 int ID; 30 int x; 31 int y; 32 bool live; 33 int speed; 34}; 35 36struct Comet 37{ 38 int ID; 39 int x; 40 int y; 41 bool live; 42 int speed; 43 int boundx; 44 int boundy; 45 46 int maxFrame; 47 int curFrame; 48 int frameDelay; 49 int frameWidth; 50 int frameHeight; 51 int animationColumns; 52 int animationDirection; 53 54 ALLEGRO_BITMAP *image; 55}; 56 57struct Explosion 58{ 59 int x; 60 int y; 61 bool live; 62 63 int maxFrame; 64 int curFrame; 65 int frameDelay; 66 int frameWidth; 67 int frameHeight; 68 int animationColumns; 69 int animationDirection; 70 71 ALLEGRO_BITMAP *image; 72};

ph03nix
Member #15,028
April 2013
avatar

Call al_init_image_addon() before loading the bitmap, you call it after

Matthew Leverton
Supreme Loser
January 1999
avatar

Go to: