# Multiple windows and interop with ImGui

**URL:** <https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819>\
**Category:** Using Cinder\
**Created:** [May 3, 2021, 9:41am UTC](https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819 "2021-05-03T09:41:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 3, 2021, 9:41am UTC](https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819/1 "2021-05-03T09:41:58Z")

</div>

Hi there, I’m trying to add a second “preview” window to my app. The main window has an ImGui UI in it, and when I create a second window (which just displays a texture/FBO), I start to get problems. I think this is because the ImGui context gets mixed up between the two windows.

I see the Options flag `autoRender`, which prevents `ImGui::NewFrame()` and `ImGui::Render()` from being called automatically. So I figured I could insert them at the appropriate places, for my main window. However, it seems the Cinder implementation does some extra stuff, which is probably important to co-exist with Cinder (the default implementation calls `ImGui_ImplCinder_NewFrameGuard()` and `ImGui_ImplCinder_PostDraw()`, which are hidden (static) functions in CinderImGui.cpp, so I can’t directly call them where I need to. They do call `ImGui::NewFrame()` and `ImGui::Render()`, but also do other things…

Any tips on the right way to do this? If necessary, I can try to extract out a minimal repro example, but maybe someone has run into this before.

Thanks,  
Glen.

P.S. Things “mostly” seem to be working fine when I open the new window (leaving `autoRender` at the default value of true), but definitely get screwed up when I move the second preview window to a different display. Also, when I close the second window it crashes, because the ImGui code is trying to access an invalid/destroyed Window.

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 3, 2021, 11:30am UTC](https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819/2 "2021-05-03T11:30:47Z")

</div>

Looking at this more, I think the Cinder ImGui code looks mostly right…the window you setup ImGui with is the one that gets the `postDraw` signal and all the others, for example. The problem is with `update` – if you have multiple Windows, it seems indeterministic which one will be current when it’s called… And so, **aha!** the problem seems to be in the use of app::getWindow()-\>toPixels() rather than using the proper window that was registered for use with ImGui. I’ll try giving this a fix…should be pretty simple.

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 3, 2021, 12:05pm UTC](https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819/3 "2021-05-03T12:05:23Z")

</div>

Fixed here:

[Fix glitches when using ImGui in an app with multiple windows by totalgee · Pull Request #2242 · cinder/Cinder (github.com)](https://github.com/cinder/Cinder/pull/2242)

---

<div class="post-metadata">

**Author:** ![totalgee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/totalgee/32/274_2.png) [@totalgee](https://discourse.libcinder.org/u/totalgee)\
**Post date:** [May 3, 2021, 12:17pm UTC](https://discourse.libcinder.org/t/multiple-windows-and-interop-with-imgui/1819/4 "2021-05-03T12:17:46Z")

</div>

~~The other thing I needed to do (besides the above fix) was move my ImGui “layout” code into the `draw()` callback for my main window, not in the `update()` function, where I had it previously.~~

**UPDATE:** Actually, with my fix there is no need to do that, it seems fine to call ImGui functions to build your UI from update(), even when the current window happens not to be the ImGui one.
