macosqtubuntunullsdl-ttf

TTF_OpenFont() returns NULL


Here are the facts: I am using codeblocks on Ubuntu. I have installed SDL and SDL_ttf and have included and linked them successfully. I want to render text to the screen of the font, FreeSerif.

Here is the problem: When the program gets to the line, TTF_OpenFont("FreeSerif.ttf,20"), it returns NULL, which would then cause a segfault if passed to the TTF_RenderText_Solid function. I have added the font file to the project and it still did not work.

Here is the code:
TTF_Init();

TTF_Font *font = TTF_OpenFont("FreeSerif.ttf",20); //This returns NULL 

if(!font){printf("Unable to open font");exit(1);} //The program exits here

Solution

  • I've had the same problem and it seems to be a path error, TTF_GetError() throw this :

    Couldn't open Arial.ttf

    You should set your font with absolute path and not a relative one. For me, it was

    /Library/Fonts/Arial.ttf

    instead of :

    Arial.ttf