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) 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:
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()
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.