Jump to content
  • Advertisement
Sign in to follow this  

SDL_ttf no text on the screen, but no errors?

This topic is 3931 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

Hi, I've been reading the tutorial from lazyfoo.net lesson 7 working with SDL_TTF and I decided to download just the font, and code the rest in my game, and I get a 100% compiled completely source, but when I go and run the application I see no text. When I exit out the app I get no debug warnings, errors, link errors, etc... It's coded correctly why do I see now text? Is it because I don't have a background image? Even though I have a black background, and white text? And if this is the case how do you make it not the case. Also is there an easy way to change the background of your screen using just SDL? I'm quite a curious student right now. Thanks,

Share this post

Link to post
Share on other sites
How do you know it's coded correctly? Post some code so we can see if it is.

About background image, you simply blit something on the display surface before blitting anything else and that will become your background image. The frame buffer doesn't get cleared automatically, so you must blit that before every redraw.

Share this post

Link to post
Share on other sites
Alright here is my code

/** -----------------------Documentation----------------
'Name: Nano Ninja's '
'Author: c.s. Finch '
'Date: [10-11-07 - 10-11-08] '

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <string>
using namespace std;

//The attributes of the screen
const int width = 640;
const int height = 480;
const int bpp = 32;

//Surfaces that will be used
SDL_Surface *screen = NULL;
SDL_Surface *image = NULL;
SDL_Surface *message = NULL;
SDL_Surface *select_character = NULL;

//The portions of the sprite map to be blitted
SDL_Rect clip [ 6 ];

//The even structure that will be used.
SDL_Event sdl_event;

//The font that's going to be used
TTF_Font * font = NULL;

//the color of the font
SDL_Color textColor = {255, 0, 0};

//Get the key states
Uint8 *keystates = SDL_GetKeyState(NULL);

//load the image
SDL_Surface *load_image(string filename)
//image that is loaded
SDL_Surface *loadedImage = NULL;

//the optimized image that will be used
SDL_Surface *optimizedImage = NULL;

//Load the image using SDL_image
loadedImage = IMG_Load( filename.c_str() );

//if nothing went wrong with loading the image
if(loadedImage!= NULL)
//create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );

//free the old image
SDL_FreeSurface( loadedImage );

//load the image onto the screen
return optimizedImage;

//draw a sprite from a sprite sheet
void draw_slitsprite(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
//Hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blig the sprite from the sprite sheet
SDL_BlitSurface(source, clip, destination, &offset);

//draw a single sprite on the screen
void draw_sprite(SDL_Surface *destination, SDL_Surface *source, int x, int y)
//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);

bool init()
//Initialize all SDL subsystems
if( SDL_Init(SDL_INIT_EVERYTHING ) == -1) return false;

//set up the screen
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);

//if there was an error setting up the screen
if(screen == NULL) return false;

//Initialize the SDL ttf
if(TTF_Init() == -1) return false;

//Set the window caption
SDL_WM_SetCaption("Nano Ninja's Beta RC1 Developed by: C.S. Finch", NULL);

//if everything initialized fine
return true;

bool load_files()
//Load the images
image = load_image("images/logo.png");

//load the fonts
font = TTF_OpenFont("fonts/lazy.ttf", 28);

//if there was an error loading the images
if(image == NULL) return false;

//if there was an error loading the fonts
if( font == NULL) return false;

//if everything was loaded successfully
return true;

//Clean up the SDL Mess, and Exit out of the program!
void clean_up()
bool quit = false;

while(quit == false)
if (keystates[SDLK_ESCAPE]) quit = true;
else if(sdl_event.type == SDL_QUIT) quit = true;

//Free the surfaces
SDL_FreeSurface( image );
SDL_FreeSurface( message );

//Close the fonts that was used
TTF_CloseFont( font );

//Quit SDL & SDL_TTF


//Draw the splash screen of our game!
void drawSplash()
draw_sprite(screen, image, width/2-150, height/2 - 100);

//Character Selecting Screen
void SelectCharacters()


//The main function we need to call.
int main( int argc, char* args[] )
//Initialize SDL
if(init() == false) return 1;

//load the files
if(load_files() == false) return 1;

//Render the text
message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy hound", textColor );

if(message == NULL) return 1;


//update the screen
if(SDL_Flip(screen) == -1) return 1;

//Free the surface and quit SDL

return 0;

Share this post

Link to post
Share on other sites
You never draw the text to the screen! The line

message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy hound", textColor );

Does not actually draw anything. It returns a newly created surface that has your text on it. You need draw it to the screen at an appropriate location with SDL_BlitSurface.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!