[SDL] SDL_ttf question

Koshmaar koshmaar at poczta.onet.pl
Fri Jan 14 13:55:57 PST 2005

> 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", WINWIDTH,WINHEIGHT,BPP, SDL_GetError());
   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", clrFg);
   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

More information about the SDL mailing list