forked from rtpHarry/Sokoban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
72 lines (57 loc) · 1.47 KB
/
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
#pragma once
#ifdef _MSC_VER
#pragma comment( lib, "winmm.lib" )
#endif
#include <Windows.h>
#include <mmsystem.h>
class CTimer
{
public:
CTimer(void);
~CTimer(void);
private:
// Frames Per Second Monitor
static float m_FPSLastCheckTime; // to make sure fps is update per second
static float m_FPSCounter; // how many frames since last second
static float m_FPS; // current fps, ready to report back
// Frame Delta
static float m_LastTime; // time of last check
static float m_FrameDelta; // frame delta, ready to report back
public:
// Update the Frame Delta
void Tick(void)
{
// frame delta update
//////////////////////////////////////////////////////////////////////////
// grab time from most accurate time source
float currentTime = timeGetTime() * 0.001f;
// calculate frame delta
m_FrameDelta = currentTime - m_LastTime;
// store for next frame
m_LastTime = currentTime;
// fps counter update
//////////////////////////////////////////////////////////////////////////
// increase the fps counter
++m_FPSCounter;
// if a second has passed update the real fps
if( currentTime - m_FPSLastCheckTime > 1.0f)
{
// prepare last check time
m_FPSLastCheckTime = currentTime;
// set real fps
m_FPS = m_FPSCounter;
// reset counter
m_FPSCounter = 0;
}
}
// Get the current frame delta
float GetFrameDelta(void)
{
return m_FrameDelta;
}
// Get the current fps
float GetFPS(void)
{
return m_FPS;
}
};