Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
ComponentReflection.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <functional>
5#include <glm/glm.hpp>
6#include "ecs/Entity.hpp"
7#include "ecs/Registry.hpp"
8#include "core/EngineAPI.hpp"
9
10namespace Engine {
11
27
28
34 std::string name;
36 size_t offset;
37 };
38
50 std::string name;
52 std::string category = "General";
55 std::string displayName;
56 std::vector<ComponentField> fields;
57
58 // Lifecycle callbacks
59 std::function<void(Registry&, Entity)> add;
60 std::function<bool(Registry&, Entity)> has;
61 std::function<void(Registry&, Entity)> remove;
62 std::function<void*(Registry&, Entity)> get;
63 };
64
70 public:
72
73 void registerComponent(const ComponentReflection& refl);
74 const std::vector<ComponentReflection>& getReflections() const;
75
76 private:
78 std::vector<ComponentReflection> reflections;
79 };
80
81} // namespace Engine
82
83// Forward declarations for type deduction specializations
84enum class RigidBodyType;
85struct RotationField;
86
87namespace Engine {
88
89 template<typename T> struct DeduceFieldType;
90 template<> struct DeduceFieldType<float> { static constexpr FieldType value = FieldType::Float; };
91 template<> struct DeduceFieldType<double> { static constexpr FieldType value = FieldType::Float; };
92 template<> struct DeduceFieldType<int> { static constexpr FieldType value = FieldType::Float; };
93 template<> struct DeduceFieldType<bool> { static constexpr FieldType value = FieldType::Bool; };
94 template<> struct DeduceFieldType<glm::vec2> { static constexpr FieldType value = FieldType::Vec2; };
95 template<> struct DeduceFieldType<glm::vec3> { static constexpr FieldType value = FieldType::Vec3; };
96 template<> struct DeduceFieldType<RotationField> { static constexpr FieldType value = FieldType::Vec3; };
97 template<> struct DeduceFieldType<glm::vec4> { static constexpr FieldType value = FieldType::Vec4; };
98 template<> struct DeduceFieldType<std::string> { static constexpr FieldType value = FieldType::String; };
99 template<> struct DeduceFieldType<Entity> { static constexpr FieldType value = FieldType::Entity; };
100 template<> struct DeduceFieldType<RigidBodyType> { static constexpr FieldType value = FieldType::RigidBodyType; };
101
102
103} // namespace Engine
104
105#define REFLECT_FIELD(Type, FieldName) \
106 refl.fields.push_back({ \
107 #FieldName, \
108 Engine::DeduceFieldType<std::decay_t<decltype(std::declval<Type>().FieldName)>>::value, \
109 offsetof(Type, FieldName) \
110 });
111
124#define REGISTER_COMPONENT_EXPAND(Type, MenuPath, Counter) \
125 namespace { \
126 struct AutoRegisterComp_##Counter { \
127 AutoRegisterComp_##Counter() { \
128 Engine::ComponentReflection refl; \
129 refl.name = #Type; \
130 size_t scopePos = refl.name.rfind("::"); \
131 if (scopePos != std::string::npos) refl.name = refl.name.substr(scopePos + 2); \
132 if (refl.name.size() > 9 && refl.name.rfind("Component") == refl.name.size() - 9) { \
133 refl.name = refl.name.substr(0, refl.name.size() - 9); \
134 } \
135 /* Parse "Category/Display Name" path */ \
136 std::string _path = MenuPath; \
137 size_t _slash = _path.rfind('/'); \
138 if (_slash != std::string::npos) { \
139 refl.category = _path.substr(0, _slash); \
140 refl.displayName = _path.substr(_slash + 1); \
141 } else { \
142 refl.category = _path; \
143 refl.displayName = ""; /* derived from name at render time */ \
144 } \
145 refl.add = [](Registry& reg, Entity e) { reg.emplace<Type>(e, Type{}); }; \
146 refl.has = [](Registry& reg, Entity e) { return reg.has<Type>(e); }; \
147 refl.remove = [](Registry& reg, Entity e) { reg.remove<Type>(e); }; \
148 refl.get = [](Registry& reg, Entity e) { return static_cast<void*>(reg.get<Type>(e)); }; \
149 Engine::ComponentReflectionRegistry::getInstance().registerComponent(refl); \
150 } \
151 }; \
152 static AutoRegisterComp_##Counter global_autoRegComp_##Counter; \
153 }
154
155#define REGISTER_COMPONENT_CONCAT(Type, MenuPath, Counter) REGISTER_COMPONENT_EXPAND(Type, MenuPath, Counter)
156#define REGISTER_COMPONENT(Type, MenuPath) REGISTER_COMPONENT_CONCAT(Type, MenuPath, __COUNTER__)
157
158#define REFLECT_COMPONENT_EXPAND(Type, MenuPath, Counter, InitFunc) \
159 namespace { \
160 struct AutoRegisterComp_##Counter { \
161 AutoRegisterComp_##Counter() { \
162 Engine::ComponentReflection refl; \
163 refl.name = #Type; \
164 size_t scopePos = refl.name.rfind("::"); \
165 if (scopePos != std::string::npos) refl.name = refl.name.substr(scopePos + 2); \
166 if (refl.name.size() > 9 && refl.name.rfind("Component") == refl.name.size() - 9) { \
167 refl.name = refl.name.substr(0, refl.name.size() - 9); \
168 } \
169 std::string _path = MenuPath; \
170 size_t _slash = _path.rfind('/'); \
171 if (_slash != std::string::npos) { \
172 refl.category = _path.substr(0, _slash); \
173 refl.displayName = _path.substr(_slash + 1); \
174 } else { \
175 refl.category = _path; \
176 refl.displayName = ""; \
177 } \
178 refl.add = [](Registry& reg, Entity e) { reg.emplace<Type>(e, Type{}); }; \
179 refl.has = [](Registry& reg, Entity e) { return reg.has<Type>(e); }; \
180 refl.remove = [](Registry& reg, Entity e) { reg.remove<Type>(e); }; \
181 refl.get = [](Registry& reg, Entity e) { return static_cast<void*>(reg.get<Type>(e)); }; \
182 auto init = InitFunc; \
183 init(refl); \
184 Engine::ComponentReflectionRegistry::getInstance().registerComponent(refl); \
185 } \
186 }; \
187 static AutoRegisterComp_##Counter global_autoRegComp_##Counter; \
188 }
189
190#define REFLECT_COMPONENT_CONCAT(Type, MenuPath, Counter, InitFunc) REFLECT_COMPONENT_EXPAND(Type, MenuPath, Counter, InitFunc)
191#define REFLECT_COMPONENT(Type, MenuPath, InitFunc) REFLECT_COMPONENT_CONCAT(Type, MenuPath, __COUNTER__, InitFunc)
192
193
194
195
196
197
198
199
200
201
#define ENGINE_API
Definition EngineAPI.hpp:10
RigidBodyType
Definition RigidBody.hpp:8
static ComponentReflectionRegistry & getInstance()
Definition ComponentReflection.cpp:6
const std::vector< ComponentReflection > & getReflections() const
Definition ComponentReflection.cpp:52
void registerComponent(const ComponentReflection &refl)
Definition ComponentReflection.cpp:13
std::vector< ComponentReflection > reflections
Definition ComponentReflection.hpp:78
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
Definition Application.hpp:16
FieldType
Supported field types for component reflection.
Definition ComponentReflection.hpp:16
@ Vec3
Definition ComponentReflection.hpp:20
@ Int
Definition ComponentReflection.hpp:18
@ Entity
Definition ComponentReflection.hpp:22
@ RigidBodyType
Definition ComponentReflection.hpp:21
@ Float
Definition ComponentReflection.hpp:17
@ String
Definition ComponentReflection.hpp:23
@ Bool
Definition ComponentReflection.hpp:19
@ Vec4
Definition ComponentReflection.hpp:25
@ Vec2
Definition ComponentReflection.hpp:24
Standard library namespace.
Reflection metadata for a single field inside a component.
Definition ComponentReflection.hpp:33
FieldType type
Definition ComponentReflection.hpp:35
size_t offset
Definition ComponentReflection.hpp:36
std::string name
Definition ComponentReflection.hpp:34
Reflection metadata and ECS callbacks for a component.
Definition ComponentReflection.hpp:48
std::vector< ComponentField > fields
Definition ComponentReflection.hpp:56
std::string name
Internal component name (derived from type, e.g. "SpriteRenderer").
Definition ComponentReflection.hpp:50
std::function< void *(Registry &, Entity)> get
Definition ComponentReflection.hpp:62
std::function< void(Registry &, Entity)> remove
Definition ComponentReflection.hpp:61
std::function< void(Registry &, Entity)> add
Definition ComponentReflection.hpp:59
std::string displayName
Human-readable label inside the category submenu (e.g. "Sprite Renderer"). If empty the editor derive...
Definition ComponentReflection.hpp:55
std::string category
Top-level menu group shown in "Add Component" (e.g. "Rendering & Lights").
Definition ComponentReflection.hpp:52
std::function< bool(Registry &, Entity)> has
Definition ComponentReflection.hpp:60
static constexpr FieldType value
Definition ComponentReflection.hpp:99
static constexpr FieldType value
Definition ComponentReflection.hpp:100
static constexpr FieldType value
Definition ComponentReflection.hpp:96
static constexpr FieldType value
Definition ComponentReflection.hpp:93
static constexpr FieldType value
Definition ComponentReflection.hpp:91
static constexpr FieldType value
Definition ComponentReflection.hpp:90
static constexpr FieldType value
Definition ComponentReflection.hpp:94
static constexpr FieldType value
Definition ComponentReflection.hpp:95
static constexpr FieldType value
Definition ComponentReflection.hpp:97
static constexpr FieldType value
Definition ComponentReflection.hpp:92
static constexpr FieldType value
Definition ComponentReflection.hpp:98
Definition ComponentReflection.hpp:89
Compatibility layer that wraps a glm::quat but behaves like a glm::vec3 for Euler angles.
Definition Transform.hpp:15