candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
DebugScene.h
Go to the documentation of this file.
1#pragma once
2
3#include "GraphicsPipeline.h"
4#include "Mesh.h"
5#include "RenderContext.h"
6#include "math_types.h"
7
8#include <entt/entity/registry.hpp>
9#include <entt/signal/sigh.hpp>
10
11namespace candlewick {
12
17
18class DebugScene;
19
25
31 virtual void update() = 0;
32 virtual ~IDebugSubSystem() = default;
33
34protected:
36 explicit IDebugSubSystem(DebugScene &scene) : m_scene(scene) {}
37};
38
42struct DebugMeshComponent;
43
51 entt::registry &m_registry;
52 const RenderContext &m_renderer;
53 GraphicsPipeline m_trianglePipeline;
54 GraphicsPipeline m_linePipeline;
55 entt::dense_map<entt::hashed_string::hash_type,
56 std::unique_ptr<IDebugSubSystem>>
57 m_subsystems;
58 std::unordered_map<DebugMeshType, Mesh> m_sharedMeshes;
59 inline static const std::array<Float4, 3> m_triadColors = {
60 Float4{1., 0., 0., 1.},
61 Float4{0., 1., 0., 1.},
62 Float4{0., 0., 1., 1.},
63 };
64
65 void initializeSharedMeshes();
66
67 void setupPipelines(const MeshLayout &layout);
68
69public:
70 enum : Uint32 { TRANSFORM_SLOT = 0 };
71 enum : Uint32 { COLOR_SLOT = 0 };
72 using enum DebugMeshType;
73
74 const Mesh &getMesh(DebugMeshType type) const {
75 return m_sharedMeshes.at(type);
76 }
77
78 DebugScene(entt::registry &registry, const RenderContext &renderer);
79 DebugScene(const DebugScene &) = delete;
80 DebugScene &operator=(const DebugScene &) = delete;
83
84 const Device &device() const noexcept { return m_renderer.device; }
85 entt::registry &registry() { return m_registry; }
86 const entt::registry &registry() const { return m_registry; }
87
89 template <std::derived_from<IDebugSubSystem> T, typename... Args>
90 T &addSystem(entt::hashed_string::hash_type name, Args &&...args) {
91 auto sys = std::make_unique<T>(*this, std::forward<Args>(args)...);
92 auto [it, flag] = m_subsystems.emplace(name, std::move(sys));
93 return static_cast<T &>(*it->second);
94 }
95
98 template <std::derived_from<IDebugSubSystem> T>
99 T *getSystem(entt::hashed_string::hash_type name) {
100 if (auto it = m_subsystems.find(name); it != m_subsystems.cend()) {
101 return dynamic_cast<T *>(it->second.get());
102 }
103 return nullptr;
104 }
105
107 template <std::derived_from<IDebugSubSystem> T>
108 T &tryGetSystem(entt::hashed_string::hash_type name) {
109 auto *p = m_subsystems.at(name).get();
110 return dynamic_cast<T &>(*p);
111 }
112
114 std::tuple<entt::entity, DebugMeshComponent &>
115 addTriad(const Float3 &scale = Float3::Ones());
116
119 std::tuple<entt::entity, DebugMeshComponent &>
120 addLineGrid(const Float4 &color = 0xE0A236FF_rgbaf);
121
123 std::tuple<entt::entity, DebugMeshComponent &>
124 addArrow(const Float4 &color = 0xEA2502FF_rgbaf);
125
126 void update() {
127 for (auto [hash, system] : m_subsystems) {
128 system->update();
129 }
130 }
131
132 void render(CommandBuffer &cmdBuf, const Camera &camera) const;
133
134 void release();
135
137};
138static_assert(Scene<DebugScene>);
139
143 std::vector<Float4> colors;
144 bool enable = true;
145 Float3 scale = Float3::Ones();
146};
147
148namespace gui {
150 bool enable_pipeline_switch = true);
151}
152
153} // namespace candlewick
Definition CommandBuffer.h:16
Scene for organizing debug entities and render systems.
Definition DebugScene.h:50
T * getSystem(entt::hashed_string::hash_type name)
Get a subsystem by key (a hashed string)
Definition DebugScene.h:99
entt::registry & registry()
Definition DebugScene.h:85
DebugScene & operator=(const DebugScene &)=delete
T & addSystem(entt::hashed_string::hash_type name, Args &&...args)
Add a subsystem (IDebugSubSystem) to the scene.
Definition DebugScene.h:90
const Mesh & getMesh(DebugMeshType type) const
Definition DebugScene.h:74
const entt::registry & registry() const
Definition DebugScene.h:86
DebugScene(entt::registry &registry, const RenderContext &renderer)
void render(CommandBuffer &cmdBuf, const Camera &camera) const
std::tuple< entt::entity, DebugMeshComponent & > addTriad(const Float3 &scale=Float3::Ones())
Just the basic 3D triad.
DebugScene(DebugScene &&other)
std::tuple< entt::entity, DebugMeshComponent & > addArrow(const Float4 &color=0xEA2502FF_rgbaf)
Add an arrow debug entity.
@ TRANSFORM_SLOT
Definition DebugScene.h:70
T & tryGetSystem(entt::hashed_string::hash_type name)
Definition DebugScene.h:108
std::tuple< entt::entity, DebugMeshComponent & > addLineGrid(const Float4 &color=0xE0A236FF_rgbaf)
Add a basic line grid.
void update()
Definition DebugScene.h:126
@ COLOR_SLOT
Definition DebugScene.h:71
DebugScene & operator=(DebugScene &&)=delete
DebugScene(const DebugScene &)=delete
~DebugScene()
Definition DebugScene.h:136
const Device & device() const noexcept
Definition DebugScene.h:84
Class representing a graphics pipeline.
Definition GraphicsPipeline.h:15
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:122
Handle class for meshes (vertex buffers and an optional index buffer) on the GPU.
Definition Mesh.h:57
GUI utilities.
Definition DebugScene.h:148
void addDebugMesh(DebugMeshComponent &dmc, bool enable_pipeline_switch=true)
Definition Camera.h:8
Eigen::Vector3f Float3
Definition math_types.h:8
DebugPipelines
Definition DebugScene.h:13
@ TRIANGLE_FILL
Definition DebugScene.h:14
@ TRIANGLE_LINE
Definition DebugScene.h:15
Eigen::Vector4f Float4
Definition math_types.h:9
DebugMeshType
Definition DebugScene.h:20
@ TRIAD
Definition DebugScene.h:21
@ GRID
Definition DebugScene.h:22
@ ARROW
Definition DebugScene.h:23
The main way of using a camera to render things.
Definition Camera.h:19
Definition DebugScene.h:140
std::vector< Float4 > colors
Definition DebugScene.h:143
DebugMeshType meshType
Definition DebugScene.h:142
DebugPipelines pipeline_type
Definition DebugScene.h:141
Float3 scale
Definition DebugScene.h:145
bool enable
Definition DebugScene.h:144
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
IDebugSubSystem(DebugScene &scene)
Definition DebugScene.h:36
DebugScene & m_scene
Definition DebugScene.h:35
virtual ~IDebugSubSystem()=default
The RenderContext class provides a rendering context for a graphical application.
Definition RenderContext.h:36