Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » printing out vectors of type BITMAP

Credits go to Carrus85 for helping out!
This thread is locked; no one can reply to it. rss feed Print
printing out vectors of type BITMAP
Money
Member #6,730
December 2005
avatar

hey, for animation im gonna store seperate sprite bitmaps into a vector, so then when a user presses a key, it prints out the first vector, or another key, it prints out another element in the vector..i don't really know how to implement this though. here is my code

1#include <allegro.h>
2 
3 
4int main() {
5 allegro_init();
6 install_keyboard();
7 set_color_conversion(COLORCONV_TOTAL); /* this is the new line */
8 set_color_depth(16);
9 set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
10 
11 
12 
13 
14 // Pass NULL to use default pallete
15 BITMAP *goku = load_bitmap("1.bmp", NULL);
16 BITMAP *gokus = load_bitmap("2.bmp", NULL);
17 vector<BITMAP> fighter;
18 fighter.push_back(goku);
19 fighter.push_back(gokus);
20 int index = 0;
21 
22 while( !key[KEY_ESC] )
23 {
24 
25 acquire_screen();
26 //print out the firstelement of the vector here, which is a static image of
27 //a sprite
28 
29
30 
31 
32 release_screen();
33 
34 }
35 
36 
37 
38 return 0;
39}
40END_OF_MAIN()

do i do,

blit( //element from vector here, screen....

or what, i don't know how i would be printint out bitmaps from a vector to the screen

i love this stuff, it really has me thinking ;)

[edit]

could

blit( fighter.front or begin // would that work?

Carrus85
Member #2,633
August 2002
avatar

Err, that shouldn't compile. You have a vector of BITMAP structs, but your pushing BITMAP*'s to the vector.

The proper method would be this:

1 // Pass NULL to use default pallete
2 BITMAP *goku = load_bitmap("1.bmp", NULL);
3 if ( !goku )
4 {
5 // Error out here
6 return 0;
7 }
8 BITMAP *gokus = load_bitmap("2.bmp", NULL);
9 if ( !gokus )
10 {
11 // Error out here
12 return 0;
13 }
14 vector<BITMAP*> fighter;
15 fighter.push_back(goku);
16 fighter.push_back(gokus);
17 // int index = 0; // I'd use iterators instead
18 
19 vector<BITMAP*>::iterator iter = fighter.begin();
20 
21 
22 bool keyIsPressed = false; // Use this to prevent "bouncing" of the images
23 while( !key[KEY_ESC] )
24 {
25 if ( key[KEY_M] && !keyIsPressed ) // go up vector
26 {
27 if ( iter != fighter.end() - 1 ) // DON'T RUN PAST THE END OF THE VECTOR
28 ++iter;
29 keyIsPressed = true;
30 }
31 else if ( key[KEY_N] && !keyIsPressed ) // go down vector
32 {
33 if ( iter != fighter.start() )
34 --iter;
35 keyIsPressed = true;
36 }
37 else
38 {
39 keyIsPressed = false;
40 }
41 acquire_screen();
42 blit ( *iter, screen, 0, 0, 0, 0, (*iter)->w, (*iter)->h );
43 
44 release_screen();
45 
46 }
47 
48 // ALWAYS clean up your memory when your done
49 destroy_bitmap ( goku );
50 destroy_bitmap ( gokus );
51 
52 // Or this would work as well
53 /*
54 for ( iter = fighter.begin(); iter != fighter.end(); ++iter )
55 destroy_bitmap ( *iter );
56 */
57 
58 return 0;

That should work. You'll need to add error checking to your liking in the two "NULL" checking loops

Money
Member #6,730
December 2005
avatar

ahh, ok

thanks carus85! :)

Evert
Member #794
November 2000
avatar

On a side and completely unrelated note, you should check the return values of allegro_init() and set_gfx_mode() (the latter being more important to check).

Go to: