imgui icon indicating copy to clipboard operation
imgui copied to clipboard

SDL3 backend: No window retargeting?

Open alexgu754 opened this issue 3 weeks ago • 5 comments

Version/Branch of Dear ImGui:

Version 1.XX, Branch: XXX (master/docking/etc.)

Back-ends:

imgui_impl_XXX.cpp + imgui_impl_XXX.cpp

Compiler, OS:

macOS + Clang++12

Full config/build information:

Git clone of latest main branch, using sdl3 + sdl3 gpu

Details:

Image

Didn't know where else to ask this, just wanted to know why there was no explicit function to set the target window we're rendering to? Without shutting down and calling init again, I think it'd be useful

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// Here's some code anyone can copy and paste to reproduce your issue
ImGui::Begin("Example Bug");
MoreCodeToExplainMyIssue();
ImGui::End();

alexgu754 avatar Dec 03 '25 11:12 alexgu754

Didn't know where else to ask this,

This is the right place.

just wanted to know why there was no explicit function to set the target window we're rendering to?

There's no Rendering done by the SDL3 backend.

Without shutting down and calling init again, I think it'd be useful

Why? Which renderer are you using? Can you clarify your use case?

ocornut avatar Dec 03 '25 12:12 ocornut

I'm making a game engine wrapper around SDL3gpu and imgui, and I have a function set_target_window() to render onto a second window

so the correct approach would be to shut down imgui every frame and re init it to the new window? wouldn't that be wasteful? I can see it's caching the window pointer and using it for mouse inputs and render bounds, so I can't get away with not switching the context window

alexgu754 avatar Dec 03 '25 16:12 alexgu754

I'm making a game engine wrapper around SDL3gpu and imgui, and I have a function set_target_window() to render onto a second window

Please clarify as this is too ambiguous to understand. Are you trying to render the exact same imgui contents into 2 os/platform windows? (therefore of the same size) Are you trying to render and interact with different imgui windows simultaneously into multiple os/platform windows? What do you have in those mutiple os/platform windows beside dear imgui stuff ?

so the correct approach would be to shut down imgui every frame and re init it to the new window?

I cannot give you the correct approach without understanding what you are trying to do.

ocornut avatar Dec 03 '25 16:12 ocornut

I'm trying to use imgui across multiple windows, the windows have completely different content.

Well not me personally, but my game engine api that has a window class, so the user can create multiple windows. the engine must be able to render to one window, then switch to the second window, then the user makes new imgui calls and they are displayed in the second window, then the game while loop starts again. So I thought there'd be a function like void ImGui_ImplSDL3_SwitchWindows(SDL_Window* newWindow); to make input and viewport relative to that window but there isn't. am I making sense?

alexgu754 avatar Dec 03 '25 16:12 alexgu754

I now understand what you are trying, but it's not going to work like that at all. There are ample persisting states required if only for interaction with OS inputs, you cannot take one imgui context and move it from window to window.

(1) The simplest way would you to create 1 imgui context per os/platform window, and initialize the backend for each of them, then you can switch context with ImGui::SetCurrentContext(). The drawback is the contexts won't communicate between each other (separate .ini settings, no drag and drop or docking cross contexts), which you might decide is actually a desirable feature. But it is overally simple.

Otherwise,

(2) Dear ImGui already support multi-viewports (https://github.com/ocornut/imgui/wiki/Multi-Viewports) in the docking branch, aka 1 imgui context can manage imgui windows in multiple os/platform windows. Which may naturally and by default resolve your actual need. The way it currently works is that you have 1 main os/platform window and then imgui windows can freely be moved around and extracted outside of the os/platform window.

(3) An extension of (2) depending on the nature of your 2+ windows and the nature of your imgui windows, is that you might want to create matching ImGuiViewport for each of your os/platform windows and mark all of them as ImGuiViewportFlags_OwnedByApp and ImGuiViewportFlags_CanHostOtherWindows, this way imgui windows can be merged into them without creating more os/platform windows. This is in theory not official supported but I suspect it already works (see #9095). It's just a little bit advanced and considering that you don't seem to know about multi-viewports I wouldn't recommend wandering there just yet and you probably don't need this now.

ocornut avatar Dec 03 '25 17:12 ocornut

@alexgu754 Have you been able to look into this? My guess is that (1) is closer you what you wanted to do.

ocornut avatar Dec 10 '25 17:12 ocornut