1
votes

In my project I use SDL2 + OpenGL. There is simple code. Function VPreRender() is called when I want to clear buffer and start to draw in it. After that, when I want to show this buffer on the screen, I use functions VPostRender().

When the program is started with integrated video card (HD Intel), the window does not blink when it appears. But when I start the program with High-performanced NVIDIA video card, it starts blinking for some seconds and then stops. Then it all looks fine. The same situation appears if I want to resize the window. So what I do wrong? Why is the window blinking?

bool SDLRender::VPreRender()
{
    glClearColor(m_backgroundColor.r, m_backgroundColor.g, m_backgroundColor.b, m_backgroundColor.a);
    glClear ( GL_COLOR_BUFFER_BIT );
}

bool SDLRender::VPostRender()
{
    SDL_GL_SwapWindow(m_pWindow);
}
1
Is this the only place you call SDL_GL_SwapWindow()? This is just a wild guess, but you could get these types of symptoms if you swap multiple times per frame.Reto Koradi
@RetoKoradi Ohh, how i could not notice this. Yes, I call SDL_GL_SwapWindow() 2 times. So now program works great.Andrej

1 Answers

2
votes

The behavior you are describing is a typical symptom of calling the window system swap function multiple times per frame.

The result is that frames that you have not rendered are shown on the display. Say if the system uses plain double buffering, and you make two swap calls, the first swap call would present the buffer with the frame you rendered, but the immediately following second swap call would replace it with the other buffer, which could contain anything.

How buffers are handled during rendering and the desktop composition process is very system dependent. So it's not surprising that the exact symptoms are system dependent, or could even change over time. Blinking between valid and invalid frames is certainly one of the possibilities.

To avoid this, you need to make sure that you call SDL_GL_SwapWindow() exactly once per frame, after you finished all your rendering.