[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
pull/109/head
jackun 4 years ago
parent f5e3f3b73b
commit 6eb2c9bc09
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -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();
}
}

@ -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();
}

@ -24,6 +24,8 @@
#include <chrono>
#include <iomanip>
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) {

Loading…
Cancel
Save