adopt engine stuff to new update strategy

This commit is contained in:
2020-12-26 19:29:39 +01:00
parent c1ae30c89c
commit 840b663d5e
28 changed files with 351 additions and 324 deletions

View File

@ -1,9 +1,11 @@
#include "./simple_scene.hpp"
#include "spdlog/fmt/bundled/core.h"
//#include "../systems_container.hpp"
#include <entt/entity/registry.hpp>
#include <tracy/Tracy.hpp>
#include <mm/logger.hpp>
@ -18,50 +20,61 @@ bool SimpleSceneService::enable(Engine& engine) {
_scene->set<MM::Engine*>(&engine);
}
{
_f_update_handle = engine.addFixedUpdate([this](Engine& e) { this->sceneFixedUpdate(e); });
if (_f_update_handle.expired()) {
return false;
}
auto tmp_lock = _f_update_handle.lock();
tmp_lock->priority = 0;
}
{
_f_update_changer_handle = engine.addFixedUpdate([this](Engine& e) { this->changeSceneFixedUpdate(e); });
if (_f_update_changer_handle.expired()) {
return false;
}
auto tmp_lock = _f_update_changer_handle.lock();
tmp_lock->priority = -100;
}
resetTime();
return true;
}
void SimpleSceneService::disable(Engine& engine) {
if (!_f_update_handle.expired()) {
engine.removeFixedUpdate(_f_update_handle);
_f_update_handle.reset();
}
if (!_f_update_changer_handle.expired()) {
engine.removeFixedUpdate(_f_update_changer_handle);
_f_update_changer_handle.reset();
}
void SimpleSceneService::disable(Engine&) {
}
void SimpleSceneService::sceneFixedUpdate(Engine& engine) {
std::vector<UpdateStrategies::UpdateCreationInfo> SimpleSceneService::registerUpdates(void) {
return {
{
"SimpleSceneService::scene_tick"_hs,
"SimpleSceneService::scene_tick",
[this](Engine& e) { this->sceneFixedUpdate(e); },
UpdateStrategies::update_phase_t::MAIN,
true,
{} // no dependencies"
},
{
"SimpleSceneService::scene_change"_hs,
"SimpleSceneService::scene_change",
[this](Engine& e) { this->changeSceneFixedUpdate(e); },
UpdateStrategies::update_phase_t::MAIN,
true,
{"SimpleSceneService::scene_update"_hs} // first update, than change????
}
};
}
void SimpleSceneService::sceneFixedUpdate(Engine&) {
ZoneScoped;
const auto f_delta = engine.getFixedDeltaTime();
::MM::EachSystemInScene(*_scene, [&](::MM::Scene& s, ::MM::System& fn) {
fn(s, f_delta);
});
auto newNow = clock::now();
auto deltaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(newNow - _last_time);
_last_time = newNow;
_accumulator += deltaTime.count();
const double dt = f_delta * 1'000'000'000.0;
TracyPlot("MM::Services::SimpleSceneService::_scene.alive", (int64_t)_scene->alive());
size_t continuous_counter = 0;
// TODO: this is just cancer
while (_accumulator >= static_cast<decltype(_accumulator)>(dt)){
_accumulator -= static_cast<decltype(_accumulator)>(dt);
continuous_counter++;
::MM::EachSystemInScene(*_scene, [&](::MM::Scene& s, ::MM::System& fn) {
fn(s, f_delta * delta_factor);
});
TracyPlot("MM::Services::SimpleSceneService::_scene.alive", (int64_t)_scene->alive());
}
if (continuous_counter > 2) {
LOG_SSS(fmt::format("had {} contiguous scene ticks!", continuous_counter));
}
}
void SimpleSceneService::changeSceneFixedUpdate(Engine& engine) {
@ -95,5 +108,11 @@ void SimpleSceneService::changeSceneNow(std::unique_ptr<Scene>&& new_scene) {
//}
}
void SimpleSceneService::resetTime(void) {
_last_time = clock::now();
_accumulator = 0;
}
} // MM::Services

View File

@ -2,6 +2,8 @@
#include <mm/services/scene_service_interface.hpp>
#include <chrono>
namespace MM::Services {
// provides an implementation for SceneServiceInterface
@ -10,15 +12,26 @@ namespace MM::Services {
std::unique_ptr<Scene> _scene;
std::unique_ptr<Scene> _next_scene; // enqueued next scene
MM::Engine::FunctionDataHandle _f_update_handle;
MM::Engine::FunctionDataHandle _f_update_changer_handle;
using clock = std::chrono::high_resolution_clock;
long long int _accumulator = 0;
std::chrono::time_point<clock> _last_time;
public:
const float f_delta;
float delta_factor = 1.f;
public:
explicit SimpleSceneService(const float update_delta = 1.f/60.f) : f_delta(update_delta) {}
const char* name(void) override { return "SimpleSceneService"; }
bool enable(Engine& engine) override;
void disable(Engine& engine) override;
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
private:
void sceneFixedUpdate(Engine& engine);
void changeSceneFixedUpdate(Engine& engine);
@ -29,6 +42,8 @@ namespace MM::Services {
// be carefull of that one
void changeSceneNow(std::unique_ptr<Scene>&& new_scene) override;
void resetTime(void);
};
} // MM::Services