I've been trying to have text rendering in my OpenGL scene using SDL2. The tutorial I came across is this one Rendering text

I followed the same code, and I get text rendering fine. However the issue I'm having is that there is obviously a "conflict" between the OpenGL rendering and the SDL_Renderer used in the code. When I run my scene the text displays but everything else keeps flickering. The following gif shows the issue:

enter image description here

Any idea on how to overcome this while still just using SDL2 and OpenGL, not another library or anything.


1 Answer 1


Try render SDL text in texture like:

SDL_Texture* renderText(const std::string &message, const std::string &fontFile,
    SDL_Color color, int fontSize, SDL_Renderer *renderer)
    //Open the font
    TTF_Font *font = TTF_OpenFont(fontFile.c_str(), fontSize);
    if (font == nullptr){
        logSDLError(std::cout, "TTF_OpenFont");
        return nullptr;
    //We need to first render to a surface as that's what TTF_RenderText
    //returns, then load that surface into a texture
    SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
    if (surf == nullptr){
        logSDLError(std::cout, "TTF_RenderText");
        return nullptr;
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
    if (texture == nullptr){
        logSDLError(std::cout, "CreateTexture");
    //Clean up the surface and font
    return texture;

See http://www.willusher.io/sdl2%20tutorials/2013/12/18/lesson-6-true-type-fonts-with-sdl_ttf/

Bind SDL_Texture

void SDL_BindTexture(SDL_Texture *t)
  glBindTexture(GL_TEXTURE_2D, t->id);

And draw using glDrawArrays() like:

const GLfloat quadVertices[] = { -1.0f, 1.0f, 0.0f, 
    1.0f, 1.0f, 0.0f, 
    1.0f,-1.0f, 0.0f,
    -1.0f,-1.0f, 0.0f

glVertexPointer(3, GL_FLOAT, 0, quadVertices);
glDrawArrays(GL_QUADS, 0, 4);
  • Do I need a shader in this case? Or is there some default thing OpenGL uses.
    – Kakalokia
    Mar 5, 2015 at 15:15
  • For OpenGL - no. OpenGLES - yes. Mar 5, 2015 at 15:19
  • Sorry this may not be a good question, but what do I pass to glDrawArrays in this case?
    – Kakalokia
    Mar 5, 2015 at 15:24
  • Thanks for your replies. However this still doesn't show anything. Also I can't access the id variable of SDL_Texture.
    – Kakalokia
    Mar 5, 2015 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.