feat: implement module lifecycle
This commit is contained in:
+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