|
Best performance when loading Bitmaps |
Manveru
Member #13,535
September 2011
|
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{ ...... "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: class Weapon1{ ..... ALLEGRO_BITMAP *Weapon1::sprite = al_load_bitmap("sprites/bullet1.png"); int main(void) .... Thanks for reading, i'm awaiting your help. |
OnlineCop
Member #7,919
October 2006
|
#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
Supreme Loser
January 1999
|
You cannot load an image before you initialize Allegro. |
Manveru
Member #13,535
September 2011
|
Thanks for your answers. I understand what you said and i think you are right, that's what i going to do. i can't load images before load Allegro...i dind't realize that, what a fail, thanks. |
David Couzelis
Member #10,079
August 2008
|
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. |
Manveru
Member #13,535
September 2011
|
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{ ..... Level::Level(){ Thanks again for your help |
Thomas Fjellstrom
Member #476
June 2000
|
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
Member #13,535
September 2011
|
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
Second in Command
February 2005
|
A matrix is a 2D array, or if you're allocating it dynamically, you have to do the y * width + x thing. They all watch too much MSNBC... they get ideas. |
Manveru
Member #13,535
September 2011
|
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
Member #476
June 2000
|
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
Member #6,783
January 2006
|
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
Member #13,535
September 2011
|
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. |
|