From dafad3903eabe7a73621259ddc34f43da99ddf18 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 7 Sep 2015 14:56:39 +0100 Subject: [PATCH] Added GetDrawData() alternative to setting a Render function. --- imgui.cpp | 31 ++++++++++++++++++++++--------- imgui.h | 12 ++++++++---- imgui_internal.h | 1 + 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 8bfd19c9..75d24747 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -96,6 +96,7 @@ io.DisplaySize.y = 1280.0f; io.DeltaTime = 1.0f/60.0f; io.IniFilename = "imgui.ini"; + io.RenderDrawListsFn = my_render_function; // Setup a render function, or set to NULL and call GetDrawData() after Render() to access the render data. // TODO: Fill others settings of the io structure // Load texture @@ -1769,6 +1770,12 @@ ImGuiStyle& ImGui::GetStyle() return GImGui->Style; } +// Same value as passed to your RenderDrawListsFn() function. valid after Render() and until the next call to NewFrame() +ImDrawData* ImGui::GetDrawData() +{ + return GImGui->RenderDrawData.Valid ? &GImGui->RenderDrawData : NULL; +} + float ImGui::GetTime() { return GImGui->Time; @@ -1786,7 +1793,6 @@ void ImGui::NewFrame() // Check user data IM_ASSERT(g.IO.DeltaTime >= 0.0f); IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f); - IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented IM_ASSERT(g.IO.Fonts->Fonts.Size > 0); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? IM_ASSERT(g.Style.CurveTessellationTol > 0.0f); // Invalid @@ -1812,6 +1818,11 @@ void ImGui::NewFrame() g.OverlayDrawList.PushClipRectFullScreen(); g.OverlayDrawList.AddDrawCmd(); + // Mark rendering data as invalid to prevent user who may have a handle on it to use it + g.RenderDrawData.Valid = false; + g.RenderDrawData.CmdLists = NULL; + g.RenderDrawData.CmdListsCount = g.RenderDrawData.TotalVtxCount = g.RenderDrawData.TotalIdxCount = 0; + // Update inputs state if (g.IO.MousePos.x < 0 && g.IO.MousePos.y < 0) g.IO.MousePos = ImVec2(-9999.0f, -9999.0f); @@ -2366,15 +2377,17 @@ void ImGui::Render() if (!g.OverlayDrawList.VtxBuffer.empty()) AddDrawListToRenderList(g.RenderDrawLists[0], &g.OverlayDrawList); - // Render - if (!g.RenderDrawLists[0].empty()) + // Setup draw data + g.RenderDrawData.Valid = true; + g.RenderDrawData.CmdLists = &g.RenderDrawLists[0][0]; + g.RenderDrawData.CmdListsCount = g.RenderDrawLists[0].Size; + g.RenderDrawData.TotalVtxCount = g.IO.MetricsRenderVertices; + g.RenderDrawData.TotalIdxCount = g.IO.MetricsRenderIndices; + + // Render. If user hasn't set a callback then they may retrieve the draw data via GetDrawData() + if (g.RenderDrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL) { - ImDrawData data; - data.CmdLists = &g.RenderDrawLists[0][0]; - data.CmdListsCount = g.RenderDrawLists[0].Size; - data.TotalVtxCount = g.IO.MetricsRenderVertices; - data.TotalIdxCount = g.IO.MetricsRenderIndices; - g.IO.RenderDrawListsFn(&data); + g.IO.RenderDrawListsFn(&g.RenderDrawData); } } } diff --git a/imgui.h b/imgui.h index 5f2a66fb..0a84b9e9 100644 --- a/imgui.h +++ b/imgui.h @@ -105,6 +105,7 @@ namespace ImGui // Main IMGUI_API ImGuiIO& GetIO(); IMGUI_API ImGuiStyle& GetStyle(); + IMGUI_API ImDrawData* GetDrawData(); // same value as passed to your RenderDrawListsFn() function. valid after Render() and until the next call to NewFrame() IMGUI_API void NewFrame(); IMGUI_API void Render(); IMGUI_API void Shutdown(); @@ -694,8 +695,9 @@ struct ImGuiIO // User Functions //------------------------------------------------------------------ - // REQUIRED: rendering function. - // See example code if you are unsure of how to implement this. + // Rendering function, will be called in Render(). + // Alternatively you can keep this to NULL and call GetDrawData() after Render() to get the same pointer. + // See example applications if you are unsure of how to implement this. void (*RenderDrawListsFn)(ImDrawData* data); // Optional: access OS clipboard @@ -1149,14 +1151,16 @@ struct ImDrawList // All draw data to render an ImGui frame struct ImDrawData { + bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. ImDrawList** CmdLists; int CmdListsCount; int TotalVtxCount; // For convenience, sum of all cmd_lists vtx_buffer.Size int TotalIdxCount; // For convenience, sum of all cmd_lists idx_buffer.Size // Functions - void DeIndexAllBuffers(); // For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! - void ScaleClipRects(const ImVec2& sc); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than ImGui expects, or if there is a difference between your window resolution and framebuffer resolution. + IMGUI_API ImDrawData() { Valid = false; CmdLists = NULL; CmdListsCount = TotalVtxCount = TotalIdxCount = 0; } + IMGUI_API void DeIndexAllBuffers(); // For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! + IMGUI_API void ScaleClipRects(const ImVec2& sc); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than ImGui expects, or if there is a difference between your window resolution and framebuffer resolution. }; struct ImFontConfig diff --git a/imgui_internal.h b/imgui_internal.h index ffcb238d..80adcff4 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -371,6 +371,7 @@ struct ImGuiState ImGuiSetCond SetNextTreeNodeOpenedCond; // Render + ImDrawData RenderDrawData; // Main ImDrawData instance to pass render information to the user ImVector RenderDrawLists[3]; float ModalWindowDarkeningRatio; ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays