Hi, I'm trying get multi-line text texture using SDL_TTF, my texture becomes just a big white box.
This is the code I have
SDL_Surface *background = SDL_CreateRGBSurface(SDL_HWSURFACE, 256, 128, 16,
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
message = TTF_RenderText_Blended(font,"adaaaefgz",textColor);
cout <<"after message"<<endl;
SDL_Rect offset;
offset.x = 0;
offset.y = 0;
SDL_BlitSurface(message, NULL,background, &offset);
prep_texture(background, font_texture);
glBindTexture(GL_TEXTURE_2D,font_texture);
draw_texture(x, y, 256, 128, 0,0, 256, 128);
SDL_FreeSurface(message);
The problem is, at prep_texture function call, where I copy the SDL_Surface to a texture, I get the following warning
warning: the image is not truecolor.. this will probably break
Its number of colors is 2
the code for prep_texture is as follows:
void prep_texture(SDL_Surface *surface, GLuint &texture)
{
GLint nOfColors;
GLenum texture_format;
//...some code
// get the number of channels in the SDL surface
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4) // contains an alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
} else {
printf("warning: the image is not truecolor.. this will probably break\n");
cout <<"Its number of colors is "<<nOfColors<<endl;
// this error should not go unhandled
}
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
int w= nextp2(surface->w);
int h = nextp2(surface->h);
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, w, h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels );
// Free the SDL_Surface only if it was successfully created
}
I have tried using prep_texture on SDL_Surface* message, and it properly displays on the screen, but I'm doing this to support multi-line, so I do want to use a larger surface.
I'm guessing
SDL_Surface *background = SDL_CreateRGBSurface(SDL_HWSURFACE, 256, 128, 16,
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
is the line I gotta fix, but, what should I change it to?
Thank you very much