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

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

本文关键字:ttf 文件 C++ IDE SDL      更新时间:2023-10-16

我将ttf文件包含在我的项目中,我将这个ttf复制到调试文件夹和System32文件夹中,然后将其安装在Windows上。我使用了在互联网上找到的另一个C++来源,它也不能正常工作。但是如果我直接从"调试"(而不是从 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);
// ...