-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
104 lines (78 loc) · 2.01 KB
/
Timer.cpp
File metadata and controls
104 lines (78 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "../Include/defGameEngine.hpp"
class Timer final : public def::GameEngine
{
public:
Timer()
{
GetWindow()->SetTitle("Timer");
}
std::chrono::system_clock::time_point begin;
std::chrono::system_clock::time_point pausedTime;
bool enableMilliseconds = false;
bool isPaused = false;
protected:
bool OnUserCreate() override
{
begin = std::chrono::system_clock::now();
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (GetInput()->GetKeyState(def::Key::SPACE).pressed)
{
isPaused = !isPaused;
if (isPaused)
pausedTime = std::chrono::system_clock::now();
else
begin += std::chrono::system_clock::now() - pausedTime;
}
if (GetInput()->GetKeyState(def::Key::ENTER).pressed)
enableMilliseconds = !enableMilliseconds;
if (!isPaused)
{
auto diff = std::chrono::system_clock::now() - begin;
auto GetTime = [&diff](auto t)
{
auto time = std::chrono::duration_cast<decltype(t)>(diff);
diff -= time;
return time;
};
auto years = GetTime(std::chrono::years());
auto days = GetTime(std::chrono::days());
auto hours = GetTime(std::chrono::hours());
auto minutes = GetTime(std::chrono::minutes());
auto seconds = GetTime(std::chrono::seconds());
auto FixValue = [](int value)
{
if (value < 10)
return "0" + std::to_string(value);
if (value > 10)
return std::to_string(value).substr(0, 2);
return std::to_string(value);
};
std::string s;
if (years.count() != 0)
s += FixValue(years.count()) + ":";
if (days.count() != 0)
s += FixValue(days.count()) + ":";
s += FixValue(hours.count()) + ":";
s += FixValue(minutes.count()) + ":";
s += FixValue(seconds.count());
if (enableMilliseconds)
{
auto milliseconds = GetTime(std::chrono::milliseconds());
s += "." + FixValue(milliseconds.count());
}
Clear(def::BLACK);
DrawString(10, 10, s);
}
return true;
}
};
int main()
{
Timer timer;
if (timer.Construct(106, 30, 6, 6))
timer.Run();
return 0;
}