Skip to content

Commit f3ec671

Browse files
committed
client: initial draft implementation of debug visualizer
1 parent a21bf72 commit f3ec671

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

client/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ list(APPEND CLDLL_SOURCES
4040
"postfx_parameters.cpp"
4141
"client_weapon_layer_impl.cpp"
4242
"weapon_predicting_context.cpp"
43+
"visualizer/debug_visualizer.cpp"
4344
)
4445

4546
# shared source files
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
debug_visualizer.cpp - utilities for visualizing data for debug purposes
3+
Copyright (C) 2025 SNMetamorph
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
*/
15+
16+
#include "debug_visualizer.h"
17+
#include "hud.h"
18+
19+
CDebugVisualizer::Primitive::Primitive(Class type, Vector color, std::optional<float> lifespan, bool depthTest, DataVariant data) :
20+
type(type),
21+
color(color),
22+
lifespan(lifespan),
23+
depthTest(depthTest),
24+
rendered(false),
25+
data(std::move(data))
26+
{
27+
}
28+
29+
CDebugVisualizer& CDebugVisualizer::GetInstance()
30+
{
31+
static CDebugVisualizer instance;
32+
return instance;
33+
}
34+
35+
void CDebugVisualizer::RunFrame()
36+
{
37+
float currentTime = gEngfuncs.GetClientTime();
38+
auto predicate = [currentTime](const Primitive& prim) {
39+
if (prim.lifespan.has_value()) {
40+
return currentTime >= prim.lifespan.value();
41+
}
42+
return prim.rendered;
43+
};
44+
m_primitives.erase(std::remove_if(m_primitives.begin(), m_primitives.end(), predicate), m_primitives.end());
45+
}
46+
47+
void CDebugVisualizer::DrawAABB(const Vector &mins, const Vector &maxs, Vector color, std::optional<float> lifespan, bool depthTest)
48+
{
49+
m_primitives.emplace_back(Primitive::Class::AABB, color, lifespan, depthTest, AABBData{ mins, maxs });
50+
}
51+
52+
void CDebugVisualizer::DrawSphere(const Vector &center, float radius, Vector color, std::optional<float> lifespan, bool depthTest)
53+
{
54+
m_primitives.emplace_back(Primitive::Class::Sphere, color, lifespan, depthTest, SphereData{ center, radius });
55+
}
56+
57+
void CDebugVisualizer::DrawVector(const Vector &position, const Vector &direction, Vector color, std::optional<float> lifespan, bool depthTest)
58+
{
59+
m_primitives.emplace_back(Primitive::Class::Vector, color, lifespan, depthTest, VectorData{ position, direction });
60+
}
61+
62+
void CDebugVisualizer::DrawFrustum(const CFrustum &frustum, Vector color, std::optional<float> lifespan, bool depthTest)
63+
{
64+
m_primitives.emplace_back(Primitive::Class::Frustum, color, lifespan, depthTest, FrustumData{ frustum });
65+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
debug_visualizer.h - utilities for visualizing data for debug purposes
3+
Copyright (C) 2025 SNMetamorph
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
*/
15+
16+
#pragma once
17+
#include "vector.h"
18+
#include "frustum.h"
19+
#include <vector>
20+
#include <optional>
21+
#include <variant>
22+
23+
class CDebugVisualizer
24+
{
25+
public:
26+
struct AABBData { Vector mins, maxs; };
27+
struct SphereData { Vector center; float radius; };
28+
struct VectorData { Vector position, direction; };
29+
struct FrustumData { CFrustum frustum; };
30+
31+
class Primitive
32+
{
33+
public:
34+
using DataVariant = std::variant<AABBData, SphereData, VectorData, FrustumData>;
35+
36+
enum class Class
37+
{
38+
AABB,
39+
Sphere,
40+
Vector,
41+
Frustum,
42+
};
43+
44+
Primitive(Class type, Vector color, std::optional<float> lifespan, bool depthTest, DataVariant data);
45+
46+
Class type;
47+
bool depthTest;
48+
bool rendered;
49+
Vector color;
50+
std::optional<float> lifespan;
51+
DataVariant data;
52+
};
53+
54+
static CDebugVisualizer& GetInstance();
55+
const std::vector<Primitive>& GetPrimitives() const { return m_primitives; }
56+
void RunFrame();
57+
58+
void DrawAABB(const Vector &mins, const Vector &maxs, Vector color, std::optional<float> lifespan, bool depthTest);
59+
void DrawSphere(const Vector &center, float radius, Vector color, std::optional<float> lifespan, bool depthTest);
60+
void DrawVector(const Vector &position, const Vector& direction, Vector color, std::optional<float> lifespan, bool depthTest);
61+
void DrawFrustum(const CFrustum &frustum, Vector color, std::optional<float> lifespan, bool depthTest);
62+
// ...and other geometric primitives as needed
63+
64+
private:
65+
CDebugVisualizer() = default;
66+
~CDebugVisualizer() = default;
67+
CDebugVisualizer(const CDebugVisualizer&) = delete;
68+
CDebugVisualizer(CDebugVisualizer&&) = delete;
69+
CDebugVisualizer& operator=(const CDebugVisualizer&) = delete;
70+
CDebugVisualizer& operator=(CDebugVisualizer&&) = delete;
71+
72+
std::vector<Primitive> m_primitives;
73+
};

0 commit comments

Comments
 (0)