Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL_ttf Example

Started by DLT_Chief Feb 24, 2005 at 12:07 AM 5 replies 8.3k views
Original Post
DLT_Chief
DLT_Chief
Can someone please write a quick program for me that includes SDL_ttf and prints some text on the screen or something. I just spent an hour trying to get it on my computer and now im too lazy to write something myself anything is appreciated.
Drew_Benton
Drew_Benton
Here's one written for OpenGL compliments of C-Junkie. Here is one from the SDL_ttf docs. Hope that helps.

- Drew
Rob Loach
Rob Loach
I'd give you the font engine that I wrote up, but it's on different computer. I'll upload it probably on monday if you remind me. It's pretty easy to understand without using the example (Init->LoadFonts->Print->Close).
Rob Loach [Website] [Projects] [
Rob Loach
Rob Loach
Here it is. It's from the Lucid Engine that I've been working on. Doesn't have text wrapping. It just dynamically takes care of the font memory usage. There are a few things you must take into account. It uses namespaces, you can take these out if you want. There's a SDL_Surface pointer named Screen for the screen output and Width and Height for the screen's width and height. Might want to set those in Initialize if you're going to use it.

// text.h#if !defined(_TEXT_H_INCLUDED_)#define _TEXT_H_INCLUDED_#include <string>#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <SDL/SDL_ttf.h>using namespace std;namespace System{namespace Text{	// DefaultFont - The default font to use.	//  Optionally set this after you have loaded all required fonts	extern std::string DefaultFont;	bool Initialize();	void Close();	// LoadFont - Loads a font into memory	//  Key - What you'll refer to the font as when using it (the name)	//  Filename - The filename of the font	//  FontSize - The size of the font	// Example:  LoadFont("bigfont","arial.ttf",72)	bool LoadFont(std::string key, std::string filename, int fontSize=16);	// CloseFont - Closes the font (not required as it is managed)	void CloseFont(std::string key);	// FontHeight - Returns the height of the font	int FontHeight(std::string key);	// Print - Prints on the screen	//  x, y - The location to print	//  text - The text to write	//  r, g, b - The colour to paint	//  hAlignment - 0=left, 1=center, 2=right	//  vAlignment - 0=top, 1=center, 2=bottom	//  fontKey - The font to use (the name)	// Example: Print(40, 40, "Hi", 255,0,0, 1, 1, "bigfont"	bool Print(int x = 0, int y = 0, std::string text = "Hello World", int r = 255, int g = 255, int b = 255, int halign = 0, int valign = 0, std::string fontKey = "default");}}#endif // _TEXT_H_INCLUDED_


// text.cpp#include "text.h"#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <SDL/SDL_ttf.h>#include <map>#include <fstream>#include <vector>using namespace std;namespace System{	extern SDL_Surface *Screen;	extern int CameraX;	extern int CameraY; namespace Text{	std::string DefaultFont;	map<string, TTF_Font*> m_Fonts;	typedef map<string, TTF_Font*>::iterator FontIterator;	TTF_Font* GetFont(std::string key){		FontIterator pos = m_Fonts.find(key);		if (pos == m_Fonts.end()) return NULL;		return pos->second;	}	void CloseFont(TTF_Font* font){		if(font != NULL){			TTF_CloseFont(font);			font = NULL;		}	}	void CloseFont(std::string key){		CloseFont(GetFont(key));		FontIterator pos = m_Fonts.find(key);		if (pos == m_Fonts.end()) return;		m_Fonts.erase(pos);	}	bool Initialize(){		if(TTF_Init() == -1)			return false;		return true;	}	void Close(){		for(FontIterator pos = m_Fonts.begin(); pos != m_Fonts.end(); ++pos){			CloseFont(pos->second);    	}		TTF_Quit();	}	bool LoadFont(std::string key, std::string filename, int fontSize){		fstream fin; 		fin.open(filename.c_str(),ios::in);		if(fin.is_open()){ // Check if the file exists			fin.close();			TTF_Font* font = TTF_OpenFont(filename.c_str(), fontSize);			if(font == NULL){				printf("TTF_OpenFont: %s\n", TTF_GetError());				return false;			}			m_Fonts[key] = font;			DefaultFont = key;			return true;		}		fin.close();		return false;	}	SDL_Surface* MakeTextSurface(std::string text, TTF_Font* font, int r, int g, int b){		SDL_Surface* txtSurface;		SDL_Color colour = {r,g,b,0};		if(!(txtSurface = TTF_RenderText_Solid(font,text.c_str(), colour))){			printf("TTF_Print: %s\n", TTF_GetError());			if(txtSurface != NULL)				SDL_FreeSurface(txtSurface);			return NULL;		}		return txtSurface;	}	int FontHeight(std::string key){		TTF_Font *font = GetFont(key);		if(font != NULL) return TTF_FontHeight(font);		return 0;	}	bool Print(int x, int y, std::string text, int r, int g, int b, int halign, int valign, std::string fontKey){		/*			FIND THE FONT		*/		TTF_Font* FontTTF;		if(fontKey == "default")			FontTTF = GetFont(DefaultFont);		else			FontTTF = GetFont(fontKey);		if(FontTTF == NULL){ // Choose any font if there isn't a default			for(FontIterator pos = m_Fonts.begin(); pos != m_Fonts.end(); ++pos){				FontTTF = GetFont(pos->first);				if(FontTTF != NULL)					break;	    	}		}		if(FontTTF == NULL)	return false; // exit if there isn't font		// Create the surface and paint		SDL_Surface* txtSurface = MakeTextSurface(text, FontTTF,r,g,b);		if(txtSurface != NULL){			SDL_Rect dest; 			switch(halign){			case 1: // Center				dest.x = x - (int)(txtSurface->w/2); 				break; 			case 2: // Right				dest.x = x - (int)txtSurface->w;				break;			default: // Left		    	dest.x = x;			}			switch(valign){			case 1: // Center				dest.y = y - (int)(txtSurface->h/2);				break;			case 2: // Bottom				dest.y = y - (int)txtSurface->h;				break;			default: // Top				dest.y = y;			}			dest.x -= System::CameraX;			dest.y -= System::CameraY;		    SDL_BlitSurface(txtSurface, NULL, System::Screen, &dest);			SDL_FreeSurface(txtSurface);			return true;		} else {			return false;		}	}	}}
Rob Loach [Website] [Projects] [
Drew_Benton
Drew_Benton
Wow! That's great Rob, thanks. You know what man, you are always making me want to go back to SDL development now [grin]. First the FMOD devpak, then the SDL_gfx... I just might have to now [wink]... I'll send you a PM on your Lucid Engine too.

- Drew

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.