Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Why can't I draw sprites to the screen?

This thread is locked; no one can reply to it. rss feed Print
Why can't I draw sprites to the screen?
cameron
Member #8,219
January 2007

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;
9 
10BITMAP *buffer = NULL;
11BITMAP *img_player = load_bitmap("img\character.bmp", NULL);
12 
13int main()
14{
15 init();
16 //draw();
17 
18 while ( !key[KEY_ESC] ){
19
20 handle_input();
21
22 clear_keybuf();
23 acquire_screen();
24 clear_to_color( buffer, makecol( 0, 0, 0) );
25 
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));
30
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
33
34 release_screen();
35
36 rest(game_speed); // pause to slow down game to a playable rate
37 
38 }
39 destroy_bitmap(buffer); // Release the bitmap data
40 return 0;
41}
42END_OF_MAIN();

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
Member #1,632
November 2001
avatar

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

cameron
Member #8,219
January 2007

yes and yes.

Richard Phipps
Member #1,632
November 2001
avatar

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

cameron
Member #8,219
January 2007

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

OICW
Member #4,069
November 2003
avatar

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.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

X-G
Member #856
December 2000
avatar

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

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

cameron
Member #8,219
January 2007

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

whatever, works. thanks.

OICW
Member #4,069
November 2003
avatar

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

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Neil Black
Member #7,867
October 2006
avatar

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.

BAF
Member #2,981
December 2002
avatar

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

Go to: