60 lines
919 B
C++
60 lines
919 B
C++
|
|
#include <iostream>
|
|
#include <Spectre/Game/GameTime.h>
|
|
#include <Spectre/System/System.h>
|
|
|
|
namespace sp {
|
|
|
|
GameTime::GameTime(unsigned long updates_per_sec)
|
|
{
|
|
setTimeStep(updates_per_sec);
|
|
}
|
|
|
|
double GameTime::getTimeStep() const
|
|
{
|
|
return m_slice.milliseconds();
|
|
}
|
|
|
|
void GameTime::setTimeStep(unsigned long updates_per_sec)
|
|
{
|
|
m_slice = Time::seconds(1.0f / ((double) updates_per_sec));
|
|
|
|
reset();
|
|
}
|
|
|
|
Time GameTime::getElapsed() const
|
|
{
|
|
return m_watch.elapsed();
|
|
}
|
|
|
|
bool GameTime::beginFrame()
|
|
{
|
|
accumulateTime();
|
|
|
|
// Signal if we should tick
|
|
return m_acc > m_slice;
|
|
}
|
|
|
|
bool GameTime::tick()
|
|
{
|
|
if (m_acc > m_slice) {
|
|
m_acc -= m_slice;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void GameTime::reset()
|
|
{
|
|
m_watch.restart();
|
|
}
|
|
|
|
void GameTime::accumulateTime()
|
|
{
|
|
m_acc += m_watch.restart();
|
|
if (m_acc.seconds() > (1.0f / m_slice.seconds())) {
|
|
m_acc = Time::seconds(1.0f / m_slice.seconds());
|
|
}
|
|
}
|
|
|
|
} // namespace sp
|