From 6eb2c9bc095a6f0145c88981c2cebd1ea6d4fb31 Mon Sep 17 00:00:00 2001 From: jackun Date: Sun, 29 Mar 2020 01:47:24 +0200 Subject: [PATCH] [OpenGL] Explicitly set ImGui's context and treat global context as foreign Apps might be using Dear ImGui so keep our stuff separate from theirs. Issue #107 --- src/gl/imgui_impl_opengl3.cpp | 3 +++ src/gl/imgui_impl_opengl3.h | 4 ++++ src/gl/inject.cpp | 12 +++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/gl/imgui_impl_opengl3.cpp b/src/gl/imgui_impl_opengl3.cpp index 6f0f6f2..cfd502c 100644 --- a/src/gl/imgui_impl_opengl3.cpp +++ b/src/gl/imgui_impl_opengl3.cpp @@ -77,6 +77,8 @@ void* get_glx_proc_address(const char* name); void (*glClipControl)(int origin, int depth); +namespace MangoHud { + // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. #if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) || !defined(GL_VERSION_3_2) #define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 0 @@ -625,3 +627,4 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects() ImGui_ImplOpenGL3_DestroyFontsTexture(); } +} diff --git a/src/gl/imgui_impl_opengl3.h b/src/gl/imgui_impl_opengl3.h index a8866b7..97044aa 100644 --- a/src/gl/imgui_impl_opengl3.h +++ b/src/gl/imgui_impl_opengl3.h @@ -23,6 +23,8 @@ #pragma once +namespace MangoHud { + // Backend API IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); @@ -34,3 +36,5 @@ IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); + +} diff --git a/src/gl/inject.cpp b/src/gl/inject.cpp index 67bd358..8a4f281 100644 --- a/src/gl/inject.cpp +++ b/src/gl/inject.cpp @@ -24,6 +24,8 @@ #include #include +using namespace MangoHud; + #define EXPORT_C_(type) extern "C" __attribute__((__visibility__("default"))) type EXPORT_C_(void *) glXGetProcAddress(const unsigned char* procName); @@ -153,6 +155,9 @@ void imgui_create(void *ctx) state.font1 = io.Fonts->AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_size * 0.55, &font_cfg, glyph_ranges); } sw_stats.font1 = state.font1; + + // Reset global context to null, might clash with apps that use Dear ImGui + ImGui::SetCurrentContext(nullptr); } void imgui_shutdown() @@ -162,6 +167,7 @@ void imgui_shutdown() #endif if (state.imgui_ctx) { + ImGui::SetCurrentContext(state.imgui_ctx); ImGui_ImplOpenGL3_Shutdown(); ImGui::DestroyContext(state.imgui_ctx); state.imgui_ctx = nullptr; @@ -183,9 +189,12 @@ void imgui_set_context(void *ctx) void imgui_render() { - if (!ImGui::GetCurrentContext()) + if (!state.imgui_ctx) return; + ImGuiContext *saved_ctx = ImGui::GetCurrentContext(); + ImGui::SetCurrentContext(state.imgui_ctx); + // check which one is affected by window resize and use that GLVec vp; glGetIntegerv (GL_VIEWPORT, vp.v); GLVec sb; glGetIntegerv (GL_SCISSOR_BOX, sb.v); @@ -224,6 +233,7 @@ void imgui_render() ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui::SetCurrentContext(saved_ctx); } void* get_proc_address(const char* name) {