Search code examples
c++openglsdltruetypesdl-ttf

SDL_ttf - Font directory/Where do fonts go?


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

Solution

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