-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHiResTimer.cpp
More file actions
86 lines (66 loc) · 2.76 KB
/
Copy pathHiResTimer.cpp
File metadata and controls
86 lines (66 loc) · 2.76 KB
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
#include "HiResTimer.h"
/****************************************************************************/
/** \file HiResTimer.cpp
\brief Contains the CHiResTimer implementation */
/****************************************************************************/
/**
If the hi-res timer is present, the tick rate is stored and the function
returns true. Otherwise, the function returns false, and the timer should
not be used.
*****************************************************************************/
bool CHiResTimer::Init()
{
m_startTime = SDL_GetTicks();
return true;
} // end Init()
/****************************************************************************/
/** \return Number of seconds since the last call to GetElapsedSeconds
\param elapsedFrames Number of frames since last call
*****************************************************************************/
float CHiResTimer::GetElapsedSeconds(unsigned long elapsedFrames)
{
static Uint32 s_lastTime = m_startTime;
Uint32 currentTime;
currentTime = SDL_GetTicks();
float seconds = ((float)currentTime - (float)s_lastTime) / 1000.0;
s_lastTime = currentTime;
return seconds;
} // end GetElapsedSeconds()
/**************************************************************************/
/**
Returns the average frames per second over elapsedFrames, which defaults to
one. If this is not called every frame, the client should track the number
of frames itself, and reset the value after this is called.
***************************************************************************/
float CHiResTimer::GetFPS(unsigned long elapsedFrames)
{
static Uint32 s_lastTime = m_startTime;
Uint32 currentTime;
currentTime = SDL_GetTicks();
float fps = (float)elapsedFrames * 1000.0 / ((float)currentTime - (float)s_lastTime);
s_lastTime = currentTime;
return fps;
} // end GetFPS
/**************************************************************************/
/**
Used to lock the frame rate to a set amount. This will block until enough
time has passed to ensure that the fps won't go over the requested amount.
Note that this can only keep the fps from going above the specified level;
it can still drop below it. It is assumed that if used, this function will
be called every frame. The value returned is the instantaneous fps, which
will be <= targetFPS.
***************************************************************************/
float CHiResTimer::LockFPS(unsigned char targetFPS)
{
if (targetFPS == 0)
targetFPS = 1;
static Uint32 s_lastTime = m_startTime;
Uint32 currentTime;
float fps;
do{
currentTime = SDL_GetTicks();
fps = 1000.0 / ((float)(currentTime - s_lastTime));
}while(fps > (float)targetFPS);
s_lastTime = currentTime;
return fps;
} // end LockFPS()