feat: implement module lifecycle
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#ifndef __MCS_ENGINE_HPP__
|
||||
#define __MCS_ENGINE_HPP__
|
||||
|
||||
#include <cstdint>
|
||||
#include <engine/core/engine_db_provider.hpp>
|
||||
#include <engine/core/engine_module.hpp>
|
||||
#include <engine/core/engine_module_api.hpp>
|
||||
#include <engine/core/engine_db_provider.hpp>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -12,8 +12,6 @@
|
||||
class MscEngine : public IMscEngineModuleAPI
|
||||
{
|
||||
private:
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
/// @brief Loaded modules list
|
||||
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
||||
|
||||
@@ -39,10 +37,12 @@ public:
|
||||
void stop();
|
||||
|
||||
MscEventBus &getBus() override;
|
||||
std::unordered_map<std::string, std::uint16_t> getLoadedModules() const override;
|
||||
std::unordered_map<std::string, std::uint16_t>
|
||||
getLoadedModules() const override;
|
||||
MscEngineModule &getModule(const std::string &name) override;
|
||||
std::unique_ptr<MscEngineDBProvider> createDBProvider() override;
|
||||
std::unique_ptr<MscEngineDBProvider> createDBProvider(const std::string &filepath) override;
|
||||
std::unique_ptr<MscEngineDBProvider>
|
||||
createDBProvider(const std::string &filepath) override;
|
||||
};
|
||||
|
||||
#endif // __MCS_ENGINE_HPP__
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __MCS_ENGINE_MODULE_HPP__
|
||||
#define __MCS_ENGINE_MODULE_HPP__
|
||||
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -34,7 +34,8 @@ public:
|
||||
/// @brief Engine update cycle step
|
||||
/// @param engine engine public API for modules
|
||||
/// @param dt loop delta
|
||||
virtual void onUpdate(IMscEngineModuleAPI &engine, float dt) = 0;
|
||||
virtual void onUpdate(IMscEngineModuleAPI &engine,
|
||||
IMscEngineModuleAPI::clock::duration dt) = 0;
|
||||
|
||||
/// @brief Engine stop event handler
|
||||
/// @param engine engine public API for modules
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#ifndef __MSC_ENGINE_MODULE_API_HPP__
|
||||
#define __MSC_ENGINE_MODULE_API_HPP__
|
||||
|
||||
#include <cstdint>
|
||||
#include <engine/core/engine_bus.hpp>
|
||||
#include <engine/core/engine_db_provider.hpp>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
class MscEngineModule;
|
||||
|
||||
class IMscEngineModuleAPI
|
||||
{
|
||||
public:
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
virtual ~IMscEngineModuleAPI() = default;
|
||||
|
||||
/// @brief Get access to engine event bus
|
||||
virtual MscEventBus &getBus() = 0;
|
||||
|
||||
/// @brief Get access to loaded module list
|
||||
virtual std::unordered_map<std::string, std::uint16_t> getLoadedModules() const = 0;
|
||||
virtual std::unordered_map<std::string, std::uint16_t>
|
||||
getLoadedModules() const = 0;
|
||||
|
||||
/// @brief Access module by its name
|
||||
virtual MscEngineModule &getModule(const std::string &name) = 0;
|
||||
@@ -28,7 +31,8 @@ public:
|
||||
virtual std::unique_ptr<MscEngineDBProvider> createDBProvider() = 0;
|
||||
|
||||
/// @brief Create database provider with file database
|
||||
virtual std::unique_ptr<MscEngineDBProvider> createDBProvider(const std::string &filepath) = 0;
|
||||
virtual std::unique_ptr<MscEngineDBProvider>
|
||||
createDBProvider(const std::string &filepath) = 0;
|
||||
};
|
||||
|
||||
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
||||
|
||||
+42
-10
@@ -1,12 +1,16 @@
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <engine/core/engine.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
uint16_t MscEngine::REV = 0;
|
||||
std::uint16_t MscEngine::REV = 1;
|
||||
|
||||
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules)
|
||||
{
|
||||
@@ -64,13 +68,42 @@ void MscEngine::start()
|
||||
_isRunning = true;
|
||||
_lastUpdate = clock::now();
|
||||
|
||||
double accumulator = 0.0;
|
||||
constexpr double fixedDt = 1.0 / 30.0;
|
||||
for (auto &m : _modules)
|
||||
m->onStart(*this);
|
||||
|
||||
constexpr auto tickDuration = std::chrono::milliseconds(50);
|
||||
auto nextTick = clock::now();
|
||||
|
||||
using UpdateFn = std::function<void(IMscEngineModuleAPI & engine,
|
||||
IMscEngineModuleAPI::clock::duration dt)>;
|
||||
std::vector<UpdateFn> updateFns;
|
||||
updateFns.reserve(_modules.size());
|
||||
|
||||
for (auto &m : _modules)
|
||||
updateFns.emplace_back([m](IMscEngineModuleAPI &engine,
|
||||
IMscEngineModuleAPI::clock::duration dt)
|
||||
{ m->onUpdate(engine, dt); });
|
||||
|
||||
while (_isRunning)
|
||||
{
|
||||
//
|
||||
nextTick += tickDuration;
|
||||
const auto currentTick = clock::now();
|
||||
const auto dt = currentTick - _lastUpdate;
|
||||
|
||||
// TODO: process
|
||||
|
||||
for (auto upd : updateFns)
|
||||
upd(*this, dt);
|
||||
|
||||
_lastUpdate = currentTick;
|
||||
|
||||
std::this_thread::sleep_until(nextTick);
|
||||
if (clock::now() > nextTick + std::chrono::seconds(1))
|
||||
nextTick = clock::now();
|
||||
}
|
||||
|
||||
for (auto &m : _modules)
|
||||
m->onStop(*this);
|
||||
}
|
||||
|
||||
void MscEngine::stop()
|
||||
@@ -81,12 +114,10 @@ void MscEngine::stop()
|
||||
_isRunning = false;
|
||||
}
|
||||
|
||||
MscEventBus &MscEngine::getBus()
|
||||
{
|
||||
return _bus;
|
||||
}
|
||||
MscEventBus &MscEngine::getBus() { return _bus; }
|
||||
|
||||
std::unordered_map<std::string, std::uint16_t> MscEngine::getLoadedModules() const
|
||||
std::unordered_map<std::string, std::uint16_t>
|
||||
MscEngine::getLoadedModules() const
|
||||
{
|
||||
std::unordered_map<std::string, std::uint16_t> result;
|
||||
for (const auto &module : _modules)
|
||||
@@ -117,7 +148,8 @@ std::unique_ptr<MscEngineDBProvider> MscEngine::createDBProvider()
|
||||
return std::make_unique<MscEngineDBProvider>();
|
||||
}
|
||||
|
||||
std::unique_ptr<MscEngineDBProvider> MscEngine::createDBProvider(const std::string &filepath)
|
||||
std::unique_ptr<MscEngineDBProvider>
|
||||
MscEngine::createDBProvider(const std::string &filepath)
|
||||
{
|
||||
return std::make_unique<MscEngineDBProvider>(filepath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user