UpdateStrategy Refactor: 6. draft, now looks good, not fully tested

This commit is contained in:
2020-12-12 16:55:27 +01:00
parent bab5552e6f
commit c1ae30c89c
18 changed files with 990 additions and 290 deletions

View File

@@ -1,5 +1,7 @@
#pragma once
#include "./engine_fwd.hpp"
#include <entt/core/family.hpp>
#include <functional>
@@ -12,6 +14,7 @@
#include <cassert>
#include <mm/services/service.hpp>
#include <mm/update_strategies/update_strategy.hpp>
namespace MM {
@@ -22,63 +25,38 @@ class Engine {
public:
using service_family_type = service_family::family_type;
// the services "internal" interface
//private:
protected:
std::unique_ptr<UpdateStrategies::UpdateStrategy> _update_strategy;
public:
struct FunctionPriorityDataStructure {
std::function<void(Engine&)> f;
int16_t priority = 0; // 0 is equal to scene update, the higher the prio the earlier
std::string name;
explicit FunctionPriorityDataStructure(std::function<void(Engine&)> fun) : f(fun) {}
FunctionPriorityDataStructure(const FunctionPriorityDataStructure& rhs)
: f(rhs.f), priority(rhs.priority), name(rhs.name) {}
};
using FunctionDataType = FunctionPriorityDataStructure;
//using FunctionDataHandle = FunctionDataType*; // important: its a pointer
using FunctionDataHandle = std::weak_ptr<FunctionDataType>; // important: its a pointer
// return nullptr on error
[[nodiscard]] FunctionDataHandle addUpdate(std::function<void(Engine&)> function);
[[nodiscard]] FunctionDataHandle addFixedUpdate(std::function<void(Engine&)> function);
void removeUpdate(FunctionDataHandle handle);
void removeFixedUpdate(FunctionDataHandle handle);
// dont use, if you are not using it to modify the engine.
// you usualy dont need to use this, if you think you need to use this, you probably dont.
void addFixedDefer(std::function<void(Engine&)> function);
private:
std::vector<std::shared_ptr<FunctionDataType>> _update_functions;
bool _update_functions_modified;
std::vector<std::shared_ptr<FunctionDataType>> _fixed_update_functions;
bool _fixed_update_functions_modified;
std::vector<std::function<void(Engine&)>> _fixed_defered;
private:
void traverseUpdateFunctions(std::vector<std::shared_ptr<FunctionDataType>>& list); // traverses an update list, gets called by update()/fixedUpdate()
UpdateStrategies::UpdateStrategy& getUpdateStrategy(void) { return *_update_strategy; }
private:
volatile bool _is_running = false;
const float _fixed_delta_time;
private:
void setup(void);
public:
Engine(void);
explicit Engine(std::unique_ptr<UpdateStrategies::UpdateStrategy> us) {
setup();
_update_strategy = std::move(us);
}
public:
explicit Engine(float f_delta_time = 1.f/60.f);
~Engine(void);
// called from destructor or explicitly
void cleanup(void);
[[nodiscard]] float getFixedDeltaTime(void) const { return _fixed_delta_time; };
//[[nodiscard]] float getFixedDeltaTime(void) const { return _fixed_delta_time; };
void update(void);
void fixedUpdate(void);
//void fixedUpdate(void);
void run(void); // calls update()/fixedUpdate() until stopped
void run(void); // calls update() until stopped
void stop(void);
private:
@@ -94,9 +72,6 @@ class Engine {
>>
> _services;
// maps I to T
//std::unordered_map<service_family::family_type, service_family::family_type> _implementation_provider;
public:
template<typename T>
constexpr auto type(void) {
@@ -117,6 +92,12 @@ class Engine {
_service_add_order.emplace_back(service_family::type<T>);
// add updates to update strategy
_update_strategy->registerService(
service_family::type<T>,
ss_entry.get()->second->registerUpdates()
);
return (T&)*ss_entry.get()->second.get();
}