Imgui + SDL_Renderer + OpenGL context flickering

Here is my problem. I’ve successfully brought Imgui into my game which is using SDL_Renderer to render using SDL_Textures (My game is a simple 2D top down game) Imgui is using OpenGL and when rendering both contexts they just flicker in and out. So this is not usable at all currently. I’ve tried looking for 12 hours to find a full example that shows how to convert a SDL_Texture to a OpenGL texture but all I’ve come across is bits and pieces of code on StackOverflow.

Do any of you know how to take a SDL_Texture or even a SDL_Surface and convert that to a OpenGL texture? If I can do this then I can use Imgui in my game which I really want to us. Any full examples in C++ out there?

I thought this would work. It’s from 2011 so I was skeptical. I was not able to get it to work.

I have successfully used SDL_GL_BindTexture() to achieve this. Having bound the texture to the OpenGL context you can retrieve its ‘name’ by calling glGetIntegerv() with the GL_TEXTURE_BINDING_2D parameter.

Thank you. I have tried SDL_GL_BindTexture but it was coming back with an error saying it’s not supported. Maybe I was doing something wrong. I do have Imgui working just fine so I’m skeptical it’s something like incorrectly set up OpenGL drivers/headers.

Are you sure SDL is using the OpenGL backend? If necessary include this before your SDL_CreateWindow():


Thank you, I will try your suggestion. I know Imgui which I embedded in my SDL game is using OpenGL because I set the OpenGL backend up for it.

Do you have a minimal working example that shows the flickering?

That said, as said above, make sure you are on the opengl (or perhaps opengles?) renderer using SDL_GetRenderDriverInfo.

Are you mixing SDL_GL_SwapWindow and SDL_RenderPresent ? That will cause flickering, because with that you are updating your window twice.

You can can get away with mixing native opengl and the renderer pretty well [1], but you must only call one function that updates the window. IIRC it doesn’t matter which one. But it must be at the end of your drawing (after both your renderer-related and your imgui-related calls).

Hope this helps

[1] If you re-enabled batching via SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); after setting an explicit render driver via SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); or similar you must call SDL_RenderFlush before doing native opengl (calling imguis opengl functions etc).

Thank you for the suggestions. I’ll try this. I am currently mixing SDL_GL_SwapWindow and SDL_RenderPresent because I had no idea what I was doing. I thought the way forward was to take my SDL_Textures and somehow convert them to OpenGL textures so that my game would render and also Imgui would render. I don’t have a minimal example but will put one together.