diff --git a/examples/Demo/main.cpp b/examples/Demo/main.cpp index efb29de..afe62c6 100644 --- a/examples/Demo/main.cpp +++ b/examples/Demo/main.cpp @@ -84,6 +84,7 @@ int WinMain(int argc, char* argv[]) float deltaTime = updateClock.GetElapsedTime().AsSeconds(); Nz::Imgui::Instance()->Update(window, deltaTime); + ImGui::EnsureContextOnThisThread(); ImGui::Begin("Loop Window"); ImGui::Image(logo.get()); diff --git a/include/NazaraImgui/NazaraImgui.hpp b/include/NazaraImgui/NazaraImgui.hpp index 6e97b1b..0e2267e 100644 --- a/include/NazaraImgui/NazaraImgui.hpp +++ b/include/NazaraImgui/NazaraImgui.hpp @@ -52,6 +52,9 @@ namespace Nz Nz::Vector2f framebufferSize; }; + static ImGuiContext* GetCurrentContext(); + static void GetAllocatorFunctions(ImGuiMemAllocFunc* allocFunc, ImGuiMemFreeFunc* freeFunc, void** userData); + private: void SetupInputs(Nz::WindowEventHandler& handler); void Update(const Nz::Vector2i& mousePosition, const Nz::Vector2ui& displaySize, float dt); @@ -66,6 +69,7 @@ namespace Nz void RenderDrawLists(Nz::RenderTarget* renderTarget, Nz::RenderFrame& frame, ImDrawData* drawData); + ImGuiContext* m_currentContext; std::string m_clipboardText; bool m_bWindowHasFocus; @@ -112,4 +116,16 @@ namespace ImGui NAZARA_IMGUI_API void DrawLine(const Nz::Vector2f& a, const Nz::Vector2f& b, const Nz::Color& col, float thickness = 1.0f); NAZARA_IMGUI_API void DrawRect(const Nz::Rectf& rect, const Nz::Color& color, float rounding = 0.0f, int rounding_corners = 0x0F, float thickness = 1.0f); NAZARA_IMGUI_API void DrawRectFilled(const Nz::Rectf& rect, const Nz::Color& color, float rounding = 0.0f, int rounding_corners = 0x0F); + + void EnsureContextOnThisThread() + { + auto* context = Nz::Imgui::GetCurrentContext(); + SetCurrentContext(context); + + ImGuiMemAllocFunc allocFunc = nullptr; + ImGuiMemFreeFunc freeFunc = nullptr; + void* userData = nullptr; + Nz::Imgui::GetAllocatorFunctions(&allocFunc, &freeFunc, &userData); + SetAllocatorFunctions(allocFunc, freeFunc, userData); + } } diff --git a/src/NazaraImgui/NazaraImgui.cpp b/src/NazaraImgui/NazaraImgui.cpp index 1e89c63..6b29745 100644 --- a/src/NazaraImgui/NazaraImgui.cpp +++ b/src/NazaraImgui/NazaraImgui.cpp @@ -99,6 +99,7 @@ namespace Nz : ModuleBase("Imgui", this) , m_bMouseMoved(false) , m_bWindowHasFocus(false) + , m_currentContext(nullptr) { ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); @@ -123,7 +124,7 @@ namespace Nz bool Imgui::Init(Nz::Window& window, bool bLoadDefaultFont) { - ImGui::CreateContext(); + m_currentContext = ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); // tell ImGui which features we support @@ -343,6 +344,16 @@ namespace Nz return backend->m_clipboardText.c_str(); } + ImGuiContext* Imgui::GetCurrentContext() + { + return ImGui::GetCurrentContext(); + } + + void Imgui::GetAllocatorFunctions(ImGuiMemAllocFunc* allocFunc, ImGuiMemFreeFunc* freeFunc, void** userData) + { + ImGui::GetAllocatorFunctions(allocFunc, freeFunc, userData); + } + void Imgui::UpdateFontTexture() { ImGuiIO& io = ImGui::GetIO();