Why can't I draw sprites to the screen?

I simply don't get this. Whenever I run this the game just freezes and i have to use ctrl-alt-delete to end the task. the draw_sprite line is what freezes the game. if i use the commented out draw_rectangle, then it works but isnt the sprite i want.

1// p_ => player variable
2int p_x = 0, // current x
3 p_y = 0, // current y
4 p_lx = 0, // last x
5 p_ly = 0, // last y
6 p_dir = 0, // 0 => left, 1 => right
7 tile_size = 32,
8 game_speed = 100;
10BITMAP *buffer = NULL;
11BITMAP *img_player = load_bitmap("img\character.bmp", NULL);
13int main()
15 init();
16 //draw();
18 while ( !key[KEY_ESC] ){
20 handle_input();
22 clear_keybuf();
23 acquire_screen();
24 clear_to_color( buffer, makecol( 0, 0, 0) );
26 // draw player
27 //rectfill( buffer, (p_x*tile_size), (p_y*tile_size), ((p_x*tile_size)+tile_size), ((p_y*tile_size)+tile_size), makecol( 255, 255, 255) );
28 //blit(img_player, buffer, (p_x*tile_size),(p_y*tile_size),(p_x*tile_size),(p_y*tile_size),((p_x*tile_size)+tile_size),((p_y*tile_size)+tile_size));
29 draw_sprite(buffer, img_player, (p_x*tile_size), (p_y*tile_size));
31 blit(buffer, screen, 0,0,0,0,640,480);//Draw the buffer to the screen
32 clear_bitmap(buffer); // Clear the contents of the buffer bitmap
34 release_screen();
36 rest(game_speed); // pause to slow down game to a playable rate
38 }
39 destroy_bitmap(buffer); // Release the bitmap data
40 return 0;

EDIT: it should be noted that after isolating the problem, i deleted code so you guys dont need to sort through any excess..

EDIT2: just saw a similar thread suggesting i check to make sure the image is being loaded... I will do that now.

EDIT3: ok, well I've triple-checked that all filenames are right.. I don't understand why it cant load the image... do i need to change the "working directory" of the program? how do i do this... its in img/character.bmp relative to the executable compiled... argh!

Richard Phipps

BITMAP *img_player = load_bitmap("img\character.bmp", NULL);
In a function? Is it called? If not, it won't work.


yes and yes.

Richard Phipps

So, next add error checking to make sure the image is correctly loaded.


i did. its not being correctly loaded but its in the right spot.. check my original post edits.


Ok, expected behaviour is, that at [0;0] your image get shown. I don't see any problem with the code. As RP pointed out, try checking if the bitmap was loaded correctly. Oh even try to draw the sprite directly onto the screen (don't forget to comment out blitting buffer).

On a sidenote I think that clearing the buffer after the blitting isn't necessary, especially when you're clearing it before drawing.


If that is in a function, we're obviously not seeing the real code. Show us that.


fixed. apparently i cant place images outside of the folder the executable is in...

whatever, works. thanks.


You can place them in subdirectories, but you have to use proper filenames and dividers between.

Neil Black

For future reference, when you include code, include all relevant code. I know I'm bad about including code myself, but when I do I make sure everything someone needs to answer my question is there. You really should have posted your init() function. But since you solved the problem it's OK this time.


You want img\\character.bmp, or better yet, img/character.bmp.

Thread #591406. Printed from Allegro.cc