Login

SDL Text

Merlin's picture
/*
When desiging text i was very confused by the 3 diffrent functions you have. 
shaded, solid and blended. It took a little while and a little experimentation 
but this is how it works:
 
Solid
------
 
Solid has a transparent background but looks really shit, this is the text 
to use for things that have to be drawn every frame (fps, score, etc). 
This function is the fastest by far.
 
Shaded
----------
 
Shaded is a nice text to use as you get to set a background colour. 
Unfortunately it is kinda slow and can really reduce the fram rate of your 
application. Reserve this one for menus or other game states where 
fps isnt too big a deal
 
Blended
----------
 
Wow is this text beautiful! slow as hell though :(. It is best to render this to a 
surface and store it as a global or in a vector, then blit this surface as the 
rendering time is huge. It does all sorts of lovely things to it such as anti
alising. It also has a transparent background like solid text (but it is pulled off 
in a much much nicer way :).)!
 
You can get the latest SDL_TTF here: http://www.libsdl.org/projects/SDL_ttf/
Dont forget to use TTF_Init() and TTF_Quit() when you want to use these functions
*/
 
void drawSolidText(const char* string,
	                      int size,
		          int x, int y,
		          int r, int g, int b)
{
	TTF_Font* font = TTF_OpenFont(FONT_FILENAME, size);
 
	SDL_Color colour = {r, g, b};
 
	SDL_Surface* textSurface = TTF_RenderText_Solid(font, string, colour);
	SDL_Rect textLocation = { x, y, 0, 0 };
 
	SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
	SDL_FreeSurface(textSurface);
	TTF_CloseFont(font);
}
 
void drawSolidText(const char* string,
	      	      const char* fontFileName,
	                     int size,
	                     int x, int y,
		          int r, int g, int b)
{
	TTF_Font* font = TTF_OpenFont(fontFileName, size);
 
	SDL_Color colour = {r, g, b};
 
	SDL_Surface* textSurface = TTF_RenderText_Solid(font, string, colour);
	SDL_Rect textLocation = { x, y, 0, 0 };
 
	SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
	SDL_FreeSurface(textSurface);
	TTF_CloseFont(font);
}
 
void drawShadedText(const char* string,
                           int size,
                           int x, int y,
                           int fR, int fG, int fB,
                           int bR, int bG, int bB)
{
    TTF_Font* font = TTF_OpenFont(FONT_FILENAME, size);
 
    SDL_Color foregroundColor = { fR, fG, fB }; 
    SDL_Color backgroundColor = { bR, bG, bB };
 
    SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string, 
                                                     foregroundColor, backgroundColor);
 
    SDL_Rect textLocation = { x, y, 0, 0 };
 
    SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
    SDL_FreeSurface(textSurface);
 
    TTF_CloseFont(font);
}
 
void drawShadedText(const char* string,
					       const char* fontFileName,
                           int size,
                           int x, int y,
                           int fR, int fG, int fB,
                           int bR, int bG, int bB)
{
    TTF_Font* font = TTF_OpenFont(fontFileName, size);
 
    SDL_Color foregroundColor = { fR, fG, fB }; 
    SDL_Color backgroundColor = { bR, bG, bB };
 
    SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string, 
                                                     foregroundColor, backgroundColor);
 
    SDL_Rect textLocation = { x, y, 0, 0 };
 
    SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
    SDL_FreeSurface(textSurface);
 
    TTF_CloseFont(font);
}
 
void drawBlendedText(const char* string,
	  				        int size,
					        int x, int y,
					        int r, int g, int b)
{
	TTF_Font* font = TTF_OpenFont(FONT_FILENAME, size);
 
	SDL_Color color = {r, g, b};
 
	SDL_Surface* textSurface = TTF_RenderText_Blended(font, string, color);
	SDL_Rect textLocation = {x, y, 0, 0};
 
	SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
	SDL_FreeSurface(textSurface);
	TTF_CloseFont(font);
}
 
void drawBlendedText(const char* string,
					        const char* fontFileName,
					        int size,
					        int x, int y,
					        int r, int g, int b)
{
	TTF_Font* font = TTF_OpenFont(fontFileName, size);
 
	SDL_Color color = {r, g, b};
 
	SDL_Surface* textSurface = TTF_RenderText_Blended(font, string, color);
	SDL_Rect textLocation = {x, y, 0, 0};
 
	SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);
 
	SDL_FreeSurface(textSurface);
	TTF_CloseFont(font);
}
No votes yet

Comments

Edit: wrapped the text so
Merlin's picture

Edit: wrapped the text so you dont have to scroll right Smiling

That's a great description
Kohaku's picture

That's a great description there on the different types of text. Neat functions too.