From 3380eb993e69685e1103b90deee122332a4feb25 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Mon, 29 Sep 2025 01:24:46 +0530 Subject: [PATCH] Debug Draw --- Src/IAESandbox/imp/cpp/Game.cpp | 2 - Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp | 108 ++++++++++++++++-- .../inc/IAEngine/Rendering/DebugDraw.hpp | 25 ++-- 3 files changed, 112 insertions(+), 23 deletions(-) diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index 45b8711..c36728d 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -58,8 +58,6 @@ namespace ia::iae::game scene->AddNode(obstacle); Engine::ChangeScene(scene.get()); - - DebugDraw::AddDebugUIWindow("Dsd", {100.0f, 100.0f}, {100.0f, 100.0f}, [](){}); } VOID Game::Terminate() diff --git a/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp b/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp index 5d62505..9ca3a72 100644 --- a/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/DebugDraw.cpp @@ -34,7 +34,35 @@ namespace ia::iae ImGuiIO g_imGUIIO{}; - Vector DebugDraw::s_debugUIWindows; + struct ShapeLine + { + ImVec2 To{}; + ImVec2 From{}; + ImColor Color{}; + FLOAT32 Thickness{1.0f}; + BOOL Visibility{true}; + }; + + struct ShapeRect + { + ImVec2 TopLeft{}; + ImVec2 BottomRight{}; + ImColor Color{}; + BOOL Visibility{true}; + }; + + struct UIWindow + { + PCCHAR Title{""}; + glm::vec2 Position{}; + glm::vec2 Size{}; + std::function ContentDrawCallback{}; + BOOL Visibility{true}; + }; + + Vector g_debugUIWindows; + Vector g_lineShapes; + Vector g_rectShapes; VOID DebugDraw::Initailize() { @@ -69,27 +97,93 @@ namespace ia::iae VOID DebugDraw::Draw() { - for (const auto &w : s_debugUIWindows) + for (const auto &w : g_debugUIWindows) { + if (!w.Visibility) + continue; ImGui::Begin(w.Title); ImGui::SetWindowPos({w.Position.x, w.Position.y}); ImGui::SetWindowSize({w.Size.x, w.Size.y}); w.ContentDrawCallback(); ImGui::End(); } + const auto drawList = ImGui::GetForegroundDrawList(); + for (const auto &t : g_lineShapes) + { + if (!t.Visibility) + continue; + drawList->AddLine(t.From, t.To, t.Color, t.Thickness); + } + for (const auto &t : g_rectShapes) + { + if (!t.Visibility) + continue; + drawList->AddRectFilled(t.TopLeft, t.BottomRight, t.Color); + } } - VOID DebugDraw::DrawLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color) + Handle DebugDraw::AddLine(IN CONST glm::vec2 &from, IN CONST glm::vec2 &to, IN CONST glm::vec4 &color, + IN FLOAT32 thickness) { + g_lineShapes.pushBack(ShapeLine{.To = ImVec2{to.x, to.y}, + .From = ImVec2{from.x, from.y}, + .Color = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), + .Thickness = thickness}); + return g_lineShapes.size() - 1; } - VOID DebugDraw::DrawRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color) + Handle DebugDraw::AddRect(IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN CONST glm::vec4 &color) { + g_rectShapes.pushBack({ + .TopLeft = + ImVec2{ + position.x, + position.y, + }, + .BottomRight = + ImVec2{ + position.x + size.x, + position.y + size.y, + }, + .Color = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255), + }); + return g_rectShapes.size() - 1; } - VOID DebugDraw::AddDebugUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, - IN std::function contentDrawCallback) + Handle DebugDraw::AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, + IN std::function contentDrawCallback) { - s_debugUIWindows.pushBack(DebugUIWindow{title, position, size, contentDrawCallback}); + g_debugUIWindows.pushBack(UIWindow{title, position, size, contentDrawCallback}); + return g_debugUIWindows.size() - 1; + } + + VOID DebugDraw::SetLineVisibility(IN Handle handle, IN BOOL visible) + { + g_lineShapes[handle].Visibility = visible; + } + + VOID DebugDraw::SetRectVisibility(IN Handle handle, IN BOOL visible) + { + g_rectShapes[handle].Visibility = visible; + } + + VOID DebugDraw::SetUIWindowVisibility(IN Handle handle, IN BOOL visible) + { + g_debugUIWindows[handle].Visibility = visible; + } + + VOID DebugDraw::ToggleLineVisibility(IN Handle handle) + { + g_lineShapes[handle].Visibility = !g_lineShapes[handle].Visibility; + } + + VOID DebugDraw::ToggleRectVisibility(IN Handle handle) + { + g_rectShapes[handle].Visibility = !g_rectShapes[handle].Visibility; + } + + VOID DebugDraw::ToggleUIWindowVisibility(IN Handle handle) + { + g_debugUIWindows[handle].Visibility = !g_debugUIWindows[handle].Visibility; } } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp b/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp index 0a25f76..a3a8654 100644 --- a/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp +++ b/Src/IAEngine/inc/IAEngine/Rendering/DebugDraw.hpp @@ -22,29 +22,26 @@ namespace ia::iae { class DebugDraw { - struct DebugUIWindow - { - PCCHAR Title{""}; - glm::vec2 Position{}; - glm::vec2 Size{}; - std::function ContentDrawCallback{}; - }; - public: - STATIC VOID DrawLine(IN CONST glm::vec2& from, IN CONST glm::vec2& to, IN CONST glm::vec4& color); - STATIC VOID DrawRect(IN CONST glm::vec2& position, IN CONST glm::vec2& size, IN CONST glm::vec4& color); - - STATIC VOID AddDebugUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, + STATIC Handle AddLine(IN CONST glm::vec2& from, IN CONST glm::vec2& to, IN CONST glm::vec4& color, IN FLOAT32 thickness = 1.0f); + STATIC Handle AddRect(IN CONST glm::vec2& position, IN CONST glm::vec2& size, IN CONST glm::vec4& color); + STATIC Handle AddUIWindow(IN PCCHAR title, IN CONST glm::vec2 &position, IN CONST glm::vec2 &size, IN std::function contentDrawCallback); + STATIC VOID SetLineVisibility(IN Handle handle, IN BOOL visible); + STATIC VOID SetRectVisibility(IN Handle handle, IN BOOL visible); + STATIC VOID SetUIWindowVisibility(IN Handle handle, IN BOOL visible); + + STATIC VOID ToggleLineVisibility(IN Handle handle); + STATIC VOID ToggleRectVisibility(IN Handle handle); + STATIC VOID ToggleUIWindowVisibility(IN Handle handle); + private: STATIC VOID Initailize(); STATIC VOID Terminate(); STATIC VOID Draw(); - STATIC Vector s_debugUIWindows; - friend class Renderer; }; } // namespace ia::iae \ No newline at end of file