Timing
A set of C++ functions for correct timing in games.
In game programming timing is a critical part, which makes sure that the game runs at a given speed, independently on the frame rate which is of course hooked on the computer's hardware.
Here you will find a set of C++ functions that allow for high precision timing on the Windows® platform using the QueryPerformanceCounter.
To use the functions, all you have to do is to #include the timer.h file.
Above each function you will find a few comments on what it does and what it returns.
Usually you will have to call timer_initialize(); and timer_start(); before the game loop starts.
Then in the game loop you can call the other functions to get information about the passed time, the time since the last frame, and more.
The functions are distributed for free under this license.
Below you will find the timer.h file, which you can also download here: timer.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#ifndef _TIMER_H
#define _TIMER_H
#include <math.h>
#define ACCURACY 20
LARGE_INTEGER curTime;
LARGE_INTEGER lastTime;
LARGE_INTEGER waitlastTime;
LARGE_INTEGER fpslastTime;
LARGE_INTEGER startTime;
LARGE_INTEGER frequency;
int fpscounter;
int fps;
double elapsedTime;
double timebuffer[ACCURACY];
double lastbuffer[ACCURACY];
void timer_initialize()
{
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&lastTime);
startTime.QuadPart = 0;
fps = 0;
fpscounter = 0;
for (int i = 0; i < ACCURACY; i++)
timebuffer[i] = 0.0;
}
void timer_start()
{
QueryPerformanceCounter(&startTime);
}
void timer_wait(float seconds)
{
do
{
QueryPerformanceCounter(&curTime);
}
while ((double)(curTime.QuadPart-waitlastTime.QuadPart)/(double)frequency.QuadPart < seconds);
QueryPerformanceCounter(&waitlastTime);
}
double timer_getTime()
{
QueryPerformanceCounter(&curTime);
return (double)((curTime.QuadPart-startTime.QuadPart)/(double)frequency.QuadPart);
}
double timer_getelapsedTime()
{
QueryPerformanceCounter(&curTime);
elapsedTime = (double)(curTime.QuadPart-lastTime.QuadPart)/(double)frequency.QuadPart;
lastTime = curTime;
return elapsedTime;
}
int timer_getFPS()
{
fpscounter++;
QueryPerformanceCounter(&curTime);
if( (double)(curTime.QuadPart-fpslastTime.QuadPart)/(double)frequency.QuadPart > 1.0f ) {
fpslastTime = curTime;
fps = fpscounter;
fpscounter = 0;
}
return fps;
}
double timer_getaveragedTime(double gt)
{
double sum = 0.0;
for (int i = 1; i<ACCURACY; i++) {
timebuffer[i] = lastbuffer[i-1];
sum += timebuffer[i];
}
timebuffer[0] = gt;
sum += timebuffer[0];
for (int i = 0; i<ACCURACY; i++)
lastbuffer[i] = timebuffer[i];
return sum/(double)ACCURACY;
}
#endif
|
<Commentbox>
| matthias |
18. November 2007 |
| Hello, what exactly is not working? |
| tk |
18. November 2007 |
| ur code ain't workin |
All contents © 2001 - 2006 by Matthias Grumet.
embege