dl
dl_time.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <chrono>
16
17#include "dl_type.h"
18
19namespace dl
20{
22class Time
23{
24 friend class System;
25public:
26 Time() :
27 _delta{ 0 },
28 _fps{ 0 },
29 _fpsExpect{ 60 },
30 _countSec{ 0 },
31 _countFrame{ 0 }
32 {
33 _start = std::chrono::high_resolution_clock::now();
34 _last = _start;
35 _delta = 0;
36 _cacl_expect();
37 }
38
42 {
43 return _delta;
44 }
45
48 unsigned GetFPS()
49 {
50 return _fps;
51 }
52
55 void SetFPS(unsigned fps)
56 {
57 _fpsExpect = fps;
58 _cacl_expect();
59 }
60
64 {
65 auto current = std::chrono::high_resolution_clock::now();
66 return std::chrono::duration<FloatTime, std::chrono::seconds::period>
67 (current - _start).count();
68 }
69
72 double GetCurrent64()
73 {
74 auto current = std::chrono::high_resolution_clock::now();
75 return std::chrono::duration<double, std::chrono::seconds::period>
76 (current - _start).count();
77 }
78 //--------------------------------当前帧时间--------------------------------
83 {
84 return _frame._timeStamp;
85 }
86
90 {
91 return _frame._timeStampMs;
92 }
93
97 unsigned GetFrameTkc()
98 {
99 return _frame._tkc;
100 }
101 //--------------------------------时间戳--------------------------------
105 static time_t GetTimeStamp();
109 static time_t GetTimeStampMs();
110
115 static std::string TimeStampDelta2String(time_t t);
116
117 enum Type
118 {
119 ET_YEAR = 1 << 1,
120 ET_MONTH = 1 << 2,
121 ET_DAY = 1 << 3,
122 ET_HOUR = 1 << 4,
123 ET_MINUTE = 1 << 5,
124 ET_SECOND = 1 << 6,
128 };
129
133 static std::string TimeStamp2String(time_t t, Type type = ET_HMS,
134 std::string_view year = "-", std::string_view month = "-", std::string_view day = " ",
135 std::string_view hour = ":", std::string_view minute = ":", std::string_view second = "");
136
137 static std::string ExTimeStamp2StringLog()
138 {
139 return TimeStamp2String(GetTimeStamp(), ET_YMD_HMS, "", "", "", "-", "", "");
140 }
141private:
142 FloatTime _delta; //(s)真实间隔时间
143 FloatTime _deltaExpect; //(s)一帧应该运行的时间
144 unsigned _fps; //真实帧数
145 unsigned _fpsExpect; //设置的帧数
146 FloatTime _countSec; //时间计数(用于fps计算)
147 unsigned _countFrame; //帧计数(用于fps计算)
148
149 std::chrono::nanoseconds _deltaExpectNS;
150
151 std::chrono::high_resolution_clock::time_point _start;//起始时间
152 std::chrono::high_resolution_clock::time_point _last;//上帧时间
153
154 struct Frame
155 {
156 time_t _timeStamp;
157 time_t _timeStampMs;
158 unsigned _tkc;
159 };
160 Frame _frame;
161
162 //帧末尾,计算这一帧花费时间(但是下一帧才访问到)
163 void _update();
164
165 // 根据期望fps计算期望delta
166 void _cacl_expect();
167 // 控制帧率
168 void _sleep();
169};
170extern Time* g_time;
171}
FloatTime GetDelta()
返回帧间隔
FloatTime GetCurrent()
返回游戏运行累计时间
static time_t GetTimeStamp()
返回时间戳
double GetCurrent64()
返回游戏运行累计时间
static std::string ExTimeStamp2StringLog()
time_t GetFrameTimeStampMs()
返回当前帧时间戳(毫秒)
static std::string TimeStamp2String(time_t t, Type type=ET_HMS, std::string_view year="-", std::string_view month="-", std::string_view day=" ", std::string_view hour=":", std::string_view minute=":", std::string_view second="")
时间戳 -> 字符串
static time_t GetTimeStampMs()
返回时间戳(毫秒)
unsigned GetFPS()
返回fps
time_t GetFrameTimeStamp()
返回当前帧时间戳
friend class System
void SetFPS(unsigned fps)
返回fps
unsigned GetFrameTkc()
返回当前帧tick(毫秒)
static std::string TimeStampDelta2String(time_t t)
时间戳间隔 -> xx:xx:xx
通用类型
float FloatTime
Time * g_time