-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.c
90 lines (73 loc) · 1.55 KB
/
Timer.c
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
#include <time.h>
#include <stdio.h>
#include "Timer.h"
#ifdef WINDOWS
#include <Windows.h>
static unsigned int g_PreviousTick;
static unsigned int g_FrameCount;
static unsigned int g_LastSecondTick;
static float g_TimeDelta;
void InitialiseTimer()
{
g_PreviousTick = GetTickCount();
g_LastSecondTick = g_PreviousTick;
g_FrameCount = 0;
g_TimeDelta = 0.0f;
}
unsigned char ProcessTimer(unsigned int *framespersecond)
{
unsigned char retval = 0;
unsigned int tick = GetTickCount();
*framespersecond = g_FrameCount;
g_TimeDelta = (tick - g_PreviousTick) / 1000.0f;
if (tick - g_LastSecondTick >= 1000)
{
retval = 1;
g_FrameCount = 0;
g_LastSecondTick = tick;
}
g_PreviousTick = tick;
g_FrameCount++;
return retval;
}
float GetPreviousFrameDeltaInSeconds()
{
return g_TimeDelta;
}
#else
static time_t g_PreviousTimeValue;
static unsigned int g_FrameCount;
static unsigned int g_PreviousFPS;
void InitialiseTimer()
{
g_PreviousTimeValue = time(NULL);
g_FrameCount = 0;
g_PreviousFPS = 0;
}
unsigned char ProcessTimer(unsigned int *framespersecond)
{
unsigned char retval = 0;
time_t tick = time(NULL);
*framespersecond = g_FrameCount;
if (tick - g_PreviousTimeValue >= 1)
{
retval = 1;
g_PreviousFPS = g_FrameCount;
g_FrameCount = 0;
g_PreviousTimeValue = tick;
}
g_FrameCount++;
return retval;
}
float GetPreviousFrameDeltaInSeconds()
{
if (g_PreviousFPS == 0)
{
return 0.01f;
}
else
{
return 1.0f / (float)g_PreviousFPS;
}
}
#endif