forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_timer.h
109 lines (83 loc) · 1.6 KB
/
linux_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
//--------------------------------------------------------------------
// 文件名: linux_timer.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月15日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_LINUX_TIMER_H
#define _SYSTEM_LINUX_TIMER_H
#include <sys/time.h>
// 高精度计时器
class CGameTimer
{
public:
CGameTimer()
{
m_bInit = false;
m_dLastTime = 0.0;
}
~CGameTimer()
{
}
// 初始化
void Initialize()
{
struct timeval tv;
gettimeofday(&tv, NULL);
m_dLastTime = ToSeconds(&tv);
m_bInit = true;
}
// 高精度计时器是否可用
bool CanUseQPF() const
{
return true;
}
// 是否使用高精度计时器
void SetUseQPF(bool flag)
{
}
// 当前时间数值
double GetCurrentTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return ToSeconds(&tv);
}
// 获得逝去的秒数
double GetElapseTime(double expect = 0.0)
{
struct timeval tv;
gettimeofday(&tv, NULL);
double now = ToSeconds(&tv);
double elapse = now - m_dLastTime;
if (elapse >= expect)
{
m_dLastTime = now;
}
return elapse;
}
// 获得逝去的毫秒数
int GetElapseMillisec(int expect = 0)
{
struct timeval tv;
gettimeofday(&tv, NULL);
double now = ToSeconds(&tv);
double elapse = now - m_dLastTime;
if (elapse >= expect)
{
m_dLastTime = now;
}
return (int)(elapse * 1000.0);
}
private:
double ToSeconds(struct timeval* tv)
{
return (double)tv->tv_sec + (double)tv->tv_usec * 0.000001;
}
private:
bool m_bInit;
double m_dLastTime;
};
#endif // _SYSTEM_LINUX_TIMER_H