SDL C++ IDE 无法打开 .ttf 文件

SDL C++ IDE can't open .ttf file

我将 ttf 文件包含到我的项目中,将此 ttf 复制到调试文件夹和 System32 文件夹中,然后将其安装在 Windows 上。我使用了在互联网上找到的另一个 C++ 源代码,但效果不佳。但是如果我 运行 二进制文件直接来自 "Debug" (而不是来自 IDE )文件夹,它可以正常工作。

我正在使用 CodeBlocks,SDL2。

您在代码中使用的路径是相对于您的应用所在目录的 运行。

如果您的 .ttf 文件与您的应用程序在同一个目录中,您应该使用 SDL_GetBasePath() 找出它的位置:

char* p = SDL_GetBasePath();
if(p == nullptr) { /* TODO: error */ }
std::string ttfPath = std::string(p) + "myfont.ttf";
SDL_free(p); // TODO: exception safety
// now, you can open the file 
TTF_Font* f = TTF_OpenFont(ttfPath.c_str(), 42);
// ...