Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Having a problem with ALLEGRO_BITMAP and a vector.

This thread is locked; no one can reply to it. rss feed Print
Having a problem with ALLEGRO_BITMAP and a vector.
Desmond Taylor
Member #11,943
May 2010
avatar

For some reason my head is not on strait and I cannot work this out.

The code below loads the bitmap I do know that and then when adding it to the vector it just crashes. I'm trying to write this so that I can use it over and over again in my project but without even getting the rest added I need to get this fixed.

Any help would be very appreciated.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3 4#include <cstdio> 5#include <vector> 6 7std::vector<ALLEGRO_BITMAP*> texture; 8 9bool LoadTexture( const char* filename, int id ) 10{ 11 ALLEGRO_BITMAP* temp_bmp = al_load_bitmap( filename ); 12 if ( !temp_bmp ) 13 return false; 14 15 if ( id > (signed)texture.size() ) 16 texture.resize( id ); 17 18 texture[id] = temp_bmp; // Breaks with this line. 19 20 printf( "Loaded Texture: %s\n", filename ); 21 22 return true; 23} 24 25void DeleteAllTextures() 26{ 27 for ( unsigned int i = 0; i < texture.size(); i++ ) 28 { 29 if ( texture[i] ) 30 { 31 al_destroy_bitmap( texture[i] ); 32 printf( "Deleted Texture: #%i\n", i ); 33 } 34 } 35} 36 37int main() 38{ 39 al_init(); 40 41 al_init_image_addon(); 42 43 if ( !LoadTexture( "media/player.bmp", 0 ) ) 44 printf( "Failed to load Texture!\n" ); 45 46 DeleteAllTextures(); 47 48 al_shutdown_image_addon(); 49 50 return 0; 51}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Think about the size of the vector for a second. If you use 'resize(10)' then what is the greatest index you can access of the vector? It's 9, or in your case 'size() - 1'. You need to resize the vector to 'id + 1' so you can access vector[id].

Desmond Taylor
Member #11,943
May 2010
avatar

I tried texture.resize( id+1 ); and even tried texture.resize( id+100 );

I still get it crashing :(

This is why it is confusing me.

Append: Fixed it. It was.

#SelectExpand
1if ( id > (signed)texture.size() )

And should have been.

#SelectExpand
1if ( id >= (signed)texture.size() )

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Desmond Taylor
Member #11,943
May 2010
avatar

;D, yea, Thanks. I just realized that though :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Desmond Taylor
Member #11,943
May 2010
avatar

This is my kind of idea.

#SelectExpand
1bool SetDisplayMode( int width, int height ); 2bool SetWindowTitle( const char* title ); 3void SyncRate( int rate ); 4bool DoLoop(); // Checks for new events and tells us if we are to redraw. 5 6bool LoadTexture( const char* filename, int id ); 7void BlitTexture( int id, int x, int y, int alpha ); 8void SetColorKey( int id, int r, int g, int b ); 9void DeleteTexture( int id ); 10 11void Sync(); // Updates the display. 12 13void CreateSprite( int id ); 14void SetSpriteTexture( int id, int imgid ); 15void SetSpriteCoord( int id, int x, int y ); 16void SetSpriteAlpha( int id, int alpha ); 17void RotateSprite( int id, float angle ); 18void BlitSprite( int id ); 19void MoveSprite( int id, int amount ); 20bool SpriteCollision( int sprite1, int sprite2 ); 21void DeleteSprite( int id ); 22 23bool LoadSound( const char* filename, int id ); 24void PlaySound( int id ); 25void LoopSound( int id ); 26void StopSound( int id ); 27void SetSoundSpeed( int id, float speed ); 28void SetSoundGain( int id, float gain ); 29void SetSoundPan( int id, float pan ); 30void DeleteSound( int id ); 31 32bool LoadFont( const char* filename, int size, int id ); 33void BlitText( const char* text, int x, int y, int r, int g, int b ); 34void BlitCentered( const char* text, int x, int y, int r, int g, int b ); 35void DeleteFont( int id );

This is to make it quicker for making games. There will be a lot more functions but I'm working on the main ones at the moment.

I could use std::map<int , ALLEGRO_BITMAP*> but either way works :P

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Desmond Taylor
Member #11,943
May 2010
avatar

To keep track of it would be as you said different classes or you can name the numbers using...

#define PLAYER_TEXTURE = 0
#define ENEMY_TEXTURE = 1
...

This is how DarkGDK works and I like the way it's so basic. Obviously the basic stuff I need to code the more complex stuff first but then when I make games I can just use my own library instead :P

Audric
Member #907
January 2001

Something suspicious about the sound API: does id refer to a sound resource, or one instance of a sound being played ? ie: If two long "character babble" sounds are playing at the same time, how does StopSound know which one it should cut ?
Same with SetSoundPan if it's for stereo and SetSoundGain for volume.

Desmond Taylor
Member #11,943
May 2010
avatar

It will be stored in a struct with the relevant ID. Then to stop it the instance id is taken from the struct.

I shall be adding more commands than just those. That's just a mockup idea not the actual functions.

Go to: