diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-08 14:09:43 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-08 14:09:43 +0000 |
commit | c514e44b73b3fc4db492e4bd333fa223c6c1eef5 (patch) | |
tree | bb1cc421392f263531b82ad1a67454eb351bbd74 /yage/engine | |
parent | 0a42123b150e06f28ae82e460e854984c2dc9648 (diff) | |
download | YAGE-c514e44b73b3fc4db492e4bd333fa223c6c1eef5.tar.gz YAGE-c514e44b73b3fc4db492e4bd333fa223c6c1eef5.zip |
[Engine] Adding entity/component system
Diffstat (limited to 'yage/engine')
-rw-r--r-- | yage/engine/engine.cpp | 52 | ||||
-rw-r--r-- | yage/engine/engine.h | 44 | ||||
-rw-r--r-- | yage/engine/system.h | 39 |
3 files changed, 135 insertions, 0 deletions
diff --git a/yage/engine/engine.cpp b/yage/engine/engine.cpp new file mode 100644 index 00000000..3918e7e9 --- /dev/null +++ b/yage/engine/engine.cpp @@ -0,0 +1,52 @@ +/** --------------------------------------------------------------------------- + * @file: engine.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#include "engine.h" + +#include "../core/window.h" + +namespace yage +{ + +void Engine::mainLoop() +{ + Window window; + + window.create("Game Engine", 800, 640); + + while(!window.shouldClose()) { + window.clearBuffer(); + + update(); + + window.swapBuffer(); + } +} + +void Engine::update() +{ + const double dt = 1.0 / 60.0; + + for(auto &&system : systems_) { + system->update(dt); + } +} + +void Engine::addSystem(System *system) +{ + systems_.push_back(system); +} + +Engine &Engine::instance() +{ + static Engine engine_instance; + + return engine_instance; +} + +} // namespace yage diff --git a/yage/engine/engine.h b/yage/engine/engine.h new file mode 100644 index 00000000..dabe5596 --- /dev/null +++ b/yage/engine/engine.h @@ -0,0 +1,44 @@ +/** --------------------------------------------------------------------------- + * @file: engine.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_CORE_ENGINE_H +#define YAGE_CORE_ENGINE_H + +#include "system.h" + +#include <vector> + +namespace yage +{ + +/// Main engine class that contains a systems, the main loop and the update +/// function that updates all the systems. +class Engine +{ +public: + /// Main game loop of the engine. + void mainLoop(); + + /// Updates the systems. + void update(); + + /// Adds a system to the engine. + void addSystem(System *system); + + /// Returns the instance of the engine, as there is only one instance of the + /// engine. + static Engine &instance(); + +private: + /// Vector of all the systems in the engine. + std::vector<System *> systems_; +}; + +} // namespace yage + +#endif diff --git a/yage/engine/system.h b/yage/engine/system.h new file mode 100644 index 00000000..c63b4733 --- /dev/null +++ b/yage/engine/system.h @@ -0,0 +1,39 @@ +/** --------------------------------------------------------------------------- + * @file: system.h + * + * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_ENGINE_SYSTEM_H +#define YAGE_ENGINE_SYSTEM_H + +namespace yage +{ + +/** + * System interface for the different systems in the engine. + */ +class System +{ +public: + /// Virtual destructor to destroy all the objects that implement this + /// properly. + virtual ~System() = 0; + + /// Initializes the system. Good practice to have this function instead + /// using the constructor. + virtual void init() = 0; + + /// Updates the system at each interval using the time step. + virtual void update(double dt) = 0; +}; + +/// Implement the default destructor, but leaving it as purely virtual in the +/// definition of the abstract class. +inline yage::System::~System() {} + +} // namespace yage + +#endif |