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 }
42 {
43 return _delta;
44 }
48 unsigned GetFPS()
49 {
50 return _fps;
51 }
55 void SetFPS(unsigned fps)
56 {
57 _fpsExpect = fps;
58 _cacl_expect();
59 }
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
70 //--------------------------------时间戳--------------------------------
74 static time_t GetTimeStamp();
75
80 static std::string TimeStampDelta2String(time_t t);
81
82 enum class StringFormat
83 {
88 };
93 static std::string TimeStamp2String(time_t t, StringFormat sf = StringFormat::HH_MM_SS);
94private:
95 FloatTime _delta; //(s)真实间隔时间
96 FloatTime _deltaExpect; //(s)一帧应该运行的时间
97 unsigned _fps; //真实帧数
98 unsigned _fpsExpect; //设置的帧数
99 FloatTime _countSec; //时间计数(用于fps计算)
100 unsigned _countFrame; //帧计数(用于fps计算)
101
102 std::chrono::nanoseconds _deltaExpectNS;
103
104 std::chrono::high_resolution_clock::time_point _start;//起始时间
105 std::chrono::high_resolution_clock::time_point _last;//上帧时间
106
107 //帧末尾,计算这一帧花费时间(但是下一帧才访问到)
108 void _update();
109
110 // 根据期望fps计算期望delta
111 void _cacl_expect();
112 // 控制帧率
113 void _sleep();
114};
115extern Time* g_time;
116}
FloatTime GetDelta()
返回帧间隔
Definition: dl_time.h:41
FloatTime GetCurrent()
返回游戏运行累计时间
Definition: dl_time.h:63
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:26
StringFormat
Definition: dl_time.h:83
unsigned GetFPS()
返回fps
Definition: dl_time.h:48
void SetFPS(unsigned fps)
返回fps
Definition: dl_time.h:55
static std::string TimeStampDelta2String(time_t t)
时间戳间隔 -> xx:xx:xx
通用类型
Definition: dl_array2.h:18
Time * g_time
float FloatTime
Definition: dl_type_def.h:48