Skip to content

Commit

Permalink
Fix debug info viewer properly to handle MT and refactor Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
IonAgorria committed Apr 23, 2024
1 parent 7f34f49 commit c86199a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 60 deletions.
6 changes: 2 additions & 4 deletions Source/UserInterface/GameShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
46 changes: 30 additions & 16 deletions Source/Util/DebugUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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();
}
}
Expand Down
98 changes: 58 additions & 40 deletions Source/Util/DebugUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vect3f> 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<Shape> List;
List shapes;
bool need_font;
};

std::vector<Shape> 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)); }
Expand Down

0 comments on commit c86199a

Please sign in to comment.