Thread: SDL_ttf and OpenGL

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    SDL_ttf and OpenGL

    >Just like the subject says, this is about using SDL_ttf with OpenGL.
    >Does anyone know how I can get the text to show up on screen with the 3d
    >environment?
    >
    >Do I need to update the main video surface as well as call
    >SDL_GL_SwapBuffers()? If so, what order do I call them in?
    >I can get SDL_ttf to work on a 2D screen, but it doesn't display
    >anything when OpenGL is enabled.
    >
    Yes, unless you use the (deprecated evil) SDL_OPENGLBLIT that won't work.

    What you should do is use the surface returned by SDL_ttf to create an
    OpenGL texture, and then draw a quad on-screen with that texture.
    Btw, SDL blitting routines are very convenient to convert from one pixel
    format to another, and thus can be used to painlessly convert your
    surface to a format suitable for OpenGL texture creation.

    Stephane
    I am having the same problem as this guy. The text will only show if SDL isnt in OpenGL mode. Being able to blit the text surface to an opengl texture then applying that texture to a quad sounds right but how can i convert an SDL_Surface* to an opengl texture?

    i modified this function i found on gamedev.net forums but it foesnt work
    Code:
    int convert_texture(SDL_Surface *surface)
    {
        GLuint texture;
    
        if(surface)
        {
            glGenTextures(1, &texture);
            glBindTexture(GL_TEXTURE_2D, texture);
    
            glTexImage2D(GL_TEXTURE_2D, 0, 3, surface->w, surface->h,
            0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
            printf("OK\n");
            SDL_FreeSurface(surface);
        }
        else
        {
            printf("Failed\nQuitting...");
            SDL_Quit();
            exit(-1);
        }
    
        return texture;
    }
    Ive look at this for hours now an it looks like it should work. I have no idea what I'm doing wrong. GL_RGB should be GL_BGR but that wont compile for some reason...
    Here is how I render it
    Code:
    SDL_Surface *text_surface = TTF_RenderText_Solid(font, "Courier 12", color );
      GLuint tex = convert_texture(text_surface);
      
    
      while(!done){ 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
        glBindTexture(GL_TEXTURE_2D, tex);
    Last edited by sand_man; 12-01-2004 at 08:08 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Which is why I don't use SDL. I think in the long run it justs complicates things.

    Look into running your code with pure OpenGL or pure DirectX code. Forget SDL.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Ok thanks Bubba
    Im usng a library called BMF_font now. It uses bitmap fonts and it comes with a program that converts .ttf fonts to .bmf fonts.
    Last edited by sand_man; 12-01-2004 at 06:22 PM.

Popular pages Recent additions subscribe to a feed