From 604513f44daad8b683a450931bf85dacdb2b98d0 Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Tue, 19 Sep 2023 08:40:45 +0100 Subject: [PATCH] Use 'Entity' type instead of uint32_t --- CMakeLists.txt | 10 ++++++++-- include/components/transform.h | 4 +++- include/{ecs_system.h => ecs.h} | 20 +++++++++++--------- include/scene.h | 14 +++++++------- include/systems/collisions.h | 20 ++++++++++---------- include/systems/custom_behaviour.h | 6 +++--- include/systems/mesh_render_system.h | 4 ++-- include/systems/transform.h | 4 ++-- include/systems/ui_render_system.h | 4 ++-- include/util/model_loader.h | 2 +- src/{ecs_system.cpp => ecs.cpp} | 2 +- src/scene.cpp | 6 +++--- src/systems/collisions.cpp | 4 ++-- src/systems/custom_behaviour.cpp | 4 ++-- src/systems/mesh_render_system.cpp | 4 ++-- src/systems/transform.cpp | 6 +++--- src/systems/ui_render_system.cpp | 2 +- src/util/model_loader.cpp | 6 +++--- test/src/camera_controller.cpp | 3 ++- test/src/camera_controller.hpp | 2 +- test/src/game.cpp | 12 ++++++++---- 21 files changed, 77 insertions(+), 62 deletions(-) rename include/{ecs_system.h => ecs.h} (74%) rename src/{ecs_system.cpp => ecs.cpp} (93%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 27c91c1..4ba30be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,13 +12,19 @@ project(engine LANGUAGES CXX # from command: find . -regex "^\.\/.*" | sort set(SRC_FILES "src/application.cpp" - "src/ecs_system.cpp" + "src/ecs.cpp" "src/gfx_device_vulkan.cpp" + "src/imgui/imconfig.h" "src/imgui/imgui.cpp" + "src/imgui/imgui.h" "src/imgui/imgui_demo.cpp" "src/imgui/imgui_draw.cpp" + "src/imgui/imgui_internal.h" "src/imgui/imgui_tables.cpp" "src/imgui/imgui_widgets.cpp" + "src/imgui/imstb_rectpack.h" + "src/imgui/imstb_textedit.h" + "src/imgui/imstb_truetype.h" "src/input_manager.cpp" "src/libs/stb_image.cpp" "src/libs/stb_truetype.cpp" @@ -55,7 +61,7 @@ set(INCLUDE_FILES "include/components/mesh_renderable.h" "include/components/ui_renderable.h" "include/components/transform.h" - "include/ecs_system.h" + "include/ecs.h" "include/engine_api.h" "include/event_system.h" "include/gfx.h" diff --git a/include/components/transform.h b/include/components/transform.h index df22134..40bf7c9 100644 --- a/include/components/transform.h +++ b/include/components/transform.h @@ -7,6 +7,8 @@ #include #include +#include "ecs.h" + namespace engine { struct TransformComponent { @@ -17,7 +19,7 @@ struct TransformComponent { glm::vec3 scale; std::string tag; - uint32_t parent; + Entity parent; bool is_static; }; diff --git a/include/ecs_system.h b/include/ecs.h similarity index 74% rename from include/ecs_system.h rename to include/ecs.h index f6bec0d..8b4c89c 100644 --- a/include/ecs_system.h +++ b/include/ecs.h @@ -1,5 +1,5 @@ -#ifndef ENGINE_INCLUDE_ECS_SYSTEM_H_ -#define ENGINE_INCLUDE_ECS_SYSTEM_H_ +#ifndef ENGINE_INCLUDE_ECS_H_ +#define ENGINE_INCLUDE_ECS_H_ #include #include @@ -13,7 +13,9 @@ namespace engine { class Scene; -constexpr size_t kMaxComponents = 64; +using Entity = uint32_t; // ECS entity + +constexpr size_t kMaxComponents = 10; class IComponentArray { public: @@ -23,7 +25,7 @@ class IComponentArray { template class ComponentArray : public IComponentArray { public: - void InsertData(uint32_t entity, T component) { + void InsertData(Entity entity, T component) { if (component_array_.size() < entity + 1) { component_array_.resize(entity + 1); } @@ -31,11 +33,11 @@ class ComponentArray : public IComponentArray { component_array_.at(entity) = component; } - void DeleteData(uint32_t entity) { + void DeleteData(Entity entity) { (void)entity; // TODO } - T* GetData(uint32_t entity) { + T* GetData(Entity entity) { assert(entity < component_array_.size()); return &component_array_[entity]; } @@ -53,15 +55,15 @@ class System { virtual void OnUpdate(float ts) = 0; - virtual void OnComponentInsert(uint32_t) {} - virtual void OnComponentRemove(uint32_t) {} + virtual void OnComponentInsert(Entity) {} + virtual void OnComponentRemove(Entity) {} Scene* const scene_; std::bitset signature_; // entities that contain the needed components - std::set entities_{}; + std::set entities_{}; }; } // namespace engine diff --git a/include/scene.h b/include/scene.h index f0c8b95..a3e47af 100644 --- a/include/scene.h +++ b/include/scene.h @@ -9,7 +9,7 @@ #include -#include "ecs_system.h" +#include "ecs.h" #include "event_system.h" namespace engine { @@ -31,10 +31,10 @@ class Scene { /* ecs stuff */ - uint32_t CreateEntity(const std::string& tag, uint32_t parent = 0, + Entity CreateEntity(const std::string& tag, Entity parent = 0, const glm::vec3& pos = glm::vec3{0.0f, 0.0f, 0.0f}); - uint32_t GetEntity(const std::string& tag, uint32_t parent = 0); + Entity GetEntity(const std::string& tag, Entity parent = 0); size_t GetComponentSignaturePosition(size_t hash); @@ -54,13 +54,13 @@ class Scene { } template - T* GetComponent(uint32_t entity) { + T* GetComponent(Entity entity) { auto array = GetComponentArray(); return array->GetData(entity); } template - T* AddComponent(uint32_t entity) { + T* AddComponent(Entity entity) { size_t hash = typeid(T).hash_code(); auto array = GetComponentArray(); @@ -107,7 +107,7 @@ class Scene { private: Application* const app_; - uint32_t next_entity_id_ = 1; // 0 is not a valid entity + Entity next_entity_id_ = 1; // 0 is not a valid entity uint64_t framecount_ = 0; @@ -117,7 +117,7 @@ class Scene { // maps component hashes to signature positions std::unordered_map component_signature_positions_{}; // maps entity ids to their signatures - std::unordered_map> signatures_{}; + std::unordered_map> signatures_{}; // maps component hashes to their arrays std::unordered_map> component_arrays_{}; diff --git a/include/systems/collisions.h b/include/systems/collisions.h index 74c0c5d..44cb06b 100644 --- a/include/systems/collisions.h +++ b/include/systems/collisions.h @@ -7,7 +7,7 @@ #include #include "components/collider.h" -#include "ecs_system.h" +#include "ecs.h" namespace engine { @@ -17,11 +17,11 @@ class PhysicsSystem : public System { void OnUpdate(float ts) override; - void OnComponentInsert(uint32_t entity) override; + void OnComponentInsert(Entity entity) override; struct CollisionEvent { bool is_collision_enter; // false == collision exit - uint32_t collided_entity; // the entity that this entity collided with + Entity collided_entity; // the entity that this entity collided with glm::vec3 normal; // the normal of the surface this entity collided with; // ignored on collision exit glm::vec3 point; // where the collision was detected @@ -30,12 +30,12 @@ class PhysicsSystem : public System { private: // dynamic arrays to avoid realloc on every frame // entity, aabb, is_trigger - std::vector> static_aabbs_{}; - std::vector> dynamic_aabbs_{}; + std::vector> static_aabbs_{}; + std::vector> dynamic_aabbs_{}; struct PossibleCollision { - PossibleCollision(uint32_t static_entity, AABB static_aabb, - bool static_trigger, uint32_t dynamic_entity, + PossibleCollision(Entity static_entity, AABB static_aabb, + bool static_trigger, Entity dynamic_entity, AABB dynamic_aabb, bool dynamic_trigger) : static_entity(static_entity), static_aabb(static_aabb), @@ -44,15 +44,15 @@ class PhysicsSystem : public System { dynamic_aabb(dynamic_aabb), dynamic_trigger(dynamic_trigger) {} - uint32_t static_entity; + Entity static_entity; AABB static_aabb; bool static_trigger; - uint32_t dynamic_entity; + Entity dynamic_entity; AABB dynamic_aabb; bool dynamic_trigger; }; std::vector possible_collisions_{}; - std::vector> + std::vector> collision_infos_{}; // target entity, event info }; diff --git a/include/systems/custom_behaviour.h b/include/systems/custom_behaviour.h index a574f79..14bc0e7 100644 --- a/include/systems/custom_behaviour.h +++ b/include/systems/custom_behaviour.h @@ -3,7 +3,7 @@ #include -#include "ecs_system.h" +#include "ecs.h" /* This system allows for one-off custom components that execute arbitrary code * It is similar to Unity's 'MonoBehavior' system */ @@ -16,10 +16,10 @@ class CustomBehaviourSystem : public System { ~CustomBehaviourSystem(); void OnUpdate(float ts) override; - void OnComponentInsert(uint32_t entity) override; + void OnComponentInsert(Entity entity) override; private: - std::unordered_map entity_is_initialised_{}; + std::unordered_map entity_is_initialised_{}; }; } // namespace engine diff --git a/include/systems/mesh_render_system.h b/include/systems/mesh_render_system.h index b644af9..925c812 100644 --- a/include/systems/mesh_render_system.h +++ b/include/systems/mesh_render_system.h @@ -3,7 +3,7 @@ #include -#include "ecs_system.h" +#include "ecs.h" #include "scene.h" #include "gfx.h" @@ -29,7 +29,7 @@ class MeshRenderSystem : public System { const RenderList* GetStaticRenderList() const { return &static_render_list_; } const RenderList* GetDynamicRenderList() const { return &dynamic_render_list_; } - void OnComponentInsert(uint32_t entity) override; + void OnComponentInsert(Entity entity) override; void OnUpdate(float ts) override; private: diff --git a/include/systems/transform.h b/include/systems/transform.h index c7baa88..da8ebf6 100644 --- a/include/systems/transform.h +++ b/include/systems/transform.h @@ -1,7 +1,7 @@ #ifndef ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_ #define ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_ -#include "ecs_system.h" +#include "ecs.h" namespace engine { @@ -12,7 +12,7 @@ namespace engine { void OnUpdate(float ts) override; - uint32_t GetChildEntity(uint32_t parent, const std::string& tag); + Entity GetChildEntity(Entity parent, const std::string& tag); }; diff --git a/include/systems/ui_render_system.h b/include/systems/ui_render_system.h index feaee3c..06936d6 100644 --- a/include/systems/ui_render_system.h +++ b/include/systems/ui_render_system.h @@ -2,7 +2,7 @@ #include -#include "ecs_system.h" +#include "ecs.h" #include "scene.h" #include "gfx.h" @@ -13,7 +13,7 @@ namespace engine { UIRenderSystem(Scene* scene); ~UIRenderSystem(); - void OnComponentInsert(uint32_t entity) override; + void OnComponentInsert(Entity entity) override; void OnUpdate(float ts) override; private: diff --git a/include/util/model_loader.h b/include/util/model_loader.h index 7728129..8acb905 100644 --- a/include/util/model_loader.h +++ b/include/util/model_loader.h @@ -8,7 +8,7 @@ namespace engine { namespace util { -uint32_t LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static = false); +Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static = false); } // namespace util } // namespace engine diff --git a/src/ecs_system.cpp b/src/ecs.cpp similarity index 93% rename from src/ecs_system.cpp rename to src/ecs.cpp index 93504c5..c31e3c6 100644 --- a/src/ecs_system.cpp +++ b/src/ecs.cpp @@ -1,4 +1,4 @@ -#include "ecs_system.h" +#include "ecs.h" #include "scene.h" diff --git a/src/scene.cpp b/src/scene.cpp index fa28d68..adfa74d 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -35,9 +35,9 @@ Scene::Scene(Application* app) : app_(app) { Scene::~Scene() {} -uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent, +Entity Scene::CreateEntity(const std::string& tag, Entity parent, const glm::vec3& pos) { - uint32_t id = next_entity_id_++; + Entity id = next_entity_id_++; signatures_.emplace(id, std::bitset{}); @@ -53,7 +53,7 @@ uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent, return id; } -uint32_t Scene::GetEntity(const std::string& tag, uint32_t parent) { +Entity Scene::GetEntity(const std::string& tag, Entity parent) { return GetSystem()->GetChildEntity(parent, tag); } diff --git a/src/systems/collisions.cpp b/src/systems/collisions.cpp index 0ba5e41..cfe4c5c 100644 --- a/src/systems/collisions.cpp +++ b/src/systems/collisions.cpp @@ -71,7 +71,7 @@ namespace engine { scene_->event_system()->RegisterEventType(); } - void PhysicsSystem::OnComponentInsert(uint32_t entity) + void PhysicsSystem::OnComponentInsert(Entity entity) { (void)entity; const size_t size = entities_.size(); @@ -91,7 +91,7 @@ namespace engine { possible_collisions_.clear(); collision_infos_.clear(); - for (uint32_t entity : entities_) { + for (Entity entity : entities_) { const auto t = scene_->GetComponent(entity); const auto c = scene_->GetComponent(entity); diff --git a/src/systems/custom_behaviour.cpp b/src/systems/custom_behaviour.cpp index 06a5f5b..879e60f 100644 --- a/src/systems/custom_behaviour.cpp +++ b/src/systems/custom_behaviour.cpp @@ -17,7 +17,7 @@ CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene) CustomBehaviourSystem::~CustomBehaviourSystem() {} void CustomBehaviourSystem::OnUpdate(float ts) { - for (uint32_t entity : entities_) { + for (Entity entity : entities_) { auto c = scene_->GetComponent(entity); assert(c != nullptr); bool& entity_initialised = entity_is_initialised_.at(entity); @@ -31,7 +31,7 @@ void CustomBehaviourSystem::OnUpdate(float ts) { } } -void CustomBehaviourSystem::OnComponentInsert(uint32_t entity) +void CustomBehaviourSystem::OnComponentInsert(Entity entity) { entity_is_initialised_.emplace(entity, false); } diff --git a/src/systems/mesh_render_system.cpp b/src/systems/mesh_render_system.cpp index 0ab722e..965ec05 100644 --- a/src/systems/mesh_render_system.cpp +++ b/src/systems/mesh_render_system.cpp @@ -19,7 +19,7 @@ void MeshRenderSystem::RebuildStaticRenderList() { list_needs_rebuild_ = false; } -void MeshRenderSystem::OnComponentInsert(uint32_t entity) { +void MeshRenderSystem::OnComponentInsert(Entity entity) { (void)entity; list_needs_rebuild_ = true; } @@ -42,7 +42,7 @@ void MeshRenderSystem::BuildRenderList(RenderList& render_list, std::unordered_map render_orders; - for (uint32_t entity : entities_) { + for (Entity entity : entities_) { auto transform = scene_->GetComponent(entity); if (transform->is_static != with_static_entities) continue; diff --git a/src/systems/transform.cpp b/src/systems/transform.cpp index 04270cd..29e5ea5 100644 --- a/src/systems/transform.cpp +++ b/src/systems/transform.cpp @@ -13,7 +13,7 @@ TransformSystem::TransformSystem(Scene* scene) void TransformSystem::OnUpdate(float ts) { (void)ts; - for (uint32_t entity : entities_) { + for (Entity entity : entities_) { auto t = scene_->GetComponent(entity); glm::mat4 transform; @@ -37,9 +37,9 @@ void TransformSystem::OnUpdate(float ts) { } } -uint32_t TransformSystem::GetChildEntity(uint32_t parent, +Entity TransformSystem::GetChildEntity(Entity parent, const std::string& tag) { - for (uint32_t entity : entities_) { + for (Entity entity : entities_) { auto t = scene_->GetComponent(entity); if (t->parent == parent) { if (t->tag == tag) { diff --git a/src/systems/ui_render_system.cpp b/src/systems/ui_render_system.cpp index a980d0c..c94149a 100644 --- a/src/systems/ui_render_system.cpp +++ b/src/systems/ui_render_system.cpp @@ -11,7 +11,7 @@ namespace engine { UIRenderSystem::~UIRenderSystem() {} - void UIRenderSystem::OnComponentInsert(uint32_t entity) { + void UIRenderSystem::OnComponentInsert(Entity entity) { (void)entity; } diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp index c4d385b..b9f6430 100644 --- a/src/util/model_loader.cpp +++ b/src/util/model_loader.cpp @@ -31,7 +31,7 @@ namespace engine::util { const std::map>& textures, const std::vector>& meshes, const std::vector& meshTextureIndices, - aiNode* parentNode, Scene* scene, uint32_t parentObj, bool is_static) + aiNode* parentNode, Scene* scene, Entity parentObj, bool is_static) { // convert to glm column major @@ -99,7 +99,7 @@ namespace engine::util { } } - uint32_t LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static) + Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static) { Assimp::Importer importer; @@ -231,7 +231,7 @@ namespace engine::util { indices)); } - uint32_t obj = parent->CreateEntity(scene->GetShortFilename(path.c_str())); + Entity obj = parent->CreateEntity(scene->GetShortFilename(path.c_str())); buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj, is_static); diff --git a/test/src/camera_controller.cpp b/test/src/camera_controller.cpp index 6f1e3fe..75d7df1 100644 --- a/test/src/camera_controller.cpp +++ b/test/src/camera_controller.cpp @@ -7,6 +7,7 @@ #include "application.h" #include "components/transform.h" +#include "ecs.h" #include "input_manager.h" #include "log.h" #include "scene.h" @@ -19,7 +20,7 @@ CameraControllerSystem::CameraControllerSystem(engine::Scene* scene) void CameraControllerSystem::OnUpdate(float ts) { if (t == nullptr || c == nullptr) { - for (uint32_t entity : entities_) { + for (engine::Entity entity : entities_) { t = scene_->GetComponent(entity); c = scene_->GetComponent(entity); break; diff --git a/test/src/camera_controller.hpp b/test/src/camera_controller.hpp index 53b764f..854b208 100644 --- a/test/src/camera_controller.hpp +++ b/test/src/camera_controller.hpp @@ -4,7 +4,7 @@ #include #include "components/transform.h" -#include "ecs_system.h" +#include "ecs.h" struct CameraControllerComponent { static constexpr float kWalkSpeed = 4.0f; diff --git a/test/src/game.cpp b/test/src/game.cpp index 1fd737b..7d71a7b 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -86,7 +86,7 @@ void PlayGame(GameSettings settings) { /* skybox */ { - uint32_t skybox = my_scene->CreateEntity("skybox"); + engine::Entity skybox = my_scene->CreateEntity("skybox"); auto skybox_renderable = my_scene->AddComponent(skybox); @@ -105,7 +105,7 @@ void PlayGame(GameSettings settings) { /* floor */ { - uint32_t floor = engine::util::LoadMeshFromFile( + engine::Entity floor = engine::util::LoadMeshFromFile( my_scene, app.GetResourcePath("models/terrain.dae"), true); auto floor_transform = @@ -134,7 +134,7 @@ void PlayGame(GameSettings settings) { /* some text */ { - uint32_t textbox = + engine::Entity textbox = my_scene->CreateEntity("textbox", 0, glm::vec3{0.0f, 0.8f, 0.0f}); auto textboxComponent = my_scene->AddComponent(textbox); @@ -153,7 +153,11 @@ void PlayGame(GameSettings settings) { } /* teapot */ - my_scene->GetComponent(engine::util::LoadMeshFromFile(my_scene, app.GetResourcePath("models/teapot.dae"), true))->position += glm::vec3{10.0f, 10.0f, 10.0f}; + my_scene + ->GetComponent( + engine::util::LoadMeshFromFile( + my_scene, app.GetResourcePath("models/teapot.dae"), true)) + ->position += glm::vec3{10.0f, 10.0f, 10.0f}; app.GameLoop(); }