|
| 1 | +/* |
| 2 | +** EPITECH PROJECT, 2024 |
| 3 | +** GameEngine |
| 4 | +** File description: |
| 5 | +** GEngine.hpp |
| 6 | +*/ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include "GEngine/BaseEngine.hpp" |
| 11 | +#include "GEngine/driver/Engine.hpp" |
| 12 | +#include "GEngine/game/Engine.hpp" |
| 13 | +#include "GEngine/libdev/Component.hpp" |
| 14 | +#include <memory> |
| 15 | +#include <mutex> |
| 16 | +#include <utility> // For std::forward |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "GEngine/libdev/System.hpp" |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief Registry class for managing components and systems registration. |
| 23 | + */ |
| 24 | +struct Registry { |
| 25 | + /** |
| 26 | + * @brief Constructor for the Registry class. |
| 27 | + * |
| 28 | + * @param engines A vector of reference wrappers to engines for component and system registration. |
| 29 | + */ |
| 30 | + Registry(gengine::BaseEngine &local, gengine::BaseEngine &remote) |
| 31 | + : m_local(local) |
| 32 | + , m_remote(remote) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Register a component type with the engines. |
| 37 | + * |
| 38 | + * This method iterates through the engines and registers the specified component type. |
| 39 | + * |
| 40 | + * @tparam ComponentType The type of component to register. |
| 41 | + */ |
| 42 | + template <typename ComponentType> |
| 43 | + void registerComponent(void) { |
| 44 | + m_local.registerComponent<ComponentType>(); |
| 45 | + m_remote.registerComponent<ComponentType>(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @brief Register a system type with the engines. |
| 50 | + * |
| 51 | + * This method iterates through the engines and registers the specified system type |
| 52 | + * with the provided parameters. |
| 53 | + * |
| 54 | + * @tparam SystemType The type of system to register. |
| 55 | + * @tparam Args Variadic template parameters for system construction arguments. |
| 56 | + * @param params Parameters for constructing the system. |
| 57 | + */ |
| 58 | + template <typename SystemType, class... Args> |
| 59 | + void registerSystem(Args &&...params) { |
| 60 | + if constexpr (!std::is_base_of<gengine::LocalSystem, SystemType>::value) |
| 61 | + m_remote.registerSystem<SystemType>(std::forward<Args>(params)...); |
| 62 | + if constexpr (!std::is_base_of<gengine::RemoteSystem, SystemType>::value) |
| 63 | + m_local.registerSystem<SystemType>(std::forward<Args>(params)...); |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + gengine::BaseEngine &m_local; |
| 68 | + gengine::BaseEngine &m_remote; |
| 69 | +}; |
| 70 | + |
| 71 | +extern "C" { |
| 72 | +/** |
| 73 | + * @brief Declare game components and systems for the engine. |
| 74 | + * |
| 75 | + * This function declares all game components and systems needed for the engine. |
| 76 | + * It is intended to be called during the initialization of the GEngine instance. |
| 77 | + * |
| 78 | + * @param r Pointer to the Registry instance for registering components and systems. |
| 79 | + */ |
| 80 | +void GEngineDeclareComponents(Registry *r); |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Declare driver components and systems for the engine. |
| 84 | + * |
| 85 | + * This function declares all driver components and systems needed for the engine. |
| 86 | + * It is intended to be called during the initialization of the GEngine instance. |
| 87 | + * |
| 88 | + * @param r Pointer to the Registry instance for registering components and systems. |
| 89 | + */ |
| 90 | +void GEngineDeclareSystems(Registry *r); |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Declare shared components and events for the engine. |
| 94 | + * |
| 95 | + * This function declares shared components and events that can be used across different systems in the engine. |
| 96 | + * It is intended to be called during the initialization of the GEngine instance. |
| 97 | + * |
| 98 | + * @param r Pointer to the Registry instance for registering components and systems. |
| 99 | + */ |
| 100 | +void GEngineDeclareEvents(Registry *r); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Main class for the Game Engine singleton. |
| 105 | + * |
| 106 | + * This class manages the initialization and access to the game engine components and systems. |
| 107 | + * It ensures that the engine is instantiated only once and provides methods to access its features. |
| 108 | + */ |
| 109 | +class GEngine { |
| 110 | +public: |
| 111 | + /** |
| 112 | + * @brief Initialize the GEngine singleton instance. |
| 113 | + * |
| 114 | + * This function ensures that the GEngine instance is initialized only once using a thread-safe mechanism. |
| 115 | + * It also registers the necessary game engine elements by calling the registerElements method. |
| 116 | + */ |
| 117 | + static void init(void) { |
| 118 | + std::call_once(initFlag, []() { |
| 119 | + instance.reset(new GEngine()); |
| 120 | + instance->registerElements(); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief Get the singleton instance of GEngine. |
| 126 | + * |
| 127 | + * This method returns a reference to the GEngine instance. If the instance is not yet created, |
| 128 | + * it calls the init method to create it. |
| 129 | + * |
| 130 | + * @return Reference to the GEngine instance. |
| 131 | + */ |
| 132 | + static GEngine &getInstance(void) { |
| 133 | + if (!instance) |
| 134 | + init(); |
| 135 | + return *instance; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @brief Get the pointer to the singleton instance of GEngine. |
| 140 | + * |
| 141 | + * This method returns a pointer to the GEngine instance. If the instance is not yet created, |
| 142 | + * it calls the init method to create it. |
| 143 | + * |
| 144 | + * @return Pointer to the GEngine instance. |
| 145 | + */ |
| 146 | + static GEngine *getInstancePointer(void) { |
| 147 | + if (!instance) |
| 148 | + init(); |
| 149 | + return instance.get(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @brief Destructor. |
| 154 | + * |
| 155 | + * This destructor is private to prevent deletion of the singleton instance through |
| 156 | + * deletion of pointers to GEngine. |
| 157 | + */ |
| 158 | + ~GEngine() = default; |
| 159 | + |
| 160 | + static gengine::BaseEngine &getLocal(void) { |
| 161 | + if (!instance) |
| 162 | + init(); |
| 163 | + return instance->m_local; |
| 164 | + } |
| 165 | + |
| 166 | + static gengine::BaseEngine &getRemote(void) { |
| 167 | + if (!instance) |
| 168 | + init(); |
| 169 | + return instance->m_remote; |
| 170 | + } |
| 171 | + |
| 172 | +private: |
| 173 | + /** |
| 174 | + * @brief Private constructor to prevent direct instantiation. |
| 175 | + * |
| 176 | + * This constructor is private to enforce the singleton pattern, preventing the creation |
| 177 | + * of multiple instances of GEngine. |
| 178 | + */ |
| 179 | + GEngine() = default; |
| 180 | + |
| 181 | + /** |
| 182 | + * @brief Register all game engine components and systems. |
| 183 | + * |
| 184 | + * This function calls the necessary functions to declare the various components and systems of the game engine. |
| 185 | + * It ensures that all necessary elements are properly registered during initialization. |
| 186 | + */ |
| 187 | + static void registerElements(void) { |
| 188 | + if (!instance) |
| 189 | + init(); |
| 190 | + |
| 191 | + auto sharedRegistry = std::make_unique<Registry>(instance->m_local, instance->m_remote); |
| 192 | + |
| 193 | + GEngineDeclareComponents(sharedRegistry.get()); |
| 194 | + GEngineDeclareSystems(sharedRegistry.get()); |
| 195 | + GEngineDeclareEvents(sharedRegistry.get()); |
| 196 | + } |
| 197 | + |
| 198 | + // Delete copy constructor and assignment operator to enforce singleton pattern |
| 199 | + GEngine(const GEngine &) = delete; |
| 200 | + GEngine &operator=(const GEngine &) = delete; |
| 201 | + |
| 202 | + // Singleton instance and initialization flag |
| 203 | + static std::unique_ptr<GEngine> instance; |
| 204 | + static std::once_flag initFlag; |
| 205 | + |
| 206 | + gengine::BaseEngine m_local; ///< Instance of the game engine. |
| 207 | + gengine::BaseEngine m_remote; ///< Instance of the driver engine. |
| 208 | +}; |
| 209 | + |
| 210 | +// Initialize static members |
| 211 | +// std::unique_ptr<GEngine> GEngine::instance = nullptr; |
| 212 | +// std::once_flag GEngine::initFlag; |
0 commit comments