|
Timer Class
Submitted by |
Here is a fairly simple timer class which gets used pretty often. It tries
to use the Win32 High Performance Counter, and will default to the Windows
Multimedia timer if it doesn't find that. The timer class keeps a member
function pointer to the TimerUpdate function and invokes it in the public
Update function which should be called once per game frame.
|
Currently browsing [Sys_time.zip] (1,630 bytes) - [Sys_time.h] - (1,425 bytes)
// COTD Entry submitted by Gaz Iqbal [devvoid@home.com]
#ifndef VOID_SYS_TIME
#define VOID_SYS_TIME
/*
====================================================
A Simple Timer class which tries to use the
Win32 HighPerformance Timer. Will default to
the Windows Multimedia timer if the High Performance
Timer is not found.
I call the GetCurrent/Frame time functions from a
pair of globally accessible functions which I define
in namespace System{} to hide the implementation of
CTime from all the other game code.
====================================================
*/
class CTime
{
public:
CTime();
~CTime();
void Init();
void Reset();
//Call this onces per game frame
void Update();
//Access funcs
inline const float & GetCurrentTime() const { return m_fCurrentTime; }
inline const float & GetFrameTime() const { return m_fFrameTime; }
private:
typedef float (CTime::* TimeUpdateFunc) ();
TimeUpdateFunc m_pfnUpdate;
float m_fBaseTime;
float m_fLastTime;
float m_fSecsPerTick;
float m_fCurrentTime;
float m_fFrameTime;
//QueryPerformancesCounter related 64bit integers.
//These are Microsoft specific, and will have to be changed for
//different compilers.
_int64 m_dTimerStart;
_int64 m_dCurTime;
//One of these gets bound to the TimeUpdateFunc pointer
float GetPerformanceCounterTime();
float GetMMTime();
};
#endif |
|
Currently browsing [Sys_time.zip] (1,630 bytes) - [Sys_time.cpp] - (2,203 bytes)
// COTD Entry submitted by Gaz Iqbal [devvoid@home.com]
#include "Sys_hdr.h"
#include <mmsystem.h>
#include "Sys_time.h"
/*
=====================================
Constructor/Destructor
=====================================
*/
CTime::CTime() :
m_pfnUpdate(0),
m_fBaseTime(0.0f),
m_fLastTime(0.0f),
m_fSecsPerTick(0.0f),
m_fCurrentTime(0.0f),
m_fFrameTime(0.0f),
m_dTimerStart(0),
m_dCurTime(0)
{
}
CTime::~CTime()
{ m_pfnUpdate = 0;
}
/*
=====================================
Initialize the timer
=====================================
*/
void CTime::Init()
{
_int64 dTicksPerSec =0;
if (QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER *>(&dTicksPerSec)))
{
// performance counter is available, use it instead of multimedia timer
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&m_dTimerStart));
m_fSecsPerTick = (1.0f)/(static_cast<float>(dTicksPerSec));
m_pfnUpdate = &CTime::GetPerformanceCounterTime;
}
else
{
//Use MM timer if unable to use the High Frequency timer
m_dTimerStart = timeGetTime();
m_fSecsPerTick = 1.0f/1000.0f;
m_pfnUpdate = &CTime::GetMMTime;
}
}
/*
================================================
Update time
================================================
*/
void CTime::Update()
{
//The ->* operator binds the function pointer to the object pointed to by
//the right hand pointer. which is THIS below
m_fCurrentTime = (this->*m_pfnUpdate)() - m_fBaseTime;
m_fFrameTime = m_fCurrentTime - m_fLastTime;
m_fLastTime = m_fCurrentTime;
}
/*
=====================================
Reset the Timer
=====================================
*/
void CTime::Reset()
{
m_fBaseTime = (this->*m_pfnUpdate)();
m_fLastTime = m_fCurrentTime = m_fFrameTime = 0.0;
}
/*
=====================================
Time update funcs
=====================================
*/
float CTime::GetPerformanceCounterTime()
{
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&m_dCurTime));
return (m_fSecsPerTick * (m_dCurTime - m_dTimerStart));
}
float CTime::GetMMTime()
{ return (m_fSecsPerTick * (timeGetTime()- m_dTimerStart));
} |
|
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
|