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
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With