Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ComponentRegistry.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 - 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6ComponentRegistry: Auto-registration system for ECS components
7
8Purpose:
9- Automatic registration of ECS components in PrefabFactory
10- Eliminates manual switch-case maintenance
11- Pattern: Place AUTO_REGISTER_COMPONENT() immediately after each struct definition
12
13Usage:
14 struct MyComponent_data {
15 float value = 0.0f;
16 };
17 AUTO_REGISTER_COMPONENT(MyComponent_data);
18
19Implementation Notes:
20- Uses static initialization (before main()) for auto-registration
21- Safe because: no cross-component dependencies, singleton pattern, simple registration
22- Keep component constructors simple (default initialization only)
23- Complex initialization should be done in specialized InstantiateXYZ() functions
24*/
25
26#pragma once
27#include "World.h"
28#include "ComponentDefinition.h"
29#include <functional>
30#include <iostream>
31
32// Forward declaration
33class PrefabFactory;
34
35//==============================================================================
36// GENERIC COMPONENT INSTANTIATION
37//==============================================================================
38
39/// Generic template function for instantiating any ECS component
40/// This handles the common case: create component if missing, get reference
41/// Specialized behavior can still use custom InstantiateXYZ() functions
42///
43/// @return true if component was created successfully, false otherwise
44/// Note: Currently always returns true for generic components.
45/// Custom factory functions can return false to indicate failure.
46template<typename T>
48{
49 // Create component if it doesn't exist
50 if (!World::Get().HasComponent<T>(entity))
51 {
52 World::Get().AddComponent<T>(entity);
53 }
54
55 // Get component reference
56 T& component = World::Get().GetComponent<T>(entity);
57
58 // Note: Parameter application is handled by specialized functions
59 // or can be extended here with reflection/traits
60
61 return true;
62}
63
64//==============================================================================
65// AUTO-REGISTRATION MACRO
66//==============================================================================
67
68/// Macro for auto-registering a component at program startup
69/// Place this immediately after the struct definition
70///
71/// Example:
72/// struct Position_data { Vector position; };
73/// AUTO_REGISTER_COMPONENT(Position_data);
74///
75/// This creates a static dummy variable whose constructor executes before main()
76#define AUTO_REGISTER_COMPONENT(ComponentType) \
77 namespace { \
78 struct ComponentRegistrar_##ComponentType { \
79 ComponentRegistrar_##ComponentType(); \
80 }; \
81 static ComponentRegistrar_##ComponentType g_registrar_##ComponentType; \
82 } \
83 inline ComponentRegistrar_##ComponentType::ComponentRegistrar_##ComponentType() { \
84 extern void RegisterComponentFactory_Internal(const char*, std::function<bool(EntityID, const ComponentDefinition&)>); \
85 RegisterComponentFactory_Internal( \
86 #ComponentType, \
87 [](EntityID entity, const ComponentDefinition& def) { \
88 return InstantiateComponentGeneric<ComponentType>(entity, def); \
89 } \
90 ); \
91 }
bool InstantiateComponentGeneric(EntityID entity, const ComponentDefinition &def)
Generic template function for instantiating any ECS component This handles the common case: create co...
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
World and ECS Manager for Olympe Engine.
Factory class for creating entities from prefab blueprints.
static World & Get()
Get singleton instance (short form)
Definition World.h:232
T & AddComponent(EntityID entity, Args &&... args)
Definition World.h:393
T & GetComponent(EntityID entity)
Definition World.h:438