Core/ApplicationBase: Add support for updaters with intervals

This commit is contained in:
SirLynix
2023-04-23 19:45:33 +02:00
parent a55560d1ca
commit de5e7bd8a8
17 changed files with 253 additions and 39 deletions

View File

@@ -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)))
{

View File

@@ -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)))
{

View File

@@ -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
}
}