Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problems drawing bitmaps (it just crashes)

This thread is locked; no one can reply to it. rss feed Print
Problems drawing bitmaps (it just crashes)
Ex777 Ex777
Member #7,101
April 2006

Okay well I'm having some problems making my program draw bitmaps. The program compiles fine and starts to run but when It gets to the main game loop where it needs to draw the scene it crashes with a runtime debug error. :-/

Here is the main C++ file:

#SelectExpand
1/************************************************************************************************* 2 File: main.cpp 3 Desc: Main jdpong2000 file 4 Author: Ex777 5 Time Stamp: 4-6-06 (date started) 6*************************************************************************************************/ 7 8/*//////////////////////////////////////////////////////////////////////////////////////////////// 9 Block: Include statements 10 Desc: Calls any .h or .hpp files needed to successfully compile the program. 11////////////////////////////////////////////////////////////////////////////////////////////////*/ 12#include "jdBase.h" 13//#include "jdMainGlobals.h" 14#include "jdAllegro.h" 15 16/*//////////////////////////////////////////////////////////////////////////////////////////////// 17 Block: Structures 18 Desc: Define structures needed by the game 19////////////////////////////////////////////////////////////////////////////////////////////////*/ 20struct jdPlayer 21{ 22 int x, y; // coords on the screen for the paddle 23 int w, h; // width and height of the paddle 24 int score; // score of the player 25 bool AI; // true if the player is computer controled false if otherwise 26 //BITMAP *paddle; // bitmap of the paddle 27}; 28 29struct jdBall 30{ 31 int x, y; // coords on the screen for the ball 32 int w, h; // widht and height of the ball 33 int speedx; // speed of the ball on the x axis 34 int speedy; // speed of the ball on the y axis 35 //BITMAP *ball; // bitmap of the ball 36}; 37 38/*//////////////////////////////////////////////////////////////////////////////////////////////// 39 Block: Global Variables 40 Desc: Define global variables needed by the game. 41////////////////////////////////////////////////////////////////////////////////////////////////*/ 42BITMAP *buffer; 43BITMAP *intro; 44BITMAP *logo; 45BITMAP *paused; 46BITMAP *paddle; 47BITMAP *ballbmp; 48 49jdBall ball; 50jdPlayer player1; 51jdPlayer player2; 52 53FILE *logfile; 54 55int playing; 56int level; 57bool training; 58 59int menuing_main; 60 61int tuffhet; // used for the computer enemy if one exists 62 63/*//////////////////////////////////////////////////////////////////////////////////////////////// 64 Block: Functions 65 Desc: Functions created to run the program. 66////////////////////////////////////////////////////////////////////////////////////////////////*/ 67/*________________________________________________________________________________________________ 68 Function: void alert ( const char* msg ) 69 Desc: Outputs an error to the user. 70 Returns: nothing. 71 Args: void. 72________________________________________________________________________________________________*/ 73void alert(const char* msg) 74{ 75 allegro_message(msg); 76} 77 78/*________________________________________________________________________________________________ 79 Function: bool LoadBitmapFromFile( BITMAP* bmp, const char* filepath, char* errMSG ) 80 Desc: Loads a bitmap from a file. 81 Returns: true on success, false on failure. 82 Args: bmp = the bitmap to load the bitmap file on. 83 filepath = the path to the bitmap to load. 84 errMSG = message to display if bitmap didnt load. 85________________________________________________________________________________________________*/ 86bool LoadBitmapFromFile(BITMAP *bmp, const char* filepath, const char* errMSG) 87{ 88 debug("Bitmap is going to be loaded from load_bitmap"); 89 bmp = load_bitmap(filepath, NULL); 90 debug("bitmap has been loaded from load_bitmap"); 91 92 if (bmp == 0) 93 { 94 alert(errMSG); 95 return false; 96 } 97 98 debug("bitmap returning true"); 99 return true; 100} 101 102/*________________________________________________________________________________________________ 103 Function: bool SetupAllegro( void ) 104 Desc: Sets up all allegro stuff needed to creat a window. 105 Returns: boolean, true on success, false on failure. 106 Args: void. 107________________________________________________________________________________________________*/ 108bool SetupAllegro( void ) 109{ 110 allegro_init(); 111 112 install_keyboard(); 113 114 install_mouse(); 115 116 install_timer(); 117 118 // seed the random generator 119 srand(time(NULL)); 120 121 // set the text mode 122 text_mode(-1); // background is transparent 123 124 // set the video mode 125 set_color_depth(desktop_color_depth()); // could be desktop_color_depth() 126 127 if (set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0) != 0) 128 { 129 alert(allegro_error); 130 exit(0); 131 return false; 132 } 133 134 // set the audio mode 135 if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0) 136 { 137 alert("Error initializing sound system!"); 138 return false; 139 } 140 141 return true; // allegro successfully setup 142} 143 144/*________________________________________________________________________________________________ 145 Function: bool SetupPlayer ( jdPlayer player, int side, bool AI ) 146 Desc: Sets up all variables inside the jdPlayer structure. 147 Returns: boolean, true on success, false on failure. 148 Args: jdPlayer = the player structer. 149 side = the side the player is located. 150 AI = is the player computer controlled? 151________________________________________________________________________________________________*/ 152bool SetupPlayer( jdPlayer player, int side, bool AI ) 153{ 154 player.w = paddle->w; 155 player.h = paddle->h; 156 157 if (side == RIGHT) 158 { 159 player.x = WIDTH-20-player.w; 160 player.y = HEIGHT/2-(player.h/2); 161 } 162 else if (side == LEFT) 163 { 164 player.x = 10+player.w; 165 player.y = HEIGHT/2-(player.h/2); 166 } 167 else 168 return false; 169 170 player.score = 0; 171 172 return true; // player successfully created 173} 174 175/*________________________________________________________________________________________________ 176 Function: bool SetupBall( jdBall sball ) 177 Desc: Sets up all variables required to creat a ball. 178 Returns: boolean, true on success, false on failure. 179 Args: sball = the ball structure to setup. 180________________________________________________________________________________________________*/ 181bool SetupBall( jdBall sball) 182{ 183 184 sball.w = ballbmp->w; 185 sball.h = ballbmp->h; 186 sball.x = WIDTH/2-(sball.w/2); 187 sball.y = HEIGHT/2-(sball.h/2); 188 sball.speedx = 9; 189 sball.speedy = 7; 190 191 return true; // ball created successfully 192} 193 194 195/*________________________________________________________________________________________________ 196 Function: bool SetupGlobals( void ) 197 Desc: Sets up all globals that are required for the game. 198 Returns: boolean, true on success, false on failure. 199 Args: void. 200________________________________________________________________________________________________*/ 201bool SetupGlobals( void ) 202{ 203 debug("SetupPlayer - player1"); 204 if (!SetupPlayer(player1, RIGHT, false)) 205 { 206 return false; 207 } 208 debug("SetupPlayer - player1 completed"); 209 210 if (!SetupPlayer(player2, LEFT, true)) 211 { 212 return false; 213 } 214 215 if (!SetupBall(ball)) 216 { 217 return false; 218 } 219 220 playing = 1; 221 level = 1; 222 training = false; 223 menuing_main = 1; 224 tuffhet = 10; 225 226 return true; // globals successfuly setup 227} 228 229/*________________________________________________________________________________________________ 230 Function: bool SetupBitmaps( void ) 231 Desc: Loads all bitmaps required by the game. 232 Returns: boolean, true on success, false on failure. 233 Args: void. 234________________________________________________________________________________________________*/ 235bool SetupBitmaps( void ) 236{ 237 // paddle bitmap 238 if(!LoadBitmapFromFile(paddle, "graphics/slider.bmp", "Error loading graphics/slider.bmp")) 239 { 240 return false; 241 } 242 243 // ball bitmap 244 if(!LoadBitmapFromFile(ballbmp, "graphics/ball.bmp", "Error loading graphics/ball.bmp")) 245 { 246 return false; 247 } 248 249 // logo bitmap 250 if(!LoadBitmapFromFile(logo, "graphics/logo.bmp", "Error loading graphics/logo.bmp")) 251 { 252 return false; 253 } 254 255 // paused bitmap 256 if(!LoadBitmapFromFile(paused, "graphics/paused.bmp", "Error loading graphics/paused.bmp")) 257 { 258 return false; 259 } 260 261 // intro bitmap 262 if(!LoadBitmapFromFile(intro, "graphics/intro.bmp", "Error loading graphics/intro.bmp")) 263 { 264 return false; 265 } 266 267 // back buffer bitmap 268 buffer = create_bitmap(WIDTH,HEIGHT); 269 if (buffer == 0) 270 { 271 alert(allegro_error); 272 return false; 273 } 274 275 return true; // all bitmaps have loaded successfully 276} 277 278/*________________________________________________________________________________________________ 279 Function: bool Setup ( void ) 280 Desc: Calls all setup functions. 281 Returns: boolean, true on success, false on failure. 282 Args: void. 283________________________________________________________________________________________________*/ 284bool Setup( void ) 285{ 286 debug("Seup called"); 287 288 if (!SetupAllegro()) 289 { 290 debug("SetupAllegro() some how messed up"); 291 292 return false; 293 } 294 debug("SetupAllegro() completed"); 295 296 if (!SetupGlobals()) 297 { 298 debug("SetupGlobals() some how messed up"); 299 300 return false; 301 } 302 debug("SetupGlobals() completed"); 303 304 if (!SetupBitmaps()) 305 { 306 debug("SetupBitmaps() some how messed up"); 307 308 return false; 309 } 310 debug("SetupBitmaps() completed"); 311 312 return true; 313} 314 315/*________________________________________________________________________________________________ 316 Function: bool DeAllocateBitmap( BITMAP *bmp ) 317 Desc: Clears and destroys allocated bitmaps 318 Returns: boolean, true on success, false on failure. 319 Args: bmp = bitmap to deallocate. 320________________________________________________________________________________________________*/ 321bool DeAllocateBitmap( BITMAP *bmp ) 322{ 323 destroy_bitmap( bmp ); 324 325 return true; 326} 327 328/*________________________________________________________________________________________________ 329 Function: bool CleanupBitmaps ( void ) 330 Desc: Clears and destroys allocated bitmaps 331 Returns: boolean, true on success, false on failure. 332 Args: void. 333________________________________________________________________________________________________*/ 334bool CleanupBitmaps( void ) 335{ 336 // logo bitmap 337 if (!DeAllocateBitmap(logo)) 338 { 339 return false; 340 } 341 342 // paused bitmap 343 if (!DeAllocateBitmap(paused)) 344 { 345 return false; 346 } 347 348 // intro bitmap 349 if (!DeAllocateBitmap(intro)) 350 { 351 return false; 352 } 353 354 // buffer bitmap 355 if (!DeAllocateBitmap(buffer)) 356 { 357 return false; 358 } 359 360 // paddle bitmap 361 if (!DeAllocateBitmap(paddle)) 362 { 363 return false; 364 } 365 366 // ball bitmap 367 if (!DeAllocateBitmap(ballbmp)) 368 { 369 return false; 370 } 371 372 return true; // cleanup was successful 373} 374 375/*________________________________________________________________________________________________ 376 Function: bool Cleanup ( void ) 377 Desc: Clears and destroys allocated bitmaps, sounds, ect.. 378 Returns: boolean, true on success, false on failure. 379 Args: void. 380________________________________________________________________________________________________*/ 381bool Cleanup( void ) 382{ 383 if (!CleanupBitmaps()) 384 { 385 return false; 386 } 387 388 return true; // cleanup was successful 389} 390 391/*________________________________________________________________________________________________ 392 Function: void HandleKeys ( void ) 393 Desc: Handles user key presses 394 Returns: nothing. 395 Args: void. 396________________________________________________________________________________________________*/ 397void HandleKeys( void ) 398{ 399 if (key[KEY_ESC]) 400 { 401 playing = 0; 402 } 403 404 return; 405} 406 407/*________________________________________________________________________________________________ 408 Function: bool CheckBallColide ( jdBall cball ) 409 Desc: checks to see if there is a colition between the ball and boundries or paddles. 410 Returns: true if hit false if no hit. 411 Args: cball = the ball we are checking for colitions 412________________________________________________________________________________________________*/ 413bool CheckBallColide( jdBall cball ) 414{ 415 if (cball.x + cball.w >= player1.x) 416 { 417 if (cball.y + cball.w >= player1.y) 418 { 419 if (cball.y <= player1.y + player1.h) 420 { 421 cball.speedx = abs(cball.speedx + rand() % 3) * -1; 422 return true; 423 } 424 } 425 } 426 427 if (cball.x <= player2.x + player2.w) 428 { 429 if (cball.y + cball.h >= player2.y) 430 { 431 if (cball.y <= player2.y + player2.h) 432 { 433 cball.speedx = abs(cball.speedx - rand() % 3); 434 return true; 435 } 436 } 437 } 438 return false; // no colition occured 439} 440 441/*________________________________________________________________________________________________ 442 Function: void UpdateBall ( void ) 443 Desc: Updates ball. 444 Returns: nothing. 445 Args: void. 446________________________________________________________________________________________________*/ 447void UpdateBall( void ) 448{ 449 ball.x = ball.x + ball.speedx; 450 ball.y = ball.y + ball.speedy; 451 452 if(CheckBallColide(ball)) 453 { 454 // colition happend - play sound 455 } 456 457 return; 458} 459 460/*________________________________________________________________________________________________ 461 Function: void DrawScene ( void ) 462 Desc: Draws bitmaps and all that. 463 Returns: boolean, true on success, false on failure. 464 Args: void. 465________________________________________________________________________________________________*/ 466void DrawScene( void ) 467{ 468 debug("Drawing player1.paddle"); 469 draw_sprite(buffer, paddle, player1.x, player1.y); 470 debug("Drawing player2.paddle"); 471 draw_sprite(buffer, paddle, player2.x, player2.y); 472 debug("Drwing ball"); 473 draw_sprite(buffer, ballbmp, ball.x, ball.y); 474 return; 475} 476 477/*________________________________________________________________________________________________ 478 Function: bool DoMainLoop ( void ) 479 Desc: Updates graphics, sound, movement, AI, everything... 480 Returns: boolean, true on success, false on failure. 481 Args: void. 482________________________________________________________________________________________________*/ 483bool DoMainLoop( void ) 484{ 485 HandleKeys(); // handle user input through keybored 486 UpdateBall(); // update ball position 487 DrawScene(); // draw the scene 488 489 return true; // tic successfull 490} 491 492/*________________________________________________________________________________________________ 493 Function: int main ( void ) 494 Desc: Main file for the whole game, calls functions to load resources, handle input, 495 colisions, Ai, the main game loop ect... 496 Returns: integer value, 0 on exit; 497 Args: void. 498________________________________________________________________________________________________*/ 499int main ( void ) 500{ 501 debug("Going to call setup()"); 502 503 if (!Setup()) 504 { 505 // clean up allocated bitmaps, sounds, ect.. 506 if(!Cleanup()) 507 exit(0); 508 509 return 0; 510 } 511 512 debug("Entering main loop"); 513 514 while (playing == 1) 515 { 516 cls(buffer); // clear buffer contents 517 518 if(!DoMainLoop()) 519 playing = 0; 520 521 vsync(); // wait for verticle synk 522 flip(buffer); // display buffer contents 523 524 } 525 526 debug("entering Cleanup()"); 527 528 Cleanup(); 529 530 debug("Cleanup() successfull"); 531 532 return 0; 533} 534END_OF_MAIN();

