Core/ApplicationBase: Add support for updaters with intervals
This commit is contained in:
parent
a55560d1ca
commit
de5e7bd8a8
|
|
@ -1140,7 +1140,7 @@ int main()
|
|||
}
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time deltaTime)
|
||||
app.AddUpdaterFunc([&](Nz::Time deltaTime)
|
||||
{
|
||||
if (lightAnimation)
|
||||
elapsedTime += deltaTime;
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ int main()
|
|||
sound.Play();
|
||||
|
||||
Nz::MillisecondClock clock;
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / 30));
|
||||
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ int main()
|
|||
}
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ int main()
|
|||
Nz::PidController<Nz::Vector3f> headingController(0.5f, 0.f, 0.05f);
|
||||
Nz::PidController<Nz::Vector3f> upController(1.f, 0.f, 0.1f);
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
fps++;
|
||||
|
||||
|
|
|
|||
|
|
@ -256,7 +256,7 @@ int main()
|
|||
headingEntity.get<Nz::NodeComponent>().SetRotation(camQuat);
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -509,7 +509,7 @@ int main()
|
|||
});
|
||||
|
||||
Nz::DegreeAnglef rotation = 0.f;
|
||||
app.AddUpdater([&](Nz::Time elapsedTime)
|
||||
app.AddUpdaterFunc([&](Nz::Time elapsedTime)
|
||||
{
|
||||
rotation += elapsedTime.AsSeconds() * 45.f;
|
||||
//physSystem.GetPhysWorld().SetGravity(Nz::Quaternionf(Nz::EulerAnglesf(0.f, rotation, 0.f)) * Nz::Vector3f::Forward() * 10.f);
|
||||
|
|
@ -517,7 +517,7 @@ int main()
|
|||
|
||||
Nz::MillisecondClock fpsClock;
|
||||
unsigned int fps = 0;
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
fps++;
|
||||
if (fpsClock.RestartIfOver(Nz::Time::Second()))
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ int main()
|
|||
|
||||
std::cout << "Playing sound..." << std::endl;
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (!music.IsPlaying())
|
||||
app.Quit();
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ int main()
|
|||
|
||||
character.emplace(physSytem.GetPhysWorld(), playerCollider, Nz::Vector3f::Up() * 5.f);
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
auto [position, rotation] = character->GetPositionAndRotation();
|
||||
|
||||
|
|
@ -417,7 +417,7 @@ int main()
|
|||
playerRotNode.SetRotation(camAngles);
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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) :
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
namespace Nz
|
||||
{
|
||||
ApplicationBase::ApplicationBase(int argc, const Pointer<const char>* argv) :
|
||||
m_running(true)
|
||||
m_running(true),
|
||||
m_currentTime(Time::Zero())
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -55,8 +56,30 @@ namespace Nz
|
|||
|
||||
bool ApplicationBase::Update(Time elapsedTime)
|
||||
{
|
||||
for (auto& updater : m_updaters)
|
||||
updater->Update(elapsedTime);
|
||||
m_currentTime += elapsedTime;
|
||||
|
||||
for (auto& updaterEntry : m_updaters)
|
||||
{
|
||||
if (updaterEntry.nextUpdate > m_currentTime)
|
||||
continue;
|
||||
|
||||
Time timeSinceLastUpdate = m_currentTime - updaterEntry.lastUpdate;
|
||||
|
||||
if NAZARA_UNLIKELY(updaterEntry.nextUpdate == updaterEntry.lastUpdate)
|
||||
{
|
||||
// First call
|
||||
timeSinceLastUpdate = Time::Zero();
|
||||
updaterEntry.lastUpdate = m_currentTime;
|
||||
}
|
||||
|
||||
Time interval = updaterEntry.updater->Update(timeSinceLastUpdate);
|
||||
if (interval >= Time::Zero())
|
||||
updaterEntry.nextUpdate = m_currentTime + interval;
|
||||
else
|
||||
updaterEntry.nextUpdate = updaterEntry.lastUpdate + (-interval);
|
||||
|
||||
updaterEntry.nextUpdate = std::max(updaterEntry.nextUpdate, m_currentTime);
|
||||
}
|
||||
|
||||
for (auto& componentPtr : m_components)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ int main()
|
|||
}
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -284,7 +284,7 @@ int main()
|
|||
uboUpdate = true;
|
||||
});
|
||||
|
||||
app.AddUpdater([&](Nz::Time /*elapsedTime*/)
|
||||
app.AddUpdaterFunc([&]
|
||||
{
|
||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
#include <Nazara/Core/Application.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
SCENARIO("Application", "[CORE][ABSTRACTHASH]")
|
||||
{
|
||||
WHEN("Updating the application multiple times")
|
||||
{
|
||||
Nz::ApplicationBase app;
|
||||
|
||||
std::size_t triggerCount = 0;
|
||||
app.AddUpdaterFunc([&](Nz::Time elapsedTime)
|
||||
{
|
||||
if (triggerCount == 0)
|
||||
{
|
||||
INFO("First update should have elapsed time as zero");
|
||||
CHECK(elapsedTime == Nz::Time::Zero());
|
||||
}
|
||||
|
||||
triggerCount++;
|
||||
});
|
||||
|
||||
app.Update(Nz::Time::Milliseconds(10));
|
||||
app.Update(Nz::Time::Milliseconds(10));
|
||||
app.Update(Nz::Time::Milliseconds(10));
|
||||
|
||||
CHECK(triggerCount == 3);
|
||||
}
|
||||
|
||||
WHEN("Using interval")
|
||||
{
|
||||
Nz::ApplicationBase app;
|
||||
|
||||
std::size_t triggerCount = 0;
|
||||
app.AddUpdaterFunc(Nz::ApplicationBase::Interval{ Nz::Time::Milliseconds(100) }, [&](Nz::Time elapsedTime)
|
||||
{
|
||||
if (triggerCount == 0)
|
||||
{
|
||||
INFO("First update should have elapsed time as zero");
|
||||
CHECK(elapsedTime == Nz::Time::Zero());
|
||||
}
|
||||
|
||||
triggerCount++;
|
||||
});
|
||||
|
||||
app.Update(Nz::Time::Milliseconds(100));
|
||||
CHECK(triggerCount == 1);
|
||||
app.Update(Nz::Time::Milliseconds(10));
|
||||
CHECK(triggerCount == 1);
|
||||
app.Update(Nz::Time::Milliseconds(100));
|
||||
CHECK(triggerCount == 2);
|
||||
app.Update(Nz::Time::Milliseconds(90));
|
||||
CHECK(triggerCount == 2); // this does not trigger since 100ms have not elapsed since last update
|
||||
}
|
||||
|
||||
WHEN("Using fixed-time interval")
|
||||
{
|
||||
Nz::ApplicationBase app;
|
||||
|
||||
std::size_t triggerCount = 0;
|
||||
app.AddUpdaterFunc(Nz::ApplicationBase::FixedInterval{ Nz::Time::Milliseconds(100) }, [&](Nz::Time elapsedTime)
|
||||
{
|
||||
CHECK(elapsedTime == Nz::Time::Milliseconds(100));
|
||||
triggerCount++;
|
||||
});
|
||||
|
||||
app.Update(Nz::Time::Milliseconds(100));
|
||||
CHECK(triggerCount == 1);
|
||||
app.Update(Nz::Time::Milliseconds(10));
|
||||
CHECK(triggerCount == 1);
|
||||
app.Update(Nz::Time::Milliseconds(100));
|
||||
CHECK(triggerCount == 2);
|
||||
app.Update(Nz::Time::Milliseconds(90));
|
||||
CHECK(triggerCount == 3); // lost time is caught up
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue