From e55581ad9cf769b77d0a249155e4345250cdbe42 Mon Sep 17 00:00:00 2001 From: Engin Manap Date: Thu, 9 Aug 2018 23:31:02 +0300 Subject: [PATCH] Fix exiting causes segfault. It was because ImGui context was not managed with world, but globally via static variables. --- src/ImGuiHelper.cpp | 12 ++---------- src/ImGuiHelper.h | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/ImGuiHelper.cpp b/src/ImGuiHelper.cpp index 8b5cc088..e7c83d2f 100644 --- a/src/ImGuiHelper.cpp +++ b/src/ImGuiHelper.cpp @@ -19,14 +19,6 @@ #include #include -// Data -static double g_Time = 0.0f; -static bool g_MousePressed[3] = { false, false, false }; -static float g_MouseWheel = 0.0f; -static GLuint g_FontTexture = 0; -static int g_VertHandle = 0, g_FragHandle = 0; -static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0; -static unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0; // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so. @@ -324,7 +316,7 @@ void ImGuiHelper::InvalidateDeviceObjects() ImGuiHelper::ImGuiHelper(GLHelper* glHelper, Options* options) : options(options) { this->glHelper = glHelper; - ImGui::CreateContext(); + context = ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.KeyMap[ImGuiKey_Tab] = SDLK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT; @@ -362,7 +354,7 @@ ImGuiHelper::ImGuiHelper(GLHelper* glHelper, Options* options) : options(options ImGuiHelper::~ImGuiHelper() { InvalidateDeviceObjects(); - ImGui::DestroyContext(); + ImGui::DestroyContext(context); } void ImGuiHelper::NewFrame() { diff --git a/src/ImGuiHelper.h b/src/ImGuiHelper.h index bf4e2a79..b21b1a30 100644 --- a/src/ImGuiHelper.h +++ b/src/ImGuiHelper.h @@ -8,6 +8,7 @@ // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. // https://github.com/ocornut/imgui +#include #include "../libs/ImGui/imgui.h" struct SDL_Window; @@ -18,10 +19,22 @@ class Options; typedef union SDL_Event SDL_Event; class ImGuiHelper { -private: + + // ImGUI Data + double g_Time = 0.0f; + bool g_MousePressed[3] = { false, false, false }; + float g_MouseWheel = 0.0f; + uint32_t g_FontTexture = 0; + int g_VertHandle = 0, g_FragHandle = 0; + int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0; + unsigned int g_VboHandle = 0, g_VaoHandle = 0, g_ElementsHandle = 0; + // ImGUI Data end" + GLHelper* glHelper = nullptr; GLSLProgram* program = nullptr; Options* options; + + ImGuiContext* context = nullptr; void CreateFontsTexture(); static const char* GetClipboardText(void*); static void SetClipboardText(void*, const char* text);