This topic is now archived and is closed to further replies.


fonts in SDL & opengl

Recommended Posts

incubator01    122
a) suppose I have an opengl scene (like a rotating object or something like that) and I decide to use SDL_TTF to draw some text over that object, would the rect I blitted the text on be properly restored when I decide so clear the rect with an SDL function? b) I was trying out Nehe lesson 13, text in opengl. I got the Linux/SDL code for it but it seems glx.h is used to load the fonts. thats why I mentioned SDL_TTF above. Now I was wondering if the surface struct returned by TTF_RenderText_Solid() would be usable for the opengl functions used in the tutorial?

Share this post

Link to post
Share on other sites
CProgrammer    303
You can use SDL_TTF for text but you'll have to convert the SDL_Surface into an opengl texture.
Just blitting wont work.
Then you make some geometrie, use glOrtho for 2d drawing, convert the coordinates of opengl into the standard windows ones and voila, display your text.
I'll tell you what. I'll show you my function for creating the texture with TTF. The drawing and scaling part should be easy. Just remember the width and height of glOrtho, the font width and height and the screen width and height and make a little formula.

bool CreateFromText(char *text, int r, int g, int b, int a)
SDL_Color clr = {r, g, b, a};

SDL_Surface *Text = TTF_RenderText_Solid(font, text, clr);

font_width = Text->w;
font_height = Text->h;

//Next power of two.

width = 1;
while(width < Text->w)
height = 1;
while(height < Text->h)

SDL_Surface *TextSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
0, 0, 0, 0);

SDL_BlitSurface(Text, 0, TextSurface, 0);

glBindTexture(GL_TEXTURE_2D, tex_id[0]);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextSurface->w, TextSurface->h,
0, GL_BGR_EXT, GL_UNSIGNED_BYTE, TextSurface->pixels);


TextSurface = NULL;
Text = NULL;

return true;


[edited by - CProgrammer on May 26, 2004 12:14:28 AM]

[edited by - CProgrammer on May 26, 2004 12:15:33 AM]

Share this post

Link to post
Share on other sites