Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL中的动态纹理加载_C++_String_Sdl_Textures_Bmp - Fatal编程技术网

C++ SDL中的动态纹理加载

C++ SDL中的动态纹理加载,c++,string,sdl,textures,bmp,C++,String,Sdl,Textures,Bmp,我在SDL中打开纹理时遇到问题。我有一个函数来读取bmp文件,优化它们并添加颜色键: SDL_Surface* SDLStuff::LoadImage( char* FileName ) { printf( "Loading texture: \"%s\"\n", FileName ); SDL_Surface* loadedImage = 0; SDL_Surface* optimizedImage = 0; loadedImage = SDL_LoadBMP( FileName ); o

我在SDL中打开纹理时遇到问题。我有一个函数来读取bmp文件,优化它们并添加颜色键:

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;
}

这很有效。然后我像这样加载我的所有纹理,这同样有效:

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" );
但是我想通过某种数据文件来控制这些东西。所以我写了一个函数来解析csv文件。然后我获取值并尝试读取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();
void数据文件(std::string Mod,std::string FileName,std::string Separator=“\t”){
ini dataf;
dataf.Init();
加载文件(“数据/mods/”+Mod+“/”+FileName);
std::vector>MData=dataf.LoopCSV(分隔符);
for(unsigned int Row=0;Row0){
如果(行中的数据)[0]=“平铺”){
if(MData.at(Row.size()==4){
std::string a=“data/mods/”+Mod+“/”+MData.at(Row)[3];
WriteLog(a.c_str());
蒂莱塞特;
TTile.WalkCost=String2Int(MData.at(Row)[2]);
TTile.Texture=SDLS.LoadImage(a.c_str());
瓷砖[String2Int(MData.at(Row)[1])]=TTile;
}否则{
WriteLog(“传递给磁贴的参数数目错误\n”);
}
}
}
}
Destroy();
}

这工作得非常好,它记录了实际存在的文件的路径,我仔细检查了每个文件。但是SDLS.LoadImage()调用仍然失败,程序崩溃。如果我注释掉那条线,除了在瓷砖应该出现的地方没有任何东西被渲染外,其他一切都是完美的。但是这些文件在那里,当我手动加载它们时可以工作,并且在我尝试调用sdl_DisplayFormat()之前,sdl已经初始化,所以我不知道这会有什么问题:(

编辑:
请大家注意,SDLStuff类使用指向纹理的指针缓存。这样我可以循环缓存,只需对SDLStuff中的一个函数调用一次,就可以释放所有加载的纹理。

更好的解决方案可能是使用资源创建一个存档文件,并在其中迭代文件

好处: 1.您不需要创建csv文件。 2.您存档的bmp大小将减小。 3.您可以为achive文件设置密码,以保护您的资源不受用户攻击

其他链接:


您可能需要使用SDL_图像官方库来加载jpg、png、tiff等:

在使用相对路径并从其他文件夹运行可执行文件时,我也有过类似的程序。您是否从正确的位置运行.exe?很抱歉,我的回复太晚。但是,是的,我是从正确的文件夹运行它。从IDE或直接从资源管理器运行它没有区别:(在
SDL\u加载bmp
失败后,检查
SDL\u GetError
的值