I have tried all I can think of and nothing works I just get errors all over the place. Please understand this is not nearly done, I'm just trying to make it display some bitmaps before I can push forward with development and add sound, networking, and input code. Please Help!!!!! I truly cant understand why I'm getting this error :'(

Jonny Cook
Member #4,055
November 2003

The problem seems to be in the function LoadBitmapFromFile. When you set the BITMAP pointer to the loaded bitmap, it's not actually changing the pointer you passed. Instead, you should change the parameter to a BITMAP **bmp (pointer to a pointer), or return the loaded bitmap.

Try something like this:

BITMAP *LoadBitmapFromFile(const char* filepath, const char* errMSG) {
    debug("Bitmap is going to be loaded from load_bitmap");
    BITMAP *bmp = load_bitmap(filepath, NULL);
    debug("bitmap has been loaded from load_bitmap");

    if (!bmp)
    {
        alert(errMSG);
        return NULL;
    }

    debug("bitmap returning loaded bitmap");
    return bmp;
}

This is pretty basic stuff... perhaps you should read a few C tutorials before you start such a project.

[edit]
Just to clear it up, a pointer is just a variable that stores the address of another variable. (And of course it has slightly different syntax).
If you had, lets say, variable "int a", and you set it to equal, variable "int b" (as in a = b), later changing the value of b would not also change the value of a. That is essentially what was happening in your function.

The face of a child can say it all, especially the mouth part of the face.

Ex777 Ex777
Member #7,101
April 2006

Thank you I shall give that one a try. ;D

[EDIT]
Um, in response to your edit I would like to belive I know a little bit more about allegro and programming then I led you to belive. I do know what a pointer is and how to use one, but thank you anyways, and your solution was correct. I'm still not a pro at allegro but now that I have this fixed out I can go back to my first love, network programing. :P

Go to: