dl
dl_time.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <chrono>
16#include "dl_type.h"
17
18namespace dl
19{
20
21class Time
22{
23 friend class System;
24public:
25 Time() :
26 _delta{ 0 },
27 _fps{ 0 },
28 _fpsExpect{ 60 },
29 _countSec{ 0 },
30 _countFrame{ 0 }
31 {
32 _start = std::chrono::high_resolution_clock::now();
33 _last = _start;
34 _delta = 0;
35 _cacl_expect();
36 }
41 {
42 return _delta;
43 }
47 unsigned GetFPS()
48 {
49 return _fps;
50 }
54 void SetFPS(unsigned fps)
55 {
56 _fpsExpect = fps;
57 _cacl_expect();
58 }
63 {
64 auto current = std::chrono::high_resolution_clock::now();
65 return std::chrono::duration<FloatTime, std::chrono::seconds::period>
66 (current - _start).count();
67 }
68
69 //--------------------------------时间戳--------------------------------
73 static time_t GetTimeStamp();
74
79 static std::string TimeStampDelta2String(time_t t);
80
81 enum class StringFormat
82 {
87 };
92 static std::string TimeStamp2String(time_t t, StringFormat sf = StringFormat::HH_MM_SS);
93private:
94 FloatTime _delta; //(s)真实间隔时间
95 FloatTime _deltaExpect; //(s)一帧应该运行的时间
96 unsigned _fps; //真实帧数
97 unsigned _fpsExpect; //设置的帧数
98 FloatTime _countSec; //时间计数(用于fps计算)
99 unsigned _countFrame; //帧计数(用于fps计算)
100
101 std::chrono::nanoseconds _deltaExpectNS;
102
103 std::chrono::high_resolution_clock::time_point _start;//起始时间
104 std::chrono::high_resolution_clock::time_point _last;//上帧时间
105
106 //帧末尾,计算这一帧花费时间(但是下一帧才访问到)
107 void _update();
108
109 // 根据期望fps计算期望delta
110 void _cacl_expect();
111 // 控制帧率
112 void _sleep();
113};
114extern Time* g_time;
115}
FloatTime GetDelta()
返回帧间隔
Definition: dl_time.h:40
FloatTime GetCurrent()
返回游戏运行累计时间
Definition: dl_time.h:62
static std::string TimeStamp2String(time_t t, StringFormat sf=StringFormat::HH_MM_SS)
时间戳 -> xxxx-xx-xx xx:xx:xx 具体参见第二个参数
static time_t GetTimeStamp()
返回时间戳
Time()
Definition: dl_time.h:25
StringFormat
Definition: dl_time.h:82
unsigned GetFPS()
返回fps
Definition: dl_time.h:47
void SetFPS(unsigned fps)
返回fps
Definition: dl_time.h:54
static std::string TimeStampDelta2String(time_t t)
时间戳间隔 -> xx:xx:xx
Definition: dl_array2.h:17
Time * g_time
float FloatTime
Definition: dl_type_def.h:48
通用类型