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
/*===========================================================================

  timer.h - Timing functions for games or demos.

  Copyright © 2005 Matthias Grumet. All rights reserved.

  http://www.embege.com/timing/

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

===========================================================================*/


#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];

//initialize timer variables
void timer_initialize()
{
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&lastTime);
    startTime.QuadPart = 0;
    fps = 0;
    fpscounter = 0;
    for (int i = 0i < ACCURACYi++)
        timebuffer[i] = 0.0;
}

//starts the timer
void timer_start()
{
    QueryPerformanceCounter(&startTime);
}

//waits for seconds
void timer_wait(float seconds)
{
    do
    {
        QueryPerformanceCounter(&curTime);
    }
    while ((double)(curTime.QuadPart-waitlastTime.QuadPart)/(double)frequency.QuadPart < seconds);

    QueryPerformanceCounter(&waitlastTime);
}

//returns the passed time
double timer_getTime()
{
    QueryPerformanceCounter(&curTime);
    return (double)((curTime.QuadPart-startTime.QuadPart)/(double)frequency.QuadPart);
}

//returns the time that has passed since the last call to this function
//good for measuring the exact time between frames
double timer_getelapsedTime()
{
    QueryPerformanceCounter(&curTime);
    elapsedTime = (double)(curTime.QuadPart-lastTime.QuadPart)/(double)frequency.QuadPart;
    lastTime = curTime;
    return elapsedTime;
}

//returns frames per seconds
int timer_getFPS()
{
    fpscounter++;
    QueryPerformanceCounter(&curTime);
    //check if one second has passed
    if( (double)(curTime.QuadPart-fpslastTime.QuadPart)/(double)frequency.QuadPart > 1.0f ) {
        fpslastTime = curTime;
        fps = fpscounter;
        fpscounter = 0;
    }
    return fps;
}

//returns an averaged time over the last ACCURACY frames
//good for avoiding outliers due to laggy frames
//you should provide the result of timer_getelapsedTime() as argument
double timer_getaveragedTime(double gt)
{
    double sum = 0.0;

    for (int i = 1i<ACCURACYi++) {
        timebuffer[i] = lastbuffer[i-1];
        sum += timebuffer[i];
    }
    timebuffer[0] = gt;
    sum += timebuffer[0];
    for (int i = 0i<ACCURACYi++)
        lastbuffer[i] = timebuffer[i];

    return sum/(double)ACCURACY;
}


#endif

<Comments>
matthias 30. November 2008
what exactly is not working?
anonymous 04. August 2008
nope, tk is right
matthias 18. November 2007
Hello, what exactly is not working?
tk 18. November 2007
ur code ain't workin

 

Post a commment:
Name:
Email:
Website:
Message:
Antispambot:insert this number in the textfield on the right (insert the numbers from the image)

All contents © 2001 - 2006 by Matthias Grumet.
embege