ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a simple game using both OpenGL (for the graphics) and SDL (for event handling and keyboard input). I can initilize the TTF API, I can load a TTF, I can use TTF_RenderText_* functions, but no matter what I do, no text appears whenever I attempt to blit a surface created by a TTF_RenderText_* function to the main SDL_Surface. Here's the (relevant) code:
int DrawStr(TTF_Font* fnt, char* str, int x, int y, SDL_Surface* dest, SDL_Color color) {
GLSCREEN has the SDL_OPENGL, SDL_GL_DOUBLEBUFFER, SDL_HWPALETTE, SDL_RESIZABLE, and SDL_HWACCEL options enabled. I am using SDL 2.0 (i.e. the latest libs in Debian Etch).
Hmmm...TTF_RenderText_* isn't failing--SDL_BlitSurface is. I think this may be because 32-bit SDL surfaces have an ARGB pixel format, whereas OpenGL's 32-bit pixel format is RGBA. Because I explicitly initilized SDL to use OpenGL for graphics (i.e. by enabling the SDL_OPENGL option), SDL_BlitSurface is simply unable to perform blits. Crap.
Thank you for the suggestions. What I currently have working is a way to generate an OpenGL texture from an SDL surface with the same pixel format. The code is currently unsuitable for many calls (i.e. I'm using it to generate HUDs), but here's what I have:
inline int GLFreeTex(GLuint* tex) {
glDeleteTextures(1,tex);
return 1;
}
//POT == Power Of Two
inline int GLMakePOT(int dim) {
double log2 = log(dim) / log(2);
int log2i = (int)ceil(log2);
return (int)(pow(2,log2i));
}
int DrawStr(TTF_Font* fnt, char* str, float x, float y, GLuint texi, SDL_Color color) {
SDL_Surface* text = TTF_RenderText_Blended(fnt, str, color);
if(text == NULL) {
printf("DrawStr(): unable to allocate text buffer\n");
return 0;
}
int width = GLMakePOT(text->w);
int height = GLMakePOT(text->h);
//FIXME: currently, the texture has to be square to prevent ugly distortions
if(width > height) height = width;
else if(height > width) width = height;
What I'll probably end up doing is creating one small 32-bit SDL surface for each HUD element one big 32-bit SDL surface offscreen, and one big OpenGL texture when the game initilizes. Then, during run-time, I'll just clear the big SDL surface, blit the HUD elements to it, bind it to the OpenGL texture, and render it as a single quad.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.