From 98e71afa72e1be10c7ed446c387666a3773167ae Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Sat, 31 Jan 2015 16:17:39 -0800 Subject: [PATCH 1/5] Removed the dependency on realloc functionality --- imgui.cpp | 6 ------ imgui.h | 13 ++++++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 60d297b8c..508e25620 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -537,7 +537,6 @@ ImGuiIO::ImGuiIO() // User functions RenderDrawListsFn = NULL; MemAllocFn = malloc; - MemReallocFn = realloc; MemFreeFn = free; GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; @@ -1479,11 +1478,6 @@ void ImGui::MemFree(void* ptr) { return GImGui.IO.MemFreeFn(ptr); } - -void* ImGui::MemRealloc(void* ptr, size_t sz) -{ - return GImGui.IO.MemReallocFn(ptr, sz); -} static ImGuiIniData* FindWindowSettings(const char* name) { diff --git a/imgui.h b/imgui.h index ba4fa75ce..afbe8f356 100644 --- a/imgui.h +++ b/imgui.h @@ -72,7 +72,6 @@ namespace ImGui // Proxy functions to access the MemAllocFn/MemFreeFn/MemReallocFn pointers in ImGui::GetIO(). The only reason they exist here is to allow ImVector<> to compile inline. IMGUI_API void* MemAlloc(size_t sz); IMGUI_API void MemFree(void* ptr); - IMGUI_API void* MemRealloc(void* ptr, size_t sz); } // std::vector<> like class to avoid dragging dependencies (also: windows implementation of STL with debug enabled is absurdly slow, so let's bypass it so our code runs fast in debug). @@ -114,8 +113,13 @@ public: inline value_type& back() { IM_ASSERT(Size > 0); return Data[Size-1]; } inline const value_type& back() const { IM_ASSERT(Size > 0); return Data[Size-1]; } inline void swap(ImVector& rhs) { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; } - - inline void reserve(size_t new_capacity) { Data = (value_type*)ImGui::MemRealloc(Data, new_capacity * sizeof(value_type)); Capacity = new_capacity; } + inline void reserve(size_t new_capacity) { if( new_capacity <= Capacity ) + return; + T* NewData = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type)); + memcpy(NewData, Data, Size * sizeof(value_type)); + ImGui::MemFree(Data); + Data = NewData; + Capacity = new_capacity; } inline void resize(size_t new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; } inline void push_back(const value_type& v) { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; } @@ -523,9 +527,8 @@ struct ImGuiIO const char* (*GetClipboardTextFn)(); void (*SetClipboardTextFn)(const char* text); - // Optional: override memory allocations (default to posix malloc/realloc/free) + // Optional: override memory allocations (default to posix malloc/free) void* (*MemAllocFn)(size_t sz); - void* (*MemReallocFn)(void* ptr, size_t sz); void (*MemFreeFn)(void* ptr); // Optional: notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese inputs in Windows) From 8149bace7b0b8debb2fdd29ea14ff8d9a4d4c757 Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Sat, 31 Jan 2015 20:28:24 -0800 Subject: [PATCH 2/5] Added a way to replace the internal state pointer so that we can optionally share it between modules (e.g. multiple DLLs) --- imgui.cpp | 250 ++++++++++++++++++++++++++++-------------------------- imgui.h | 2 + 2 files changed, 133 insertions(+), 119 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 508e25620..37865c9e4 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1029,7 +1029,8 @@ struct ImGuiState } }; -static ImGuiState GImGui; +static ImGuiState GImDefaultState; +static ImGuiState* GImGui = &GImDefaultState; struct ImGuiWindow { @@ -1084,27 +1085,27 @@ public: void FocusItemUnregister(); ImGuiAabb Aabb() const { return ImGuiAabb(Pos, Pos+Size); } - ImFont* Font() const { return GImGui.Font; } - float FontSize() const { return GImGui.FontSize * FontWindowScale; } + ImFont* Font() const { return GImGui->Font; } + float FontSize() const { return GImGui->FontSize * FontWindowScale; } ImVec2 CursorPos() const { return DC.CursorPos; } - float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0 : FontSize() + GImGui.Style.FramePadding.y * 2.0f; } + float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0 : FontSize() + GImGui->Style.FramePadding.y * 2.0f; } ImGuiAabb TitleBarAabb() const { return ImGuiAabb(Pos, Pos + ImVec2(SizeFull.x, TitleBarHeight())); } - ImVec2 WindowPadding() const { return ((Flags & ImGuiWindowFlags_ChildWindow) && !(Flags & ImGuiWindowFlags_ShowBorders)) ? ImVec2(1,1) : GImGui.Style.WindowPadding; } - ImU32 Color(ImGuiCol idx, float a=1.f) const { ImVec4 c = GImGui.Style.Colors[idx]; c.w *= GImGui.Style.Alpha * a; return ImGui::ColorConvertFloat4ToU32(c); } - ImU32 Color(const ImVec4& col) const { ImVec4 c = col; c.w *= GImGui.Style.Alpha; return ImGui::ColorConvertFloat4ToU32(c); } + ImVec2 WindowPadding() const { return ((Flags & ImGuiWindowFlags_ChildWindow) && !(Flags & ImGuiWindowFlags_ShowBorders)) ? ImVec2(1,1) : GImGui->Style.WindowPadding; } + ImU32 Color(ImGuiCol idx, float a=1.f) const { ImVec4 c = GImGui->Style.Colors[idx]; c.w *= GImGui->Style.Alpha * a; return ImGui::ColorConvertFloat4ToU32(c); } + ImU32 Color(const ImVec4& col) const { ImVec4 c = col; c.w *= GImGui->Style.Alpha; return ImGui::ColorConvertFloat4ToU32(c); } }; static ImGuiWindow* GetCurrentWindow() { - IM_ASSERT(GImGui.CurrentWindow != NULL); // ImGui::NewFrame() hasn't been called yet? - GImGui.CurrentWindow->Accessed = true; - return GImGui.CurrentWindow; + IM_ASSERT(GImGui->CurrentWindow != NULL); // ImGui::NewFrame() hasn't been called yet? + GImGui->CurrentWindow->Accessed = true; + return GImGui->CurrentWindow; } static void RegisterAliveId(const ImGuiID& id) { - if (GImGui.ActiveId == id) - GImGui.ActiveIdIsAlive = true; + if (GImGui->ActiveId == id) + GImGui->ActiveIdIsAlive = true; } //----------------------------------------------------------------------------- @@ -1235,7 +1236,7 @@ void ImGuiTextFilter::Draw(const char* label, float width) if (width < 0.0f) { ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); - width = ImMax(window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x - (label_size.x + GImGui.Style.ItemSpacing.x*4), 10.0f); + width = ImMax(window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x - (label_size.x + GImGui->Style.ItemSpacing.x*4), 10.0f); } ImGui::PushItemWidth(width); ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf)); @@ -1417,7 +1418,7 @@ ImGuiID ImGuiWindow::GetID(const void* ptr) bool ImGuiWindow::FocusItemRegister(bool is_active, bool tab_stop) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); const bool allow_keyboard_focus = window->DC.AllowKeyboardFocus.back(); @@ -1451,7 +1452,7 @@ void ImGuiWindow::FocusItemUnregister() void ImGuiWindow::AddToRenderList() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (!DrawList->commands.empty() && !DrawList->vtx_buffer.empty()) { @@ -1471,17 +1472,17 @@ void ImGuiWindow::AddToRenderList() void* ImGui::MemAlloc(size_t sz) { - return GImGui.IO.MemAllocFn(sz); + return GImGui->IO.MemAllocFn(sz); } void ImGui::MemFree(void* ptr) { - return GImGui.IO.MemFreeFn(ptr); + return GImGui->IO.MemFreeFn(ptr); } static ImGuiIniData* FindWindowSettings(const char* name) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; for (size_t i = 0; i != g.Settings.size(); i++) { ImGuiIniData* ini = g.Settings[i]; @@ -1499,7 +1500,7 @@ static ImGuiIniData* AddWindowSettings(const char* name) ini->Collapsed = false; ini->Pos = ImVec2(FLT_MAX,FLT_MAX); ini->Size = ImVec2(0,0); - GImGui.Settings.push_back(ini); + GImGui->Settings.push_back(ini); return ini; } @@ -1507,7 +1508,7 @@ static ImGuiIniData* AddWindowSettings(const char* name) // FIXME: Write something less rubbish static void LoadSettings() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; const char* filename = g.IO.IniFilename; if (!filename) return; @@ -1553,7 +1554,7 @@ static void LoadSettings() static void SaveSettings() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; const char* filename = g.IO.IniFilename; if (!filename) return; @@ -1592,25 +1593,36 @@ static void SaveSettings() static void MarkSettingsDirty() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (g.SettingsDirtyTimer <= 0.0f) g.SettingsDirtyTimer = g.IO.IniSavingRate; } +void* ImGui::GetInternalState() +{ + return GImGui; +} + +void ImGui::SetInternalState(void* state) +{ + GImGui = (ImGuiState*)state; +} + + ImGuiIO& ImGui::GetIO() { - return GImGui.IO; + return GImGui->IO; } ImGuiStyle& ImGui::GetStyle() { - return GImGui.Style; + return GImGui->Style; } void ImGui::NewFrame() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; // Check user data IM_ASSERT(g.IO.DeltaTime > 0.0f); @@ -1752,7 +1764,7 @@ void ImGui::NewFrame() // NB: behaviour of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations. void ImGui::Shutdown() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (!g.Initialized) return; @@ -1840,7 +1852,7 @@ static void PopClipRect() void ImGui::Render() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame() const bool first_render_of_the_frame = (g.FrameCountRendered != g.FrameCount); @@ -1940,7 +1952,7 @@ static const char* FindTextDisplayEnd(const char* text, const char* text_end = // Pass text data straight to log (without being displayed) void ImGui::LogText(const char* fmt, ...) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (!g.LogEnabled) return; @@ -1960,7 +1972,7 @@ void ImGui::LogText(const char* fmt, ...) // Internal version that takes a position to decide on newline placement and pad items according to their depth. static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (!text_end) @@ -2026,7 +2038,7 @@ static float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) // RenderText() calls ImDrawList::AddText() calls ImBitmapFont::RenderText() static void RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash, float wrap_width) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); // Hide anything after a '##' string @@ -2098,7 +2110,7 @@ static void RenderCollapseTriangle(ImVec2 p_min, bool opened, float scale, bool } // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker. -// CalcTextSize("") should return ImVec2(0.0f, GImGui.FontSize) +// CalcTextSize("") should return ImVec2(0.0f, GImGui->FontSize) ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width) { ImGuiWindow* window = GetCurrentWindow(); @@ -2125,7 +2137,7 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex // Find window given position, search front-to-back static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; for (int i = (int)g.Windows.size()-1; i >= 0; i--) { ImGuiWindow* window = g.Windows[(size_t)i]; @@ -2145,7 +2157,7 @@ static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs) // NB- Expand the aabb to be generous on imprecise inputs systems (g.Style.TouchExtraPadding) static bool IsMouseHoveringBox(const ImGuiAabb& box) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); // Clip @@ -2168,14 +2180,14 @@ bool ImGui::IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max) bool ImGui::IsMouseHoveringWindow() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); return g.HoveredWindow == window; } bool ImGui::IsMouseHoveringAnyWindow() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; return g.HoveredWindow != NULL; } @@ -2186,14 +2198,14 @@ bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos) static bool IsKeyPressedMap(ImGuiKey key, bool repeat) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; const int key_index = g.IO.KeyMap[key]; return ImGui::IsKeyPressed(key_index, repeat); } bool ImGui::IsKeyPressed(int key_index, bool repeat) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); const float t = g.IO.KeysDownTime[key_index]; if (t == 0.0f) @@ -2211,7 +2223,7 @@ bool ImGui::IsKeyPressed(int key_index, bool repeat) bool ImGui::IsMouseClicked(int button, bool repeat) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); const float t = g.IO.MouseDownTime[button]; if (t == 0.0f) @@ -2229,14 +2241,14 @@ bool ImGui::IsMouseClicked(int button, bool repeat) bool ImGui::IsMouseDoubleClicked(int button) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseDoubleClicked[button]; } ImVec2 ImGui::GetMousePos() { - return GImGui.IO.MousePos; + return GImGui->IO.MousePos; } bool ImGui::IsItemHovered() @@ -2247,7 +2259,7 @@ bool ImGui::IsItemHovered() bool ImGui::IsItemActive() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (g.ActiveId) { ImGuiWindow* window = GetCurrentWindow(); @@ -2271,7 +2283,7 @@ ImVec2 ImGui::GetItemBoxMax() // Tooltip is stored and turned into a BeginTooltip()/EndTooltip() sequence at the end of the frame. Each call override previous value. void ImGui::SetTooltipV(const char* fmt, va_list args) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImFormatStringV(g.Tooltip, IM_ARRAYSIZE(g.Tooltip), fmt, args); } @@ -2285,17 +2297,17 @@ void ImGui::SetTooltip(const char* fmt, ...) float ImGui::GetTime() { - return GImGui.Time; + return GImGui->Time; } int ImGui::GetFrameCount() { - return GImGui.FrameCount; + return GImGui->FrameCount; } void ImGui::BeginTooltip() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_Tooltip; ImGui::Begin("##Tooltip", NULL, ImVec2(0,0), g.Style.Colors[ImGuiCol_TooltipBg].w, window_flags); } @@ -2308,7 +2320,7 @@ void ImGui::EndTooltip() void ImGui::BeginChild(const char* str_id, ImVec2 size, bool border, ImGuiWindowFlags extra_flags) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow; @@ -2367,7 +2379,7 @@ void ImGui::EndChild() static ImGuiWindow* FindWindowByName(const char* name) { // FIXME-OPT: Consider optimizing this (e.g. sorted hashes to window pointers) - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; for (size_t i = 0; i != g.Windows.size(); i++) if (strcmp(g.Windows[i]->Name, name) == 0) return g.Windows[i]; @@ -2376,7 +2388,7 @@ static ImGuiWindow* FindWindowByName(const char* name) static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; // Create window the first time ImGuiWindow* window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow)); @@ -2431,7 +2443,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl // - Passing non-zero 'size' is roughly equivalent to calling SetNextWindowSize(size, ImGuiSetCondition_FirstUseEver) prior to calling Begin(). bool ImGui::Begin(const char* name, bool* p_opened, ImVec2 size, float fill_alpha, ImGuiWindowFlags flags) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; const ImGuiStyle& style = g.Style; IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame() IM_ASSERT(name != NULL); // Must pass a name @@ -2846,7 +2858,7 @@ bool ImGui::Begin(const char* name, bool* p_opened, ImVec2 size, float fill_alph void ImGui::End() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImGui::Columns(1, "#CloseColumns"); @@ -2867,7 +2879,7 @@ void ImGui::End() // Moving window to front of display (which happens to be back of our sorted list) static void FocusWindow(ImGuiWindow* window) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; g.FocusedWindow = window; if (g.Windows.back() == window) @@ -2903,7 +2915,7 @@ float ImGui::GetItemWidth() static void SetFont(ImFont* font) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; IM_ASSERT(font && font->IsLoaded()); IM_ASSERT(font->Scale > 0.0f); @@ -2916,7 +2928,7 @@ static void SetFont(ImFont* font) void ImGui::PushFont(ImFont* font) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (!font) font = g.IO.Fonts->Fonts[0]; @@ -2928,7 +2940,7 @@ void ImGui::PushFont(ImFont* font) void ImGui::PopFont() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; g.CurrentWindow->DrawList->PopTextureID(); g.FontStack.pop_back(); @@ -2961,7 +2973,7 @@ void ImGui::PopTextWrapPos() void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiColMod backup; backup.Col = idx; @@ -2972,7 +2984,7 @@ void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) void ImGui::PopStyleColor(int count) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; while (count > 0) { @@ -2985,7 +2997,7 @@ void ImGui::PopStyleColor(int count) static float* GetStyleVarFloatAddr(ImGuiStyleVar idx) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; switch (idx) { case ImGuiStyleVar_Alpha: return &g.Style.Alpha; @@ -2998,7 +3010,7 @@ static float* GetStyleVarFloatAddr(ImGuiStyleVar idx) static ImVec2* GetStyleVarVec2Addr(ImGuiStyleVar idx) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; switch (idx) { case ImGuiStyleVar_WindowPadding: return &g.Style.WindowPadding; @@ -3011,7 +3023,7 @@ static ImVec2* GetStyleVarVec2Addr(ImGuiStyleVar idx) void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; float* pvar = GetStyleVarFloatAddr(idx); IM_ASSERT(pvar != NULL); // Called function with wrong-type? Variable is not a float. @@ -3025,7 +3037,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImVec2* pvar = GetStyleVarVec2Addr(idx); IM_ASSERT(pvar != NULL); // Called function with wrong-type? Varialble is not a ImVec2. @@ -3038,7 +3050,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) void ImGui::PopStyleVar(int count) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; while (count > 0) { @@ -3102,7 +3114,7 @@ const char* ImGui::GetStyleColName(ImGuiCol idx) bool ImGui::GetWindowIsFocused() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); return g.FocusedWindow == window; } @@ -3179,21 +3191,21 @@ void ImGui::SetWindowCollapsed(bool collapsed, ImGuiSetCondition cond) void ImGui::SetNextWindowPos(const ImVec2& pos, ImGuiSetCondition cond) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; g.SetNextWindowPosVal = pos; g.SetNextWindowPosCond = cond ? cond : ImGuiSetCondition_Always; } void ImGui::SetNextWindowSize(const ImVec2& size, ImGuiSetCondition cond) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; g.SetNextWindowSizeVal = size; g.SetNextWindowSizeCond = cond ? cond : ImGuiSetCondition_Always; } void ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiSetCondition cond) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; g.SetNextWindowCollapsedVal = collapsed; g.SetNextWindowCollapsedCond = cond ? cond : ImGuiSetCondition_Always; } @@ -3206,12 +3218,12 @@ ImVec2 ImGui::GetContentRegionMax() if (window->DC.ColumnsCount != 1) { m.x = GetColumnOffset(window->DC.ColumnsCurrent + 1); - m.x -= GImGui.Style.WindowPadding.x; + m.x -= GImGui->Style.WindowPadding.x; } else { if (window->ScrollbarY) - m.x -= GImGui.Style.ScrollBarWidth; + m.x -= GImGui->Style.ScrollBarWidth; } return m; @@ -3228,7 +3240,7 @@ ImVec2 ImGui::GetWindowContentRegionMax() ImGuiWindow* window = GetCurrentWindow(); ImVec2 m = window->Size - window->WindowPadding(); if (window->ScrollbarY) - m.x -= GImGui.Style.ScrollBarWidth; + m.x -= GImGui->Style.ScrollBarWidth; return m; } @@ -3240,7 +3252,7 @@ float ImGui::GetTextLineHeight() float ImGui::GetTextLineSpacing() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); return window->FontSize() + g.Style.ItemSpacing.y; } @@ -3383,7 +3395,7 @@ void ImGui::TextWrapped(const char* fmt, ...) void ImGui::TextUnformatted(const char* text, const char* text_end) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -3485,7 +3497,7 @@ void ImGui::TextUnformatted(const char* text, const char* text_end) void ImGui::AlignFirstTextHeightToWidgets() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -3498,7 +3510,7 @@ void ImGui::AlignFirstTextHeightToWidgets() // Add a label+text combo aligned to other label+value widgets void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -3531,7 +3543,7 @@ void ImGui::LabelText(const char* label, const char* fmt, ...) static bool IsHovered(const ImGuiAabb& bb, const ImGuiID& id) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (g.HoveredId == 0) { ImGuiWindow* window = GetCurrentWindow(); @@ -3543,7 +3555,7 @@ static bool IsHovered(const ImGuiAabb& bb, const ImGuiID& id) static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, bool repeat) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); const bool hovered = IsHovered(bb, id); @@ -3588,7 +3600,7 @@ static bool ButtonBehaviour(const ImGuiAabb& bb, const ImGuiID& id, bool* out_ho bool ImGui::Button(const char* label, const ImVec2& size_arg, bool repeat_when_held) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -3624,7 +3636,7 @@ bool ImGui::Button(const char* label, const ImVec2& size_arg, bool repeat_when_h // Small buttons fits within text without additional spacing. bool ImGui::SmallButton(const char* label) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -3729,7 +3741,7 @@ void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& // The color used are the button colors. bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -3766,7 +3778,7 @@ bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const I // Start logging ImGui output to TTY void ImGui::LogToTTY(int max_depth) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (g.LogEnabled) return; @@ -3781,7 +3793,7 @@ void ImGui::LogToTTY(int max_depth) // Start logging ImGui output to given file void ImGui::LogToFile(int max_depth, const char* filename) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (g.LogEnabled) return; @@ -3804,7 +3816,7 @@ void ImGui::LogToFile(int max_depth, const char* filename) void ImGui::LogToClipboard(int max_depth) { ImGuiWindow* window = GetCurrentWindow(); - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (g.LogEnabled) return; @@ -3817,7 +3829,7 @@ void ImGui::LogToClipboard(int max_depth) void ImGui::LogFinish() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; if (!g.LogEnabled) return; @@ -3842,7 +3854,7 @@ void ImGui::LogFinish() // Helper to display logging buttons void ImGui::LogButtons() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGui::PushID("LogButtons"); const bool log_to_tty = ImGui::Button("Log To TTY"); @@ -3870,7 +3882,7 @@ void ImGui::LogButtons() bool ImGui::CollapsingHeader(const char* label, const char* str_id, const bool display_frame, const bool default_open) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -3968,7 +3980,7 @@ bool ImGui::CollapsingHeader(const char* label, const char* str_id, const bool d // Text with a little bullet aligned to the typical tree node. void ImGui::BulletTextV(const char* fmt, va_list args) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -4129,7 +4141,7 @@ static void ApplyNumericalTextInput(const char* buf, float *v) float ref_v = *v; if (op) - if (sscanf(GImGui.InputTextState.InitialText, "%f", &ref_v) < 1) + if (sscanf(GImGui->InputTextState.InitialText, "%f", &ref_v) < 1) return; float op_v = 0.0f; @@ -4154,7 +4166,7 @@ static void ApplyNumericalTextInput(const char* buf, float *v) // Adjust display_format to decorate the value with a prefix or a suffix. bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format, float power) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -4391,7 +4403,7 @@ bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const cha // Add multiple sliders on 1 line for compact edition of multiple components static bool SliderFloatN(const char* label, float v[3], int components, float v_min, float v_max, const char* display_format, float power) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -4441,7 +4453,7 @@ bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max static bool SliderIntN(const char* label, int v[3], int components, int v_min, int v_max, const char* display_format) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -4497,7 +4509,7 @@ enum ImGuiPlotType static void Plot(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -4630,7 +4642,7 @@ void ImGui::PlotHistogram(const char* label, float (*values_getter)(void* data, bool ImGui::Checkbox(const char* label, bool* v) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -4688,7 +4700,7 @@ bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int f bool ImGui::RadioButton(const char* label, bool active) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -4898,7 +4910,7 @@ void ImGuiTextEditState::RenderTextScrolledClipped(ImFont* font, float font_size bool ImGui::InputFloat(const char* label, float *v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags extra_flags) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5024,7 +5036,7 @@ static bool InputTextFilterCharacter(ImWchar c, ImGuiInputTextFlags flags) // Edit a string of text bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, void (*callback)(ImGuiTextEditCallbackData*), void* user_data) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5316,7 +5328,7 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT static bool InputFloatN(const char* label, float* v, int components, int decimal_precision) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5416,7 +5428,7 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa // Combo box function. bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_height_items) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5538,7 +5550,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi // A little colored square. Return true when clicked. bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_border) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5585,7 +5597,7 @@ bool ImGui::ColorEdit3(const char* label, float col[3]) // Use CTRL-Click to input value and TAB to go to next item. bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -5768,7 +5780,7 @@ void ImGui::Spacing() // Advance cursor given item size. static void ItemSize(ImVec2 size, ImVec2* adjust_vertical_offset) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -5794,7 +5806,7 @@ static void ItemSize(const ImGuiAabb& aabb, ImVec2* adjust_start_offset) static bool IsClipped(const ImGuiAabb& bb) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (!bb.Overlaps(ImGuiAabb(window->ClipRectStack.back())) && !g.LogEnabled) @@ -5829,7 +5841,7 @@ static bool ItemAdd(const ImGuiAabb& bb, const ImGuiID* id) // spacing_w >= 0 : enforce spacing void ImGui::SameLine(int column_x, int spacing_w) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -5853,7 +5865,7 @@ void ImGui::SameLine(int column_x, int spacing_w) void ImGui::NextColumn() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -5885,7 +5897,7 @@ void ImGui::NextColumn() float ImGui::GetColumnOffset(int column_index) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (column_index < 0) column_index = window->DC.ColumnsCurrent; @@ -5901,7 +5913,7 @@ float ImGui::GetColumnOffset(int column_index) void ImGui::SetColumnOffset(int column_index, float offset) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (column_index < 0) column_index = window->DC.ColumnsCurrent; @@ -5934,7 +5946,7 @@ static void PushColumnClipRect(int column_index) void ImGui::Columns(int columns_count, const char* id, bool border) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; @@ -6002,7 +6014,7 @@ void ImGui::Columns(int columns_count, const char* id, bool border) void ImGui::TreePush(const char* str_id) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.ColumnsStartX += g.Style.TreeNodeSpacing; window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX; @@ -6012,7 +6024,7 @@ void ImGui::TreePush(const char* str_id) void ImGui::TreePush(const void* ptr_id) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.ColumnsStartX += g.Style.TreeNodeSpacing; window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX; @@ -6022,7 +6034,7 @@ void ImGui::TreePush(const void* ptr_id) void ImGui::TreePop() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.ColumnsStartX -= g.Style.TreeNodeSpacing; window->DC.CursorPos.x = window->Pos.x + window->DC.ColumnsStartX + window->DC.ColumnsOffsetX; @@ -6189,7 +6201,7 @@ void ImDrawList::AddVtx(const ImVec2& pos, ImU32 col) { vtx_write->pos = pos; vtx_write->col = col; - vtx_write->uv = GImGui.FontTexUvWhitePixel; + vtx_write->uv = GImGui->FontTexUvWhitePixel; vtx_write++; } @@ -7513,21 +7525,21 @@ static void SetClipboardTextFn_DefaultImpl(const char* text) // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers static const char* GetClipboardTextFn_DefaultImpl() { - return GImGui.PrivateClipboard; + return GImGui->PrivateClipboard; } // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers static void SetClipboardTextFn_DefaultImpl(const char* text) { - if (GImGui.PrivateClipboard) + if (GImGui->PrivateClipboard) { - ImGui::MemFree(GImGui.PrivateClipboard); - GImGui.PrivateClipboard = NULL; + ImGui::MemFree(GImGui->PrivateClipboard); + GImGui->PrivateClipboard = NULL; } const char* text_end = text + strlen(text); - GImGui.PrivateClipboard = (char*)ImGui::MemAlloc((size_t)(text_end - text) + 1); - memcpy(GImGui.PrivateClipboard, text, (size_t)(text_end - text)); - GImGui.PrivateClipboard[(size_t)(text_end - text)] = 0; + GImGui->PrivateClipboard = (char*)ImGui::MemAlloc((size_t)(text_end - text) + 1); + memcpy(GImGui->PrivateClipboard, text, (size_t)(text_end - text)); + GImGui->PrivateClipboard[(size_t)(text_end - text)] = 0; } #endif @@ -7538,7 +7550,7 @@ static void SetClipboardTextFn_DefaultImpl(const char* text) void ImGui::ShowUserGuide() { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGui::BulletText("Double-click on title bar to collapse window."); ImGui::BulletText("Click and drag on lower right corner to resize window."); @@ -7562,7 +7574,7 @@ void ImGui::ShowUserGuide() void ImGui::ShowStyleEditor(ImGuiStyle* ref) { - ImGuiState& g = GImGui; + ImGuiState& g = *GImGui; ImGuiStyle& style = g.Style; const ImGuiStyle def; // Default style @@ -7682,8 +7694,8 @@ void ImGui::ShowTestWindow(bool* opened) ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); ImGui::Text("ImGui says hello."); - //ImGui::Text("MousePos (%g, %g)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y); - //ImGui::Text("MouseWheel %d", ImGui::GetIO().MouseWheel); + ImGui::Text("MousePos (%g, %g)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y); + ImGui::Text("MouseWheel %d", ImGui::GetIO().MouseWheel); ImGui::Spacing(); if (ImGui::CollapsingHeader("Help")) diff --git a/imgui.h b/imgui.h index afbe8f356..7110bd778 100644 --- a/imgui.h +++ b/imgui.h @@ -143,6 +143,8 @@ public: namespace ImGui { // Main + IMGUI_API void* GetInternalState(); + IMGUI_API void SetInternalState(void* state); IMGUI_API ImGuiIO& GetIO(); IMGUI_API ImGuiStyle& GetStyle(); IMGUI_API void NewFrame(); From 1be460402752435b5ca5685ba1c62566c3c9e49f Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Sat, 31 Jan 2015 20:35:42 -0800 Subject: [PATCH 3/5] Fixed pointer dereferencing error from merge --- imgui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui.cpp b/imgui.cpp index 4f49d1cd1..01d81ec4c 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1104,7 +1104,7 @@ static ImGuiWindow* GetCurrentWindow() static void SetActiveId(ImGuiID id) { - GImGui.ActiveId = id; + GImGui->ActiveId = id; } static void RegisterAliveId(const ImGuiID& id) From 3478e3a708515cf9f58f53adcee1c1bfa492f428 Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Sat, 31 Jan 2015 21:55:19 -0800 Subject: [PATCH 4/5] Enabled us to create the internal state on client side allocated memory, e.g. a heap --- imgui.cpp | 10 +++++++++- imgui.h | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 01d81ec4c..ec19efe85 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1609,8 +1609,16 @@ void* ImGui::GetInternalState() return GImGui; } -void ImGui::SetInternalState(void* state) +unsigned ImGui::GetInternalStateSize() { + return sizeof(ImGuiState); +} + +void ImGui::SetInternalState(void* state, bool construct) +{ + if( construct ) + new (state) ImGuiState; + GImGui = (ImGuiState*)state; } diff --git a/imgui.h b/imgui.h index 7110bd778..4b5e0526a 100644 --- a/imgui.h +++ b/imgui.h @@ -144,7 +144,8 @@ namespace ImGui { // Main IMGUI_API void* GetInternalState(); - IMGUI_API void SetInternalState(void* state); + IMGUI_API unsigned GetInternalStateSize(); + IMGUI_API void SetInternalState(void* state, bool construct = false); IMGUI_API ImGuiIO& GetIO(); IMGUI_API ImGuiStyle& GetStyle(); IMGUI_API void NewFrame(); From 087ded0c71e12f43058d8d817ca7d3a1c07c15ad Mon Sep 17 00:00:00 2001 From: Jim Tilander Date: Sun, 1 Feb 2015 11:02:33 -0800 Subject: [PATCH 5/5] If you install with brew, it also installs links into /usr/local -- and if the version changes having the extra link line here will cause warnings. --- examples/opengl_example/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/opengl_example/Makefile b/examples/opengl_example/Makefile index ae08a2313..8d7091e59 100644 --- a/examples/opengl_example/Makefile +++ b/examples/opengl_example/Makefile @@ -30,10 +30,10 @@ ifeq ($(UNAME_S), Darwin) #APPLE ECHO_MESSAGE = "Mac OS X" LIBS = -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo - LIBS += -L/usr/local/Cellar/glew/1.10.0/lib -L/usr/local/lib + LIBS += -L/usr/local/lib LIBS += -lglew -lglfw3 - CXXFLAGS = -I../../ -I/usr/local/Cellar/glew/1.10.0/include -I/usr/local/include + CXXFLAGS = -I../../ -I/usr/local/include CXXFLAGS += -Wall # CXXFLAGS += -D__APPLE__ endif