SDL2 window lagging when using OpenGL

I made a GitHub repo with my tests if anyone wants to have a look at what I wrote.

So long story short, using the SDL renderer to draw to the window works great, but just opening the window when the SDL_WINDOW_OPENGL flag is used, makes the entire window lag. It stops responding, moving doesn’t always work, trying to scale the window result in freezing and a “Not responding” error.

I don’t know what I’m doing wrong as it is my first attempt at using SDL and OpenGL with C.
I did try to use it with Rust and that seemed to work Ok-ish but this time is just not working as I expected.
System Monitor doesn’t report any spikes in memory usage or CPU when using OpenGL.

I am using:

OS: Fedora Linux 37 x86_64
Kernel: 6.1.15-200.fc37.x86_64
Shell: bash 5.2.15
DE: GNOME 43.3
CPU: AMD Ryzen 5 5600X (12) @ 3.700GHz
GPU: AMD ATI Radeon RX 6700/6700 XT/6750 XT / 68 
Memory: 4047MiB / 32007MiB 

Link to the GitHub - ionutrogojan/test_SDL2_OpenGL

Am I doing something wrong or why does the SDL2 renderer version not lag while the OpenGL version lags?

I have not used OpenGL with SDL yet but I do notice one thing. The end of your while loop may need an SDL_Delay(). It looks like your rendering as fast as your computer will go. From what I understand there is generally an SDL_Delay() call at the end of the while loops to slow down the FPS. In a tetris clone that I am working on I have some logic to target the FPS down to 144. Without this logic the program will run at thousands of frames per second, though I didn’t see the same slowdown in my application that you are describing even without frame rate throttling. I would try at least SDL_Delay(1) for 1 ms at the very least. Maybe try 10 or 100 as well and see if you notice a change.


Does it help if you use a loop to handle the events?

while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT) {
        isRunning = false;
1 Like

I tried using the SDL_Delay() function in the OpenGL implementation and I got even weirder results.
Anything bellow 30ms seems to make the window move a little better but still very laggy.
Anything over 30ms makes the entire window freeze, all the controls are unresponsive and I’m getting the "test_opengl_sdl_c" stopped responding error almost as soon as I click on wait.

None of this happens when I’m using the SDL_Renderer

Both examples I wrote uses the SDL_PoolEvent to handle input. Please see the code on Github

Your example code doesn’t use a while-loop with SDL_PollEvent, so it only removes a maximum of one event from the queue each frame. If you change the if to a while, it will process all pending events that happened since the last frame instead of just one of them. This makes sure events don’t get backed up in the queue.

1 Like

I know. I meant you should change the if statement into a while loop so that you make sure to handle all the events. See the code that I posted above.

In main.c VSYNC was probably not enabled so the program manage to poll the events fast enough.

In opengl.c VSYNC is enabled. If the the refresh rate of your monitor is 60 Hz that means you’re only handle 60 events per frame.

The problem I experience with opengl.c is that closing the window often gets delayed by several seconds. Especially if I move the mouse cursor over the window first. Making the change I suggested above fixes the problem.

I am so sorry, I didn’t notice the difference between what you wrote from what I did. That was my bad.
This makes so much more sense and actually fixed it. I can’t imagine how I didn’t think of that.

Legend!! Thank you! :slightly_smiling_face:

That was my bad. I didn’t notice the change from Peter87

That makes a lot more sense. I didn’t think of that. Thank you :slightly_smiling_face: