Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 566
biscuit Avatar asked Dec 26 '12 16:12

biscuit


2 Answers

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

like image 52
Thomas Ayoub Avatar answered Sep 30 '22 02:09

Thomas Ayoub


You must specify the full path name. That, or you must be certain sure that the file is in the current directory for your program (which is not the same as the executable's directory).

So use this instead:

TTF_Font *font = TTF_OpenFont("/path/to/FreeSerif.ttf",20);
like image 30
sashoalm Avatar answered Sep 30 '22 02:09

sashoalm