Jump to content
  • Advertisement
Sign in to follow this  
polarboy

SDL_TTF + OpenGL problem

This topic is 3424 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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!