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 }
141
144 static void Sleep(unsigned t = 0);
145private:
146 FloatTime _delta; //(s)真实间隔时间
147 FloatTime _deltaExpect; //(s)一帧应该运行的时间
148 unsigned _fps; //真实帧数
149 unsigned _fpsExpect; //设置的帧数
150 FloatTime _countSec; //时间计数(用于fps计算)
151 unsigned _countFrame; //帧计数(用于fps计算)
152
153 std::chrono::nanoseconds _deltaExpectNS;
154
155 std::chrono::high_resolution_clock::time_point _start;//起始时间
156 std::chrono::high_resolution_clock::time_point _last;//上帧时间
157
158 struct Frame
159 {
160 time_t _timeStamp;
161 time_t _timeStampMs;
162 unsigned _tkc;
163 Frame() :
164 _timeStamp{},
165 _timeStampMs{},
166 _tkc{}
167 {
168 }
169 };
170 Frame _frame;
171
172 // 帧末尾,计算这一帧花费时间(但是下一帧才访问到)
173 void _update();
174 // 帧时间信息
175 void _update_pre();
176
177 // 根据期望fps计算期望delta
178 void _cacl_expect();
179 // 控制帧率
180 void _sleep();
181};
182extern Time* g_time;
183}
FloatTime GetDelta()
返回帧间隔
FloatTime GetCurrent()
返回游戏运行累计时间
static time_t GetTimeStamp()
返回时间戳
double GetCurrent64()
返回游戏运行累计时间
static void Sleep(unsigned t=0)
阻塞线程毫秒数(不精确)
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