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

@ -2,6 +2,9 @@
#ifndef SPECTRE_GAME_TIME_H #ifndef SPECTRE_GAME_TIME_H
#define SPECTRE_GAME_TIME_H #define SPECTRE_GAME_TIME_H
#include <Spectre/Math/Time.h>
#include <Spectre/System/Stopwatch.h>
namespace sp { namespace sp {
class GameTime class GameTime
@ -13,7 +16,7 @@ public :
double getTimeStep() const; double getTimeStep() const;
unsigned long getElapsed() const; Time getElapsed() const;
//bool shouldTick(); //bool shouldTick();
@ -33,13 +36,12 @@ protected :
// TODO: min,max framerates // TODO: min,max framerates
// Time(in ms) when the last update occured. Stopwatch m_watch;
unsigned long m_current;
unsigned long m_lastUpdate;
double m_acc;
// timeslice in ms. Time m_acc;
double m_slice;
// Timeslice.
Time m_slice;
bool m_inLoop; bool m_inLoop;
}; };

View file

@ -7,32 +7,29 @@ namespace sp {
GameTime::GameTime(unsigned long updates_per_sec) GameTime::GameTime(unsigned long updates_per_sec)
{ {
m_acc = 0;
m_current = system::getMilliseconds();
m_lastUpdate = m_current;
setTimeStep(updates_per_sec); setTimeStep(updates_per_sec);
} }
double GameTime::getTimeStep() const double GameTime::getTimeStep() const
{ {
return m_slice; return m_slice.milliseconds();
} }
void GameTime::setTimeStep(unsigned long updates_per_sec) 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(); reset();
} }
unsigned long GameTime::getElapsed() const Time GameTime::getElapsed() const
{ {
return m_current - m_lastUpdate; return m_watch.elapsed();
} }
bool GameTime::beginFrame() bool GameTime::beginFrame()
{ {
reset(); m_inLoop = false;
accumulateTime(); accumulateTime();
@ -53,27 +50,15 @@ bool GameTime::tick()
void GameTime::reset() void GameTime::reset()
{ {
m_lastUpdate = m_current; m_watch.restart();
m_inLoop = false; m_inLoop = false;
} }
void GameTime::accumulateTime() void GameTime::accumulateTime()
{ {
updateTime(); m_acc += m_watch.restart();
if (m_acc.seconds() > (1.0f / m_slice.seconds())) {
m_acc += (double) (m_current - m_lastUpdate); m_acc = Time::seconds(1.0f / m_slice.seconds());
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;
} }
} }