Game Engine Architecture

(Ben Green) #1
323

public:
// Call this when the game first starts up.
static void init()
{
s_cyclesPerSecond
= (F32)readHiResTimerFrequency();
}

// Construct a clock.
explicit Clock(F32startTimeSeconds = 0.0f) :
m_timeCycles( secondsToCycles(startTimeSeconds)),
m_timeScale( 1.0f), // default to unscaled
m_isPaused( false) // default to running
{
}

// Return the current time in cycles. NOTE that we do
// not return absolute time measurements in floating
// point seconds, because a 32-bit float doesn’t have
// enough precision. See calcDeltaSeconds().
U64 getTimeCycles() const
{
return m_timeCycles;
}

// Determine the difference between this clock’s
// absolute time and that of another clock, in
// seconds. We only return time deltas as floating
// point seconds, due to the precision limitations of
// a 32-bit float.
F32 calcDeltaSeconds(const Clock& other)
{
U64 dt = m_timeCycles – other.m_timeCycles;
return cyclesToSeconds(dt);
}

// This function should be called once per frame,
// with the real measured frame time delta in seconds.
void update(F32 dtRealSeconds)
{
if (!m_isPaused)
{
U64 dtScaledCycles
= secondsToCycles(
dtRealSeconds * m_timeScale);
m_timeCycles += dtScaledCycles;
}
}

7.5. Measuring and Dealing with Time

Free download pdf