Best performance when loading Bitmaps
Manveru

Hello everyone.

I am making a shot'em up game, and now i am programming the weapons. I make a class for a weapon, and the class have an ALLEGRO_BITMAP for the sprite of the shoot. My question is if it's better to load the bitmap one time as a static const member, or loading and destroying it every time the bullet is fired and destroyed.

I had try first without the static member, and it was ok, but now i try the static const way and i get and error:

class Weapon1{
private:
static const ALLEGRO_BITMAP *sprite = al_load_bitmap("sprites/bullet1.png");

......

"error C2864: 'Weapon1::sprite' : only static const integral data members can be initialized within a class"

Then i create "sprite" as static, not const, and i initialized it before the main class but i get a memory error:
"Unhandled exception at 0x00000000 in Thunder Force IV.exe: 0xC0000005: Access violation reading location 0x00000000."

class Weapon1{
private:
static ALLEGRO_BITMAP *sprite;

.....

ALLEGRO_BITMAP *Weapon1::sprite = al_load_bitmap("sprites/bullet1.png");

int main(void)
{

....

Thanks for reading, i'm awaiting your help.

OnlineCop

#1 is bad, since you want to store your resources earlier in the program cycle. When you launch the level, you load your bullet resources. If you have multiple guns, preload them all; not when you've picked up your new gun.

#2 is going to be impractical, since it takes longer to access the hard drive and load the bitmap of the bullet than it takes for the bullet to be fired AND "die".

So preload it. In your main load_level(int which_level) function, determine which bullets/weapons you'll find in this level. Preload the images there. Once the level is over and the player has died or passed it, unload the images.

Matthew Leverton

You cannot load an image before you initialize Allegro.

Manveru

Thanks for your answers. I understand what you said and i think you are right, that's what i going to do.

;D i can't load images before load Allegro...i dind't realize that, what a fail, thanks.

David Couzelis

I like to do one more thing: Only load each image file once, and then have many ALLEGRO_BITMAP pointers point to the same image.

In other words, only load the ALLEGRO_BITMAP for your bullet once, and make every bullet object use the same ALLEGRO_BITMAP.

I discovered many years ago that doing it this way gave me much better performance. I assume it's still true. :P

Manveru

With your advice, i create in "level" class a sprite cache, in this case i try a matrix of ALLEGRO_BITMAP because i want to have the sprites grouped, where every row represents a weapon and the columns are the sprites of that weapon. Now i have a problem, is a C++ problem not an Allegro one, but i can't find a solution in my books or in google, so i need your help again to create and destroy a matrix of ALLEGRO_BITMAP:

class Level{
private:
ALLEGRO_BITMAP ***weapon_sprites;
public:
Level();
~Level(){ ? ? ? ? ? }

.....

Level::Level(){
weapon_sprites = ? ? ? ? ? //I need a 7x5 matrix

Thanks again for your help

Thomas Fjellstrom

You'd use new and a loop to allocate your array of bitmaps, something like:

weapon_sprites = new ALLEGRO_BITMAP*[number_of_sprites];
for(int i = 0; i < number_of_sprites; i++) { weapon_sprites = load_sprite(...); }

If you find performance isn't as good as you like it, you may want to consider storing as many sprites as possible in a single bitmap, and use the al_hold_bitmap_drawing function to group all sprites from the same bitmap together.

Manveru

Thanks for your help, but what i want to do is a matrix of ALLEGRO_BITMAP, not an array. I know how to do an array, but i don't know how to create and initialize an array of arrays of ALLEGRO_BITMAP, and how it has to be deleted.

I find the description of the function "al_hold_bitmap_drawing" very usefull, but i'm still learning c++ and Allegro and my project is in an early stage, althought i will use this function very soon, thanks for it.

Arthur Kalliokoski

A matrix is a 2D array, or if you're allocating it dynamically, you have to do the y * width + x thing.

Manveru

I would appreciate if you put me a simple example filling my example above, thanks for your help.

EDIT: Oh i do know now what you mean. I had thought that way, but i think it is less intuitive than a matrix, but if it is easier or simply better i will do that way.

Thomas Fjellstrom

A matrix just adds an extra *...

something like:

ALLEGRO_BITMAP ***weapon_sprites = new ALLEGRO_BITMAP**[rows];
for(int i = 0; i < rows; i++) {
   weapon_sprites[i] = new ALLEGRO_BITMAP*[columns];
   for(int j = 0; j < columns; j++) {
      weapon_sprites[i][j] = load_sprite(...);
   }
}

Thats just pseudo code. You'll have to figure out how to fit it into your own setup.

amber

For better performance, you should put as many of your sprite images as you can on large bitmaps, and then use al_create_sub_bitmap and/or al_draw_bitmap_region to handle drawing. This allows you to use al_hold_bitmap_drawing quite effectively, which really improves performance. :)

Manveru

Thanks for the code. I discover what i was doing wrong and why it returned a costructor error. That confused me and it makes me crazy. I'm very grateful for your help.

I will try that functions you suggest me, but i feel a bit overwhelmed because i'm still beggining with Allegro, but i promise to learn and use these functions.

Thanks to all people that help begginers like me.

Thread #608463. Printed from Allegro.cc