I´ve got problems with opening textures in SDL. I´ve got a function to read bmp files, optimize them and add colorkey:
SDL_Surface* SDLStuff::LoadImage( char* FileName ) {
printf( "Loading texture: \"%s\"\n", FileName );
SDL_Surface* loadedImage = 0;
SDL_Surface* optimizedImage = 0;
loadedImage = SDL_LoadBMP( FileName );
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0, 255 );
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
//SDL_SetColorKey(Tiles[0].Texture, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Tiles[0].Texture->format, 255, 0 ,255));
Cache.push_back( optimizedImage );
return optimizedImage;
}
Which works great. I then load all my textures like this and this also works:
Objects[0].Texture = SDLS.LoadImage( "data/mods/default/sprites/house.bmp" );
Objects[1].Texture = SDLS.LoadImage( "data/mods/default/sprites/wall0.bmp" );
Objects[2].Texture = SDLS.LoadImage( "data/mods/default/sprites/wall1.bmp" );
Selector.Texture = SDLS.LoadImage( "data/mods/default/selector.bmp" );
Tiles[0].Texture = SDLS.LoadImage( "data/mods/default/tiles/grass.bmp" );
Tiles[1].Texture = SDLS.LoadImage( "data/mods/default/tiles/dirt.bmp" );
Tiles[2].Texture = SDLS.LoadImage( "data/mods/default/tiles/black.bmp" );
But I want to be able to control this stuff through some kind of data files. So I wrote a functionton parse a csv file. Then I get the values and try to read the bmp-files, like this:
void DataFile( std::string Mod, std::string FileName, std::string Separator = "\t" ) {
ini dataf;
dataf.Init();
dataf.LoadFile( "data/mods/" + Mod + "/" + FileName );
std::vector< std::vector< std::string > > MData = dataf.LoopCSV( Separator );
for ( unsigned int Row = 0; Row < MData.size(); Row++ ) {
if ( MData.at( Row ).size() > 0 ) {
if ( MData.at( Row )[0] == "TILE" ) {
if ( MData.at( Row ).size() == 4 ) {
std::string a = "data/mods/" + Mod + "/" + MData.at( Row )[3];
WriteLog( a.c_str() );
Tileset TTile;
TTile.WalkCost = String2Int( MData.at( Row )[2] );
TTile.Texture = SDLS.LoadImage( a.c_str() );
Tiles[String2Int(MData.at( Row )[1])] = TTile;
} else {
WriteLog( "Wrong number of arguments passed to TILE\n" );
}
}
}
}
dataf.Destroy();
}
This works perfectly well and it logs paths to files that actually exists, I´ve double checked every file. BUT the SDLS.LoadImage()-call fails anyway and the program crashes. If I comment out that line it all works perfect except that nothing is rendered where the tiles should be. But the files is there and works when I load them manually, and sdl is initialized before I try to call SDL_DisplayFormat(), so I don´t know what can be wrong with this :(
EDIT: Just a note to not cunfuse people; the SDLStuff class uses a cache of the pointers to the textures. That way I can loop through the cache, being able to free all loaded textures with a single function call to a function in SDLStuff.