简体   繁体   English

SDL中的动态纹理加载

[英]Dynamic texture loading in SDL

I´ve got problems with opening textures in SDL. 我在SDL中打开纹理时遇到问题。 I´ve got a function to read bmp files, optimize them and add colorkey: 我有一个读取bmp文件,优化它们并添加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. 所以我写了一个functionton解析一个csv文件。 Then I get the values and try to read the bmp-files, like this: 然后,我得到值并尝试读取bmp文件,如下所示:

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. 但是无论如何,SDLS.LoadImage()调用都会失败,并且程序将崩溃。 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 :( 但是文件在那里并且在我手动加载它们时可以工作,并且在我尝试调用SDL_DisplayFormat()之前初始化了sdl,所以我不知道这有什么问题:(

EDIT: Just a note to not cunfuse people; 编辑:只是注意不要使人困惑; the SDLStuff class uses a cache of the pointers to the textures. SDLStuff类使用指向纹理的指针的缓存。 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. 这样,我就可以遍历缓存,能够通过对SDLStuff中的函数的单个函数调用来释放所有加载的纹理。

您可能需要使用SDL_image官方库来加载jpg,png,tiff等: http : //www.libsdl.org/projects/SDL_image/

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. 好处:1.不需要创建csv文件。 2. your archived bmp will be of less size. 2.您存档的bmp的大小将减少。 3. you can set a password for achive file to protect your resources from users. 3.您可以为achive文件设置密码,以保护您的资源免受用户攻击。

Additional links: 附加链接:

http://www.zlib.net/ http://www.zlib.net/

What libraries should I use to manipulate archives from C++? 我应该使用哪些库来处理C ++中的存档?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM