3

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);
Matt Reynolds
  • 787
  • 2
  • 9
  • 22

1 Answers1

7

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);
  • Okay. What would the path be on a Mac? I tried both "Macintosh HD/FreeSans.ttf" and "Users/Name/FreeSans.ttf", both with the file in the respective spot, and neither worked. – Matt Reynolds Feb 28 '13 at 21:29
  • The first time I moved it into the disk itself(not in any folders), so 'Macintosh HD/FreeSans.ttf". – Matt Reynolds Feb 28 '13 at 21:34
  • The second time I had it in my main user directory, so "Macintosh HD/Users//FreeSans.ttf" (Or "~/FreeSans.ttf") – Matt Reynolds Feb 28 '13 at 21:35
  • Thanks, made me find my mistake. The second way works, I just forgot the initial '/' before the 'Users'. – Matt Reynolds Feb 28 '13 at 21:42