From c514e44b73b3fc4db492e4bd333fa223c6c1eef5 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 8 Jan 2018 14:09:43 +0000 Subject: [Engine] Adding entity/component system --- yage/engine/engine.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ yage/engine/engine.h | 44 ++++++++++++++++++++++++++++++++++++++++++ yage/engine/system.h | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 yage/engine/engine.cpp create mode 100644 yage/engine/engine.h create mode 100644 yage/engine/system.h (limited to 'yage/engine') 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 + * 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 + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#ifndef YAGE_CORE_ENGINE_H +#define YAGE_CORE_ENGINE_H + +#include "system.h" + +#include + +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 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 + * 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 -- cgit