From 98e1d500d4ca3fe6ac025e50f35a0ae29a48d6fa Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 12 Nov 2016 17:08:31 +0100 Subject: [PATCH] Support for #define-ing GImGui and IMGUI_SET_CURRENT_CONTEXT_FUNC to enable custom thread-based hackery (#586) --- imgui.cpp | 19 ++++++++++++++----- imgui_internal.h | 4 +++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 28e1c4c3d..75c83fdb9 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -720,14 +720,19 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); // Context //----------------------------------------------------------------------------- -// Default context, default font atlas. +// Default font atlas storage . // New contexts always point by default to this font atlas. It can be changed by reassigning the GetIO().Fonts variable. -static ImGuiContext GImDefaultContext; static ImFontAtlas GImDefaultFontAtlas; -// Current context pointer. Implicitely used by all ImGui functions. Always assumed to be != NULL. Change to a different context by calling ImGui::SetCurrentContext() -// ImGui is currently not thread-safe because of this variable. If you want thread-safety to allow N threads to access N different contexts, you might work around it by (A) having two instances of the ImGui code under different namespaces or (B) change this variable to be TLS. Further development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 +// Default context storage + current context pointer. +// Implicitely used by all ImGui functions. Always assumed to be != NULL. Change to a different context by calling ImGui::SetCurrentContext() +// ImGui is currently not thread-safe because of this variable. If you want thread-safety to allow N threads to access N different contexts, you might work around it by: +// - Having multiple instances of the ImGui code compiled inside different namespace (easiest/safest, if you have a finite number of contexts) +// - or: Changing this variable to be TLS. You may #define GImGui in imconfig.h for further custom hackery. Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 +#ifndef GImGui +static ImGuiContext GImDefaultContext; ImGuiContext* GImGui = &GImDefaultContext; +#endif //----------------------------------------------------------------------------- // User facing structures @@ -2044,7 +2049,11 @@ ImGuiContext* ImGui::GetCurrentContext() void ImGui::SetCurrentContext(ImGuiContext* ctx) { +#ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC + IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. +#else GImGui = ctx; +#endif } ImGuiContext* ImGui::CreateContext(void* (*malloc_fn)(size_t), void (*free_fn)(void*)) @@ -2063,7 +2072,7 @@ void ImGui::DestroyContext(ImGuiContext* ctx) ctx->~ImGuiContext(); free_fn(ctx); if (GImGui == ctx) - GImGui = NULL; + SetCurrentContext(NULL); } ImGuiIO& ImGui::GetIO() diff --git a/imgui_internal.h b/imgui_internal.h index 08f206adc..395973786 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -67,7 +67,9 @@ namespace ImGuiStb // Context //----------------------------------------------------------------------------- -extern IMGUI_API ImGuiContext* GImGui; // current implicit ImGui context pointer +#ifndef GImGui +extern IMGUI_API ImGuiContext* GImGui; // Current implicit ImGui context pointer +#endif //----------------------------------------------------------------------------- // Helpers