Core/ApplicationBase: Add support for updaters with intervals
This commit is contained in:
@@ -19,6 +19,9 @@ namespace Nz
|
||||
class NAZARA_CORE_API ApplicationBase
|
||||
{
|
||||
public:
|
||||
struct FixedInterval { Time interval; };
|
||||
struct Interval { Time interval; };
|
||||
|
||||
inline ApplicationBase();
|
||||
inline ApplicationBase(int argc, char** argv);
|
||||
ApplicationBase(int argc, const Pointer<const char>* argv);
|
||||
@@ -26,7 +29,10 @@ namespace Nz
|
||||
ApplicationBase(ApplicationBase&&) = delete;
|
||||
~ApplicationBase() = default;
|
||||
|
||||
template<typename F> void AddUpdater(F&& functor);
|
||||
inline void AddUpdater(std::unique_ptr<ApplicationUpdater>&& functor);
|
||||
template<typename F> void AddUpdaterFunc(F&& functor);
|
||||
template<typename F> void AddUpdaterFunc(FixedInterval fixedInterval, F&& functor);
|
||||
template<typename F> void AddUpdaterFunc(Interval interval, F&& functor);
|
||||
|
||||
inline void ClearComponents();
|
||||
|
||||
@@ -46,10 +52,20 @@ namespace Nz
|
||||
template<typename T, typename... Args> T& AddComponent(Args&&... args);
|
||||
|
||||
private:
|
||||
template<typename F, bool FixedInterval> void AddUpdaterFunc(Time interval, F&& functor);
|
||||
|
||||
struct Updater
|
||||
{
|
||||
std::unique_ptr<ApplicationUpdater> updater;
|
||||
Time lastUpdate;
|
||||
Time nextUpdate;
|
||||
};
|
||||
|
||||
std::atomic_bool m_running;
|
||||
std::vector<std::unique_ptr<ApplicationComponent>> m_components;
|
||||
std::vector<std::unique_ptr<ApplicationUpdater>> m_updaters;
|
||||
std::vector<Updater> m_updaters;
|
||||
HighPrecisionClock m_clock;
|
||||
Time m_currentTime;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -18,28 +18,31 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T& ApplicationBase::AddComponent(Args&&... args)
|
||||
inline void ApplicationBase::AddUpdater(std::unique_ptr<ApplicationUpdater>&& functor)
|
||||
{
|
||||
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
|
||||
|
||||
std::unique_ptr<T> component = std::make_unique<T>(*this, std::forward<Args>(args)...);
|
||||
T& componentRef = *component;
|
||||
|
||||
if (componentIndex >= m_components.size())
|
||||
m_components.resize(componentIndex + 1);
|
||||
else if (m_components[componentIndex] != nullptr)
|
||||
throw std::runtime_error("component was added multiple times");
|
||||
|
||||
m_components[componentIndex] = std::move(component);
|
||||
|
||||
return componentRef;
|
||||
auto& updaterEntry = m_updaters.emplace_back();
|
||||
updaterEntry.lastUpdate = Time::Zero();
|
||||
updaterEntry.nextUpdate = Time::Zero();
|
||||
updaterEntry.updater = std::move(functor);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void ApplicationBase::AddUpdater(F&& functor)
|
||||
void ApplicationBase::AddUpdaterFunc(F&& functor)
|
||||
{
|
||||
m_updaters.emplace_back(std::make_unique<ApplicationUpdaterFunctor<std::decay_t<F>>>(std::forward<F>(functor)));
|
||||
static_assert(std::is_invocable_v<F> || std::is_invocable_v<F, Time>, "functor must be callable with either a Time parameter or no parameter");
|
||||
return AddUpdater(std::make_unique<ApplicationUpdaterFunctor<std::decay_t<F>>>(std::forward<F>(functor)));
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void ApplicationBase::AddUpdaterFunc(FixedInterval fixedInterval, F&& functor)
|
||||
{
|
||||
return AddUpdaterFunc<F, true>(fixedInterval.interval, std::forward<F>(functor));
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void ApplicationBase::AddUpdaterFunc(Interval interval, F&& functor)
|
||||
{
|
||||
return AddUpdaterFunc<F, false>(interval.interval, std::forward<F>(functor));
|
||||
}
|
||||
|
||||
inline void ApplicationBase::ClearComponents()
|
||||
@@ -71,6 +74,36 @@ namespace Nz
|
||||
{
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T& ApplicationBase::AddComponent(Args&&... args)
|
||||
{
|
||||
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
|
||||
|
||||
std::unique_ptr<T> component = std::make_unique<T>(*this, std::forward<Args>(args)...);
|
||||
T& componentRef = *component;
|
||||
|
||||
if (componentIndex >= m_components.size())
|
||||
m_components.resize(componentIndex + 1);
|
||||
else if (m_components[componentIndex] != nullptr)
|
||||
throw std::runtime_error("component was added multiple times");
|
||||
|
||||
m_components[componentIndex] = std::move(component);
|
||||
|
||||
return componentRef;
|
||||
}
|
||||
|
||||
template<typename F, bool FixedInterval>
|
||||
void ApplicationBase::AddUpdaterFunc(Time interval, F&& functor)
|
||||
{
|
||||
if constexpr (std::is_invocable_r_v<void, F> || std::is_invocable_r_v<void, F, Time>)
|
||||
return AddUpdater(std::make_unique<ApplicationUpdaterFunctorWithInterval<std::decay_t<F>, FixedInterval>>(std::forward<F>(functor), interval));
|
||||
else if constexpr (std::is_invocable_r_v<Time, F> || std::is_invocable_r_v<Time, F, Time>)
|
||||
return AddUpdater(std::make_unique<ApplicationUpdaterFunctor<std::decay_t<F>>>(std::forward<F>(functor)));
|
||||
else
|
||||
static_assert(AlwaysFalse<F>(), "functor must be callable with either elapsed time or nothing and return void or next update time");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
||||
@@ -21,21 +21,36 @@ namespace Nz
|
||||
ApplicationUpdater(ApplicationUpdater&&) = delete;
|
||||
virtual ~ApplicationUpdater();
|
||||
|
||||
virtual void Update(Time elapsedTime) = 0;
|
||||
virtual Time Update(Time elapsedTime) = 0;
|
||||
|
||||
ApplicationUpdater& operator=(const ApplicationUpdater&) = delete;
|
||||
ApplicationUpdater& operator=(ApplicationUpdater&&) = delete;
|
||||
};
|
||||
|
||||
|
||||
template<typename F>
|
||||
class ApplicationUpdaterFunctor : public ApplicationUpdater
|
||||
{
|
||||
public:
|
||||
ApplicationUpdaterFunctor(F functor);
|
||||
|
||||
void Update(Time elapsedTime) override;
|
||||
Time Update(Time elapsedTime) override;
|
||||
|
||||
private:
|
||||
template<typename... Args> Time TriggerFunctor(Args&&... args);
|
||||
|
||||
F m_functor;
|
||||
};
|
||||
|
||||
template<typename F, bool FixedInterval>
|
||||
class ApplicationUpdaterFunctorWithInterval : public ApplicationUpdater
|
||||
{
|
||||
public:
|
||||
ApplicationUpdaterFunctorWithInterval(F functor, Time interval);
|
||||
|
||||
Time Update(Time elapsedTime) override;
|
||||
|
||||
private:
|
||||
Time m_interval;
|
||||
F m_functor;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,9 +13,59 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void ApplicationUpdaterFunctor<F>::Update(Time elapsedTime)
|
||||
Time ApplicationUpdaterFunctor<F>::Update(Time elapsedTime)
|
||||
{
|
||||
m_functor(elapsedTime);
|
||||
if constexpr (std::is_invocable_v<F, Time>)
|
||||
return TriggerFunctor(elapsedTime);
|
||||
else if constexpr (std::is_invocable_v<F>)
|
||||
return TriggerFunctor();
|
||||
else
|
||||
static_assert(AlwaysFalse<F>(), "updater functor must be callable with a Time or nothing");
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
template<typename... Args>
|
||||
Time ApplicationUpdaterFunctor<F>::TriggerFunctor(Args&&... args)
|
||||
{
|
||||
if constexpr (std::is_same_v<std::invoke_result_t<F, Args...>, Time>)
|
||||
return m_functor(std::forward<Args>(args)...);
|
||||
else if constexpr (std::is_same_v<std::invoke_result_t<F, Args...>, void>)
|
||||
{
|
||||
m_functor(std::forward<Args>(args)...);
|
||||
return Time::Zero();
|
||||
}
|
||||
else
|
||||
static_assert(AlwaysFalse<F>(), "updater functor must either return Time or void");
|
||||
}
|
||||
|
||||
|
||||
template<typename F, bool FixedInterval>
|
||||
ApplicationUpdaterFunctorWithInterval<F, FixedInterval>::ApplicationUpdaterFunctorWithInterval(F functor, Time interval) :
|
||||
m_interval(interval),
|
||||
m_functor(std::move(functor))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename F, bool FixedInterval>
|
||||
Time ApplicationUpdaterFunctorWithInterval<F, FixedInterval>::Update(Time elapsedTime)
|
||||
{
|
||||
if constexpr (std::is_invocable_v<F, Time>)
|
||||
{
|
||||
if constexpr (FixedInterval)
|
||||
m_functor(m_interval);
|
||||
else
|
||||
m_functor(elapsedTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(std::is_invocable_v<F>, "updater functor must be callable with a Time or nothing");
|
||||
m_functor();
|
||||
}
|
||||
|
||||
if constexpr (FixedInterval)
|
||||
return -m_interval;
|
||||
else
|
||||
return m_interval;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace Nz
|
||||
return ModuleTuple<Modules...>::template Get<T>();
|
||||
}
|
||||
|
||||
|
||||
template<typename Module>
|
||||
template<typename... ModuleConfig>
|
||||
ModuleTuple<Module>::ModuleTuple(ModuleConfig&&... configs) :
|
||||
|
||||
Reference in New Issue
Block a user