Here's some source code I wrote for timers in an engine I made. The engine used SDL and so the class had support for SDL. This can be easily removed.
Timer.h:
Code:
#if defined(_MSC_VER) || defined(_BORLANDC_)
#define u64 __int64
#define WINDOWS
#elif defined(__LINUX__) || defined(__MACOSX__)
#define u64 unsigned long long
#define LINUX
#elif defined(__MINGW32__)
#define u64 unsigned long long
#define WINDOWS
#endif
class CTimer {
public:
CTimer();
CTimer(unsigned int type);
CTimer(CTimer& oldTimer);
~CTimer() { }
void SetTimerType(unsigned int newType);
unsigned int GetTimerType();
void SetUpdateInterval(float newInterval);
float GetUpdateInterval();
void Reset();
float GetFPS();
float GetTime();
private:
void InitTimer();
float timeAtStart, lastUpdate, updateInterval, fps;
u64 ticksPerSecond;
unsigned int timerType, frames;
};
enum Timers {
Z_TIMER_SDL, //Uses SDL_GetTicks() for time
Z_TIMER_QPC, //Uses QueryPerformanceCounter() for time (Windows Only)
};
Timer.cpp:
Code:
#include "Timers.h"
#include <SDL.h>
#if defined (WINDOWS)
#include <afxwin.h>
#endif
CTimer::CTimer() : timerType(Z_TIMER_SDL) {
//We Default To SDL Timers, Then Reset Timers
Reset();
}
CTimer::CTimer(unsigned int type) {
timerType = Z_TIMER_SDL;
timerType = type;
Reset();
}
CTimer::CTimer(CTimer& oldTimer) {
//Copy Old Data Into This Class
timerType = oldTimer.timerType;
timeAtStart = oldTimer.timeAtStart;
ticksPerSecond = oldTimer.ticksPerSecond;
timerType = oldTimer.timerType;
lastUpdate = oldTimer.lastUpdate;
fps = oldTimer.fps;
updateInterval = oldTimer.updateInterval;
//Do Timer Initialization
InitTimer();
}
void CTimer::SetTimerType(unsigned int newType) {
if(newType != Z_TIMER_SDL || newType != Z_TIMER_QPC)
newType = Z_TIMER_SDL;
timerType = newType;
timerType = Z_TIMER_SDL;
//Initialize The Timer
InitTimer();
}
unsigned int CTimer::GetTimerType() {
return (timerType);
}
void CTimer::SetUpdateInterval(float newInterval) {
updateInterval = newInterval;
}
float CTimer::GetUpdateInterval() {
return updateInterval;
}
float CTimer::GetFPS() {
frames++;
float currentUpdate = GetTime();
if(currentUpdate - lastUpdate > updateInterval) {
fps = frames / (currentUpdate - lastUpdate);
lastUpdate = currentUpdate;
frames = 0;
}
return (fps);
}
void CTimer::Reset() {
timeAtStart = 0;
ticksPerSecond = 0;
frames = 0;
lastUpdate = 0;
fps = 0;
updateInterval = 0.5;
InitTimer();
timeAtStart = GetTime();
}
void CTimer::InitTimer() {
if(timerType == Z_TIMER_QPC) {
//We Only Need To Do Initialization For the QPC Timer
//We Need To Know How Often The Clock Is Updated
if( !::QueryPerformanceFrequency((LARGE_INTEGER*)&ticksPerSecond))
ticksPerSecond = 1000;
}
}
float CTimer::GetTime() {
if(timerType == Z_TIMER_SDL) {
return ((float)SDL_GetTicks()/1000.0f);
}
else if(timerType == Z_TIMER_QPC) {
u64 ticks;
float time;
//This is the number of clock ticks since the start
if( !::QueryPerformanceCounter((LARGE_INTEGER*)&ticks))
ticks++;
//Divide by frequency to get time in seconds
time = (float)(u64)ticks / (float)(u64)ticksPerSecond;
//Calculate Actual Time
time -= timeAtStart;
return (time);
}
return (0.0f);
}