diff --git a/include/Spectre/Game/GameTime.h b/include/Spectre/Game/GameTime.h index c1d322b..aa85f17 100644 --- a/include/Spectre/Game/GameTime.h +++ b/include/Spectre/Game/GameTime.h @@ -2,6 +2,9 @@ #ifndef SPECTRE_GAME_TIME_H #define SPECTRE_GAME_TIME_H +#include +#include + namespace sp { class GameTime @@ -13,7 +16,7 @@ public : double getTimeStep() const; - unsigned long getElapsed() const; + Time getElapsed() const; //bool shouldTick(); @@ -33,13 +36,12 @@ protected : // TODO: min,max framerates - // Time(in ms) when the last update occured. - unsigned long m_current; - unsigned long m_lastUpdate; - double m_acc; + Stopwatch m_watch; - // timeslice in ms. - double m_slice; + Time m_acc; + + // Timeslice. + Time m_slice; bool m_inLoop; }; diff --git a/source/Game/GameTime.cpp b/source/Game/GameTime.cpp index f5a1d18..fa98d35 100644 --- a/source/Game/GameTime.cpp +++ b/source/Game/GameTime.cpp @@ -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()); } }