Texture mapping fonts using SDL_tff

I’m finding it difficult to find information on how to go about using
text in an OpenGL+SDL environment.

I can blit stuff onto a surface for a 2D world, but I’m of the
understanding that I then need to convert an SDL surface into a
texture (or something) for use with texture mapping.

I can fget as far as creating a text surface using:

TTF_Init();
TTF_Font* font = TTF_OpenFont(“ARIAL.TTF”, 12);
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, “This is my
text.”, foregroundColor, backgroundColor);

SDL_Rect textLocation = { 100, 100, 0, 0 };
SDL_Event event;
bool gameRunning = true;
while (isRunning
{
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_Flip(screen);
}

etc

So how do I go about converting the surface to a texture map?

If there are other font libraries that can be used with PSPGL for the
PSP, let me know but I think I’m stuck with SDL_ttf. I actually need
text for debugging.

Cheers

Glenn

here is the function I use for converting sdl surfaces into opengl textures, hope that helps.
though I bet there are problems on little-endian machines with it. not sure though.

GLuint load(SDL_Surface* pSurface) {
SDL_Surface* surface = pSurface; // Copy surface pointer to temporary pointer, so that we don’t change original surface
surface = SDL_DisplayFormatAlpha(surface); // Convert it into display format

 if (!surface) {
     printf("WARNING: Could not load Texture");
     return EXIT_FAILURE;
 }
 GLuint texture_id = 0;
 glGenTextures(1, &texture_id);                        // Generate a openGL texture

 glBindTexture(GL_TEXTURE_2D, texture_id);             // Tell openGL to use the current texture

 // Copy pixel data from the SDL surface into the texture
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);

 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   // Define behaviour at resize
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   // ...

 return texture_id;    // All done properly, return OpenGL texture ID

}

Glenn McCord schrieb:> I’m finding it difficult to find information on how to go about using

text in an OpenGL+SDL environment.

I can blit stuff onto a surface for a 2D world, but I’m of the
understanding that I then need to convert an SDL surface into a
texture (or something) for use with texture mapping.

I can fget as far as creating a text surface using:

TTF_Init();
TTF_Font* font = TTF_OpenFont(“ARIAL.TTF”, 12);
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, “This is my
text.”, foregroundColor, backgroundColor);

SDL_Rect textLocation = { 100, 100, 0, 0 };
SDL_Event event;
bool gameRunning = true;
while (isRunning
{
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_Flip(screen);
}

etc

So how do I go about converting the surface to a texture map?

If there are other font libraries that can be used with PSPGL for the
PSP, let me know but I think I’m stuck with SDL_ttf. I actually need
text for debugging.

Cheers

Glenn


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Jessica

 // Copy pixel data from the SDL surface into the texture
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);

Naturally, this only works if surface->w and surface->h are powers of
two (or you have the GL_ARB_texture_non_power_of_two extension)…this
can be difficult to promise when SDL_ttf hands back an arbitrary glyph’s
bitmap, so you’ll probably need to manually scale it or pad it with
empty pixels before uploading it to a texture.

Specifically for text, you might be able to put FTGL to work for you
better, though…it’ll take a .ttf file, like SDL_ttf, but it can render
it via OpenGL primitives, so it will scale cleanly and probably take
less resources than building a surface, then building a texture. It can
use GL display lists or just immediate mode, so it should work on
anything that can legitimately call itself OpenGL.

It uses FreeType like SDL_ttf, so you really are just replacing the
SDL_ttf dependency with something else.

http://homepages.paradise.net.nz/henryj/code/#FTGL

Although I’ve used it with OpenGL too, SDL_ttf is really meant for 2D
display.

–ryan.