[SDL] SDL_ttf question

Kostas Kostiadis kos at climaxgroup.com
Mon Jan 17 00:52:54 PST 2005

I'm using both SDL and OpenGL.
Basicaly, I'm doing all the init stuff and handling input, events, timers,
etc. via
SDL, and I'm using openGL for rendering...

I've based my font code to the glfont.c example that comes with
the SDL_ttf sources.  Is there a more efficient way than the one described

If so, I'd be interested in a link or reference to it pls.


-----Original Message-----
From: sdl-bounces+kos=climaxgroup.com at libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of Koshmaar
Sent: 14 January 2005 21:56
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] SDL_ttf question

> I'm trying to add font support to my project.
> I've downloaded SDL_ttf and everything works fine.
> However, I've had a look at the glfont.c sample,
> and it only shows how to do the blended mode
> (which is the slowest/nicest one).

Are you using OpenGL or just plain SDL? 

> So I need to clarify something...In the SDL_GL_Enter2DMode
> for this example, glEnable(GL_BLEND) and the glBlendFunc(...)
> only need to be called for the blended more, right?

Don't know, in my game engine GL_BLEND is all time enabled and they are
rendered ok... probably if you disable GL_BLEND, your texts will also have
background drawn (in shaded mode, solid should give the same results).

> I.e. if I'm rendering in Solid or Shaded modes, I don't
> actually need to enable blending in openGL.
> Is this correct?

See above... probably not, but output will vary.

Also, you could take a look at this little snippet I've found on my hard
drive, it's not in OGL (plain SDL) but if you want I can send you my
SDL_TTF/OGL (very efficient*) font engine:

include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <time.h>

#include <SDL.h>
#include <SDL_TTF.h>

int main( int argc, char* argv[] ) {
   if( SDL_Init(SDL_INIT_VIDEO)==(-1) ) { 
      printf("Could not initialize SDL: %s.\n", SDL_GetError());

   SDL_Surface *screen;
   int WINWIDTH = 800, WINHEIGHT = 600, BPP = 32;
   if( screen == NULL ) {
        fprintf(stderr, "Couldn't set %ix%ix%i video mode: %s\n",
   TTF_Font *fntCourier = TTF_OpenFont( "cour.ttf", 25 );
   SDL_Color clrFg = {220,50,50,0};  // Blue ("Fg" is foreground)
   SDL_Color clrBg  = {0,0,0,0};
   SDL_Rect rcDest = {20,20,0,0};
   SDL_Surface * sText = TTF_RenderText_Solid( fntCourier, "Courier 12",
   SDL_BlitSurface( sText,NULL, screen,&rcDest );
   rcDest.y = 60;
   sText = TTF_RenderText_Shaded( fntCourier, "Courier 12!@#$%^&*()_+[]",
clrFg, clrBg );               
   SDL_BlitSurface( sText,NULL, screen,&rcDest );   
   rcDest.y = 100;
   sText = TTF_RenderText_Blended( fntCourier, "So we met there, and started
to bangle \n each other and drink green tea (YEEEAH!).", clrFg);
   SDL_BlitSurface( sText,NULL, screen,&rcDest );
   fprintf(stderr, " Width: %i  Height: %i", sText->w, sText->h);
   SDL_Event event ;

   for ( ; ; )
   if ( SDL_PollEvent ( &event ) )
  if ( event.type == SDL_KEYDOWN ) break ;
   SDL_FreeSurface( sText );    
   TTF_CloseFont( fntCourier );
   return 0;


* - it uses Bob Pendelton's solution for prerendering individual glyphs,
therefore -> no need for dynamic memory allocation and sending textures to
video card every frame

SDL mailing list
SDL at libsdl.org

More information about the SDL mailing list