diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-10 12:02:04 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-10 12:02:04 +0000 |
commit | a62fbea8d40f623ffcd60eced63f295cd55db084 (patch) | |
tree | 2374234d4364eb389df98fff0c4c180118331b27 /yage/engine | |
parent | cbd6bf4a695370dbfc088bbe0cd6f270e1c112a7 (diff) | |
download | YAGE-a62fbea8d40f623ffcd60eced63f295cd55db084.tar.gz YAGE-a62fbea8d40f623ffcd60eced63f295cd55db084.zip |
[Engine] Adding initial components for an ECS.
Diffstat (limited to 'yage/engine')
-rw-r--r-- | yage/engine/component.h | 21 | ||||
-rw-r--r-- | yage/engine/engine.cpp | 21 | ||||
-rw-r--r-- | yage/engine/engine.h | 7 | ||||
-rw-r--r-- | yage/engine/entity.cpp | 23 | ||||
-rw-r--r-- | yage/engine/entity.h | 53 | ||||
-rw-r--r-- | yage/engine/entitymanager.cpp | 34 | ||||
-rw-r--r-- | yage/engine/entitymanager.h | 83 | ||||
-rw-r--r-- | yage/engine/space.cpp | 21 | ||||
-rw-r--r-- | yage/engine/space.h | 61 |
9 files changed, 315 insertions, 9 deletions
diff --git a/yage/engine/component.h b/yage/engine/component.h new file mode 100644 index 00000000..9fd85d82 --- /dev/null +++ b/yage/engine/component.h @@ -0,0 +1,21 @@ +/** --------------------------------------------------------------------------- + * @file: component.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_ENGINE_COMPONENT_H +#define YAGE_ENGINE_COMPONENT_H + +class Component +{ +}; + +enum class ComponentEnum { + POSITION, + NAX_COMPONENTS, +}; + +#endif diff --git a/yage/engine/engine.cpp b/yage/engine/engine.cpp index 3918e7e9..cf6f73b7 100644 --- a/yage/engine/engine.cpp +++ b/yage/engine/engine.cpp @@ -8,23 +8,26 @@ #include "engine.h" -#include "../core/window.h" - namespace yage { -void Engine::mainLoop() +void Engine::init() { - Window window; + window_.create("Game Engine", 800, 640); - window.create("Game Engine", 800, 640); + for (auto &system : systems_) { + system->init(); + } +} - while(!window.shouldClose()) { - window.clearBuffer(); +void Engine::mainLoop() +{ + while (!window_.shouldClose()) { + window_.clearBuffer(); update(); - window.swapBuffer(); + window_.swapBuffer(); } } @@ -32,7 +35,7 @@ void Engine::update() { const double dt = 1.0 / 60.0; - for(auto &&system : systems_) { + for (auto &system : systems_) { system->update(dt); } } diff --git a/yage/engine/engine.h b/yage/engine/engine.h index dabe5596..147769e5 100644 --- a/yage/engine/engine.h +++ b/yage/engine/engine.h @@ -9,6 +9,7 @@ #ifndef YAGE_CORE_ENGINE_H #define YAGE_CORE_ENGINE_H +#include "../core/window.h" #include "system.h" #include <vector> @@ -21,6 +22,9 @@ namespace yage class Engine { public: + /// Initialize window and other aspects of the engine. + void init(); + /// Main game loop of the engine. void mainLoop(); @@ -37,6 +41,9 @@ public: private: /// Vector of all the systems in the engine. std::vector<System *> systems_; + + /// Window + Window window_; }; } // namespace yage diff --git a/yage/engine/entity.cpp b/yage/engine/entity.cpp new file mode 100644 index 00000000..4d9a4b0a --- /dev/null +++ b/yage/engine/entity.cpp @@ -0,0 +1,23 @@ +/** --------------------------------------------------------------------------- + * @file: entity.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#include "entity.h" + +#include "space.h" + +namespace yage +{ + +Entity::Entity(unsigned handle) : handle_(handle) {} + +unsigned Entity::getHandle() const +{ + return handle_; +} + +} // naemspace yage diff --git a/yage/engine/entity.h b/yage/engine/entity.h new file mode 100644 index 00000000..c31490e5 --- /dev/null +++ b/yage/engine/entity.h @@ -0,0 +1,53 @@ +/** --------------------------------------------------------------------------- + * @file: entity.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_ENGINE_ENTITY_H +#define YAGE_ENGINE_ENTITY_H + +#include <vector> + +namespace yage +{ + +class Space; + +/** + * Entity convenience class. It contains handles to where the entity is in the + * entity manager of the space, and a pointer back to the space itself. + * + * This class cannot be instantiated outside of a Space and should only be + * instantiated through an entity manager, as otherwise the handle will not have + * a meaning. + */ +class Entity +{ +public: + /** + * Creates an instance of an Entity with a handle that is associated to it. + * This handle refers to the position of he Entity in the list that is held + * by the EntityManager, and therefore the id is enough to refer to it. + */ + Entity(unsigned handle); + + /** + * Handle getter, as the user will only interact with the id itself. The + * handle is the unique identifier that the user can use to refer to the + * entity. + */ + unsigned getHandle() const; + +private: + /** + * Entity handle for the entity manager. + */ + unsigned handle_; +}; + +} // namespace yage + +#endif diff --git a/yage/engine/entitymanager.cpp b/yage/engine/entitymanager.cpp new file mode 100644 index 00000000..332ed9b8 --- /dev/null +++ b/yage/engine/entitymanager.cpp @@ -0,0 +1,34 @@ +/** --------------------------------------------------------------------------- + * @file: entitymanager.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#include "entitymanager.h" + +namespace yage +{ + +EntityManager::EntityManager(Space *space) : next_handle_(0), space_(space) {} + +EntityManager::EntityManager(Space *space, std::size_t n) + : next_handle_(0), space_(space) +{ + entities_.reserve(n); +} + +unsigned EntityManager::createEntity() +{ + return createEntityInstance().getHandle(); +} + +Entity EntityManager::createEntityInstance() +{ + Entity entity(next_handle_++); + entities_.push_back(entity); + return entity; +} + +} // namespace yage diff --git a/yage/engine/entitymanager.h b/yage/engine/entitymanager.h new file mode 100644 index 00000000..da125d94 --- /dev/null +++ b/yage/engine/entitymanager.h @@ -0,0 +1,83 @@ +/** --------------------------------------------------------------------------- + * @file: entitymanager.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_ENGINE_ENTITYMANAGER_H +#define YAGE_ENGINE_ENTITYMANAGER_H + +#include "entity.h" + +#include <vector> + +namespace yage +{ + +class Space; + +/** + * Manages entities in a space. + */ +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); + + /** + * 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); + + /** + * 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. + */ + unsigned createEntity(); + + /** + * Creates an Entity and returns it. + * + * @return The entity that was created by the entity manager in the current + * space. + */ + Entity createEntityInstance(); + +private: + /** + * The next available handle to give to the user. + */ + unsigned next_handle_; + + /** + * The space that the entity manager belongs to. + */ + Space *space_; + + /** + * The entities in the current space. + */ + std::vector<Entity> entities_; +}; + +} // namespace yage + +#endif diff --git a/yage/engine/space.cpp b/yage/engine/space.cpp new file mode 100644 index 00000000..f3e343b5 --- /dev/null +++ b/yage/engine/space.cpp @@ -0,0 +1,21 @@ +/** --------------------------------------------------------------------------- + * @file: space.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#include "space.h" + +namespace yage +{ + +Space::Space() : em_(this) {} + +unsigned Space::createEntity() +{ + return em_.createEntity(); +} + +} // namespace yage diff --git a/yage/engine/space.h b/yage/engine/space.h new file mode 100644 index 00000000..e69df37a --- /dev/null +++ b/yage/engine/space.h @@ -0,0 +1,61 @@ +/** --------------------------------------------------------------------------- + * @file: space.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_ENGINE_SPACE_H +#define YAGE_ENGINE_SPACE_H + +#include <vector> + +#include "entitymanager.h" + +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. + */ + unsigned createEntity(); + +private: + /** + * The subspaces 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<System *> systems_; + + /** + * Manages all the entities in the system, can create them for the current + * space. + */ + EntityManager em_; +}; + +} // namespace yage + +#endif |