From a4aad6b7293b4b4c0de0e30331935c8f05890605 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Wed, 24 Apr 2024 01:28:28 +0200 Subject: [PATCH] Fix debug info viewer properly to handle MT and refactor Shape --- Source/UserInterface/GameShell.cpp | 6 +- Source/Util/DebugUtil.cpp | 46 +++++++++----- Source/Util/DebugUtil.h | 98 ++++++++++++++++++------------ 3 files changed, 90 insertions(+), 60 deletions(-) diff --git a/Source/UserInterface/GameShell.cpp b/Source/UserInterface/GameShell.cpp index fb9370f5..b1f5d0a7 100644 --- a/Source/UserInterface/GameShell.cpp +++ b/Source/UserInterface/GameShell.cpp @@ -899,10 +899,8 @@ void GameShell::Show() showWays(); terRenderDevice->SetDrawTransform(terCamera->GetCamera()); - if(debug_show_mode){ - if (debug_show_mode != 2) { - show_dispatcher.clear(); - } + if (debug_show_mode) { + MTAutoSingleThread debug_show_lock; universe()->showDebugInfo(); show_dispatcher.draw(); } diff --git a/Source/Util/DebugUtil.cpp b/Source/Util/DebugUtil.cpp index bf6d088d..d5f8e22a 100644 --- a/Source/Util/DebugUtil.cpp +++ b/Source/Util/DebugUtil.cpp @@ -107,26 +107,29 @@ void ShowDispatcher::Shape::show() { switch(type){ case Point: { - Vect3f vs = G2S(point1); - if(vs.z > 10) - clip_pixel(vs.xi(), vs.yi(), color, 1); - } break; + Vect3f vs = G2S(points[0]); + if(vs.z > 10) { + clip_pixel(vs.xi(), vs.yi(), color, 1); + } + break; + } case Text: { - Vect3f vs = G2S(point1); - terRenderDevice->OutText(vs.xi(), vs.yi(), text, sColor4f(color)); - } break; + Vect3f vs = G2S(points[0]); + terRenderDevice->OutText(vs.xi(), vs.yi(), text.c_str(), sColor4f(color)); + break; + } case Circle: - clip_circle_3D(point1, radius, color); + clip_circle_3D(points[0], radius, color); break; case Delta: - clip_line_3D(point1, point1 + point2*show_vector_scale, color); + clip_line_3D(points[0], points[0] + points[1] * show_vector_scale, color); break; case Line: - clip_line_3D(point1, point2, color); + clip_line_3D(points[0], points[1], color); break; case Triangle: @@ -154,19 +157,30 @@ void ShowDispatcher::Shape::show() void ShowDispatcher::draw() { - cFont* font = 0; + cFont* font = nullptr; if(need_font){ font = terVisGeneric->CreateDebugFont(); terRenderDevice->SetFont(font); need_font = false; } - - List::iterator i; - FOR_EACH(shapes, i) - i -> show(); + + bool paused = gameShell->isPaused(); + unsigned int time = gameShell->gameTimer(); + auto i_shape = shapes.begin(); + while(i_shape != shapes.end()){ + if (i_shape->rendered_at == 0) { + i_shape->rendered_at = time; + } else if (paused || i_shape->rendered_at < time) { + //From older logic quant, discard + i_shape = shapes.erase(i_shape); + continue; + } + i_shape->show(); + ++i_shape; + } if(font){ - terRenderDevice->SetFont(0); + terRenderDevice->SetFont(nullptr); font->Release(); } } diff --git a/Source/Util/DebugUtil.h b/Source/Util/DebugUtil.h index a14dc50e..fc93d0ac 100644 --- a/Source/Util/DebugUtil.h +++ b/Source/Util/DebugUtil.h @@ -25,54 +25,72 @@ enum terStatisticsGroupType class ShowDispatcher { class Shape { + public: enum Type { Point, Text, Circle, Delta, Line, Triangle, Quadrangle, ConvexArray }; Type type; - sColor4c color; - float radius; - const char* text; - Vect3f point1; - Vect3f point2; - int n_points; - Vect3f* points; - static bool isArray(Type type) { return type == Triangle || type == Quadrangle || type == ConvexArray; } - public: - Shape(const Vect3f& v, sColor4c color_) { type = Point; point1 = v; color = color_; } - Shape(const Vect3f& v, const char* text_, sColor4c color_) { type = Text; point1 = v; text = strdup(text_); color = color_; } - Shape(const Vect3f& v, float radius_, sColor4c color_) { type = Circle; point1 = v; radius = radius_; color = color_; } - Shape(const Vect3f& v0, const Vect3f& v1, sColor4c color_, int line) { type = line ? Line : Delta; point1 = v0; point2 = v1; color = color_; } - Shape(const Vect3f& v0, const Vect3f& v1, const Vect3f& v2, sColor4c color_) { type = Triangle; points = new Vect3f[n_points = 3]; points[0] = v0; points[1] = v1; points[2] = v2; color = color_; } - Shape(const Vect3f& v0, const Vect3f& v1, const Vect3f& v2, const Vect3f& v3, sColor4c color_) { type = Quadrangle; points = new Vect3f[n_points = 4]; points[0] = v0; points[1] = v1; points[2] = v2; points[3] = v3; color = color_; } - Shape(int n_points_, const Vect3f* points_, sColor4c color_) { type = ConvexArray; points = new Vect3f[n_points = n_points_]; memcpy(points, points_, sizeof(Vect3f)*n_points); color = color_; } - /* - Shape(const Shape& shape) - { - *this = shape; - if(isArray(type)){ - points = new Vect3f[n_points = shape.n_points]; - memcpy(points, shape.points, sizeof(Vect3f)*n_points); - } - else if(type == Text) - text = strdup(shape.text); - } - */ - ~Shape() { - if(isArray(type)) - delete points; - else if(type == Text) - free((void*)text); - } + sColor4c color = {}; + float radius = 1.0f; + std::string text = {}; + std::vector points = {}; + unsigned int rendered_at = 0; + + Shape(const Vect3f& v, sColor4c color_) { + type = Point; + points.push_back(v); + color = color_; + } + Shape(const Vect3f& v, const char* text_, sColor4c color_) { + type = Text; + points.push_back(v); + text = text_; + color = color_; + } + Shape(const Vect3f& v, float radius_, sColor4c color_) { + type = Circle; + points.push_back(v); + radius = radius_; + color = color_; + } + Shape(const Vect3f& v0, const Vect3f& v1, sColor4c color_, int line) { + type = line ? Line : Delta; + points.push_back(v0); + points.push_back(v1); + color = color_; + } + Shape(const Vect3f& v0, const Vect3f& v1, const Vect3f& v2, sColor4c color_) { + type = Triangle; + points.push_back(v0); + points.push_back(v1); + points.push_back(v2); + color = color_; + } + Shape(const Vect3f& v0, const Vect3f& v1, const Vect3f& v2, const Vect3f& v3, sColor4c color_) { + type = Quadrangle; + points.push_back(v0); + points.push_back(v1); + points.push_back(v2); + points.push_back(v3); + color = color_; + } + Shape(int n_points_, const Vect3f* points_, sColor4c color_) { + type = ConvexArray; + for (int i = 0; i < n_points_; i++) { + points.push_back(points_[i]); + } + color = color_; + } + void show(); - void showConvex(); - }; - typedef std::vector List; - List shapes; - bool need_font; + }; + + std::vector shapes = {}; + bool need_font = false; public: void draw(); void clear() { shapes.clear(); need_font = false; } - void point(const Vect3f& v, sColor4c color) { shapes.push_back(Shape(v, color)); } + void point(const Vect3f& v, sColor4c color) { shapes.emplace_back(Shape(v, color)); } void text(const Vect3f& v, const char* text, sColor4c color) { shapes.push_back(Shape(v, text, color)); need_font = true; } void circle(const Vect3f& v, float radius, sColor4c color) { shapes.push_back(Shape(v, radius, color)); } void line(const Vect3f &v0, const Vect3f &v1, sColor4c color) { shapes.push_back(Shape(v0, v1, color, 1)); }