Skip to content

Commit

Permalink
Fix exiting causes segfault.
Browse files Browse the repository at this point in the history
It was because ImGui context was not managed with world, but
globally via static variables.
  • Loading branch information
enginmanap committed Aug 9, 2018
1 parent 829a5f8 commit e55581a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/ImGuiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
#include <SDL.h>
#include <SDL_syswm.h>

// 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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -362,7 +354,7 @@ ImGuiHelper::ImGuiHelper(GLHelper* glHelper, Options* options) : options(options
ImGuiHelper::~ImGuiHelper()
{
InvalidateDeviceObjects();
ImGui::DestroyContext();
ImGui::DestroyContext(context);
}

void ImGuiHelper::NewFrame() {
Expand Down
15 changes: 14 additions & 1 deletion src/ImGuiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint>
#include "../libs/ImGui/imgui.h"

struct SDL_Window;
Expand All @@ -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);
Expand Down

0 comments on commit e55581a

Please sign in to comment.