Core/Clock: Add Tick method
This commit is contained in:
parent
8efebef26c
commit
f2201404f3
|
|
@ -32,6 +32,8 @@ namespace Nz
|
||||||
Time Restart(Time startingPoint = Time::Zero(), bool paused = false);
|
Time Restart(Time startingPoint = Time::Zero(), bool paused = false);
|
||||||
std::optional<Time> RestartIfOver(Time time);
|
std::optional<Time> RestartIfOver(Time time);
|
||||||
|
|
||||||
|
bool Tick(Time time);
|
||||||
|
|
||||||
void Unpause();
|
void Unpause();
|
||||||
|
|
||||||
Clock& operator=(const Clock& clock) = default;
|
Clock& operator=(const Clock& clock) = default;
|
||||||
|
|
|
||||||
|
|
@ -97,8 +97,10 @@ namespace Nz
|
||||||
* \brief Restart the clock if more than time elapsed
|
* \brief Restart the clock if more than time elapsed
|
||||||
* \return If more than time elapsed since creation or last restart call
|
* \return If more than time elapsed since creation or last restart call
|
||||||
*
|
*
|
||||||
* Restarts the clock, putting its time counter back to zero
|
* Restarts the clock if over a specified duration, putting its time counter back to zero
|
||||||
* This function allows to check the elapsed time of a clock and restart it if over some value in a single call, preventing some loss between GetElapsedTime and Restart
|
* This function allows to check the elapsed time of a clock and restart it if over some value in a single call, preventing some loss between GetElapsedTime and Restart
|
||||||
|
*
|
||||||
|
* \seealso Tick
|
||||||
*/
|
*/
|
||||||
template<bool HighPrecision>
|
template<bool HighPrecision>
|
||||||
std::optional<Time> Clock<HighPrecision>::RestartIfOver(Time time)
|
std::optional<Time> Clock<HighPrecision>::RestartIfOver(Time time)
|
||||||
|
|
@ -118,6 +120,33 @@ namespace Nz
|
||||||
return elapsedTime;
|
return elapsedTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Restart the clock if more than time elapsed, but keeps the overflow
|
||||||
|
* \return True if more time has passed than time, false otherwise
|
||||||
|
*
|
||||||
|
* Restarts the clock if over a specified duration, but keeps the extra time (for example if elapsed time is 1.05s and this function is called with 1s, the clock will restart at 0.05s)
|
||||||
|
* This function allows to execute a code at periodic interval (tick) without losing time
|
||||||
|
*
|
||||||
|
* \seealso RestartIfOver
|
||||||
|
*/
|
||||||
|
template<bool HighPrecision>
|
||||||
|
bool Clock<HighPrecision>::Tick(Time time)
|
||||||
|
{
|
||||||
|
Time now = Now();
|
||||||
|
|
||||||
|
Time elapsedTime = m_elapsedTime;
|
||||||
|
if (!m_paused)
|
||||||
|
elapsedTime += now - m_refTime;
|
||||||
|
|
||||||
|
if (elapsedTime < time)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_elapsedTime = elapsedTime - time;
|
||||||
|
m_refTime = now;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Unpause the clock
|
* \brief Unpause the clock
|
||||||
|
|
|
||||||
|
|
@ -48,5 +48,20 @@ SCENARIO("Clock", "[CORE][CLOCK]")
|
||||||
CHECK(clock.GetElapsedTime() != initialTime);
|
CHECK(clock.GetElapsedTime() != initialTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WHEN("We restart if over")
|
||||||
|
{
|
||||||
|
clock.Restart(Nz::Time::Milliseconds(1'500), true);
|
||||||
|
CHECK(clock.RestartIfOver(Nz::Time::Microseconds(1'000)) == Nz::Time::Milliseconds(1'500));
|
||||||
|
CHECK(clock.GetElapsedTime() == Nz::Time::Zero());
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("We tick it")
|
||||||
|
{
|
||||||
|
clock.Restart(Nz::Time::Milliseconds(1'500), true);
|
||||||
|
CHECK(clock.Tick(Nz::Time::Milliseconds(1'000)));
|
||||||
|
CHECK(clock.GetElapsedTime() == Nz::Time::Milliseconds(500));
|
||||||
|
CHECK_FALSE(clock.Tick(Nz::Time::Milliseconds(1'000)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue