Is it possible to use STL with allegro BITMAP structures?
such as:
list<BITMAP> images; //or list<BITMAP*> images; list<BITMAP>::iterator iter // or list<BITMAP*>::iterator iter; images.insert(iter,*(load_bitmap("SomeImage.bmp",NULL))); // or images.insert(iter,load_bitmap("SomeImage.bmp",NULL));
I've tried but can't seem to get it to work correctly. I am not very experienced with using STL... I can get it to store an image, but when you add another one, it replaces all the others in the list!:o
Thanks as always,
Donald
BITMAP *
So, basically no... The allegro BITMAP structure just holds the basic layout...it also holds the image data, but in an array...
I am confused. I would have to write a "wrapper" class around BITMAP? And if so, how would I handle creation/deletion properly?
Store BITMAP *. Iterate through the list and destroy_bitmap them. That's the easiest way.
The allegro functions work with pointers to BITMAP structures, not with the structures themselves. You just have to do the same with the STL functions, you must work with pointers.
There is no problem.
Creation:
std::list<BITMAP*> bmps;
BITMAP *bmp=create_bitmap(...);
bmps.push_back(bmp);
Removal:
for(std::list<BITMAP*>::iterator i=bmps.begin(); i!=bmps.end(); ++i)
destroy_bitmap(*i);
bmps.clear();
I think that's right...
This is how I get it to SORTA work...
But, like I said...when the Forest image is loaded...bye-bye to the Plains image....
| 1 | //... |
| 2 | //allegro startup code... |
| 3 | //... |
| 4 | list<BITMAP*> *images = new list<BITMAP*>; |
| 5 | list<BITMAP*>::iterator it; |
| 6 | it = images->begin(); |
| 7 | BITMAP *tmp = load_bitmap("WarLand/GFX/Terrain/Plains.bmp",NULL); |
| 8 | if ( !tmp ) |
| 9 | printf("Cound not create BITMAP structure...\n"); |
| 10 | else |
| 11 | printf("Found image and you should see it...\n"); |
| 12 | images->insert(it,tmp); |
| 13 | destroy_bitmap(tmp); |
| 14 | it++; |
| 15 | tmp = load_bitmap("WarLand/GFX/Terrain/Forest.bmp",NULL); |
| 16 | if ( !tmp ) |
| 17 | printf("Cound not create BITMAP structure...\n"); |
| 18 | else |
| 19 | printf("Found image and you should see it...\n"); |
| 20 | images->insert(it,tmp); |
| 21 | destroy_bitmap(tmp); |
| 22 | clear_keybuf(); |
| 23 | int x = 0; |
| 24 | while ( !keypressed() ) |
| 25 | { |
| 26 | it = images->begin(); |
| 27 | x+=40; |
| 28 | if ( x > 160 ) |
| 29 | x = 0; |
| 30 | if ( it == images->end() ) |
| 31 | { |
| 32 | it = images->begin(); |
| 33 | } |
| 34 | ++it; |
| 35 | draw_sprite(screen,(*it),x,0); |
| 36 | rest(1); |
| 37 | } |
| 38 | delete images; |
| 39 | return 0; |
| 40 | }END_OF_MAIN() |
Don't you have to create the iterator after filling the list? You don't need an iterator to add elements to a list, just to get them out.
Well DUH! You're destroying it.
Doesn't matter...both do the same thing...(creating the iterator before or after)
Oh yeah, Trezker is right! You are destroying the bitmap after the insertion! Your list contains only invalid pointers.
[edit]
X-G mentioned the solution in his second post on the topic.
Yeah...I'm special::) We can do that on Mars...8-)
Ok....here is the "working" code...
| 1 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | #include <list> |
| 3 | using std::list; |
| 4 | int main(int argc, char *argv[]) |
| 5 | { |
| 6 | allegro_init(); |
| 7 | set_color_depth(32); |
| 8 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0); |
| 9 | set_color_conversion(COLORCONV_TOTAL); |
| 10 | text_mode(-1); |
| 11 | install_timer(); |
| 12 | install_keyboard(); |
| 13 | install_mouse(); |
| 14 | /////////////////////////////////////////////////// |
| 15 | list<BITMAP*> *images = new list<BITMAP*>; |
| 16 | list<BITMAP*>::iterator it; |
| 17 | it = images->begin(); |
| 18 | BITMAP *tmp = load_bitmap("WarLand/GFX/Terrain/Plains.bmp",NULL); |
| 19 | if ( !tmp ) |
| 20 | printf("Cound not create BITMAP structure...\n"); |
| 21 | else |
| 22 | printf("Found image and you should see it...\n"); |
| 23 | images->insert(it,tmp); |
| 24 | //destroy_bitmap(tmp); |
| 25 | it++; |
| 26 | tmp = load_bitmap("WarLand/GFX/Terrain/Forest.bmp",NULL); |
| 27 | if ( !tmp ) |
| 28 | printf("Cound not create BITMAP structure...\n"); |
| 29 | else |
| 30 | printf("Found image and you should see it...\n"); |
| 31 | images->insert(it,tmp); |
| 32 | //destroy_bitmap(tmp); |
| 33 | clear_keybuf(); |
| 34 | int x = 0; |
| 35 | while ( !keypressed() ) |
| 36 | { |
| 37 | for ( it = images->begin(); it != images->end(); ++it ) |
| 38 | { |
| 39 | draw_sprite(screen,(*it),x,0); |
| 40 | x+=40; |
| 41 | } |
| 42 | x = 0; |
| 43 | rest(1); |
| 44 | } |
| 45 | for ( it = images->begin(); it != images->end(); ++it ) |
| 46 | { |
| 47 | destroy_bitmap((*it)); |
| 48 | } |
| 49 | images->clear(); |
| 50 | delete images; |
| 51 | clear_keybuf(); |
| 52 | return 0; |
| 53 | }END_OF_MAIN(); |
Thanks again to all of you:-*,
Donald
| 1 | //... |
| 2 | //allegro startup code... |
| 3 | //... |
| 4 | list<BITMAP*> images; |
| 5 | list<BITMAP*>::iterator it; |
| 6 | |
| 7 | BITMAP *tmp = load_bitmap("WarLand/GFX/Terrain/Plains.bmp",NULL); |
| 8 | if ( !tmp ) |
| 9 | printf("Cound not create BITMAP structure...\n"); |
| 10 | else |
| 11 | printf("Found image and you should see it...\n"); |
| 12 | images.push_back(tmp); |
| 13 | |
| 14 | tmp = load_bitmap("WarLand/GFX/Terrain/Forest.bmp",NULL); |
| 15 | if ( !tmp ) |
| 16 | printf("Cound not create BITMAP structure...\n"); |
| 17 | else |
| 18 | printf("Found image and you should see it...\n"); |
| 19 | images.push_back(tmp); |
| 20 | |
| 21 | clear_keybuf(); |
| 22 | int x = 0; |
| 23 | while ( !keypressed() ) |
| 24 | { |
| 25 | for(it=images.begin(); it!=images.end(); ++it) |
| 26 | { |
| 27 | x+=40; |
| 28 | if ( x > 160 ) |
| 29 | x = 0; |
| 30 | draw_sprite(screen,(*it),x,0); |
| 31 | } |
| 32 | rest(1); |
| 33 | } |
| 34 | for(it=images.begin(); it!=images.end(); ++it) |
| 35 | { |
| 36 | destroy_bitmap(*it); |
| 37 | } |
| 38 | images.clear(); |
| 39 | return 0; |
| 40 | }END_OF_MAIN() |
EDIT: This thread is fast.
Trezker: So you would call delete on the bitmap object? Yes...I should use the push_back instead...
You should use destroy_bitmap(), not delete...
Oops, no delete.
Must call destroy_bitmap function.
I'm too used to C++ objects.