Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Core.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <entt/entt.hpp>
5#include <functional>
6#include <memory>
7#include <typeindex>
8#include <unordered_map>
9#include <vector>
10
11#include "Id.hpp"
12#include "Logger.hpp"
13#include "plugin/IPlugin.hpp"
15#include "scheduler/Update.hpp"
16
17namespace Engine {
18
19class Entity;
20
21template <typename TScheduler>
22concept CScheduler = std::derived_from<TScheduler, Scheduler::AScheduler>;
23
24template <typename TPlugin>
25concept CPlugin = std::derived_from<TPlugin, IPlugin>;
26
33class Core {
34 public:
37 using Registry = entt::basic_registry<Id>;
38
41 Core();
42
44 ~Core();
45
50
54 const Registry &GetRegistry() const;
55
59
62 void KillEntity(Id entity);
63
68 template <typename TResource> TResource &RegisterResource(TResource &&resource);
69
73 template <typename TResource> TResource &GetResource();
74
78 template <typename TResource> const TResource &GetResource() const;
79
83 template <typename TResource> bool HasResource() const;
84
87 template <typename TResource> void DeleteResource();
88
92 template <CScheduler TScheduler, typename... Args> TScheduler &RegisterScheduler(Args &&...args);
93
97 template <CScheduler TScheduler> TScheduler &GetScheduler();
98
102 Scheduler::AScheduler &GetScheduler(std::type_index id);
103
107 template <typename TSchedulerA, typename TSchedulerB> void SetSchedulerBefore();
108
112 template <typename TSchedulerA, typename TSchedulerB> void SetSchedulerAfter();
113
118 template <typename TSchedulerA, typename TSchedulerB> void RemoveDependencyAfter();
119
124 template <typename TSchedulerA, typename TSchedulerB> void RemoveDependencyBefore();
125
128 bool IsRunning();
129
131 void Stop();
132
134 void Run(void);
135
145 template <CScheduler TScheduler, typename... Systems> decltype(auto) RegisterSystem(Systems... systems);
146
154 template <typename... Systems> decltype(auto) RegisterSystem(Systems... systems);
155
162 template <CScheduler TScheduler, typename System, typename ErrorCallback>
163 decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback);
164
172 template <typename System, typename ErrorCallback>
173 decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback);
174
179 template <CScheduler TScheduler> void DeleteScheduler();
180
182 void RunSystems();
183
186 bool IsEntityValid(Id entity) const;
187
192 template <CPlugin... TPlugins> void AddPlugins();
193
197 template <CPlugin TPlugin> bool HasPlugin() const;
198
202 bool HasPlugin(std::type_index type) const;
203
205 void ClearEntities();
206
209 template <CScheduler TScheduler> void SetDefaultScheduler();
210
214 void SetDefaultScheduler(std::type_index scheduler);
215
219
220 private:
223 template <CPlugin TPlugin> void AddPlugin();
224
225 private:
227 std::unique_ptr<Registry> _registry;
233 std::vector<std::type_index> _schedulersToDelete;
235 std::unordered_map<std::type_index, std::unique_ptr<IPlugin>> _plugins;
238 bool _running = false;
239};
240} // namespace Engine
241
242#include "core/Core.ipp"
void AddPlugin()
Adds a plugin.
Definition Core.ipp:110
bool IsRunning()
Get the running state of the core.
Definition Core.cpp:66
void SetErrorPolicyForAllSchedulers(Scheduler::SchedulerErrorPolicy policy)
Sets the error policy for all schedulers.
Definition Core.cpp:105
std::unique_ptr< Registry > _registry
The registry that contains all components (and by definition, the entities).
Definition Core.hpp:227
void Stop()
Stop the core execution. It will finish the current loop, call shutdown systems if any and stop.
Definition Core.cpp:68
TResource & GetResource()
Get a reference of a resource.
Definition Core.ipp:14
~Core()
Destructor of the core. It will clear the registry and the scheduler container.
Definition Core.cpp:42
void RunSystems()
Run all the systems once by calling each scheduler.
Definition Core.cpp:87
TScheduler & RegisterScheduler(Args &&...args)
Add a new scheduler to the registry.
Definition Core.ipp:36
void SetSchedulerBefore()
Sets the execution order of two schedulers, ensuring that TSchedulerA is executed before TSchedulerB.
Definition Core.ipp:66
bool HasResource() const
Check if a resource is registered in the core.
Definition Core.ipp:24
bool IsEntityValid(Id entity) const
Check if entity is valid in the context of the registry. It check if the id of the entity exist.
Definition Core.cpp:99
TScheduler & GetScheduler()
Get a scheduler from the registry by its type.
Definition Core.ipp:47
Engine::SchedulerContainer _schedulers
The container that contains all the schedulers.
Definition Core.hpp:229
void RemoveDependencyAfter()
Removes a dependency between two schedulers, ensuring that TSchedulerB is no longer dependent on TSch...
Definition Core.ipp:76
void ClearEntities()
Clear all entities and components from the registry.
Definition Core.cpp:101
decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback)
Add a system to a specific scheduler, associated with a callback that should run if the system fails ...
Definition Core.ipp:97
void SetDefaultScheduler()
Sets the default scheduler.
Definition Core.ipp:125
decltype(auto) RegisterSystem(Systems... systems)
Add one or multiple systems associated with a specific scheduler.
Definition Core.ipp:61
Core()
Default constructor of the core. It will initialize the registry and the scheduler container....
Definition Core.cpp:11
std::type_index _defaultScheduler
The default scheduler type index. It is used when adding systems without specifying a scheduler.
Definition Core.hpp:231
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.cpp:44
bool HasPlugin() const
Checks if a plugin is present.
Definition Core.ipp:120
void AddPlugins()
Adds multiple plugins that will be call instantly through the Bind method to agregate their systems a...
Definition Core.ipp:108
void RemoveDependencyBefore()
Removes a dependency between two schedulers, ensuring that TSchedulerA is no longer dependent on TSch...
Definition Core.ipp:81
void KillEntity(Id entity)
Kill an entity. It will remove all components from the entity.
Definition Core.cpp:55
entt::basic_registry< Id > Registry
The type of the registry used by the core. It is an alias for entt::basic_registry with Id as entity ...
Definition Core.hpp:37
void Run(void)
Run a loop of schedulers until _running is false.
Definition Core.cpp:78
bool _running
The running state of the core. It is true if the core is running, false otherwise....
Definition Core.hpp:238
std::vector< std::type_index > _schedulersToDelete
The list of schedulers to delete at the end of the current loop.
Definition Core.hpp:233
void SetSchedulerAfter()
Sets the execution order of two schedulers by specifying that one scheduler should execute after anot...
Definition Core.ipp:71
std::unordered_map< std::type_index, std::unique_ptr< IPlugin > > _plugins
The plugins added to the core.
Definition Core.hpp:235
void DeleteScheduler()
Deletes a scheduler.
Definition Core.ipp:42
void DeleteResource()
Delete a resource from the core.
Definition Core.ipp:29
Entity CreateEntity()
Create an entity in the context of the registry.
Definition Core.cpp:48
TResource & RegisterResource(TResource &&resource)
Store a resource instance.
Definition Core.ipp:9
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
Manages a collection of schedulers, allowing addition, retrieval, and deletion of schedulers.
Definition SchedulerContainer.hpp:39
AScheduler is an abstract class that implements the IScheduler interface. It provides common function...
Definition AScheduler.hpp:16
Update scheduler that runs systems every time it is called.
Definition Update.hpp:8
Definition Core.hpp:25
Definition Core.hpp:22
SchedulerErrorPolicy
Enum that defines how the scheduler will handle errors.
Definition IScheduler.hpp:11
Definition Core.hpp:17
FunctionUtils::CallableFunction< TCallable, void, Core & > System
Definition System.hpp:20
A strongly-typed identifier wrapper based on entt::id_type. This structure provides a type-safe wrapp...
Definition Id.hpp:47