msegreto
msegreto

Reputation: 79

SDL_ttf "Couldn't load font file" with SDL2 in c++

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:

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.

Upvotes: 2

Views: 6107

Answers (4)

chiasmos
chiasmos

Reputation: 140

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".

Upvotes: 0

Tobia Bocchi
Tobia Bocchi

Reputation: 11

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!

Upvotes: 1

Tim
Tim

Reputation: 4948

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.

Upvotes: 2

i-am-wells
i-am-wells

Reputation: 61

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.

Upvotes: 0

Related Questions