SDL2 + OpenGL - Problem with font color!

Hi, I have problem with setting font color. When I set white color, I see white color, when I set red color, I see blue color and so it works with all colors except white! How can I fix this error?

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_opengl.h>
#include <string>

SDL_Window *window;
SDL_Event event;
SDL_Surface *surface;
GLuint textureID=0;
TTF_Font *font;

SDL_Color color={250, 0, 0, 0};

void loadFont(std::string path, int size){
    font=TTF_OpenFont(path.c_str(), size);
}

void setText(std::string text){
    surface=TTF_RenderText_Blended(font, text.c_str(), color);

    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, 
                            GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}

void display(){
    glPushMatrix();
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glTranslatef(0, 0-surface->h/4, 0);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glBegin(GL_POLYGON);
        glTexCoord2f(0, 0); glVertex2f(0, 0);
        glTexCoord2f(1, 0); glVertex2f(surface->w, 0);
        glTexCoord2f(1, 1); glVertex2f(surface->w, surface->h);
        glTexCoord2f(0, 1); glVertex2f(0, surface->h);
    glEnd();

    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
}

int main(){
    TTF_Init();

    window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 800, SDL_WINDOW_OPENGL);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    SDL_GLContext context=SDL_GL_CreateContext(window);

    SDL_GL_SetSwapInterval(SDL_FALSE);

    loadFont("sans.ttf", 24);
    setText("blablabla");

    while(window!=NULL){
        while(SDL_PollEvent(&event)){
            if(event.type==SDL_QUIT)
                window=NULL;
            if(event.key.keysym.scancode==SDL_SCANCODE_ESCAPE)
                window=NULL;
        }

        glClearColor(0, 0, 0, 0);
        glClear(GL_COLOR_BUFFER_BIT);

        glViewport(0, 0, 800, 800);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 800, 800, 0, -10, 10);

        display();

        SDL_GL_SwapWindow(window);
    }
}

Hi and welcome to the forum.

In the variable color, which is used to set the color of the text, the alpha value is set to 0, is this intentional?

This is not intentional

Try to set the alpha value to 255.
You have also set the alpha value of glClearColor to 0, which is probably not intentional either. Try setting it to 1.0f.

Try setting those two values and check the result.

I tried, but still the same red remained blue ; )

Is is still rendered the same or did something change after you changed the values?

After I changed the values ​​all the same red appears in blue!

I have a feeling it has something to do with the color channels. I’m not sure in what format TTF_RenderText_Blended() represents the color channels, if it’s RGBA, ARGB etc. Might be a mix-up when passing the pixels from the SDL_Surface to the glTexImage2D() function, which expects the image to be in RGBA format.

How can I convert surface in RGBA format?

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, surface->w, surface->h, 0, 
                            GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);

I found the answer!

Glad you got it fixed.

I just want to note that I just checked TTF_RenderText_Blended() and that function returns an SDL_Surface in ARGB format, so you have to convert it to a proper format before giving it to Opengl.

1 Like