# SDL2 window lagging when using OpenGL

**URL:** <https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076>\
**Category:** SDL Development\
**Created:** [March 12, 2023, 4:43am UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076 "2023-03-12T04:43:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_sadcat](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mr_sadcat/32/4978_2.png) [@mr\_sadcat](https://discourse.libsdl.org/u/mr_sadcat)\
**Post date:** [March 12, 2023, 4:43am UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/1 "2023-03-12T04:43:50Z")

</div>

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:

```plaintext
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](https://github.com/ionutrogojan/test_SDL2_OpenGL)

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

---

<div class="post-metadata">

**Author:** ![electrosys](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@electrosys](https://discourse.libsdl.org/u/electrosys)\
**Post date:** [March 12, 2023, 5:14am UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/2 "2023-03-12T05:14:14Z")

</div>

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.

Electrosys

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [March 12, 2023, 8:11am UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/3 "2023-03-12T08:11:29Z")

</div>

> [@mr\_sadcat](#):
>
> It stops responding, moving doesn’t always work, trying to scale the window result in freezing and a “Not responding” error.

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

```auto
while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT) {
        isRunning = false;
    }
}

```

---

<div class="post-metadata">

**Author:** ![mr\_sadcat](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mr_sadcat/32/4978_2.png) [@mr\_sadcat](https://discourse.libsdl.org/u/mr_sadcat)\
**Post date:** [March 12, 2023, 3:18pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/4 "2023-03-12T15:18:56Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![mr\_sadcat](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mr_sadcat/32/4978_2.png) [@mr\_sadcat](https://discourse.libsdl.org/u/mr_sadcat)\
**Post date:** [March 12, 2023, 3:21pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/5 "2023-03-12T15:21:16Z")

</div>

> [@Peter87](#):
>
> Does it help if you use a **loop** to handle the events?

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

> [@mr\_sadcat](#):
>
> Link to the [GitHub - ionutrogojan/test\_SDL2\_OpenGL](https://github.com/ionutrogojan/test_SDL2_OpenGL)

---

<div class="post-metadata">

**Author:** ![slime](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/slime/32/3845_2.png) [@slime](https://discourse.libsdl.org/u/slime)\
**Post date:** [March 12, 2023, 3:31pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/6 "2023-03-12T15:31:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [March 12, 2023, 3:31pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/7 "2023-03-12T15:31:55Z")

</div>

> [@mr\_sadcat](#):
>
> Both examples I wrote uses the `SDL_PoolEvent` to handle input. Please see the code on Github

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.

---

<div class="post-metadata">

**Author:** ![mr\_sadcat](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mr_sadcat/32/4978_2.png) [@mr\_sadcat](https://discourse.libsdl.org/u/mr_sadcat)\
**Post date:** [March 12, 2023, 3:40pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/8 "2023-03-12T15:40:32Z")

</div>

> [@Peter87](#):
>
> 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.

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! 🙂

---

<div class="post-metadata">

**Author:** ![mr\_sadcat](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mr_sadcat/32/4978_2.png) [@mr\_sadcat](https://discourse.libsdl.org/u/mr_sadcat)\
**Post date:** [March 12, 2023, 3:42pm UTC](https://discourse.libsdl.org/t/sdl2-window-lagging-when-using-opengl/43076/9 "2023-03-12T15:42:25Z")

</div>

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

> [@slime](#):
>
> 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.

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