【问题标题】:SDL_ttf - Font directory/Where do fonts go?SDL_ttf - 字体目录/字体在哪里?
【发布时间】:2013-02-28 21:09:34
【问题描述】:

我一直在研究 SDL 和 OpenGL(在 C++ 中),并决定在我的游戏中加入一些文本。

我已经学习了一些教程,但我总是得到同样的错误:“找不到 .ttf”我敢肯定以前有人问过,但是你应该把字体放在哪里,你应该写什么TTF_OpenFont 的第一个参数?这是到目前为止的 TTF 部分。

if (TTF_Init() != 0)
{
    cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
    SDL_Quit();
    exit(1);
}

TTF_Font *font;
font = TTF_OpenFont("FreeSans.ttf", 24);
if (font == NULL)
{
    cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl; // <-- This the error report
    TTF_Quit();
    SDL_Quit();
    exit(1);
}


SDL_Surface *text;
SDL_Color text_color = {255, 255, 255};
text = TTF_RenderText_Solid(font, "BLAH, BLAH, BLAH!", text_color);

【问题讨论】:

    标签: c++ opengl sdl truetype sdl-ttf


    【解决方案1】:

    您可以将文件放在您想要的任何位置。但是你必须告诉TTF_OpenFont()它在哪里。

     TTF_OpenFont("FreeSans.ttf", 24);
    

    您是说 FreeSans.ttf 文件位于程序的 working directory 中。


    如果你愿意,你可以把文件放在任何地方。 例如:

     TTF_OpenFont("D:\\My Folder\\FreeSans.ttf", 24);
    

    TTF_OpenFont("D:/My Folder/FreeSans.ttf", 24);
    

    【讨论】:

    • 好的。 Mac 上的路径是什么?我尝试了“Macintosh HD/FreeSans.ttf”和“Users/Name/FreeSans.ttf”,文件都在各自的位置,但都没有成功。
    • 我第一次将它移动到磁盘本身(不是在任何文件夹中),所以'Macintosh HD/FreeSans.ttf"。
    • 我第二次将它放在我的主用户目录中,所以“Macintosh HD/Users//FreeSans.ttf”(或“~/FreeSans.ttf”)
    • 谢谢,让我发现了我的错误。第二种方法有效,我只是忘记了“用户”之前的初始“/”。
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2014-03-10
    • 2013-05-22
    • 1970-01-01
    相关资源
    最近更新 更多