neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
ecs.hpp
Go to the documentation of this file.
1// ecs.hpp
2/*
3 * Copyright (c) 2018, 2020 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <atomic>
40#include <neolib/core/mutex.hpp>
42#include <neolib/task/timer.hpp>
43#include <neolib/app/object.hpp>
44#include <neolib/ecs/i_ecs.hpp>
46#include <neolib/ecs/system.hpp>
47
48namespace neolib::ecs
49{
50 class NEOLIB_EXPORT ecs : public neolib::object<i_ecs>
51 {
52 public:
53 define_declared_event(SystemsPaused, systems_paused)
54 define_declared_event(SystemsResumed, systems_resumed)
55 define_declared_event(EntityCreated, entity_created, entity_id)
56 define_declared_event(EntityDestroyed, entity_destroyed, entity_id)
57 define_declared_event(HandleUpdated, handle_updated, handle_id)
58 private:
59 typedef std::vector<handle_t> handles_t;
60 public:
61 ecs(ecs_flags aCreationFlags = ecs_flags::Default);
62 ~ecs();
63 public:
64 neolib::recursive_spinlock& mutex() const override;
65 neolib::thread_pool& thread_pool() const override;
66 public:
67 ecs_flags flags() const override;
68 entity_id create_entity(const entity_archetype_id& aArchetypeId) override;
69 void async_create_entity(const std::function<void()>& aCreator) override;
70 void commit_async_entity_creation() override;
71 void destroy_entity(entity_id aEntityId, bool aNotify = true) override;
72 void async_destroy_entity(entity_id aEntityId, bool aNotify = true) override;
73 void commit_async_entity_destruction() override;
74 public:
75 bool run_threaded(const system_id& aSystemId) const override;
76 bool all_systems_paused() const override;
77 void pause_all_systems() override;
78 void resume_all_systems() override;
79 public:
80 const archetype_registry_t& archetypes() const override;
81 archetype_registry_t& archetypes() override;
82 const component_factories_t& component_factories() const override;
83 component_factories_t& component_factories() override;
84 const components_t& components() const override;
85 components_t& components() override;
86 const shared_component_factories_t& shared_component_factories() const override;
87 shared_component_factories_t& shared_component_factories() override;
88 const shared_components_t& shared_components() const override;
89 shared_components_t& shared_components() override;
90 const system_factories_t& system_factories() const override;
91 system_factories_t& system_factories() override;
92 const systems_t& systems() const override;
93 systems_t& systems() override;
94 public:
95 const i_entity_archetype& archetype(entity_archetype_id aArchetypeId) const override;
96 i_entity_archetype& archetype(entity_archetype_id aArchetypeId) override;
97 bool component_instantiated(component_id aComponentId) const override;
98 const i_component& component(component_id aComponentId) const override;
99 i_component& component(component_id aComponentId) override;
100 bool shared_component_instantiated(component_id aComponentId) const override;
101 const i_shared_component& shared_component(component_id aComponentId) const override;
103 bool system_instantiated(system_id aSystemId) const override;
104 const i_system& system(system_id aSystemId) const override;
105 i_system& system(system_id aSystemId) override;
106 public:
107 entity_id next_entity_id() override;
108 void free_entity_id(entity_id aId) override;
109 public:
110 bool archetype_registered(const i_entity_archetype& aArchetype) const override;
111 void register_archetype(const i_entity_archetype& aArchetype) override;
112 void register_archetype(std::shared_ptr<const i_entity_archetype> aArchetype) override;
113 bool component_registered(component_id aComponentId) const override;
114 void register_component(component_id aComponentId, component_factory aFactory) override;
115 bool shared_component_registered(component_id aComponentId) const override;
116 void register_shared_component(component_id aComponentId, shared_component_factory aFactory) override;
117 bool system_registered(system_id aSystemId) const override;
118 void register_system(system_id aSystemId, system_factory aFactory) override;
119 public:
120 handle_t to_handle(handle_id aId) const override;
121 handle_id add_handle(const std::type_info& aTypeInfo, handle_t aHandle) override;
122 handle_t update_handle(handle_id aId, const std::type_info& aTypeInfo, handle_t aHandle) override;
123 handle_t release_handle(handle_id aId) override;
124 private:
125 handle_id next_handle_id();
126 void free_handle_id(handle_id aId);
127 public:
128 using i_ecs::create_entity;
129 public:
130 using i_ecs::populate;
131 using i_ecs::populate_shared;
132 using i_ecs::component_instantiated;
133 using i_ecs::component;
134 using i_ecs::shared_component_instantiated;
135 using i_ecs::shared_component;
136 using i_ecs::system_instantiated;
137 using i_ecs::system;
138 public:
139 using i_ecs::component_registered;
140 using i_ecs::register_component;
141 using i_ecs::shared_component_registered;
142 using i_ecs::register_shared_component;
143 using i_ecs::system_registered;
144 using i_ecs::register_system;
145 private:
146 mutable neolib::recursive_spinlock iMutex;
147 mutable std::optional<neolib::thread_pool> iThreadPool;
148 ecs_flags iFlags;
149 archetype_registry_t iArchetypeRegistry;
150 component_factories_t iComponentFactories;
151 mutable components_t iComponents;
152 mutable std::vector<proxy_mutex<i_lockable>> iComponentMutexes;
153 shared_component_factories_t iSharedComponentFactories;
154 mutable shared_components_t iSharedComponents;
155 system_factories_t iSystemFactories;
156 mutable systems_t iSystems;
157 std::vector<std::function<void()>> iEntitiesToCreate;
158 std::vector<std::pair<entity_id, bool>> iEntitiesToDestroy;
159 entity_id iNextEntityId;
160 std::vector<entity_id> iFreedEntityIds;
161 handle_id iNextHandleId;
162 std::vector<handle_id> iFreedHandleIds;
163 handles_t iHandles;
164 neolib::callback_timer iSystemTimer;
165 std::atomic<bool> iSystemsPaused;
166 };
167}
define_declared_event(SystemsPaused, systems_paused) define_declared_event(SystemsResumed
void * handle_t
Definition ecs_ids.hpp:47
id_t handle_id
Definition ecs_ids.hpp:50
id_t entity_id
Definition ecs_ids.hpp:51
Definition plf_hive.h:79
#define define_declared_event(name, declName,...)
Definition event.hpp:195