1
0
Fork 0

Spectre/Game/GameTime: Use sp::Time and sp::Stopwatch

This commit is contained in:
Henrik Hautakoski 2020-12-16 16:36:19 +01:00
parent 6ee1752735
commit d49d91a294
2 changed files with 18 additions and 31 deletions

View file

@ -7,32 +7,29 @@ namespace sp {
GameTime::GameTime(unsigned long updates_per_sec)
{
m_acc = 0;
m_current = system::getMilliseconds();
m_lastUpdate = m_current;
setTimeStep(updates_per_sec);
}
double GameTime::getTimeStep() const
{
return m_slice;
return m_slice.milliseconds();
}
void GameTime::setTimeStep(unsigned long updates_per_sec)
{
m_slice = (1000.0f / ((double) updates_per_sec));
m_slice = Time::seconds(1.0f / ((double) updates_per_sec));
reset();
}
unsigned long GameTime::getElapsed() const
Time GameTime::getElapsed() const
{
return m_current - m_lastUpdate;
return m_watch.elapsed();
}
bool GameTime::beginFrame()
{
reset();
m_inLoop = false;
accumulateTime();
@ -53,27 +50,15 @@ bool GameTime::tick()
void GameTime::reset()
{
m_lastUpdate = m_current;
m_watch.restart();
m_inLoop = false;
}
void GameTime::accumulateTime()
{
updateTime();
m_acc += (double) (m_current - m_lastUpdate);
if (m_acc > (1000.f / m_slice)) {
m_acc = 1000.f / m_slice;
}
}
void GameTime::updateTime()
{
m_current = system::getMilliseconds();
// Just to be safe. check so we don't get a negative interval.
if (m_current < m_lastUpdate) {
m_lastUpdate = m_current;
m_acc += m_watch.restart();
if (m_acc.seconds() > (1.0f / m_slice.seconds())) {
m_acc = Time::seconds(1.0f / m_slice.seconds());
}
}