问题描述
事实如下:我在 Ubuntu 上使用代码块.我已经安装了 SDL 和 SDL_ttf 并成功包含和链接了它们.我想将文本呈现到字体 FreeSerif 的屏幕上.
Here are the facts: I am using codeblocks on Ubuntu. I have installed SDL and SDL_ttf and have included and linked them successfully. I want to render text to the screen of the font, FreeSerif.
问题在于:当程序到达 TTF_OpenFont("FreeSerif.ttf,20") 行时,它返回 NULL,如果传递给 TTF_RenderText_Solid 函数,则会导致段错误.我已将字体文件添加到项目中,但它仍然不起作用.
Here is the problem: When the program gets to the line, TTF_OpenFont("FreeSerif.ttf,20"), it returns NULL, which would then cause a segfault if passed to the TTF_RenderText_Solid function. I have added the font file to the project and it still did not work.
代码如下:TTF_Init();
TTF_Font *font = TTF_OpenFont("FreeSerif.ttf",20); //This returns NULL
if(!font){printf("Unable to open font");exit(1);} //The program exits here
推荐答案
我遇到了同样的问题,似乎是路径错误,TTF_GetError() 抛出这个:
I've had the same problem and it seems to be a path error, TTF_GetError() throw this :
无法打开 Arial.ttf
您应该使用绝对路径而不是相对路径来设置字体.对我来说是
You should set your font with absolute path and not a relative one. For me, it was
/Library/Fonts/Arial.ttf
而不是:
Arial.ttf
这篇关于TTF_OpenFont() 返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!