[英]SDL_ttf “Couldn't load font file” with SDL2 in c++
I have the following code to draw some text in an SDL2 application. 我有以下代码在SDL2应用程序中绘制一些文本。 When I build and run, I'm consistently seeing an error of
TTF_OpenFont() Failed: Couldn't load font file
. 在生成和运行时,始终会看到
TTF_OpenFont() Failed: Couldn't load font file
错误TTF_OpenFont() Failed: Couldn't load font file
。 I've tried the following: 我尝试了以下方法:
SDL_RWFromFile
as described here: http://www.gamedev.net/topic/275525-sdl_ttf-weirdness/ SDL_RWFromFile
分别打开文件: http : SDL_RWFromFile
Here's my code: 这是我的代码:
void SDLRenderer::drawText(
const Vector2d& pos,
string message,
const Color& color)
{
if(!TTF_WasInit()) {
cerr << "TTF_Init failed " << TTF_GetError() << endl;
exit(1);
}
TTF_Font* fixed = TTF_OpenFont("./DejaVuSansMono.ttf", 16);
if (fixed == NULL) {
cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
TTF_Quit();
SDL_Quit();
exit(1);
}
...
I'm also calling TTF_Init()
from the constructor of this code's class. 我还
TTF_Init()
代码的类的构造函数调用TTF_Init()
。 I'm also a little unsure how to debug further because gdb doesn't even give a backtrace after the error and doesn't seem to let me step into the TTF_OpenFont function. 我也不太确定如何进一步调试,因为gdb甚至在错误发生后都没有提供回溯,而且似乎也无法让我进入TTF_OpenFont函数。
I ran into this issue and it was caused by linking against the incorrect version of the SDL_ttf library. 我遇到了这个问题,它是由链接到SDL_ttf库的错误版本引起的。 I was using SDL 2.0, but I was linking against
libSDL_ttf.so
instead of libSDL2_ttf.so
. 我使用的是SDL 2.0,但是我链接的是
libSDL_ttf.so
而不是libSDL2_ttf.so
。 libSDL_ttf.so
is for SDL 1.2, and is not compatible with SDK 2.0. libSDL_ttf.so
适用于SDL 1.2,并且与SDK 2.0不兼容。
My original command line was: 我原来的命令行是:
$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Couldn't load 18 pt font from /usr/share/fonts/truetype/freefont/FreeSans.ttf: Couldn't load font file
I fixed it by linking against libSDL2_ttf.so
instead: 我通过链接
libSDL2_ttf.so
来修复它:
$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL2_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Font is generally 21 big, and string is 21 big
The showfont.c
program is an example included with SDL_ttf . showfont.c
程序是SDL_ttf附带的示例 。
My thoughts probably belong in a comment but I don't have enough reputation. 我的想法可能是在评论中,但是我没有足够的声誉。 You can make sure you're in the right directory by explicitly setting the current working directory (
chdir
in unistd.h on Linux, or SetCurrentDirectory
in windows.h on Windows). 您可以通过显式设置当前工作目录(Linux上的unistd.h中的
chdir
或Windows上的windows.h中的SetCurrentDirectory
)来确保您在正确的目录中。 I don't think you need to include ./
in the file name. 我认为您不需要在文件名中包含
./
。
I recall having problems with SDL_ttf when calling TTF_Init
, TTF_Quit
, and then TTF_Init
again. 我记得在调用
TTF_Init
, TTF_Quit
和TTF_Init时, TTF_Init
出现问题。 This may not be causing your problem but I would recommend doing your TTF_Init
just once at the beginning of the program and TTF_Quit
once at the end, not every time your constructor runs. 这可能不会造成您的问题,但我会建议做你
TTF_Init
只是一次在节目的开头和TTF_Quit
一旦到了最后,不是每一个你的构造函数运行时间。
If this doesn't work look into building a debug version of SDL_ttf that will play nicer with GDB. 如果这样做不起作用,请考虑构建一个SDL_ttf调试版本,该版本将与GDB配合使用。
I've had your same problem and managed to fix it by entering the full path to the font. 我遇到了同样的问题,并设法通过输入字体的完整路径来解决它。
Instead of just passing the string "./font.ttf" 而不是仅传递字符串“ ./font.ttf”
I used: "/User/MyUsername/Projects/MyProject/font.tff" Hope this helps! 我用过:“ /User/MyUsername/Projects/MyProject/font.tff”希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.