1
0
Fork 0
spectre/source/Game/GameTime.cpp

65 lines
968 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()
{
m_inLoop = false;
accumulateTime();
if (m_acc > m_slice) {
m_inLoop = true;
}
return m_inLoop;
}
bool GameTime::tick()
{
if (m_acc > m_slice) {
m_acc -= m_slice;
return true;
}
return false;
}
void GameTime::reset()
{
m_watch.restart();
m_inLoop = false;
}
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