SDL_ttf - Font directory/Where do fonts go?

会有一股神秘感。 提交于 2020-01-24 18:39:24

问题


I've been noodling around with SDL and OpenGL (in C++), and decided to get some text into my game.

I've followed a few tutorials, but I always get the same error: "Couldn't find .ttf" I'm sure it's been asked before, but where should you place the font, and what should you write in the TTF_OpenFont's first parameter? Here's the TTF part so far.

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);

回答1:


You can put the file anywhere you want. But you have to tell TTF_OpenFont() where is it.

With

 TTF_OpenFont("FreeSans.ttf", 24);

You are saying the FreeSans.ttf file is in the working directory of the program.


If you want you can put the file anywhere. For example:

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

or

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


来源:https://stackoverflow.com/questions/15145683/sdl-ttf-font-directory-where-do-fonts-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!