iDevGames Forums
SDL_TTF Problems (Can't display text) - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: SDL_TTF Problems (Can't display text) (/thread-4018.html)



SDL_TTF Problems (Can't display text) - pb2ya - Aug 7, 2006 09:05 AM

I've been trying to get this to work for a couple hours now, I'm following a number of SDL tutorials at the moment and can't seem to get past the simple hurdle of displaying a block of text. The weird thing is, I get no compiler warnings or errors when trying to compile the following code, but the screen ends up blank with no text displayed. I've tried adding a colored background to see if I'm just printing black text on a black screen, and I've also tried this with a couple different fonts.

So, I start a new SDL application in XCode (this is written in C), include SDL_ttf.h, link the SDL_ttf framework to the compiler, and proceed to add the following code:

Code:
SDL_Surface *message = NULL;

TTF_Font *font;
SDL_Color color = { 255, 255, 255 };
font = TTF_OpenFont( "lazy.ttf", 28 );

message = TTF_RenderText_Solid( font, "blah blah blah", color );

SDL_BlitSurface( message, NULL, screen, NULL );
SDL_Flip(screen);

Normally, this compiles and runs without trouble, but when I test each one of the statements individually, the line that renders the text is the one that ends up returning. What am I doing wrong? I'm completely stumped as of what's not working here, so tell me if I'm just shooting vague questions.


SDL_TTF Problems (Can't display text) - szymczyk - Aug 7, 2006 11:07 AM

I would guess SDL_ttf isn't finding the font file lazy.ttf. I haven't used SDL_ttf, but I know SDL sets the working directory to the directory above the application bundle. If you have the lazy.ttf file in the Resources folder of your application bundle, you must call TTF_OpenFont() with the following code to open the file:

Code:
font = TTF_OpenFont("AppTitle.app/Contents/Resources/lazy.ttf", 28 );



SDL_TTF Problems (Can't display text) - pb2ya - Aug 7, 2006 01:18 PM

That doesn't seem to be working either... I'm using an older version of XCode (being on 10.3), and all of the resources I need work fine just being the in the "build" directory.


SDL_TTF Problems (Can't display text) - AaronA - Sep 6, 2006 09:41 AM

Quote:font = TTF_OpenFont("AppTitle.app/Contents/Resources/lazy.ttf", 28 );
Make sure lazy.ttf is beeing accessed localy and its infact, lazy.ttf.

Quote:SDL_Surface *message = NULL;

TTF_Font *font;
SDL_Color color = { 255, 255, 255 };
font = TTF_OpenFont( "lazy.ttf", 28 );

message = TTF_RenderText_Solid( font, "blah blah blah", color );

SDL_BlitSurface( message, NULL, screen, NULL );
SDL_Flip(screen);

Try...

Code:
SDL_Surface *message = NULL;

TTF_Font *font;
SDL_Color color = { 255, 255, 255 };
font = TTF_OpenFont( "lazy.ttf", 28 );
message = TTF_RenderText_Solid( font, "blah blah blah", color );

SDL_Rect dest;
dest.x = 10;
dest.y = 10;
SDL_BlitSurface(message, NULL, screen, &dest);

SDL_Flip(screen);



SDL_TTF Problems (Can't display text) - Raptor007 - Sep 18, 2008 06:50 PM

Sorry for resurrecting this thread from the dead, but it seemed unresolved and there's another possible solution.

I was having this same problem, and I solved it by including this in my graphics initialization:
TTF_Init();

With SDL_ttf properly initialized, my "couldn't open font.ttf" errors went away.

(I'm sure the OP has already solved their issue, but hopefully this will help others in the same situation.)