Sign in to follow this  

SDL Font help

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys please can someone help me out with this! I've being pulling my hair out for about 4 hours with it! So I'm following the tutorials over at lazyfoo about loading a font system in, I've had to slice it up a bit to go along with what is already in my code but I'm having a few problems, namely that it doesn't actually work [bawling] I don't get any compiler errors however it doesn't display the text. Here is the code that I am using at the moment:
// class constructor
text::text()
{
   // Initialise TTF
   TTF_Init() == -1; 
}

bool text::loadFont (SDL_Surface *screen)
{
   // Load TTF font
   TTF_Font *font;
   if (font == NULL)
   {  return false;  }
   
   // create the surface for a message
   SDL_Surface *messageText;

   // set the font color to black (tried with white too but no joy)
   SDL_Color textColor = {0, 0, 0, 0};

   // load the font
   font = TTF_OpenFont ( "lazy.ttf", 16);
   
   // assign the text to the surface
   messageText = TTF_RenderText_Solid( font, "why does this not render?", textColor);

   // use drawGFX to render the text
   drawGFX (10, 10, messageText, screenSurface);
   return true;
   
}
drawGFX is in a seperate visualisation class and it looks like this:
void visualisation::drawGFX( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}
this works for blitting bitmaps and PNG files without a problem, doesn't anyone have any ideas as to what could possibly be wrong with it? Cheers for your help -pete-

Share this post


Link to post
Share on other sites
A couple things to check:

-Why are you doing
TTF_Init() == -1;
? It would probably be better to be actually checking that to see if TTF_Init() is working, like

if (TTF_Init() == -1) {
//exit or something
}


-Also, you are not checking if the font loads correctly. From the SDL_ttf documentation it should be something like this:

TTF_Font *font;
font=TTF_OpenFont("font.ttf", 16);
if(!font) {
printf("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}


-Lastly, I'm pretty sure this is unnecessary:

if (font == NULL)

{ return false; }

It shouldn't matter what the value of font is yet because it hasn't been assigned to anything yet. In fact, it might even be initializing to NULL depending on how your compiler is set up.

Share this post


Link to post
Share on other sites
Thanks for the tips kman, I've changed the code slightly to be more like the documentation


bool text::loadFont (SDL_Surface *screen)
{
if (TTF_Init() == -1)
{ return false; }

TTF_Font *font;
font = TTF_OpenFont ( "lazy.ttf", 16);
if (!font)
{
printf ("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}

SDL_Surface *messageText;
SDL_Color textColor = {255, 255, 255, 255};



messageText = TTF_RenderText_Solid( font, "Text to render goes in here", textColor);
drawGFX (10, 10, messageText, screen);
return true;

}


I've tried that but still nothing. Will printf actually work? I thought that I would have to do this to get any form of text to display? I only really want to get this setup for debugging so is there another method that I could use to display text?

Share this post


Link to post
Share on other sites
Well, if you're just using it for debugging, SDL actually creates a file in the same directory of the executable called "stdout.txt" which will contain everything you print to the standard output (e.g. printf and std::cout).

Share this post


Link to post
Share on other sites

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this