neoGFX
Cross-platform C++ app/game engine
ecs.hpp
Go to the documentation of this file.
1 // ecs.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2018 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <neogfx/neogfx.hpp>
23 #include <neolib/timer.hpp>
24 #include <neogfx/core/object.hpp>
25 #include <neogfx/game/i_ecs.hpp>
26 
27 namespace neogfx::game
28 {
29  class ecs : public object<i_ecs>
30  {
31  public:
32  define_declared_event(SystemsPaused, systems_paused)
33  define_declared_event(SystemsResumed, systems_resumed)
34  define_declared_event(EntityCreated, entity_created, entity_id)
36  define_declared_event(HandleUpdated, handle_updated, handle_id)
37  private:
38  typedef std::vector<handle_t> handles_t;
39  public:
40  ecs(ecs_flags aCreationFlags = ecs_flags::PopulateEntityInfo);
41  ~ecs();
42  public:
43  ecs_flags flags() const override;
44  entity_id create_entity(const entity_archetype_id& aArchetypeId) override;
45  void destroy_entity(entity_id aEntityId) override;
46  public:
47  bool all_systems_paused() const override;
48  void pause_all_systems() override;
49  void resume_all_systems() override;
50  public:
51  const archetype_registry_t& archetypes() const override;
52  archetype_registry_t& archetypes() override;
53  const component_factories_t& component_factories() const override;
54  component_factories_t& component_factories() override;
55  const components_t& components() const override;
56  components_t& components() override;
57  const shared_component_factories_t& shared_component_factories() const override;
58  shared_component_factories_t& shared_component_factories() override;
59  const shared_components_t& shared_components() const override;
60  shared_components_t& shared_components() override;
61  const system_factories_t& system_factories() const override;
62  system_factories_t& system_factories() override;
63  const systems_t& systems() const override;
64  systems_t& systems() override;
65  public:
66  const i_entity_archetype& archetype(entity_archetype_id aArchetypeId) const override;
67  i_entity_archetype& archetype(entity_archetype_id aArchetypeId) override;
68  bool component_instantiated(component_id aComponentId) const override;
69  const i_component& component(component_id aComponentId) const override;
70  i_component& component(component_id aComponentId) override;
71  bool shared_component_instantiated(component_id aComponentId) const override;
72  const i_shared_component& shared_component(component_id aComponentId) const override;
73  i_shared_component& shared_component(component_id aComponentId) override;
74  bool system_instantiated(system_id aSystemId) const override;
75  const i_system& system(system_id aSystemId) const override;
76  i_system& system(system_id aSystemId) override;
77  public:
78  entity_id next_entity_id() override;
79  void free_entity_id(entity_id aId) override;
80  public:
81  bool archetype_registered(const i_entity_archetype& aArchetype) const override;
82  void register_archetype(const i_entity_archetype& aArchetype) override;
83  void register_archetype(std::shared_ptr<const i_entity_archetype> aArchetype) override;
84  bool component_registered(component_id aComponentId) const override;
85  void register_component(component_id aComponentId, component_factory aFactory) override;
86  bool shared_component_registered(component_id aComponentId) const override;
87  void register_shared_component(component_id aComponentId, shared_component_factory aFactory) override;
88  bool system_registered(system_id aSystemId) const override;
89  void register_system(system_id aSystemId, system_factory aFactory) override;
90  public:
91  handle_t to_handle(handle_id aId) const override;
92  handle_id add_handle(const std::type_info& aTypeInfo, handle_t aHandle) override;
93  handle_t update_handle(handle_id aId, const std::type_info& aTypeInfo, handle_t aHandle) override;
94  handle_t release_handle(handle_id aId) override;
95  private:
96  handle_id next_handle_id();
97  void free_handle_id(handle_id aId);
98  public:
99  using i_ecs::create_entity;
100  public:
101  using i_ecs::populate;
102  using i_ecs::populate_shared;
104  using i_ecs::component;
106  using i_ecs::shared_component;
107  using i_ecs::system_instantiated;
108  using i_ecs::system;
109  public:
111  using i_ecs::register_component;
114  using i_ecs::system_registered;
115  using i_ecs::register_system;
116  private:
117  ecs_flags iFlags;
118  archetype_registry_t iArchetypeRegistry;
119  component_factories_t iComponentFactories;
120  mutable components_t iComponents;
121  shared_component_factories_t iSharedComponentFactories;
122  mutable shared_components_t iSharedComponents;
123  system_factories_t iSystemFactories;
124  mutable systems_t iSystems;
125  entity_id iNextEntityId;
126  std::vector<entity_id> iFreedEntityIds;
127  handle_id iNextHandleId;
128  std::vector<handle_id> iFreedHandleIds;
129  handles_t iHandles;
130  neolib::callback_timer iSystemTimer;
131  std::atomic<bool> iSystemsPaused;
132  };
133 }
ecs_flags flags() const override
define_declared_event(SystemsPaused, systems_paused) define_declared_event(SystemsResumed
const systems_t & systems() const override
const shared_component_factories_t & shared_component_factories() const override
void resume_all_systems() override
const i_shared_component & shared_component(component_id aComponentId) const override
bool all_systems_paused() const override
void pause_all_systems() override
void free_entity_id(entity_id aId) override
void register_shared_component(component_id aComponentId, shared_component_factory aFactory) override
bool shared_component_registered(component_id aComponentId) const override
bool archetype_registered(const i_entity_archetype &aArchetype) const override
void register_archetype(const i_entity_archetype &aArchetype) override
entity_id create_entity(const entity_archetype_id &aArchetypeId) override
const system_factories_t & system_factories() const override
const archetype_registry_t & archetypes() const override
const i_component & component(component_id aComponentId) const override
bool shared_component_instantiated(component_id aComponentId) const override
const i_entity_archetype & archetype(entity_archetype_id aArchetypeId) const override
const component_factories_t & component_factories() const override
void register_system(system_id aSystemId, system_factory aFactory) override
handle_id add_handle(const std::type_info &aTypeInfo, handle_t aHandle) override
handle_t to_handle(handle_id aId) const override
bool system_registered(system_id aSystemId) const override
void destroy_entity(entity_id aEntityId) override
bool component_registered(component_id aComponentId) const override
bool component_instantiated(component_id aComponentId) const override
id_t entity_id
Definition: ecs_ids.hpp:34
id_t handle_id
Definition: ecs_ids.hpp:33
handle_t release_handle(handle_id aId) override
entity_id next_entity_id() override
const shared_components_t & shared_components() const override
const components_t & components() const override
handle_t update_handle(handle_id aId, const std::type_info &aTypeInfo, handle_t aHandle) override
void register_component(component_id aComponentId, component_factory aFactory) override
systems_resumed entity_destroyed
Definition: ecs.hpp:35
void * handle_t
Definition: ecs_ids.hpp:30
bool system_instantiated(system_id aSystemId) const override