2

I have the following code to draw some text in an SDL2 application. When I build and run, I'm consistently seeing an error of TTF_OpenFont() Failed: Couldn't load font file. I've tried the following:

  • Ensured the font file is in the current directory of running program. In fact, I've put the font in almost any directory the program could be running from and have tried with an absolute path.
  • Setting different permissions and owning the file
  • Opening the file separately with SDL_RWFromFile as described here: http://www.gamedev.net/topic/275525-sdl_ttf-weirdness/
  • Downloaded and recompiled a newer version of SDL_ttf (2.0.14)

Here's my code:

 void SDLRenderer::drawText(
    const Vector2d& pos,
    string message,
    const Color& color)
{
  if(!TTF_WasInit()) {
    cerr << "TTF_Init failed " << TTF_GetError() << endl;
    exit(1);
  }
  TTF_Font* fixed = TTF_OpenFont("./DejaVuSansMono.ttf", 16);
  if (fixed == NULL) {
    cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
    TTF_Quit();
    SDL_Quit();
    exit(1);
  }
  ...

I'm also calling TTF_Init() from the constructor of this code's class. I'm also a little unsure how to debug further because gdb doesn't even give a backtrace after the error and doesn't seem to let me step into the TTF_OpenFont function.

msegreto
  • 69
  • 2
  • 3

4 Answers4

2

I ran into this issue and it was caused by linking against the incorrect version of the SDL_ttf library. I was using SDL 2.0, but I was linking against libSDL_ttf.so instead of libSDL2_ttf.so. libSDL_ttf.so is for SDL 1.2, and is not compatible with SDK 2.0.

My original command line was:

$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Couldn't load 18 pt font from /usr/share/fonts/truetype/freefont/FreeSans.ttf: Couldn't load font file

I fixed it by linking against libSDL2_ttf.so instead:

$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL2_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Font is generally 21 big, and string is 21 big

The showfont.c program is an example included with SDL_ttf.

Tim
  • 4,790
  • 4
  • 33
  • 41
1

I've had your same problem and managed to fix it by entering the full path to the font.

Instead of just passing the string "./font.ttf"

I used: "/User/MyUsername/Projects/MyProject/font.tff" Hope this helps!

0

My thoughts probably belong in a comment but I don't have enough reputation. You can make sure you're in the right directory by explicitly setting the current working directory (chdir in unistd.h on Linux, or SetCurrentDirectory in windows.h on Windows). I don't think you need to include ./ in the file name.

I recall having problems with SDL_ttf when calling TTF_Init, TTF_Quit, and then TTF_Init again. This may not be causing your problem but I would recommend doing your TTF_Init just once at the beginning of the program and TTF_Quit once at the end, not every time your constructor runs.

If this doesn't work look into building a debug version of SDL_ttf that will play nicer with GDB.

i-am-wells
  • 61
  • 4
0

When you use a relative path, then the path is relative to your executable. If your executable is located in a build directory then you may have to use "../DejaVuSansMono.ttf".

chiasmos
  • 110
  • 7