views:

475

answers:

2

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.

A: 

May be the better solution is to create an archive file with your resources and iterate files in it.

Benefits: 1. you do not need to create csv file. 2. your archived bmp will be of less size. 3. you can set a password for achive file to protect your resources from users.

Additional links:

http://www.zlib.net/

http://stackoverflow.com/questions/2406211/what-libraries-should-i-use-to-manipulate-archives-from-c/2443727#2443727

topright
A: 

You may want to use SDL_image official library to load jpg, png, tiff etc: http://www.libsdl.org/projects/SDL_image/

topright