Jump to content
  • Advertisement
Sign in to follow this  
Benjamin Heath

OpenGL SDL_ttf and OpenGL

This topic is 4268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My situation: SDL_ttf's TTF_Render* functions all return SDL_Surfaces. From those, texture objects can be created and rendered, and that's basically what I've done. The trouble is that OpenGL likes its textures to be by powers of two. Otherwise it's solid white. So, when I make a texture object out of an SDL_Surface created by TTF_Render*(), it's dimensions are most likely not powers of two, and I have to resize it to render the text properly. So, what do you all do for text and fonts in OpenGL? I'm not using Win32 so the NeHe article isn't particularly applicable. How do you do it? Any help is appreciated. Thank you, Benji

Share this post


Link to post
Share on other sites
Advertisement
I'm not familiar with OpenGL, but I use SDL. It shouldn't be hard to resize a SDL_Surface, so why not do so?

If the TTF text isn't a power of two, blit it to a new surface of the width of the TTF text + 1.

Just wrap it in something like this:

//Your constant ColorKey colors
const int COLORKEY_RED = 0xFF;
const int COLORKEY_GREEN = 0x00;
const int COLORKEY_BLUE = 0xFF;

SDL_Surface *CreateText(std::string text, TTF_Font *font, SDL_Color textColor)
{
//Make text as normal...
SDL_Surface* TextSurface = TTF_RenderText_Solid(font, text.c_str(), textColor);

cout << "Old Width: " << TextSurface->w << endl;
cout << "Old Height: " << TextSurface->h << endl;

//Checks whether TextSurface is a power of two or not
int WidthRemainder = (TextSurface->w % 2);
int HeightRemainder = (TextSurface->h % 2);



//If it is, return the surface
if(WidthRemainder == 0 && HeightRemainder == 0) return TextSurface;

bool WidthPlus = 0;
bool HeightPlus = 0;

if(WidthRemainder != 0) WidthPlus = 1;
if(HeightRemainder != 0) HeightPlus = 1;

//Else, Create new surface
SDL_Surface *NewSurface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY, (TextSurface->w + WidthPlus), (TextSurface->h + HeightPlus), 32,
0xFF000000, 0x00FF0000, 0x0000FF00, 0);

//Create a rectangle for coloring the empty image
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = NewSurface->w;
rect.h = NewSurface->h;

//Fill the new image with your colorkey color
SDL_FillRect(NewSurface, &rect, SDL_MapRGB(NewSurface->format, COLORKEY_RED, COLORKEY_GREEN, COLORKEY_BLUE));

//Place your text onto the new surface
SDL_BlitSurface(TextSurface, NULL, NewSurface, &rect);

//Free the old surface
SDL_FreeSurface(TextSurface);

//Set the colorkey on the empty surface to make the COLORKEY_ color clear
Uint32 colorkey = SDL_MapRGB(NewSurface->format, COLORKEY_RED, COLORKEY_GREEN, COLORKEY_BLUE);
SDL_SetColorKey(NewSurface, SDL_RLEACCEL|SDL_SRCCOLORKEY, colorkey);

cout << "New Width: " << NewSurface->w << endl;
cout << "New Height: " << NewSurface->h << endl;

//Return your new surface, with dimensions of a power of two.
return NewSurface;
}



[Edit:] Fixed code.

[Edited by - Servant of the Lord on November 7, 2006 9:41:39 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
I'm not familiar with OpenGL, but I use SDL. It shouldn't be hard to resize a SDL_Surface, so why not do so?

If the TTF text isn't a power of two, blit it to a new surface of the width of the TTF text + 1.

Just wrap it in something like this:
*** Source Snippet Removed ***
**UNTESTED**


It is not difficult. I know how to do this, but it's one more step to do. What happens when the text changes? Then it has to be rerendered, then resized, and then finally made into a texture object. I can do it, that's how I'm doing it now, but I'd like to minimize if I could.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!