Jump to content
  • Advertisement
Sign in to follow this  
fredrikhcs

SDL_TTF won't render text

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

I'm trying do display some text using SDL_TTF. However, it won't render (or at least it doesn't show).
#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"

/* Cleans up and exits */
int shutdown(int status);

int main(int argc, char *argv[])
{
	/* Variables */
	SDL_Surface *screen;
	SDL_Event event;
	TTF_Font *font;
	
	int x_pos = 0, x_vel = 0, y_pos = 0, y_vel = 0;
	int playing = 1;
	
	/* Initialize SDL */
	if (SDL_Init(SDL_INIT_VIDEO) > 0)
	{
		fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
		exit(1);
	}
	
	/* Set video mode */
	screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
	
	if (screen == NULL)
	{
		fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
		shutdown(1);
	}
	
	SDL_ShowCursor(SDL_DISABLE);
	
	/* Initialize SDL_TTF */
	if (TTF_Init() == -1)
	{
		fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
		shutdown(1);
	}
	
	/* Initialize the font */
	font = TTF_OpenFont("data/Vera.ttf", 12);
	
	if (font == NULL)
	{
		fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError());
		shutdown(1);
	}
	
	SDL_Color colorFg = {0,0,255,0};  // Blue
	SDL_Surface *text = TTF_RenderText_Solid(font, "Print something please! :(", colorFg);
   	SDL_Rect destRect = {0,0,0,0};
   	SDL_BlitSurface(text, NULL, screen, &destRect);

	/* The game loop */
	while (playing)
	{
		// Check input
		while (SDL_PollEvent(&event))
		{
			switch (event.type) 
			{
				case SDL_KEYDOWN:
					switch (event.key.keysym.sym)
					{
						case SDLK_UP:
							y_vel = -1;
							break;
						case SDLK_DOWN:
							y_vel = 1;
							break;
						case SDLK_LEFT:
							x_vel = -1;
							break;
						case SDLK_RIGHT:
							x_vel = 1;
							break;
						default:
							break;
					}
					break;
				case SDL_KEYUP:
					switch (event.key.keysym.sym)
					{
						case SDLK_UP:
							if (y_vel > 0)
								y_vel = 0;	
							break;
						case SDLK_DOWN:
							if (y_vel < 0)
								y_vel = 0;
							break;
						case SDLK_LEFT:
							if (x_vel < 0)
								x_vel = 0;
							break;
						case SDLK_RIGHT:
							if (x_vel > 0)
								x_vel = 0;
							break;
						case SDLK_q:
							playing = 0;
							break;
						default:
							break;
					}
				default:
					break;
			}
		}
		
		x_pos += x_vel, y_pos += y_vel;

		/* Play ball! */
		
	}
	SDL_FreeSurface(text);
	TTF_CloseFont(font);
	
	shutdown(0);
}

/* Cleans up and exits */
int shutdown(int status)
{
	if (TTF_WasInit())
		TTF_Quit();
	SDL_Quit();
	
	exit(status);
}

Share this post


Link to post
Share on other sites
Advertisement
I don't see a call to SDL_Flip(), so that could be why nothing is working. You should also check if TTF_RenderText_Solid is returning NULL.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!