Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » what is wrong?

This thread is locked; no one can reply to it. rss feed Print
what is wrong?
Money
Member #6,730
December 2005
avatar

what is wrong with this code?? the sprite doens't show up on the screen, i checked the source and the .bmp's and i don't understand why the sprite doens't show on the screen, there is just a black screen.

1#include <allegro.h>
2#include <vector>
3 
4using namespace std;
5 
6 
7 
8 
9int main()
10{
11 allegro_init();
12 install_timer();
13 install_keyboard();
14 set_color_conversion(COLORCONV_TOTAL); /* this is the new line */
15 set_color_depth(16);
16 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
17 
18 
19 
20 
21 // Pass NULL to use default pallete
22 BITMAP *goku = load_bitmap("1.bmp", NULL);
23 if (!goku)
24 {
25 allegro_message("error: could not load 1.bmp");
26 }
27 BITMAP *gokus = load_bitmap("2.bmp", NULL);
28 if (!gokus)
29 {
30 allegro_message("error: could not load 2.bmp");
31 }
32 
33 
34 
35 
36 
37 
38 vector<BITMAP*> fighter;
39 fighter.push_back(goku);
40 fighter.push_back(gokus);
41 
42 vector<BITMAP*>::iterator iter = fighter.begin();
43 
44 
45while( !key[KEY_ESC] )
46{
47 
48 
49 if (key[KEY_S])
50 {
51 if ( iter != fighter.end() -1 )
52 {
53 ++iter;
54 }
55 }
56 else if (key[KEY_A])
57 {
58 if ( iter != fighter.begin() )
59 {
60 --iter;
61 }
62 }
63 
64}
65 
66 
67 
68 acquire_screen();
69 
70 blit( *iter, screen, 0, 0, 0, 0, (*iter)->w,(*iter)->h );
71 
72 release_screen();
73 rest(50);
74 
75 
76 
77 
78destroy_bitmap(goku);
79destroy_bitmap(gokus);
80 
81 
82 return 0;
83}
84 
85END_OF_MAIN()

GullRaDriel
Member #3,861
September 2003
avatar

Quote:

blit( *iter,...

I'm not sure that allegro is able to blit that , but i'm noob at c++ purpose.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

miran
Member #2,407
June 2002

Because you draw only after the main loop and only let the sprite stay on the screen for 50 ms.

--
sig used to be here

GullRaDriel
Member #3,861
September 2003
avatar

He isn't clearing the screen, it should be right for that.

EDIT: And I think He could throw this "rest(50)" line.

He will have better results using QueryPerformanceCounter and an adapted loop.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

miran
Member #2,407
June 2002

GullRaDriel: You're missing the point. He's asking why he gets a black screen. The answer is because he doesn't draw anything in the main loop.

--
sig used to be here

GullRaDriel
Member #3,861
September 2003
avatar

Ho YES ! He didn't put the blit in the loop !
I was thinking ( affected by some strange spell ) it was in :p

EDIT:

1#include <allegro.h>
2#include <vector>
3 
4using namespace std;
5 
6 
7 
8 
9int main()
10{
11 allegro_init();
12 install_timer();
13 install_keyboard();
14 set_color_conversion(COLORCONV_TOTAL); /* this is the new line */
15 set_color_depth(16);
16 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
17 
18 
19 
20 
21 // Pass NULL to use default pallete
22 BITMAP *goku = load_bitmap("1.bmp", NULL);
23 if (!goku)
24 {
25 allegro_message("error: could not load 1.bmp");
26 }
27 BITMAP *gokus = load_bitmap("2.bmp", NULL);
28 if (!gokus)
29 {
30 allegro_message("error: could not load 2.bmp");
31 }
32 
33 
34 
35 
36 
37 
38 vector<BITMAP*> fighter;
39 fighter.push_back(goku);
40 fighter.push_back(gokus);
41 
42 vector<BITMAP*>::iterator iter = fighter.begin();
43 
44 
45while( !key[KEY_ESC] )
46{
47 
48 
49 if (key[KEY_S])
50 {
51 if ( iter != fighter.end() -1 )
52 {
53 ++iter;
54 }
55 }
56 else if (key[KEY_A])
57 {
58 if ( iter != fighter.begin() )
59 {
60 --iter;
61 }
62 }
63 acquire_screen();
64 
65 blit( *iter, screen, 0, 0, 0, 0, (*iter)->w,(*iter)->h );
66 
67 release_screen();
68 rest(50);
69}
70 
71 
72 
73
74 
75 
76 
77 
78destroy_bitmap(goku);
79destroy_bitmap(gokus);
80 
81 
82 return 0;
83}
84 
85END_OF_MAIN();

Now this should work !

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Money
Member #6,730
December 2005
avatar

i did put the blit in the loop..its in a while loop

Jonatan Hedborg
Member #4,886
July 2004
avatar

No, it isnt. Only your input is in the loop.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Money
Member #6,730
December 2005
avatar

ahhhh, ok

wow, all of the nested if and loops and whiles, that was crazy, thank you all so much :)

im such an idiot for not seeing that

GullRaDriel
Member #3,861
September 2003
avatar

Quote:

im such an idiot for not seeing that

Don't be too hard with yourself. I remember having doing such things and more :-p

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

miran
Member #2,407
June 2002

Which editor do you use to write your code? I though every decent editor higlights your loops and things like that so it's easy to see what goes where...

--
sig used to be here

GullRaDriel
Member #3,861
September 2003
avatar

That's how Scite does, for example.

DevCpp isn't (Or i don't found how)

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

HoHo
Member #4,534
April 2004
avatar

devcpp can highlight matching braces too. It was somewhere in the editor options

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Tobias Dammers
Member #2,604
August 2002
avatar

Notepad however doesn't :P

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Don Freeman
Member #5,110
October 2004
avatar

Hey Money...

I think that set_color_conversion is also in the wrong order, it should be:

allegro_init();
install_timer();
install_keyboard();
install_mouse();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT,800,600,0,0);
set_color_conversion(COLORCONV_TOTAL);

I'm not exactly sure that it needs to be in that order, but it works fine for me.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Tobias Dammers
Member #2,604
August 2002
avatar

Doesn't matter when you call set_color_conversion, as long as it's after allegro_init() and before loading the bitmap or datafile.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: