add imgui function to ensure crossdll context is valid

This commit is contained in:
SweetId 2023-09-19 20:16:07 -04:00
parent eddcd4b906
commit a08a9b377c
3 changed files with 29 additions and 1 deletions

View File

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

View File

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

View File

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