To display text in SDL you have to get SDL_ttf.
You can download it here: http://www.libsdl.org/projects/SDL_ttf/
You want the "SDL_ttf-devel-2.0.9-VC8.zip" under Binary for Win32.
Extract it's content to your SDL folder.
And copy SDL_ttf.dll in the same folder as your .exe.
You also have to include a font file in your exe folder.
This can be used: arial.ttf
Now go to your global compiler settings.
Add "-lSDL_ttf" to the other linker options list. (without the quotation marks)
Note: You should have read the Setting up SDL tutorial first.
Start with adding this on top of the project.
#include <SDL/SDL.h> #include <SDL/SDL_ttf.h>
Now you have to prepare the surfaces and font.
SDL_Surface *screen = NULL; SDL_Surface *message = NULL; TTF_Font *font = NULL; SDL_Color color = {255,255,255};
Here you must create a function to apply the surfacing:
void apply_surface(float x, float y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source, clip, destination, &offset); }
In your main function add this:
//Initialize stuff if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { return 1; } screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); if (screen == NULL) { return 2; } if (TTF_Init() == -1) { return 3; } SDL_WM_SetCaption("Displaying Text", NULL); //Load the font font = TTF_OpenFont("arial.ttf",12); //The message message = TTF_RenderText_Solid(font, "Displaying Text...", color);
And now add this in the while(isRunning) loop:
//Draws the message on the screen apply_surface(0, 0, message, screen); if(SDL_Flip(screen) == -1) { return 4; }
And at the bottom with the SDL_Quit(), add this:
//Frees the surface SDL_FreeSurface(message); //Quits the ttf TTF_CloseFont(font); TTF_Quit();
| Categories: Graphics Tutorials : SDL Tutorials : Tutorials |