diff --git a/README.md b/README.md index ba9fde6..0bb0acb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -I'm messing around with GLFW to build an OpenGL app in wayland, theoretically this is the starting point for a game engine. +# Wayland Application ### Setup diff --git a/libs/ecs/component.hpp b/libs/ecs/component.hpp deleted file mode 100644 index 323866e..0000000 --- a/libs/ecs/component.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef COMPONENT_HPP -#define COMPONENT_HPP - -#include -#include - -using ComponentType = std::uint8_t; - -const ComponentType MAX_COMPONENTS = 32; - -using Signature = std::bitset; - -#endif // COMPONENT_HPP diff --git a/libs/ecs/componentmanager.cpp b/libs/ecs/componentmanager.cpp deleted file mode 100644 index 352c01f..0000000 --- a/libs/ecs/componentmanager.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "componentmanager.hpp" - -#include -#include - -/** - * ComponentArray - **/ - -template -void ComponentArray::InsertData(Entity entity, T component) { - assert(mEntityToIndexMap.find(entity) == mEntityToIndexMap.end() && - "Component added to same entity more than once."); - - // Put new entry at end and update the maps - size_t newIndex = mSize; - mEntityToIndexMap[entity] = newIndex; - mIndexToEntityMap[newIndex] = entity; - mComponentArray[newIndex] = component; - ++mSize; -}; -template void ComponentArray::RemoveData(Entity entity) { - assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && - "Removing non-existent component."); - - // Copy element at end into deleted element's place to maintain density - size_t indexOfRemovedEntity = mEntityToIndexMap[entity]; - size_t indexOfLastElement = mSize - 1; - mComponentArray[indexOfRemovedEntity] = mComponentArray[indexOfLastElement]; - - // Update map to point to moved spot - Entity entityOfLastElement = mIndexToEntityMap[indexOfLastElement]; - mEntityToIndexMap[entityOfLastElement] = indexOfRemovedEntity; - mIndexToEntityMap[indexOfRemovedEntity] = entityOfLastElement; - - mEntityToIndexMap.erase(entity); - mIndexToEntityMap.erase(indexOfLastElement); - - --mSize; -}; -template T &ComponentArray::GetData(Entity entity) { - assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && - "Retrieving non-existent component."); - - // Return a reference to the entity's component - return mComponentArray[mEntityToIndexMap[entity]]; -}; -template void ComponentArray::EntityDestroyed(Entity entity) { - if (mEntityToIndexMap.find(entity) != mEntityToIndexMap.end()) { - // Remove the entity's component if it existed - RemoveData(entity); - } -}; - -/** - * ComponentManager - **/ - -// Private functions -template -std::shared_ptr> ComponentManager::GetComponentArray() { - const char *typeName = typeid(T).name(); - - assert(mComponentTypes.find(typeName) != mComponentTypes.end() && - "Component not registered before use."); - - return std::static_pointer_cast>( - mComponentArrays[typeName]); -} - -// Public functions -template void ComponentManager::RegisterComponent() { - const char *typeName = typeid(T).name(); - - assert(mComponentTypes.find(typeName) == mComponentTypes.end() && - "Registering component type more than once."); - - // Add this component type to the component type map - mComponentTypes.insert({typeName, mNextComponentType}); - - // Create a ComponentArray pointer and add it to the component arrays map - mComponentArrays.insert({typeName, std::make_shared>()}); - - // Increment the value so that the next component registered will be different - ++mNextComponentType; -}; -template ComponentType ComponentManager::GetComponentType() { - const char *typeName = typeid(T).name(); - - assert(mComponentTypes.find(typeName) != mComponentTypes.end() && - "Component not registered before use."); - - // Return this component's type - used for creating signatures - return mComponentTypes[typeName]; -} -template -void ComponentManager::AddComponent(Entity entity, T component) { - // Add a component to the array for an entity - GetComponentArray()->InsertData(entity, component); -} -template void ComponentManager::RemoveComponent(Entity entity) { - // Remove a component from the array for an entity - GetComponentArray()->RemoveData(entity); -} -template T &ComponentManager::GetComponent(Entity entity) { - // Get a reference to a component from the array for an entity - return GetComponentArray()->GetData(entity); -} -void ComponentManager::EntityDestroyed(Entity entity) { - // Notify each component array that an entity has been destroyed - // If it has a component for that entity, it will remove it - for (auto const &pair : mComponentArrays) { - auto const &component = pair.second; - - component->EntityDestroyed(entity); - } -}; diff --git a/libs/ecs/componentmanager.hpp b/libs/ecs/componentmanager.hpp deleted file mode 100644 index a11f17e..0000000 --- a/libs/ecs/componentmanager.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef COMPONENTMANAGER_HPP -#define COMPONENTMANAGER_HPP - -#include -#include -#include -#include "entity.hpp" - -class IComponentArray { -public: - virtual ~IComponentArray() = default; - virtual void EntityDestroyed(Entity entity) = 0; -}; - -template class ComponentArray : public IComponentArray { - // The packed array of components (of generic type T), - // set to a specified maximum amount, matching the maximum number - // of entities allowed to exist simultaneously, so that each entity - // has a unique spot. - std::array mComponentArray; - - // Map from an entity ID to an array index. - std::unordered_map mEntityToIndexMap; - - // Map from an array index to an entity ID. - std::unordered_map mIndexToEntityMap; - - // Total size of valid entries in the array. - size_t mSize; - -public: - void InsertData(Entity entity, T component); - void RemoveData(Entity entity); - T &GetData(Entity entity); - void EntityDestroyed(Entity entity) override; -}; - -class ComponentManager { - // Map from type string pointer to a component type - std::unordered_map mComponentTypes{}; - - // Map from type string pointer to a component array - std::unordered_map> - mComponentArrays{}; - - // The component type to be assigned to the next registered component - - // starting at 0 - ComponentType mNextComponentType{}; - - // Convenience function to get the statically casted pointer to the - // ComponentArray of type T. - template std::shared_ptr> GetComponentArray(); - -public: - template void RegisterComponent(); - template ComponentType GetComponentType(); - template void AddComponent(Entity entity, T component); - template void RemoveComponent(Entity entity); - template T &GetComponent(Entity entity); - void EntityDestroyed(Entity entity); -}; - -#endif // COMPONENTMANAGER_HPP diff --git a/libs/ecs/coordinator.cpp b/libs/ecs/coordinator.cpp deleted file mode 100644 index af6f545..0000000 --- a/libs/ecs/coordinator.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "coordinator.hpp" - -void Coordinator::Init() { - // Create pointers to each manager - mComponentManager = std::make_unique(); - mEntityManager = std::make_unique(); - mSystemManager = std::make_unique(); -} - -// Entity methods -Entity Coordinator::CreateEntity() { return mEntityManager->CreateEntity(); } - -void Coordinator::DestroyEntity(Entity entity) { - mEntityManager->DestroyEntity(entity); - - mComponentManager->EntityDestroyed(entity); - - mSystemManager->EntityDestroyed(entity); -} - -// Component methods -template void Coordinator::RegisterComponent() { - mComponentManager->RegisterComponent(); -} - -template -void Coordinator::AddComponent(Entity entity, T component) { - mComponentManager->AddComponent(entity, component); - - auto signature = mEntityManager->GetSignature(entity); - signature.set(mComponentManager->GetComponentType(), true); - mEntityManager->SetSignature(entity, signature); - - mSystemManager->EntitySignatureChanged(entity, signature); -} - -template void Coordinator::RemoveComponent(Entity entity) { - mComponentManager->RemoveComponent(entity); - - auto signature = mEntityManager->GetSignature(entity); - signature.set(mComponentManager->GetComponentType(), false); - mEntityManager->SetSignature(entity, signature); - - mSystemManager->EntitySignatureChanged(entity, signature); -} - -template T &Coordinator::GetComponent(Entity entity) { - return mComponentManager->GetComponent(entity); -} - -template ComponentType Coordinator::GetComponentType() { - return mComponentManager->GetComponentType(); -} - -// System methods -template std::shared_ptr Coordinator::RegisterSystem() { - return mSystemManager->RegisterSystem(); -} - -template -void Coordinator::SetSystemSignature(Signature signature) { - mSystemManager->SetSignature(signature); -} diff --git a/libs/ecs/coordinator.hpp b/libs/ecs/coordinator.hpp deleted file mode 100644 index 73f5a36..0000000 --- a/libs/ecs/coordinator.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef COORDINATOR_HPP -#define COORDINATOR_HPP - -#include -#include "componentmanager.hpp" -#include "entity.hpp" -#include "system.hpp" - -class Coordinator { - std::unique_ptr mComponentManager; - std::unique_ptr mEntityManager; - std::unique_ptr mSystemManager; - -public: - void Init(); - // Entity methods - Entity CreateEntity(); - void DestroyEntity(Entity entity); - // Component methods - template void RegisterComponent(); - template void AddComponent(Entity entity, T component); - template void RemoveComponent(Entity entity); - template T &GetComponent(Entity entity); - template ComponentType GetComponentType(); - // System methods - template std::shared_ptr RegisterSystem(); - template void SetSystemSignature(Signature signature); -}; - -#endif // COORDINATOR_HPP diff --git a/libs/ecs/entity.cpp b/libs/ecs/entity.cpp deleted file mode 100644 index 83f7262..0000000 --- a/libs/ecs/entity.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "entity.hpp" -#include - -EntityManager::EntityManager() { - // Initialize the queue with all possible entity IDs - for (Entity entity = 0; entity < MAX_ENTITIES; ++entity) { - mAvailableEntities.push(entity); - } -} -Entity EntityManager::CreateEntity() { - assert(mLivingEntityCount < MAX_ENTITIES && - "Too many entities in existence."); - - // Take an ID from the front of the queue - Entity id = mAvailableEntities.front(); - mAvailableEntities.pop(); - ++mLivingEntityCount; - - return id; -} -void EntityManager::DestroyEntity(Entity entity) { - assert(entity < MAX_ENTITIES && "Entity out of range."); - - // Invalidate the destroyed entity's signature - mSignatures[entity].reset(); - - // Put the destroyed ID at the back of the queue - mAvailableEntities.push(entity); - --mLivingEntityCount; -} -void EntityManager::SetSignature(Entity entity, Signature signature) { - assert(entity < MAX_ENTITIES && "Entity out of range."); - - // Put this entity's signature into the array - mSignatures[entity] = signature; -} -Signature EntityManager::GetSignature(Entity entity) { - assert(entity < MAX_ENTITIES && "Entity out of range."); - - // Get this entity's signature from the array - return mSignatures[entity]; -} diff --git a/libs/ecs/entity.hpp b/libs/ecs/entity.hpp deleted file mode 100644 index e2f7a15..0000000 --- a/libs/ecs/entity.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef ENTITY_HPP -#define ENTITY_HPP - -#include -#include -#include -#include "component.hpp" - -using Entity = std::uint32_t; - -const Entity MAX_ENTITIES = 5000; - -class EntityManager { - // Queue of unused entity IDs - std::queue mAvailableEntities{}; - - // Array of signatures where the index corresponds to the entity ID - std::array mSignatures{}; - - // Total living entities - used to keep limits on how many exist - uint32_t mLivingEntityCount{}; - -public: - EntityManager(); - Entity CreateEntity(); - void DestroyEntity(Entity entity); - void SetSignature(Entity entity, Signature signature); - Signature GetSignature(Entity entity); -}; - -#endif // ENTITY_HPP diff --git a/libs/ecs/system.cpp b/libs/ecs/system.cpp deleted file mode 100644 index a7e4ef4..0000000 --- a/libs/ecs/system.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "system.hpp" - -#include - -template std::shared_ptr SystemManager::RegisterSystem() { - const char *typeName = typeid(T).name(); - - assert(mSystems.find(typeName) == mSystems.end() && - "Registering system more than once."); - - // Create a pointer to the system and return it so it can be used externally - auto system = std::make_shared(); - mSystems.insert({typeName, system}); - return system; -} - -template void SystemManager::SetSignature(Signature signature) { - const char *typeName = typeid(T).name(); - - assert(mSystems.find(typeName) != mSystems.end() && - "System used before registered."); - - // Set the signature for this system - mSignatures.insert({typeName, signature}); -} - -void SystemManager::EntityDestroyed(Entity entity) { - // Erase a destroyed entity from all system lists - // mEntities is a set so no check needed - for (auto const &pair : mSystems) { - auto const &system = pair.second; - - system->mEntities.erase(entity); - } -} - -void SystemManager::EntitySignatureChanged(Entity entity, - Signature entitySignature) { - // Notify each system that an entity's signature changed - for (auto const &pair : mSystems) { - auto const &type = pair.first; - auto const &system = pair.second; - auto const &systemSignature = mSignatures[type]; - - // Entity signature matches system signature - insert into set - if ((entitySignature & systemSignature) == systemSignature) { - system->mEntities.insert(entity); - } - // Entity signature does not match system signature - erase from set - else { - system->mEntities.erase(entity); - } - } -} diff --git a/libs/ecs/system.hpp b/libs/ecs/system.hpp deleted file mode 100644 index af7e831..0000000 --- a/libs/ecs/system.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SYSTEM_HPP -#define SYSTEM_HPP - -#include -#include -#include -#include "component.hpp" -#include "entity.hpp" - -class System { -public: - std::set mEntities; -}; - -class SystemManager { - // Map from system type string pointer to a signature - std::unordered_map mSignatures{}; - - // Map from system type string pointer to a system pointer - std::unordered_map> mSystems{}; - -public: - template std::shared_ptr RegisterSystem(); - template void SetSignature(Signature signature); - void EntityDestroyed(Entity entity); - void EntitySignatureChanged(Entity entity, Signature entitySignature); -}; - -#endif // !SYSTEM_HPP diff --git a/meson.build b/meson.build index 19160c9..e6a8aed 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,5 @@ project('test', 'cpp') -sources = [] - deps = [] deps += dependency('fmt') deps += dependency('glfw3') diff --git a/src/components/meson.build b/src/components/meson.build deleted file mode 100644 index 32f0f06..0000000 --- a/src/components/meson.build +++ /dev/null @@ -1 +0,0 @@ -sources += files('shader.cpp', 'shader.hpp') diff --git a/src/components/shader.cpp b/src/components/shader.cpp deleted file mode 100644 index 779df45..0000000 --- a/src/components/shader.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "shader.hpp" - -Shader::Shader(GLuint shader) { this->shader = shader; } -void Shader::SetSource(std::string source) { this->SetSource(source.c_str()); } -void Shader::SetSource(const char *source) { - glShaderSource(shader, 1, &source, NULL); -} -void Shader::Compile() { glCompileShader(shader); } -void Shader::Attach(GLuint program) { glAttachShader(program, shader); } -VertexShader::VertexShader() : Shader(glCreateShader(GL_VERTEX_SHADER)) {} -FragmentShader::FragmentShader() : Shader(glCreateShader(GL_FRAGMENT_SHADER)) {} diff --git a/src/main.cpp b/src/main.cpp index ddf1bfa..b51934e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include #include -#include "systems/windowsystem.hpp" +#include "systems/window-system.hpp" void error_callback(int error_code, const char *description) { fmt::print("[ERROR - {}] {}\n", error_code, description); diff --git a/src/meson.build b/src/meson.build index 8a251b6..bb1cc6f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,4 +1,2 @@ -sources += files('main.cpp') - -subdir('components') +sources = files('main.cpp') subdir('systems') diff --git a/src/systems/meson.build b/src/systems/meson.build index db627d1..e42b237 100644 --- a/src/systems/meson.build +++ b/src/systems/meson.build @@ -1,6 +1,6 @@ sources += files( - 'shadersystem.cpp', - 'shadersystem.hpp', - 'windowsystem.cpp', - 'windowsystem.hpp', + 'shader-system.cpp', + 'shader-system.hpp', + 'window-system.cpp', + 'window-system.hpp', ) diff --git a/src/systems/shadersystem.cpp b/src/systems/shader-system.cpp similarity index 81% rename from src/systems/shadersystem.cpp rename to src/systems/shader-system.cpp index 6cf2d01..3de31da 100644 --- a/src/systems/shadersystem.cpp +++ b/src/systems/shader-system.cpp @@ -1,6 +1,4 @@ -#include "shadersystem.hpp" -#include -#include +#include "shader-system.hpp" typedef struct Vertex { vec2 pos; @@ -84,3 +82,13 @@ int ShaderSystem::Draw(int width, int height, float time) { glDrawArrays(GL_TRIANGLES, 0, 3); return 0; } + +Shader::Shader(GLuint shader) { this->shader = shader; } +void Shader::SetSource(std::string source) { this->SetSource(source.c_str()); } +void Shader::SetSource(const char *source) { + glShaderSource(shader, 1, &source, NULL); +} +void Shader::Compile() { glCompileShader(shader); } +void Shader::Attach(GLuint program) { glAttachShader(program, shader); } +VertexShader::VertexShader() : Shader(glCreateShader(GL_VERTEX_SHADER)) {} +FragmentShader::FragmentShader() : Shader(glCreateShader(GL_FRAGMENT_SHADER)) {} diff --git a/src/components/shader.hpp b/src/systems/shader-system.hpp similarity index 58% rename from src/components/shader.hpp rename to src/systems/shader-system.hpp index 58c74c1..145ae75 100644 --- a/src/components/shader.hpp +++ b/src/systems/shader-system.hpp @@ -1,10 +1,21 @@ -#ifndef SHADER_HPP -#define SHADER_HPP - -#define SHADER_HPP #include +#include #include +class ShaderSystem { + GLuint vertex_buffer; + GLuint program; + GLint mvp_location; + GLint vpos_location; + GLint vcol_location; + GLuint vertex_array; + +public: + ShaderSystem(); + int Draw(int width, int height, float time); + GLuint *CreateBuffer(); +}; + class Shader { public: Shader(GLuint shader); @@ -28,5 +39,3 @@ class FragmentShader : public Shader { public: FragmentShader(); }; - -#endif // SHADER_HPP diff --git a/src/systems/shadersystem.hpp b/src/systems/shadersystem.hpp deleted file mode 100644 index c8367fb..0000000 --- a/src/systems/shadersystem.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef SHADERSYSTEM_HPP -#define SHADERSYSTEM_HPP - -#include -#include - -class ShaderSystem { - GLuint vertex_buffer; - GLuint program; - GLint mvp_location; - GLint vpos_location; - GLint vcol_location; - GLuint vertex_array; - -public: - ShaderSystem(); - int Draw(int width, int height, float time); - GLuint *CreateBuffer(); -}; - -#endif // SHADERSYSTEM_HPP diff --git a/src/systems/windowsystem.cpp b/src/systems/window-system.cpp similarity index 93% rename from src/systems/windowsystem.cpp rename to src/systems/window-system.cpp index 3d65a00..626abc0 100644 --- a/src/systems/windowsystem.cpp +++ b/src/systems/window-system.cpp @@ -1,4 +1,4 @@ -#include "windowsystem.hpp" +#include "window-system.hpp" WindowSystem::WindowSystem(GLFWwindow *window, class ShaderSystem shaderSystem) { diff --git a/src/systems/windowsystem.hpp b/src/systems/window-system.hpp similarity index 67% rename from src/systems/windowsystem.hpp rename to src/systems/window-system.hpp index ac26f60..9fd6f59 100644 --- a/src/systems/windowsystem.hpp +++ b/src/systems/window-system.hpp @@ -1,9 +1,6 @@ -#ifndef WINDOWSYSTEM_HPP -#define WINDOWSYSTEM_HPP - #define GLFW_INCLUDE_NONE #include -#include "shadersystem.hpp" +#include "shader-system.hpp" class WindowSystem { GLFWwindow *window; @@ -13,5 +10,3 @@ public: WindowSystem(GLFWwindow *window, class ShaderSystem shaderSystem); int Loop(); }; - -#endif // WINDOWSYSTEM_HPP