SDL_TTF + OpenGL problem

Started by
3 comments, last by Jason Petrasko 14 years, 8 months ago
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
Advertisement
I went around this problem by making a bitmap and use SDL's IMG_Load to create a surface based on that bitmap

It works now, but it's not exactly the right way I think
I wouldn't use SDL_TTF if I could avoid it by any means.
Rosália G. Schneiderhttp://rosaliaschneider.wordpress.com/
I have tried TTF fonts in opengl/sdl. it's a bit of a hack job but you can get it going. i wouldn't recommend it tho.

try TTF Solid instead of blended just as a test. if you want to use blended you have to enable alpha channel in opengl and blend mode.
When I read "I went around this problem by making a bitmap and use SDL's IMG_Load to create a surface based on that bitmap", you pretty much did what I was thinking anyway. I can't see why you set the SDL_HWSURFACE (docs say: SDL will attempt to create the surface in video memory.) when what you want (since you are just turning it over to a OpenGL texture anyway) is SDL_SWSURFACE. Did you try it with this flag?

This topic is closed to new replies.

Advertisement