使用SDL库的可执行文件的奇怪行为
Strange behaviour with executable using SDL library
我使用SDL构建了一个空间入侵者克隆,并使用MSVC11编译。该代码加载4个独立的PNG文件和一个TTF字体,用于渲染。当给定一个绝对文件路径时,该程序运行得非常好,例如"C:\/spaceinvider.png".
我尝试将其更改为相对文件路径,即"./res/spaceinvider.png
然后在发布模式下对程序进行编译,并将其放置在自己的文件夹中。
目录结构:
gamegame.exe
game(all the required SDL dlls)
gameresimage1.png (and the other 3 images)
gameresfont.ttf
最奇怪的事情发生在你试图运行游戏的时候。第一次尝试运行时,程序控制台加载,SDL窗口打开,然后崩溃到桌面。(我已经确保main()中没有过早的返回代码)。然而,在随后的所有时间里,程序都会加载并工作,尽管其中一个精灵不会显示在屏幕上。控制台中出现的唯一错误与两个PNG文件的sRGB配置文件不正确有关。
如果整个文件夹被移到不同的位置,程序第一次不会再次加载,但随后会加载
所以我的问题是
1) 为什么程序不是第一次加载,而是随后加载?2) 当文件都相同(在photoshop中创建)并且加载它们的代码相同时,为什么只有一个sprite无法显示?
(很抱歉没有在这里包含任何代码,因为我不觉得这是一个编码问题-很高兴根据要求粘贴特定的代码段)
谢谢!K
编辑:
图像加载代码(loadTexture函数从这里抄袭:
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren)
{
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
log_CheckSDLPointer(std::cout, texture, "LoadTexture");
return texture;
}
int Gamestate::initialiseSprites(SDL_Renderer *Renderer)
{
enemytexture = loadTexture("./res/invader.png", Renderer);
if (enemytexture == nullptr) {return 1;}
playertexture = loadTexture("./res/player.png", Renderer);
if (playertexture == nullptr) {return 1;}
missiletexture = loadTexture("./res/bullet.png", Renderer);
if (missiletexture == nullptr) {return 1;}
background = loadTexture("./res/background.png", Renderer);
if (background == nullptr) {return 1;}
return 0;
}
这个函数主要被调用:
if (Active_Gamestate.initialiseSprites(mainRenderer) != 0)
{
return 1; //quit game if sprites could not be initialised
}
感谢您的帮助。我想我已经弄清楚了发生了什么。如果图像加载代码改为以下代码:
char executablePath[FILENAME_MAX];
std::stringstream invaderpathstream;
invaderpathstream << executablePath << "/res/invader.png";
std::string invaderpath = invaderpathstream.str();
enemytexture = loadTexture(invaderpath, Renderer);
if (enemytexture == nullptr)
{return 1;}
问题消失了。也许问题出在相对路径上(至少在我的系统上),也许在程序第一次运行后,操作系统会以某种方式找到所需的文件,这就是为什么它在后续运行中不会出现问题?
K
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何更改使用 CPack 安装的可执行文件的名称?
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- 如何在 linux 可执行文件中使用静态库
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 编译的C可执行文件被Windows defender检测为病毒
- Linux 可执行文件通过 dlopen 在emplace_back崩溃打开共享库
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何在可执行文件中存储常量数组?
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- 可执行文件C++包括.obj,.lib和.dll吗?
- 使用SDL库的可执行文件的奇怪行为
- SDL在程序可执行文件中嵌入图像