# Question about mixing SDL\_Renderer and OpenGL Context \[Solved\]

**URL:** <https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767>\
**Category:** SDL Development\
**Created:** [September 10, 2021, 2:44am UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767 "2021-09-10T02:44:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny-E33](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/858c86/32.png) [@Danny-E33](https://discourse.libsdl.org/u/Danny-E33)\
**Post date:** [September 10, 2021, 2:44am UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/1 "2021-09-10T02:44:04Z")

</div>

For starters, hello, I’m new here. I’ve been experimenting with SDL2 for a few months and I absolutely love it and I am incredibly grateful to everyone who helps make it happen.

This is a question about retroactively adding an OpenGL context (really about adding Dear ImGui) to a project that is already deeply committed to using `SDL_Renderer`.

Firstly, I’m aware that SDL 2.0.18 (and therefore [https://github.com/ocornut/imgui/pull/3926](https://github.com/ocornut/imgui/pull/3926)) is on the horizon, which would completely solve my issue but I wanted to ask my question anyway, at least to improve my understanding of the problem (I’m curious of the technical details) or, if possible, to find a reasonable workaround to hold me over until 2.0.18.

I have code like the following:

```cpp
bool Graphics::beginFrame() const {
	if (imguiInitialized) {
		ImGui_ImplSDL2_NewFrame();
		ImGui_ImplOpenGL3_NewFrame();
		ImGui::NewFrame();
	}

	setDrawColor(Color::BLACK);
	if (SDL_RenderClear(renderer.get()) < 0) {
		SDL_Log("Graphics::beginFrame - SDL_RenderClear failed: %s\n", SDL_GetError());
		return false;
	}
	return true;
}

void Graphics::endFrame() const {
	if (imguiInitialized) {
		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
		// SDL_GL_SwapWindow(window.get());
	}

	SDL_RenderPresent(renderer.get());
}

```

If I _uncomment_ `SDL_GL_SwapWindow` then I get the flickering described here: [Problem with SDL\_RenderPresent() + SDL\_GL\_SwapWindow()](https://discourse.libsdl.org/t/problem-with-sdl-renderpresent-sdl-gl-swapwindow/338)  
which makes perfect sense.

The strange thing is, if I leave `SDL_GL_SwapWindow` commented out, then on Linux I get the perfect outcome. All ImGui windows are drawn on top of everything that was drawn with `SDL_Renderer`.  
But on Windows and Mac, if I leave `SDL_GL_SwapWindow` commented out, then only things drawn with `SDL_Renderer` will be visible and all ImGui windows are invisible (but of course they are still fully functional with the mouse and keyboard).

Why might the OpenGL context and SDL Renderer play so nicely on Linux but not on Windows or Mac?  
And is there possibly a hacky workaround I can use to get it to work just the same on Windows and Mac for the time being?

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [September 10, 2021, 7:55am UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/2 "2021-09-10T07:55:49Z")

</div>

hello,

I’m not 100% sure, but maybe this is the fact that on Mac and Window, the SDL Renderer isn’t using an OpenGL backend, but the Metal back-end on Mac and D3D11 back-end on Window.

You can check on Mac, by forcing the OpenGl back-end for SDL\_Renderer using  
`SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");`  
before creating the window.

Btw: about the flickering Problem with SDL\_RenderPresent() + SDL\_GL\_SwapWindow():  
SDL\_RenderPresent ends up being a swap window.  
So if you call the two functions. you present two frames successively.  
(1 correct + 1 blank, or 1 imgui + 1 sdl, depending on where you place the calls).

---

<div class="post-metadata">

**Author:** ![Danny-E33](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/858c86/32.png) [@Danny-E33](https://discourse.libsdl.org/u/Danny-E33)\
**Post date:** [September 10, 2021, 2:45pm UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/3 "2021-09-10T14:45:08Z")

</div>

Wow, that absolutely fixed the issue (for Windows)! Thanks for the tip.  
I naively assumed that by including `SDL_WINDOW_OPENGL` in my `SDL_WindowFlags` when calling `SDL_CreateWindow` that it would also force the `SDL_Renderer` to use OpenGL as well.

However, by adding `SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl")` on Mac, that is causing ImGui initialization to fail with:

```bash
ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile vertex shader! With GLSL: #version 150

ERROR: 0:1: '' : version '150' is not supported

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile fragment shader! With GLSL: #version 150

ERROR: 0:1: '' : version '150' is not supported

ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link shader program! With GLSL #version 150

ERROR: One or more attached shaders not successfully compiled

```

Obviously, GLSL version 150 works just fine on Mac when I do not include `SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl")`.  
I know we’re getting a bit more off topic from the original issue, but any ideas why this might be happening or how to fix it?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [September 10, 2021, 3:43pm UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/4 "2021-09-10T15:43:39Z")

</div>

> [@Danny-E33](#):
>
> more off topic from the original issue, but any ideas why this might be happening or how to fix it?

still not sure, but maybe try the imgui opengl2 example

---

<div class="post-metadata">

**Author:** ![brada](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@brada](https://discourse.libsdl.org/u/brada)\
**Post date:** [September 10, 2021, 4:12pm UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/5 "2021-09-10T16:12:52Z")

</div>

`SDL_Renderer` creates an OpenGL 2 (on Mac at least) so you wont be able to use OpenGL 3 with it unless there is a way I dont know of to make `SDL_Renderer` create a 3 context or use an already created context.

---

<div class="post-metadata">

**Author:** ![Danny-E33](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/858c86/32.png) [@Danny-E33](https://discourse.libsdl.org/u/Danny-E33)\
**Post date:** [September 10, 2021, 5:13pm UTC](https://discourse.libsdl.org/t/question-about-mixing-sdl-renderer-and-opengl-context-solved/32767/6 "2021-09-10T17:13:44Z")

</div>

Yep, switching to imgui\_impl\_opengl2 fixed my issue on Mac. Many thanks to both of you.
