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 inline Registry &GetRegistry() { return *_registry; }
51
56 inline const Registry &GetRegistry() const { return *_registry; }
57
61
64 void KillEntity(Id entity);
65
70 template <typename TResource> TResource &RegisterResource(TResource &&resource);
71
75 template <typename TResource> TResource &GetResource();
76
80 template <typename TResource> const TResource &GetResource() const;
81
85 template <typename TResource> bool HasResource() const;
86
89 template <typename TResource> void DeleteResource();
90
94 template <CScheduler TScheduler, typename... Args> TScheduler &RegisterScheduler(Args &&...args);
95
99 template <CScheduler TScheduler> TScheduler &GetScheduler();
100
104 Scheduler::AScheduler &GetScheduler(std::type_index id);
105
110 template <typename TSchedulerA, typename TSchedulerB> inline void SetSchedulerBefore()
111 {
112 this->_schedulers.Before<TSchedulerA, TSchedulerB>();
113 }
114
119 template <typename TSchedulerA, typename TSchedulerB> inline void SetSchedulerAfter()
120 {
121 this->_schedulers.After<TSchedulerA, TSchedulerB>();
122 }
123
129 template <typename TSchedulerA, typename TSchedulerB> inline void RemoveDependencyAfter()
130 {
131 this->_schedulers.RemoveDependencyAfter<TSchedulerA, TSchedulerB>();
132 }
133
139 template <typename TSchedulerA, typename TSchedulerB> inline void RemoveDependencyBefore()
140 {
141 this->_schedulers.RemoveDependencyBefore<TSchedulerA, TSchedulerB>();
142 }
143
146 bool IsRunning();
147
149 void Stop();
150
152 void RunCore();
153
163 template <CScheduler TScheduler, typename... Systems> decltype(auto) RegisterSystem(Systems... systems);
164
172 template <typename... Systems> decltype(auto) RegisterSystem(Systems... systems);
173
180 template <CScheduler TScheduler, typename System, typename ErrorCallback>
181 decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback);
182
190 template <typename System, typename ErrorCallback>
191 decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback);
192
197 template <CScheduler TScheduler> void DeleteScheduler();
198
200 void RunSystems();
201
204 bool IsEntityValid(Id entity) const;
205
210 template <CPlugin... TPlugins> void AddPlugins();
211
215 template <CPlugin TPlugin> bool HasPlugin() const;
216
220 bool HasPlugin(std::type_index type) const;
221
223 void ClearEntities();
224
228 template <CScheduler TScheduler> inline void SetDefaultScheduler()
229 {
230 SetDefaultScheduler(std::type_index(typeid(TScheduler)));
231 }
232
236 void SetDefaultScheduler(std::type_index scheduler);
237
241
242 private:
245 template <CPlugin TPlugin> void AddPlugin();
246
247 private:
249 std::unique_ptr<Registry> _registry;
255 std::vector<std::type_index> _schedulersToDelete;
257 std::unordered_map<std::type_index, std::unique_ptr<IPlugin>> _plugins;
260 bool _running = false;
261};
262} // namespace Engine
263
264#include "core/Core.inl"
void AddPlugin()
Adds a plugin.
Definition Core.inl:90
bool IsRunning()
Get the running state of the core.
Definition Core.cpp:62
void SetErrorPolicyForAllSchedulers(Scheduler::SchedulerErrorPolicy policy)
Sets the error policy for all schedulers.
Definition Core.cpp:101
std::unique_ptr< Registry > _registry
The registry that contains all components (and by definition, the entities).
Definition Core.hpp:249
void Stop()
Stop the core execution. It will finish the current loop, call shutdown systems if any and stop.
Definition Core.cpp:64
const Registry & GetRegistry() const
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.hpp:56
TResource & GetResource()
Get a reference of a resource.
Definition Core.inl: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:83
TScheduler & RegisterScheduler(Args &&...args)
Add a new scheduler to the registry.
Definition Core.inl:36
void SetSchedulerBefore()
Sets the execution order of two schedulers, ensuring that TSchedulerA is executed before TSchedulerB.
Definition Core.hpp:110
bool HasResource() const
Check if a resource is registered in the core.
Definition Core.inl: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:95
TScheduler & GetScheduler()
Get a scheduler from the registry by its type.
Definition Core.inl:47
Engine::SchedulerContainer _schedulers
The container that contains all the schedulers.
Definition Core.hpp:251
void RemoveDependencyAfter()
Removes a dependency between two schedulers, ensuring that TSchedulerB is no longer dependent on TSch...
Definition Core.hpp:129
void ClearEntities()
Clear all entities and components from the registry.
Definition Core.cpp:97
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.inl:77
void SetDefaultScheduler()
Sets the default scheduler.
Definition Core.hpp:228
decltype(auto) RegisterSystem(Systems... systems)
Add one or multiple systems associated with a specific scheduler.
Definition Core.inl: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:253
bool HasPlugin() const
Checks if a plugin is present.
Definition Core.inl:100
void AddPlugins()
Adds multiple plugins that will be call instantly through the Bind method to agregate their systems a...
Definition Core.inl:88
void RemoveDependencyBefore()
Removes a dependency between two schedulers, ensuring that TSchedulerA is no longer dependent on TSch...
Definition Core.hpp:139
void KillEntity(Id entity)
Kill an entity. It will remove all components from the entity.
Definition Core.cpp:51
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
bool _running
The running state of the core. It is true if the core is running, false otherwise....
Definition Core.hpp:260
std::vector< std::type_index > _schedulersToDelete
The list of schedulers to delete at the end of the current loop.
Definition Core.hpp:255
void SetSchedulerAfter()
Sets the execution order of two schedulers by specifying that one scheduler should execute after anot...
Definition Core.hpp:119
std::unordered_map< std::type_index, std::unique_ptr< IPlugin > > _plugins
The plugins added to the core.
Definition Core.hpp:257
void DeleteScheduler()
Deletes a scheduler.
Definition Core.inl:42
void DeleteResource()
Delete a resource from the core.
Definition Core.inl:29
Entity CreateEntity()
Create an entity in the context of the registry.
Definition Core.cpp:44
TResource & RegisterResource(TResource &&resource)
Store a resource instance.
Definition Core.inl:9
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.hpp:50
void RunCore()
Execute the core loop, which will run all the schedulers.
Definition Core.cpp:74
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:15
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:19
A strongly-typed identifier wrapper based on entt::id_type. This structure provides a type-safe wrapp...
Definition Id.hpp:49