I am reorganizing context management functions to be more explicit.
I have created a branch "context" to test those changes, and would very much like feedback on them!
https://github.com/ocornut/imgui/tree/context
The purpose of those changes is:
Those topics are discussed in e.g. #586, #992, #1007, #1558
Those changes DO NOT address multi-threading issues from #586.
Also note that the incoming virtual viewport feature (#1542) will allow you to manage multiple OS windows with a single context, therefore multiple context will become _less_ useful and less exercised onward :) but we should still support them.
ImGui::CreateContext() AT THE BEGINNING OF YOUR APP, AND CALL ImGui::DestroyContext() AT THE END. Shutdown() function, as DestroyContext() serve this purpose.CreateContext() to share a font atlas between contexts. Otherwise CreateContext() will create its own font atlas instance and destroy it on DestroyContext().CreateContext(), they are now setup with SetAllocatorFunctions(), and shared by all contexts. Added a void* user_data to allocators function.IMGUI_DISABLE_DEFAULT_ALLOCATORS imconfig.h directive for people who don't want to link with malloc/free at all.Those changes are touchy because it will break existing codebase:
I think I should push those change to master as they are for the better but would really like feedback from anyone using multiple contexts, multiple font atlas or DLL reloading. If you could quickly try to merge the context branch, see if it works for you. Any feedback on the changes are welcome..
Tagging people involved or who may be interested in this.. Sorry for the tag-spam.
@aiekick @sonoro1234 @zx64 @thedmd @zlnimda @Pagghiu @itamago @galloman @matteomandelli @pdoane-blizzard
Thank you!
I love it.
Hi there, thanks for the update !
I just pulled and tested your changes and it worked fine to me.
Initialization and destruction is far better intuitive this way.
Also, I really like the global Set of Alloc/Free callbacks SetAllocatorFunctions() and IMGUI_DISABLE_DEFAULT_ALLOCATORS . (By the way the define is missing in imconfig.h, so I added it)
I tested with a single shared font right now and the instance was not loaded from a dynamic library.
It didn't change much to me, I just removed the Get/Set of default context which was useless to my use case, removed the Shutdown(), called SetAllocatorFunctions() instead of set functions on each context, and pre-allocated my own shared ImFontAtlas.
Coincidentally this weekend I was working around a shutdown crash due to the global font atlas getting destructed after a context was disposed. This mod is most welcome.
This looks a very nice addition!
I should admit though that I will be even happier when we will have an ImGui object to use instead of free functions operating on a globally set context, but I can wait some more time for such a breaking change ;)
Thanks @ocornut !
Works here.
Coalescing context shutdown is welcomed. I've made the goof twice of using ImGui::DestroyContext without first using ImGui::Shutdown.
Works great for us, a welcome improvement, thanks!
I did similar thing this weekend before I noticed this thread. :) I tried the official code from the branch and it works great. Good job!
This is now merged, I've also merged the Navigation branch and bumped versions to 1.60.
Will make a more visible forum post to help people to know about CreateContext.
I've added asserts to make this more visible:
ImGuiIO& ImGui::GetIO()
{
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?");
return GImGui->IO;
}
ImGuiStyle& ImGui::GetStyle()
{
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?");
return GImGui->Style;
}
Most helpful comment
This looks a very nice addition!
I should admit though that I will be even happier when we will have an ImGui object to use instead of free functions operating on a globally set context, but I can wait some more time for such a breaking change ;)
Thanks @ocornut !