Support for #define-ing GImGui and IMGUI_SET_CURRENT_CONTEXT_FUNC to enable custom thread-based hackery (#586)

This commit is contained in:
ocornut 2016-11-12 17:08:31 +01:00
parent e0aef0018f
commit 98e1d500d4
2 changed files with 17 additions and 6 deletions

View File

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

View File

@ -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