@@ -104,18 +104,16 @@ void imgui_init()
104104 init_cpu_stats (params);
105105}
106106
107- // static
108- void imgui_create (void *ctx, const gl_wsi plat)
107+ void imgui_create (gl_context *ctx, const gl_wsi plat)
109108{
110- if (inited)
109+ // SPDLOG_DEBUG("ctx {}", (void *)ctx);
110+ if (!ctx)
111111 return ;
112112
113- if (!ctx )
113+ if (inited )
114114 return ;
115115
116- imgui_shutdown ();
117116 imgui_init ();
118- inited = true ;
119117
120118 if (!gladLoadGL ())
121119 spdlog::error (" Failed to initialize OpenGL context, crash incoming" );
@@ -160,6 +158,7 @@ void imgui_create(void *ctx, const gl_wsi plat)
160158 IMGUI_CHECKVERSION ();
161159 ImGuiContext *saved_ctx = ImGui::GetCurrentContext ();
162160 state.imgui_ctx = ImGui::CreateContext ();
161+ ImGui::SetCurrentContext (state.imgui_ctx );
163162#ifdef __linux__
164163 ImPlot::CreateContext ();
165164#endif
@@ -178,35 +177,36 @@ void imgui_create(void *ctx, const gl_wsi plat)
178177 ImGui::GetIO ().IniFilename = NULL ;
179178 ImGui::GetIO ().DisplaySize = ImVec2 (last_vp[2 ], last_vp[3 ]);
180179
181- ImGui_ImplOpenGL3_Init ();
180+ ImGui_ImplOpenGL3_Init (ctx );
182181
183182 create_fonts (nullptr , params, sw_stats.font_small , sw_stats.font_text , sw_stats.font_secondary );
184183 sw_stats.font_params_hash = params.font_params_hash ;
184+ inited = true ;
185185
186186 // Restore global context or ours might clash with apps that use Dear ImGui
187187 ImGui::SetCurrentContext (saved_ctx);
188188}
189189
190- void imgui_shutdown ()
190+ void imgui_shutdown (gl_context *ctx, bool last )
191191{
192+ // SPDLOG_DEBUG("destroying ctx {}, imgui_ctx {}, last {}", (void *)ctx, (void *)state.imgui_ctx, last);
192193 if (state.imgui_ctx ) {
194+ ImGuiContext *saved_ctx = ImGui::GetCurrentContext ();
193195 ImGui::SetCurrentContext (state.imgui_ctx );
194- ImGui_ImplOpenGL3_Shutdown ();
195- ImGui::DestroyContext (state.imgui_ctx );
196- state.imgui_ctx = nullptr ;
196+ ImGui_ImplOpenGL3_Shutdown (ctx);
197+ if (last)
198+ {
199+ ImGui::DestroyContext (state.imgui_ctx );
200+ state.imgui_ctx = nullptr ;
201+ inited = false ;
202+ }
203+ ImGui::SetCurrentContext (saved_ctx);
197204 }
198- inited = false ;
199- }
200-
201- void imgui_set_context (void *ctx, const gl_wsi plat)
202- {
203- if (!ctx)
204- return ;
205- imgui_create (ctx, plat);
206205}
207206
208- void imgui_render (unsigned int width, unsigned int height)
207+ void imgui_render (gl_context *ctx, unsigned int width, unsigned int height)
209208{
209+ // SPDLOG_DEBUG("imgui_ctx {}", (void *)state.imgui_ctx);
210210 if (!state.imgui_ctx )
211211 return ;
212212
@@ -229,10 +229,10 @@ void imgui_render(unsigned int width, unsigned int height)
229229 {
230230 sw_stats.font_params_hash = params.font_params_hash ;
231231 create_fonts (nullptr , params, sw_stats.font_small , sw_stats.font_text , sw_stats.font_secondary );
232- ImGui_ImplOpenGL3_CreateFontsTexture ();
232+ ImGui_ImplOpenGL3_CreateFontsTexture (ctx );
233233 }
234234
235- ImGui_ImplOpenGL3_NewFrame ();
235+ ImGui_ImplOpenGL3_NewFrame (ctx );
236236 ImGui::NewFrame ();
237237 {
238238 std::lock_guard<std::mutex> lk (notifier.mutex );
0 commit comments