loading Bitmaps into a vector
Mark Oates

ok here's the code (using OpenLayer):

3class graphics_database_class
6 vector<Bitmap> image;
8 int draw(int t, float xx, float yy, float zz)
9 {
10 if ((t >= image.size()) || (t < 0) || (!tile[t])) return 0;
11 draw_image(&image[t], xx, yy, zz);
12 return 1;
13 }
15 void load()
16 {
17 al_ffblk info;
18 string gimp_string = "";
20 if (al_findfirst("graphics/tiles/*.bmp", &info, FA_ALL) == 0)
21 {
22 do
23 {
24 gimp_string = "graphics/tiles/" + ToString(info.name);
25 image.push_back(Bitmap(gimp_string.c_str()));
26 image[image.size()-1].Load(gimp_string.c_str());
27 } while (al_findnext(&info) == 0);
28 }
29 else allegro_message("no bitmap images found");
31 al_findclose(&info);
32 }

it's a class that contains all the bmp images in the "graphics/tiles/" directory and puts each one into the "vector<Bitmap> image", giving them each an index number, and allowing you to draw them using the index number. But it doesn't work because when displaying the images, they are all the last image that was loaded. What's going on?


Have you tried printing out gimp_string in the load() function?

Mark Oates

the gimp_string gets the right address for each image. I also tried slapping in if (!image[image.size()-1]) allegro_message("blurb!"); and the images load without error.

It's like they are being overwritten with the wrong Bitmap address or something.

Don Freeman

I would try to load them into a dumby BITMAP * first to test they are correct and then add them to the vector.


2void load()
4 al_ffblk info;
5 string gimp_string = "";
9 if (al_findfirst("graphics/tiles/*.bmp", &info, FA_ALL) == 0)
10 {
11 do
12 {
13 // get file name
14 gimp_string = "graphics/tiles/" + ToString(info.name);
15 // load the image to the pointer
16 pBMP = load_bitmap(gimp_string.cstr(),NULL);
17 // push the image pointer into the vector
18 image.push_back(pBMP);
19 // clean up the temp pointer so no mem leak
20 destroy_bitmap(pBMP);
21 pBMP = NULL;
22 // I don't know what you are doing here...
23 // looks like loading the image twice?
24 //image[image.size()-1].Load(gimp_string.c_str());
25 }
26 while (al_findnext(&info) == 0){}
27 }
28 else
29 allegro_message("no bitmap images found");
30 al_findclose(&info);

This should work...thought some of the functions you are using such as the gimp functions, I've never used or seen...so I am guessing at how they work.

Also...you may not need to destroy the bitmap pointer, as I can't remeber how allegro handles the memory. I would try it with it first....and if you get an access error, then remove the destroy_bitmap(pBMP); and pBMP = NULL;


It seems to be correct but as you say there must be a problem somewhere

I'll just list some stupid ideas :)

Have you checked the value of image.size()?
Does the Bitmap::Load function work correctly?
Are the image frames really different from each other?
Where do you initialize the image vector an what size does it has right before calling the load function?

If everything else fails use a debugger to step through your program

Don Freeman

And if you are using the image.Load(...) function above to load the next image...that is your error!

If you are trying:


then image.size()-1 would be the image you just loaded!

This is because of the push_back function. Everything is added to the end.

Mark Oates

if I comment out image[image.size()-1].Load(gimp_string.c_str()); then all the images are white. So the idea is that I push_back(*garbage*) then re-load the image with that last line.

I double checked your list and everything is there, each image is different (see attached), the vector has a size of 5 and starts out empty.


Does it happen to be that in Bitmap class you store BITMAP pointers and for some reason they are pointing to the exact same image? Perhaps some static or uninitialized variable causes it.

Could you post more source? Bitmap class definition, constructor and Bitmap::Load functions would probably help.

Mark Oates

it's OpenLayer's Bitmap...

Don Freeman

Are you loading the images after initializing allegro? You may not know, but even objects that are created before allegro is initialized (if they contain BITMAPs) will cause the BITMAPs to be garbage.

Also, I agree that more source will reveal the problem...it looks like there is something somewhere else too that is causing this.



if (!image[image.size()-1]) allegro_message("blurb!");

This does not neccessarly mean that the image is correct. The way the vector is optimized is that it adds at least 2 to 3 extra spots. The vector may not be growing correctly.


it's OpenLayer's Bitmap...

heh, I should have thougt of that :)
Then I would start checking if the pointers in the vector are the same. If they are then there is something quite wrong.


This does not neccessarly mean that the image is correct

There are two separate things: size and capacity. Usually, when vector resizes itself its capacity increases 50% but its size doesn't.

Thread #557146. Printed from Allegro.cc