From c6217d8b43b6ddf360485f9ba3b731a783482eaa Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 12 Jun 2018 06:45:25 +0100 Subject: Making function `const` --- yage/core/window.cpp | 3 ++- yage/core/window.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/yage/core/window.cpp b/yage/core/window.cpp index 25f9f834..392d4363 100644 --- a/yage/core/window.cpp +++ b/yage/core/window.cpp @@ -61,6 +61,7 @@ void Window::create(std::string window_name, int width, int height) // set key callback glfwSetKeyCallback(window_, key_callback); + // set resize callback glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback); @@ -108,7 +109,7 @@ void Window::pollEvents() const glfwPollEvents(); } -bool Window::keyPressed(key k) +bool Window::keyPressed(key k) const { if (window_ == nullptr) { throw runtime_error("Window is not initialized"); diff --git a/yage/core/window.h b/yage/core/window.h index 2ffafba8..6b0e7fed 100644 --- a/yage/core/window.h +++ b/yage/core/window.h @@ -56,7 +56,7 @@ public: bool shouldClose(); void pollEvents() const; - bool keyPressed(key k); + bool keyPressed(key k) const; }; } // namespace yage -- cgit From c3a7658120c9bb396bde01bed2bcec938fef1f10 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 12 Jun 2018 06:45:47 +0100 Subject: Making functions return themselves This is so that the functions can be chained together --- yage/entity/engine.cpp | 10 +++++++--- yage/entity/engine.h | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/yage/entity/engine.cpp b/yage/entity/engine.cpp index 1cef4504..73774093 100644 --- a/yage/entity/engine.cpp +++ b/yage/entity/engine.cpp @@ -21,13 +21,14 @@ Engine::~Engine() quit(); } -void Engine::init() +Engine &Engine::init() { yage::init(); window_.create("Game Engine", 800, 640); + return *this; } -void Engine::mainLoop() +Engine &Engine::mainLoop() { while (!window_.shouldClose()) { window_.pollEvents(); @@ -37,6 +38,8 @@ void Engine::mainLoop() window_.swapBuffer(); } + + return *this; } void Engine::update() @@ -46,9 +49,10 @@ void Engine::update() } } -void Engine::addSpace(std::unique_ptr space) +Engine &Engine::addSpace(std::unique_ptr space) { spaces_.push_back(std::move(space)); + return *this; } Engine &Engine::instance() diff --git a/yage/entity/engine.h b/yage/entity/engine.h index 9a1f6d2b..6719eeaf 100644 --- a/yage/entity/engine.h +++ b/yage/entity/engine.h @@ -32,16 +32,16 @@ public: ~Engine(); /// Initialize window and other aspects of the engine. - void init(); + Engine &init(); /// Main game loop of the engine. - void mainLoop(); + Engine &mainLoop(); /// Updates the systems. void update(); /// Add spaces to the engine - void addSpace(std::unique_ptr space); + Engine &addSpace(std::unique_ptr space); /// Returns the instance of the engine, as there is only one instance of the /// engine. -- cgit From 2acf4bfcbb5c4b457590e0ce6336645bc3900d83 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 15 Jun 2018 20:00:26 +0100 Subject: Deleting entity system so that it can be rewritten --- yage/entity/engine.cpp | 65 ------------------------------------------ yage/entity/engine.h | 58 -------------------------------------- yage/entity/entity.cpp | 42 ---------------------------- yage/entity/space.cpp | 29 ------------------- yage/entity/space.h | 76 -------------------------------------------------- 5 files changed, 270 deletions(-) delete mode 100644 yage/entity/engine.cpp delete mode 100644 yage/entity/engine.h delete mode 100644 yage/entity/entity.cpp delete mode 100644 yage/entity/space.cpp delete mode 100644 yage/entity/space.h diff --git a/yage/entity/engine.cpp b/yage/entity/engine.cpp deleted file mode 100644 index 73774093..00000000 --- a/yage/entity/engine.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: engine.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "engine.h" - -#include "../core/core.h" - -#include "space.h" - -namespace yage -{ - -Engine::~Engine() -{ - quit(); -} - -Engine &Engine::init() -{ - yage::init(); - window_.create("Game Engine", 800, 640); - return *this; -} - -Engine &Engine::mainLoop() -{ - while (!window_.shouldClose()) { - window_.pollEvents(); - window_.clearBuffer(); - - update(); - - window_.swapBuffer(); - } - - return *this; -} - -void Engine::update() -{ - for (auto &space : spaces_) { - space->update(); - } -} - -Engine &Engine::addSpace(std::unique_ptr space) -{ - spaces_.push_back(std::move(space)); - return *this; -} - -Engine &Engine::instance() -{ - static Engine engine_instance; - - return engine_instance; -} - -} // namespace yage diff --git a/yage/entity/engine.h b/yage/entity/engine.h deleted file mode 100644 index 6719eeaf..00000000 --- a/yage/entity/engine.h +++ /dev/null @@ -1,58 +0,0 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: engine.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#pragma once - -#include "system.h" - -#include "../core/window.h" -#include "../util/noncopyable.h" - -#include -#include - -namespace yage -{ - -class Space; - -/** - * Main engine class that contains a systems, the main loop and the update - * function that updates all the systems. - */ -class Engine : public NonCopyable -{ -public: - ~Engine(); - - /// Initialize window and other aspects of the engine. - Engine &init(); - - /// Main game loop of the engine. - Engine &mainLoop(); - - /// Updates the systems. - void update(); - - /// Add spaces to the engine - Engine &addSpace(std::unique_ptr space); - - /// Returns the instance of the engine, as there is only one instance of the - /// engine. - static Engine &instance(); - -private: - /// Window - Window window_; - - /// A vector of all the spaces - std::vector> spaces_; -}; - -} // namespace yage diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp deleted file mode 100644 index 1a2b16ad..00000000 --- a/yage/entity/entity.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: entity.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "entity.h" - -#include - -namespace yage -{ - -BaseComponent::Group BaseComponent::group_id_counter_ = 0; - -EntityManager::EntityManager(Space *space) : space_(space) {} - -EntityManager::EntityManager(Space *space, std::size_t n) : space_(space) -{ - entities_.reserve(n); -} - -Entity EntityManager::createEntity() -{ - Entity entity = next_entity_++; - entities_.push_back(entity); - return entity; -} - -void EntityManager::deleteEntity(Entity entity) -{ - auto index = std::find_if(entities_.begin(), entities_.end(), - [&](Entity &value) { return value == entity; }); - if (index != entities_.end()) { - entities_.erase(index); - } -} - -} // namespace yage diff --git a/yage/entity/space.cpp b/yage/entity/space.cpp deleted file mode 100644 index 530f5b49..00000000 --- a/yage/entity/space.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: space.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "space.h" - -namespace yage -{ - -Space::Space() : em_(this) {} - -unsigned Space::createEntity() -{ - return em_.createEntity(); -} - -void Space::update() -{ - for (auto &system : systems_) { - system->update(); - } -} - -} // namespace yage diff --git a/yage/entity/space.h b/yage/entity/space.h deleted file mode 100644 index 0763b189..00000000 --- a/yage/entity/space.h +++ /dev/null @@ -1,76 +0,0 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: space.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#pragma once - -#include -#include - -#include "entity.h" -#include "system.h" - -struct Movement { -}; - -namespace yage -{ - -class System; - -/** - * Space that keeps track of all the entities, componenets and runs - * the systems on the data to update them. There can be multiple - * instances of a space, which can be used, for example, for different - * levels in the game that can be loaded separately, or a game menu - * that can be loaded above the other spaces when the user presses on - * pause. - */ -class Space -{ -public: - /** - * Default instance for a space. - */ - Space(); - - /** - * Create an entity that will belong to this space, and return the - * handle to the user. The Entity class itself should not be - * visible to the user, as the user only needs to worry about the - * handle when referring to the Entity and changing it. - */ - Entity createEntity(); - - /** - * Update all the systems. - */ - void update(); - -private: - /** - * The systems of the Space that act on the data and on their - * respective component. These are specific to the Space, as other - * spaces might have different Systems and not act on the same - * entities. - */ - std::vector> systems_; - - /** - * Manages all the entities in the system, can create them for the - * current space. - */ - EntityManager em_; - - /** - * Manages all the components - */ - // ComponentManager cm_; -}; - -} // namespace yage -- cgit From 98b8df435329df6ff3a19ad46f13c8f5bb22ca53 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 15 Jun 2018 20:00:39 +0100 Subject: Adding entity system --- yage/entity/entity.h | 137 +++++++++++---------------------------------------- 1 file changed, 30 insertions(+), 107 deletions(-) diff --git a/yage/entity/entity.h b/yage/entity/entity.h index a032ad8a..01a47f5c 100644 --- a/yage/entity/entity.h +++ b/yage/entity/entity.h @@ -1,127 +1,50 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: entity.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#pragma once - -#include +#include #include -namespace yage -{ - -class Space; -class ComponentGroup; - +/** + * The entity is currently just an unsigned integer, which may change to a + * class in the future. + */ typedef unsigned int Entity; -/** Manages entities in a space. +/** + * The component mask represents all the components that the entity is + * currently attached to. */ -class EntityManager -{ -public: - /** Default instance of an EntityManager. - */ - EntityManager() = default; - - /** Creates an instance of the entity manager, which refers back to the - * space it was created in and belongs to. - * - * @param space Current space that the EntityManager belongs to. - */ - EntityManager(Space *space); +typedef std::bitset<64> ComponentMask; - /** Creates an instance of the entitiy manager with an initial size. - * - * @param space Current space that the EntityManager belongs to. @param n - * Initial size of the EntityManager. - */ - EntityManager(Space *space, std::size_t n); +class EntityManager; - /** Creates an Entity and returns the handle to the entity, which can then - * be used by the user to do operations on it. - * - * @return The handle to the entity that was created in the space. - */ - Entity createEntity(); +class BaseSystem; - /** Delete an entity. - */ - void deleteEntity(Entity entity); +template +class System; -private: - /** The next available handle to give to the user. - */ - Entity next_entity_ = 0; +class BaseComponent; - /** The space that the entity manager belongs to. - */ - Space *space_ = nullptr; +template +class Component; - /** The entities in the current space. - */ - std::vector entities_; +class BaseComponentGroup; - /** Component list of different component groups, that then contain the - * different components for the entities. - */ - std::vector> components; -}; +template +class ComponentGroup; -/** Base component used to store components. - * - * Should not be inherited from when declaring a new component. Instead, the - * Component should be used. +/** + * Has to keep track of all the different entities and their current state. + * + * The key actions on an Entity are: deleting, creating. */ -class BaseComponent +class EntityManager { public: - /** Group used to register a specific component internally with the entity - * manager. - */ - typedef unsigned Group; - -protected: - static Group group_id_counter_; -}; - -/** The main component that is used to make a component from a defined struct. - * - * A component should only be declared as a struct and should not contain any - * data itself. - */ -template -class Component : public BaseComponent -{ -private: - friend class EntityManager; - - BaseComponent::Group group(); -}; + Entity createEntity(); + EntityManager &deleteEntity(Entity entity); + void addComponent(Entity entity, BaseComponent *component); -/** Contains a list of all components that belong to a sepecific group, these - * are then stored in the main entity manager. - */ -class ComponentGroup -{ private: - friend class EntityManager; + Entity next_entity_; - std::vector components_; + std::vector component_group_; + std::vector component_masks_; }; - -// Template definitions - -template -BaseComponent::Group Component::group() -{ - static Group group_id = group_id_counter_++; - return group_id; -} - -} // namespace yage -- cgit From 65c75747db540be33dab64b23059de1387daf220 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 15 Jun 2018 20:00:46 +0100 Subject: Adding new system --- yage/entity/system.h | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/yage/entity/system.h b/yage/entity/system.h index 84536cc9..b540555f 100644 --- a/yage/entity/system.h +++ b/yage/entity/system.h @@ -1,26 +1,10 @@ -/** --------------------------------------------------------------------------- - * -*- c++ -*- - * @file: system.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#pragma once - -#include - -#include "../util/noncopyable.h" - -namespace yage -{ - -class System : public NonCopyable +class BaseSystem { public: - virtual void init() = 0; virtual void update() = 0; }; -} // namespace yage +template +class System : public BaseSystem +{ +}; -- cgit From 40d48773a393edadea97e501a4a47cd1746dadbe Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 15 Jun 2018 20:01:05 +0100 Subject: Adding components with test --- tests/component_test.cpp | 13 +++++++++++++ yage/entity/component.cpp | 6 ++++++ yage/entity/component.h | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 tests/component_test.cpp create mode 100644 yage/entity/component.cpp create mode 100644 yage/entity/component.h diff --git a/tests/component_test.cpp b/tests/component_test.cpp new file mode 100644 index 00000000..db42c287 --- /dev/null +++ b/tests/component_test.cpp @@ -0,0 +1,13 @@ +#include + +struct Position { + double x; + double y; + + Position(double x_i, double y_i) : x(x_i), y(y_i) {} +}; + +int main() { + EntityManager em(); + Entity player = em.createEntity(); +} diff --git a/yage/entity/component.cpp b/yage/entity/component.cpp new file mode 100644 index 00000000..83fe4424 --- /dev/null +++ b/yage/entity/component.cpp @@ -0,0 +1,6 @@ +#include "component.h" + +GroupId BaseComponent::getGroup() { + static GroupId group_id = group_id_counter_++; + return group_id; +} diff --git a/yage/entity/component.h b/yage/entity/component.h new file mode 100644 index 00000000..84eb8b54 --- /dev/null +++ b/yage/entity/component.h @@ -0,0 +1,17 @@ +typedef unsigned int GroupId; + +class BaseComponent +{ +protected: + GroupId getGroup(); + +private: + friend class EntityManager; + + static GroupId group_id_counter_; +}; + +template +class Component : public BaseComponent +{ +}; -- cgit From 32f9959d93322b5e26ef9843672a77928bf9c6bf Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 20 Jun 2018 23:19:38 +0100 Subject: Adding imports to test entity system --- tests/component_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/component_test.cpp b/tests/component_test.cpp index db42c287..e5a381cf 100644 --- a/tests/component_test.cpp +++ b/tests/component_test.cpp @@ -1,4 +1,6 @@ #include +#include +#include struct Position { double x; -- cgit From aaf7c1662abb78228b774f728061b092e4467b07 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 20 Jun 2018 23:20:05 +0100 Subject: Adding entity implementation and changing API --- yage/entity/entity.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ yage/entity/entity.h | 14 +++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 yage/entity/entity.cpp diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp new file mode 100644 index 00000000..050514c3 --- /dev/null +++ b/yage/entity/entity.cpp @@ -0,0 +1,40 @@ +#include "entity.h" + +#include + +namespace yage +{ + +Entity EntityManager::create_entity() +{ + Entity entity = update_next_entity(); + component_masks_.push_back(ComponentMask(0)); + return entity; +} + +EntityManager &EntityManager::delete_entity(Entity entity) +{ + deleted_.push_back(entity); + return *this; +} + +bool EntityManager::is_valid(Entity entity) const +{ + auto it = std::find(deleted_.begin(), deleted_.end(), entity); + if (it == deleted_.end()) { + return true; + } + return false; +} + +Entity EntityManager::update_next_entity() +{ + if (deleted_.empty()) { + return ++next_entity_; + } + next_entity_ = deleted_.back(); + deleted_.pop_back(); + return next_entity_; +} + +} // namespace yage diff --git a/yage/entity/entity.h b/yage/entity/entity.h index 01a47f5c..d9a221b9 100644 --- a/yage/entity/entity.h +++ b/yage/entity/entity.h @@ -1,6 +1,8 @@ #include #include +namespace yage { + /** * The entity is currently just an unsigned integer, which may change to a * class in the future. @@ -38,13 +40,19 @@ class ComponentGroup; class EntityManager { public: - Entity createEntity(); - EntityManager &deleteEntity(Entity entity); - void addComponent(Entity entity, BaseComponent *component); + Entity create_entity(); + EntityManager &delete_entity(Entity entity); + bool is_valid(Entity entity) const; + void add_component(Entity entity, BaseComponent *component); private: + Entity update_next_entity(); + Entity next_entity_; std::vector component_group_; std::vector component_masks_; + std::vector deleted_; }; + +} // namespace yage -- cgit From 9f2a6274a2e634e9b9b862f8b0c00a6a75768342 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 21 Jun 2018 20:58:11 +0100 Subject: Making C++17 the standard --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e55c01d..55539b04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ option(YAGE_BUILD_DOCS "Build documentation using Doxygen" OFF) option(YAGE_BUILD_EXAMPLES "Build example executables" OFF) # set standard -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) # find other libraries from source set(EXTERNAL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs) -- cgit From 8e038f0114a6a2f6d2971ac9772bd0a053e3ccef Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:08 +0100 Subject: Removing iostream --- yage/core/imageloader.cpp | 5 ----- yage/math/matrix.h | 4 ---- yage/physics/particlebody.cpp | 1 - yage/render/spritebatch.cpp | 3 --- 4 files changed, 13 deletions(-) diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp index 4905bf5e..d3c978c1 100644 --- a/yage/core/imageloader.cpp +++ b/yage/core/imageloader.cpp @@ -11,15 +11,11 @@ #include "../data/texture.h" #include "logger.h" #include "stb_image.h" -#include #include -#include #include -using std::cout; - namespace yage { @@ -32,7 +28,6 @@ Texture ImageLoader::loadPng(const std::string &file_path) yLogDebug << "Sucessfully loaded file"; Texture texture(0, static_cast(width), static_cast(height)); yLogDebug << "Creating texture"; - cout << "Hello"; glGenTextures(1, &texture.id); glBindTexture(GL_TEXTURE_2D, texture.id); diff --git a/yage/math/matrix.h b/yage/math/matrix.h index a41c522f..c1a93413 100644 --- a/yage/math/matrix.h +++ b/yage/math/matrix.h @@ -7,14 +7,10 @@ * ---------------------------------------------------------------------------- */ -/** @file - */ - #pragma once #include #include -#include #include #include #include diff --git a/yage/physics/particlebody.cpp b/yage/physics/particlebody.cpp index 49520558..c6d8f469 100644 --- a/yage/physics/particlebody.cpp +++ b/yage/physics/particlebody.cpp @@ -10,7 +10,6 @@ #include #include -#include namespace yage { diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp index fc0103f4..bfce102a 100644 --- a/yage/render/spritebatch.cpp +++ b/yage/render/spritebatch.cpp @@ -11,11 +11,8 @@ #include "../core/logger.h" #include -#include #include -using std::cout; - #include namespace yage -- cgit From 3cc80b221c34dcc15579dcf8bf7f4fff8c6b5879 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:17 +0100 Subject: Moving test name --- tests/component_test.cpp | 15 -------------- tests/entity_test.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) delete mode 100644 tests/component_test.cpp create mode 100644 tests/entity_test.cpp diff --git a/tests/component_test.cpp b/tests/component_test.cpp deleted file mode 100644 index e5a381cf..00000000 --- a/tests/component_test.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -struct Position { - double x; - double y; - - Position(double x_i, double y_i) : x(x_i), y(y_i) {} -}; - -int main() { - EntityManager em(); - Entity player = em.createEntity(); -} diff --git a/tests/entity_test.cpp b/tests/entity_test.cpp new file mode 100644 index 00000000..0e40d3ea --- /dev/null +++ b/tests/entity_test.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace yage; + +#include +#include + +struct Position : public Component { + double x; + double y; + + Position(double x_, double y_) : x(x_), y(y_) {} +}; + +struct Size : public Component { + double width; + double height; + + Size(double w, double h) : width(w), height(h) {} +}; + +class MovementSystem : public System +{ +public: + void update(double dt, EntityManager &em) override + { + for (auto &&x : em.component_masks_) { + if(x[1] == 1) { + std::cout << "Found size: "; + } + } + } +}; + +int main() +{ + EntityManager em; + Position p1(1, 2); + Position p2(2, 1); + Size s1(5, 5); + Entity e1 = em.create_entity(); + Entity e2 = em.create_entity(); + std::cout << "e1: " << e1 << ", e2: " << e2 << "\n"; + + MovementSystem s; + + em.add_component(e1, &p1).add_component(e2, &p2).add_component(e2, &s1); + + s.update(60, em); +} -- cgit From c7838e4fb65546c82314f30502d162953c967fa1 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:24 +0100 Subject: Improving dir locals for compilation in emacs --- .dir-locals.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.dir-locals.el b/.dir-locals.el index 40e6adb0..3ead1dc4 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -1,3 +1,3 @@ -((nil . ((projectile-project-compilation-cmd . "mkdir -p build && cd build && cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. && ninja -j4") +((nil . ((projectile-project-compilation-cmd . "cd build && ninja -j4") (projectile-project-test-cmd . "cd build/tests && ctest -j4 --schedule-random") (projectile-project-run-cmd . "./build/bin/simplegame")))) -- cgit From 774a0acb51b7e97c1f7951fc90ddd51c19527a50 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:37 +0100 Subject: Expanding entity system --- yage/entity/entity.cpp | 17 ++++++++++++++--- yage/entity/entity.h | 44 +++++++++++++------------------------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp index 050514c3..25e8e2d0 100644 --- a/yage/entity/entity.cpp +++ b/yage/entity/entity.cpp @@ -1,5 +1,7 @@ #include "entity.h" +#include "component.h" + #include namespace yage @@ -27,14 +29,23 @@ bool EntityManager::is_valid(Entity entity) const return false; } +EntityManager &EntityManager::add_component(Entity entity, + BaseComponent *component) +{ + auto id = component->getGroup(); + component_masks_[entity] = + component_masks_[entity] | ComponentMask(1 << id); + return *this; +} + Entity EntityManager::update_next_entity() { if (deleted_.empty()) { - return ++next_entity_; + return next_entity_++; } - next_entity_ = deleted_.back(); + Entity ent = deleted_.back(); deleted_.pop_back(); - return next_entity_; + return ent; } } // namespace yage diff --git a/yage/entity/entity.h b/yage/entity/entity.h index d9a221b9..2aa31a66 100644 --- a/yage/entity/entity.h +++ b/yage/entity/entity.h @@ -1,40 +1,21 @@ -#include +#pragma once + #include -namespace yage { +#include "component.h" + +namespace yage +{ -/** +/** * The entity is currently just an unsigned integer, which may change to a * class in the future. */ typedef unsigned int Entity; -/** - * The component mask represents all the components that the entity is - * currently attached to. - */ -typedef std::bitset<64> ComponentMask; - -class EntityManager; - -class BaseSystem; - -template -class System; - -class BaseComponent; - -template -class Component; - -class BaseComponentGroup; - -template -class ComponentGroup; - -/** +/** * Has to keep track of all the different entities and their current state. - * + * * The key actions on an Entity are: deleting, creating. */ class EntityManager @@ -43,14 +24,15 @@ public: Entity create_entity(); EntityManager &delete_entity(Entity entity); bool is_valid(Entity entity) const; - void add_component(Entity entity, BaseComponent *component); + EntityManager &add_component(Entity entity, BaseComponent *component); private: Entity update_next_entity(); - Entity next_entity_; + Entity next_entity_ = 0; - std::vector component_group_; +public: + std::vector component_group_; std::vector component_masks_; std::vector deleted_; }; -- cgit From a88e5ca9a0db09a1e46fedfe7d7e03918578e911 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:46 +0100 Subject: Writing systems for entity system --- yage/entity/system.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/yage/entity/system.h b/yage/entity/system.h index b540555f..4f8306a8 100644 --- a/yage/entity/system.h +++ b/yage/entity/system.h @@ -1,10 +1,21 @@ +#pragma once + +#include "entity.h" + +namespace yage +{ + class BaseSystem { public: - virtual void update() = 0; + virtual void update(double dt, EntityManager &em) = 0; }; template class System : public BaseSystem { +protected: + ComponentMask active_mask_; }; + +} // namespace yage -- cgit From f4b6f813db1721421cadaf906c2763546e0190b4 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:07:57 +0100 Subject: Removing entity system from main header --- yage/yage.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/yage/yage.h b/yage/yage.h index 089c2797..bff3610c 100644 --- a/yage/yage.h +++ b/yage/yage.h @@ -44,9 +44,3 @@ */ #include "render/shader.h" #include "render/spritebatch.h" - -/** - * Engine that includes a Entity Component System to organize the data and make - * it more flexible and efficient. - */ -#include "entity/engine.h" -- cgit From 3702e753a5f7b31c31261c968757e19e808a84ec Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 22 Jun 2018 00:08:12 +0100 Subject: Adding components implementation --- yage/entity/component.cpp | 10 ++++++---- yage/entity/component.h | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/yage/entity/component.cpp b/yage/entity/component.cpp index 83fe4424..eba2ad0a 100644 --- a/yage/entity/component.cpp +++ b/yage/entity/component.cpp @@ -1,6 +1,8 @@ #include "component.h" -GroupId BaseComponent::getGroup() { - static GroupId group_id = group_id_counter_++; - return group_id; -} +namespace yage +{ + +GroupId BaseComponent::group_id_counter_ = 0; + +} // namespace yage diff --git a/yage/entity/component.h b/yage/entity/component.h index 84eb8b54..a21409ff 100644 --- a/yage/entity/component.h +++ b/yage/entity/component.h @@ -1,17 +1,47 @@ +#pragma once + +#include +#include +#include + +namespace yage +{ + +/** + * The component mask represents all the components that the entity is + * currently attached to. + */ +typedef std::bitset<64> ComponentMask; + typedef unsigned int GroupId; class BaseComponent { protected: - GroupId getGroup(); + virtual GroupId getGroup() = 0; + static GroupId group_id_counter_; private: friend class EntityManager; - - static GroupId group_id_counter_; }; template class Component : public BaseComponent { + GroupId getGroup() override; }; + +class ComponentGroup +{ +public: + std::vector> components_; +}; + +template +GroupId Component::getGroup() +{ + static GroupId group_id = group_id_counter_++; + return group_id; +} + +} // namespace yage -- cgit