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{
21
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._timeStampMill;
92 }
93 //--------------------------------时间戳--------------------------------
97 static time_t GetTimeStamp();
101 static time_t GetTimeStampMillisecond();
102
107 static std::string TimeStampDelta2String(time_t t);
108
109 enum Type
110 {
111 ET_YEAR = 1 << 1,
112 ET_MONTH = 1 << 2,
113 ET_DAY = 1 << 3,
114 ET_HOUR = 1 << 4,
115 ET_MINUTE = 1 << 5,
116 ET_SECOND = 1 << 6,
120 };
121
125 static std::string TimeStamp2String(time_t t, Type type = ET_HMS,
126 std::string_view year = "-", std::string_view month = "-", std::string_view day = " ",
127 std::string_view hour = ":", std::string_view minute = ":", std::string_view second = "");
128
129 static std::string ExTimeStamp2StringLog()
130 {
131 return TimeStamp2String(GetTimeStamp(), ET_YMD_HMS, "", "", "", "-", "", "");
132 }
133private:
134 FloatTime _delta; //(s)真实间隔时间
135 FloatTime _deltaExpect; //(s)一帧应该运行的时间
136 unsigned _fps; //真实帧数
137 unsigned _fpsExpect; //设置的帧数
138 FloatTime _countSec; //时间计数(用于fps计算)
139 unsigned _countFrame; //帧计数(用于fps计算)
140
141 std::chrono::nanoseconds _deltaExpectNS;
142
143 std::chrono::high_resolution_clock::time_point _start;//起始时间
144 std::chrono::high_resolution_clock::time_point _last;//上帧时间
145
146 struct Frame
147 {
148 time_t _timeStamp;
149 time_t _timeStampMill;
150 };
151 Frame _frame;
152
153 //帧末尾,计算这一帧花费时间(但是下一帧才访问到)
154 void _update();
155
156 // 根据期望fps计算期望delta
157 void _cacl_expect();
158 // 控制帧率
159 void _sleep();
160};
161extern Time* g_time;
162}
FloatTime GetDelta()
返回帧间隔
FloatTime GetCurrent()
返回游戏运行累计时间
static time_t GetTimeStamp()
返回时间戳
double GetCurrent64()
返回游戏运行累计时间
time_t GetFrameTimeStampMill()
返回当前帧时间戳
static std::string ExTimeStamp2StringLog()
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 GetTimeStampMillisecond()
返回时间戳,毫秒
unsigned GetFPS()
返回fps
time_t GetFrameTimeStamp()
返回当前帧时间戳
friend class System
void SetFPS(unsigned fps)
返回fps
static std::string TimeStampDelta2String(time_t t)
时间戳间隔 -> xx:xx:xx
通用类型
float FloatTime
Time * g_time