|
Having a problem with ALLEGRO_BITMAP and a vector. |
Desmond Taylor
Member #11,943
May 2010
|
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. 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
|
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]. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Desmond Taylor
Member #11,943
May 2010
|
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. 1if ( id > (signed)texture.size() )
And should have been. 1if ( id >= (signed)texture.size() )
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Try this : if ( id >= (signed)texture.size() ) { texture.resize( id + 1); } Note the >= sign instead of >. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Desmond Taylor
Member #11,943
May 2010
|
, yea, Thanks. I just realized that though |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Maybe you would like a std::map<std::string , ALLEGRO_BITMAP*> better? Then you could lookup textures by their name. Just an idea, it may not suit you. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Desmond Taylor
Member #11,943
May 2010
|
This is my kind of idea. 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 |
Edgar Reynaldo
Major Reynaldo
May 2007
|
All those ids make me dizzy. How do you keep track of which id you want to use? I suppose you store it in a class? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Desmond Taylor
Member #11,943
May 2010
|
To keep track of it would be as you said different classes or you can name the numbers using... #define PLAYER_TEXTURE = 0 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 |
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 ? |
Desmond Taylor
Member #11,943
May 2010
|
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. |
|