Use 'Entity' type instead of uint32_t

This commit is contained in:
Bailey Harrison 2023-09-19 08:40:45 +01:00
parent 3f013b2860
commit 604513f44d
21 changed files with 77 additions and 62 deletions

View File

@ -12,13 +12,19 @@ project(engine LANGUAGES CXX
# from command: find . -regex "^\.\/.*" | sort # from command: find . -regex "^\.\/.*" | sort
set(SRC_FILES set(SRC_FILES
"src/application.cpp" "src/application.cpp"
"src/ecs_system.cpp" "src/ecs.cpp"
"src/gfx_device_vulkan.cpp" "src/gfx_device_vulkan.cpp"
"src/imgui/imconfig.h"
"src/imgui/imgui.cpp" "src/imgui/imgui.cpp"
"src/imgui/imgui.h"
"src/imgui/imgui_demo.cpp" "src/imgui/imgui_demo.cpp"
"src/imgui/imgui_draw.cpp" "src/imgui/imgui_draw.cpp"
"src/imgui/imgui_internal.h"
"src/imgui/imgui_tables.cpp" "src/imgui/imgui_tables.cpp"
"src/imgui/imgui_widgets.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/input_manager.cpp"
"src/libs/stb_image.cpp" "src/libs/stb_image.cpp"
"src/libs/stb_truetype.cpp" "src/libs/stb_truetype.cpp"
@ -55,7 +61,7 @@ set(INCLUDE_FILES
"include/components/mesh_renderable.h" "include/components/mesh_renderable.h"
"include/components/ui_renderable.h" "include/components/ui_renderable.h"
"include/components/transform.h" "include/components/transform.h"
"include/ecs_system.h" "include/ecs.h"
"include/engine_api.h" "include/engine_api.h"
"include/event_system.h" "include/event_system.h"
"include/gfx.h" "include/gfx.h"

View File

@ -7,6 +7,8 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "ecs.h"
namespace engine { namespace engine {
struct TransformComponent { struct TransformComponent {
@ -17,7 +19,7 @@ struct TransformComponent {
glm::vec3 scale; glm::vec3 scale;
std::string tag; std::string tag;
uint32_t parent; Entity parent;
bool is_static; bool is_static;
}; };

View File

@ -1,5 +1,5 @@
#ifndef ENGINE_INCLUDE_ECS_SYSTEM_H_ #ifndef ENGINE_INCLUDE_ECS_H_
#define ENGINE_INCLUDE_ECS_SYSTEM_H_ #define ENGINE_INCLUDE_ECS_H_
#include <bitset> #include <bitset>
#include <cassert> #include <cassert>
@ -13,7 +13,9 @@ namespace engine {
class Scene; class Scene;
constexpr size_t kMaxComponents = 64; using Entity = uint32_t; // ECS entity
constexpr size_t kMaxComponents = 10;
class IComponentArray { class IComponentArray {
public: public:
@ -23,7 +25,7 @@ class IComponentArray {
template <typename T> template <typename T>
class ComponentArray : public IComponentArray { class ComponentArray : public IComponentArray {
public: public:
void InsertData(uint32_t entity, T component) { void InsertData(Entity entity, T component) {
if (component_array_.size() < entity + 1) { if (component_array_.size() < entity + 1) {
component_array_.resize(entity + 1); component_array_.resize(entity + 1);
} }
@ -31,11 +33,11 @@ class ComponentArray : public IComponentArray {
component_array_.at(entity) = component; component_array_.at(entity) = component;
} }
void DeleteData(uint32_t entity) { void DeleteData(Entity entity) {
(void)entity; // TODO (void)entity; // TODO
} }
T* GetData(uint32_t entity) { T* GetData(Entity entity) {
assert(entity < component_array_.size()); assert(entity < component_array_.size());
return &component_array_[entity]; return &component_array_[entity];
} }
@ -53,15 +55,15 @@ class System {
virtual void OnUpdate(float ts) = 0; virtual void OnUpdate(float ts) = 0;
virtual void OnComponentInsert(uint32_t) {} virtual void OnComponentInsert(Entity) {}
virtual void OnComponentRemove(uint32_t) {} virtual void OnComponentRemove(Entity) {}
Scene* const scene_; Scene* const scene_;
std::bitset<kMaxComponents> signature_; std::bitset<kMaxComponents> signature_;
// entities that contain the needed components // entities that contain the needed components
std::set<uint32_t> entities_{}; std::set<Entity> entities_{};
}; };
} // namespace engine } // namespace engine

View File

@ -9,7 +9,7 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "ecs_system.h" #include "ecs.h"
#include "event_system.h" #include "event_system.h"
namespace engine { namespace engine {
@ -31,10 +31,10 @@ class Scene {
/* ecs stuff */ /* 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}); 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); size_t GetComponentSignaturePosition(size_t hash);
@ -54,13 +54,13 @@ class Scene {
} }
template <typename T> template <typename T>
T* GetComponent(uint32_t entity) { T* GetComponent(Entity entity) {
auto array = GetComponentArray<T>(); auto array = GetComponentArray<T>();
return array->GetData(entity); return array->GetData(entity);
} }
template <typename T> template <typename T>
T* AddComponent(uint32_t entity) { T* AddComponent(Entity entity) {
size_t hash = typeid(T).hash_code(); size_t hash = typeid(T).hash_code();
auto array = GetComponentArray<T>(); auto array = GetComponentArray<T>();
@ -107,7 +107,7 @@ class Scene {
private: private:
Application* const app_; 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; uint64_t framecount_ = 0;
@ -117,7 +117,7 @@ class Scene {
// maps component hashes to signature positions // maps component hashes to signature positions
std::unordered_map<size_t, size_t> component_signature_positions_{}; std::unordered_map<size_t, size_t> component_signature_positions_{};
// maps entity ids to their signatures // maps entity ids to their signatures
std::unordered_map<uint32_t, std::bitset<kMaxComponents>> signatures_{}; std::unordered_map<Entity, std::bitset<kMaxComponents>> signatures_{};
// maps component hashes to their arrays // maps component hashes to their arrays
std::unordered_map<size_t, std::unique_ptr<IComponentArray>> std::unordered_map<size_t, std::unique_ptr<IComponentArray>>
component_arrays_{}; component_arrays_{};

View File

@ -7,7 +7,7 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "components/collider.h" #include "components/collider.h"
#include "ecs_system.h" #include "ecs.h"
namespace engine { namespace engine {
@ -17,11 +17,11 @@ class PhysicsSystem : public System {
void OnUpdate(float ts) override; void OnUpdate(float ts) override;
void OnComponentInsert(uint32_t entity) override; void OnComponentInsert(Entity entity) override;
struct CollisionEvent { struct CollisionEvent {
bool is_collision_enter; // false == collision exit 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; glm::vec3 normal; // the normal of the surface this entity collided with;
// ignored on collision exit // ignored on collision exit
glm::vec3 point; // where the collision was detected glm::vec3 point; // where the collision was detected
@ -30,12 +30,12 @@ class PhysicsSystem : public System {
private: private:
// dynamic arrays to avoid realloc on every frame // dynamic arrays to avoid realloc on every frame
// entity, aabb, is_trigger // entity, aabb, is_trigger
std::vector<std::tuple<uint32_t, AABB, bool>> static_aabbs_{}; std::vector<std::tuple<Entity, AABB, bool>> static_aabbs_{};
std::vector<std::tuple<uint32_t, AABB, bool>> dynamic_aabbs_{}; std::vector<std::tuple<Entity, AABB, bool>> dynamic_aabbs_{};
struct PossibleCollision { struct PossibleCollision {
PossibleCollision(uint32_t static_entity, AABB static_aabb, PossibleCollision(Entity static_entity, AABB static_aabb,
bool static_trigger, uint32_t dynamic_entity, bool static_trigger, Entity dynamic_entity,
AABB dynamic_aabb, bool dynamic_trigger) AABB dynamic_aabb, bool dynamic_trigger)
: static_entity(static_entity), : static_entity(static_entity),
static_aabb(static_aabb), static_aabb(static_aabb),
@ -44,15 +44,15 @@ class PhysicsSystem : public System {
dynamic_aabb(dynamic_aabb), dynamic_aabb(dynamic_aabb),
dynamic_trigger(dynamic_trigger) {} dynamic_trigger(dynamic_trigger) {}
uint32_t static_entity; Entity static_entity;
AABB static_aabb; AABB static_aabb;
bool static_trigger; bool static_trigger;
uint32_t dynamic_entity; Entity dynamic_entity;
AABB dynamic_aabb; AABB dynamic_aabb;
bool dynamic_trigger; bool dynamic_trigger;
}; };
std::vector<PossibleCollision> possible_collisions_{}; std::vector<PossibleCollision> possible_collisions_{};
std::vector<std::pair<uint32_t, CollisionEvent>> std::vector<std::pair<Entity, CollisionEvent>>
collision_infos_{}; // target entity, event info collision_infos_{}; // target entity, event info
}; };

View File

@ -3,7 +3,7 @@
#include <unordered_map> #include <unordered_map>
#include "ecs_system.h" #include "ecs.h"
/* This system allows for one-off custom components that execute arbitrary code /* This system allows for one-off custom components that execute arbitrary code
* It is similar to Unity's 'MonoBehavior' system */ * It is similar to Unity's 'MonoBehavior' system */
@ -16,10 +16,10 @@ class CustomBehaviourSystem : public System {
~CustomBehaviourSystem(); ~CustomBehaviourSystem();
void OnUpdate(float ts) override; void OnUpdate(float ts) override;
void OnComponentInsert(uint32_t entity) override; void OnComponentInsert(Entity entity) override;
private: private:
std::unordered_map<uint32_t, bool> entity_is_initialised_{}; std::unordered_map<Entity, bool> entity_is_initialised_{};
}; };
} // namespace engine } // namespace engine

View File

@ -3,7 +3,7 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "ecs_system.h" #include "ecs.h"
#include "scene.h" #include "scene.h"
#include "gfx.h" #include "gfx.h"
@ -29,7 +29,7 @@ class MeshRenderSystem : public System {
const RenderList* GetStaticRenderList() const { return &static_render_list_; } const RenderList* GetStaticRenderList() const { return &static_render_list_; }
const RenderList* GetDynamicRenderList() const { return &dynamic_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; void OnUpdate(float ts) override;
private: private:

View File

@ -1,7 +1,7 @@
#ifndef ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_ #ifndef ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_
#define ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_ #define ENGINE_INCLUDE_SYSTEMS_TRANSFORM_H_
#include "ecs_system.h" #include "ecs.h"
namespace engine { namespace engine {
@ -12,7 +12,7 @@ namespace engine {
void OnUpdate(float ts) override; void OnUpdate(float ts) override;
uint32_t GetChildEntity(uint32_t parent, const std::string& tag); Entity GetChildEntity(Entity parent, const std::string& tag);
}; };

View File

@ -2,7 +2,7 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "ecs_system.h" #include "ecs.h"
#include "scene.h" #include "scene.h"
#include "gfx.h" #include "gfx.h"
@ -13,7 +13,7 @@ namespace engine {
UIRenderSystem(Scene* scene); UIRenderSystem(Scene* scene);
~UIRenderSystem(); ~UIRenderSystem();
void OnComponentInsert(uint32_t entity) override; void OnComponentInsert(Entity entity) override;
void OnUpdate(float ts) override; void OnUpdate(float ts) override;
private: private:

View File

@ -8,7 +8,7 @@
namespace engine { namespace engine {
namespace util { 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 util
} // namespace engine } // namespace engine

View File

@ -1,4 +1,4 @@
#include "ecs_system.h" #include "ecs.h"
#include "scene.h" #include "scene.h"

View File

@ -35,9 +35,9 @@ Scene::Scene(Application* app) : app_(app) {
Scene::~Scene() {} 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) { const glm::vec3& pos) {
uint32_t id = next_entity_id_++; Entity id = next_entity_id_++;
signatures_.emplace(id, std::bitset<kMaxComponents>{}); signatures_.emplace(id, std::bitset<kMaxComponents>{});
@ -53,7 +53,7 @@ uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent,
return id; return id;
} }
uint32_t Scene::GetEntity(const std::string& tag, uint32_t parent) { Entity Scene::GetEntity(const std::string& tag, Entity parent) {
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag); return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
} }

View File

@ -71,7 +71,7 @@ namespace engine {
scene_->event_system()->RegisterEventType<CollisionEvent>(); scene_->event_system()->RegisterEventType<CollisionEvent>();
} }
void PhysicsSystem::OnComponentInsert(uint32_t entity) void PhysicsSystem::OnComponentInsert(Entity entity)
{ {
(void)entity; (void)entity;
const size_t size = entities_.size(); const size_t size = entities_.size();
@ -91,7 +91,7 @@ namespace engine {
possible_collisions_.clear(); possible_collisions_.clear();
collision_infos_.clear(); collision_infos_.clear();
for (uint32_t entity : entities_) { for (Entity entity : entities_) {
const auto t = scene_->GetComponent<TransformComponent>(entity); const auto t = scene_->GetComponent<TransformComponent>(entity);
const auto c = scene_->GetComponent<ColliderComponent>(entity); const auto c = scene_->GetComponent<ColliderComponent>(entity);

View File

@ -17,7 +17,7 @@ CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene)
CustomBehaviourSystem::~CustomBehaviourSystem() {} CustomBehaviourSystem::~CustomBehaviourSystem() {}
void CustomBehaviourSystem::OnUpdate(float ts) { void CustomBehaviourSystem::OnUpdate(float ts) {
for (uint32_t entity : entities_) { for (Entity entity : entities_) {
auto c = scene_->GetComponent<CustomComponent>(entity); auto c = scene_->GetComponent<CustomComponent>(entity);
assert(c != nullptr); assert(c != nullptr);
bool& entity_initialised = entity_is_initialised_.at(entity); 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); entity_is_initialised_.emplace(entity, false);
} }

View File

@ -19,7 +19,7 @@ void MeshRenderSystem::RebuildStaticRenderList() {
list_needs_rebuild_ = false; list_needs_rebuild_ = false;
} }
void MeshRenderSystem::OnComponentInsert(uint32_t entity) { void MeshRenderSystem::OnComponentInsert(Entity entity) {
(void)entity; (void)entity;
list_needs_rebuild_ = true; list_needs_rebuild_ = true;
} }
@ -42,7 +42,7 @@ void MeshRenderSystem::BuildRenderList(RenderList& render_list,
std::unordered_map<const gfx::Pipeline*, int> render_orders; std::unordered_map<const gfx::Pipeline*, int> render_orders;
for (uint32_t entity : entities_) { for (Entity entity : entities_) {
auto transform = scene_->GetComponent<engine::TransformComponent>(entity); auto transform = scene_->GetComponent<engine::TransformComponent>(entity);
if (transform->is_static != with_static_entities) continue; if (transform->is_static != with_static_entities) continue;

View File

@ -13,7 +13,7 @@ TransformSystem::TransformSystem(Scene* scene)
void TransformSystem::OnUpdate(float ts) { void TransformSystem::OnUpdate(float ts) {
(void)ts; (void)ts;
for (uint32_t entity : entities_) { for (Entity entity : entities_) {
auto t = scene_->GetComponent<TransformComponent>(entity); auto t = scene_->GetComponent<TransformComponent>(entity);
glm::mat4 transform; 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) { const std::string& tag) {
for (uint32_t entity : entities_) { for (Entity entity : entities_) {
auto t = scene_->GetComponent<TransformComponent>(entity); auto t = scene_->GetComponent<TransformComponent>(entity);
if (t->parent == parent) { if (t->parent == parent) {
if (t->tag == tag) { if (t->tag == tag) {

View File

@ -11,7 +11,7 @@ namespace engine {
UIRenderSystem::~UIRenderSystem() {} UIRenderSystem::~UIRenderSystem() {}
void UIRenderSystem::OnComponentInsert(uint32_t entity) { void UIRenderSystem::OnComponentInsert(Entity entity) {
(void)entity; (void)entity;
} }

View File

@ -31,7 +31,7 @@ namespace engine::util {
const std::map<int, std::shared_ptr<resources::Texture>>& textures, const std::map<int, std::shared_ptr<resources::Texture>>& textures,
const std::vector<std::shared_ptr<resources::Mesh>>& meshes, const std::vector<std::shared_ptr<resources::Mesh>>& meshes,
const std::vector<unsigned int>& meshTextureIndices, const std::vector<unsigned int>& 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 // 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; Assimp::Importer importer;
@ -231,7 +231,7 @@ namespace engine::util {
indices)); 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); buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj, is_static);

View File

@ -7,6 +7,7 @@
#include "application.h" #include "application.h"
#include "components/transform.h" #include "components/transform.h"
#include "ecs.h"
#include "input_manager.h" #include "input_manager.h"
#include "log.h" #include "log.h"
#include "scene.h" #include "scene.h"
@ -19,7 +20,7 @@ CameraControllerSystem::CameraControllerSystem(engine::Scene* scene)
void CameraControllerSystem::OnUpdate(float ts) { void CameraControllerSystem::OnUpdate(float ts) {
if (t == nullptr || c == nullptr) { if (t == nullptr || c == nullptr) {
for (uint32_t entity : entities_) { for (engine::Entity entity : entities_) {
t = scene_->GetComponent<engine::TransformComponent>(entity); t = scene_->GetComponent<engine::TransformComponent>(entity);
c = scene_->GetComponent<CameraControllerComponent>(entity); c = scene_->GetComponent<CameraControllerComponent>(entity);
break; break;

View File

@ -4,7 +4,7 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "components/transform.h" #include "components/transform.h"
#include "ecs_system.h" #include "ecs.h"
struct CameraControllerComponent { struct CameraControllerComponent {
static constexpr float kWalkSpeed = 4.0f; static constexpr float kWalkSpeed = 4.0f;

View File

@ -86,7 +86,7 @@ void PlayGame(GameSettings settings) {
/* skybox */ /* skybox */
{ {
uint32_t skybox = my_scene->CreateEntity("skybox"); engine::Entity skybox = my_scene->CreateEntity("skybox");
auto skybox_renderable = auto skybox_renderable =
my_scene->AddComponent<engine::MeshRenderableComponent>(skybox); my_scene->AddComponent<engine::MeshRenderableComponent>(skybox);
@ -105,7 +105,7 @@ void PlayGame(GameSettings settings) {
/* floor */ /* floor */
{ {
uint32_t floor = engine::util::LoadMeshFromFile( engine::Entity floor = engine::util::LoadMeshFromFile(
my_scene, app.GetResourcePath("models/terrain.dae"), true); my_scene, app.GetResourcePath("models/terrain.dae"), true);
auto floor_transform = auto floor_transform =
@ -134,7 +134,7 @@ void PlayGame(GameSettings settings) {
/* some text */ /* some text */
{ {
uint32_t textbox = engine::Entity textbox =
my_scene->CreateEntity("textbox", 0, glm::vec3{0.0f, 0.8f, 0.0f}); my_scene->CreateEntity("textbox", 0, glm::vec3{0.0f, 0.8f, 0.0f});
auto textboxComponent = auto textboxComponent =
my_scene->AddComponent<engine::CustomComponent>(textbox); my_scene->AddComponent<engine::CustomComponent>(textbox);
@ -153,7 +153,11 @@ void PlayGame(GameSettings settings) {
} }
/* teapot */ /* teapot */
my_scene->GetComponent<engine::TransformComponent>(engine::util::LoadMeshFromFile(my_scene, app.GetResourcePath("models/teapot.dae"), true))->position += glm::vec3{10.0f, 10.0f, 10.0f}; my_scene
->GetComponent<engine::TransformComponent>(
engine::util::LoadMeshFromFile(
my_scene, app.GetResourcePath("models/teapot.dae"), true))
->position += glm::vec3{10.0f, 10.0f, 10.0f};
app.GameLoop(); app.GameLoop();
} }