-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.hpp
182 lines (142 loc) · 5.72 KB
/
Time.hpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
///
/// Langulus::Flow
/// Copyright (c) 2017 Dimo Markov <[email protected]>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: GPL-3.0-or-later
///
#pragma once
#include "Common.hpp"
#include <Anyness/Neat.hpp>
#include <chrono>
#include <thread>
#include <fmt/chrono.h>
namespace Langulus
{
using namespace ::std::literals::chrono_literals;
namespace A
{
/// An abstract clock
struct Clock {
LANGULUS(ABSTRACT) true;
};
/// An abstract time
struct Time {
LANGULUS(ABSTRACT) true;
};
} // namespace Langulus::A
using StdClock = ::std::chrono::steady_clock;
///
/// A time point
///
struct TimePoint : A::Time, StdClock::time_point {
LANGULUS(ABSTRACT) false;
LANGULUS(POD) true;
LANGULUS_BASES(A::Time);
using Base = time_point;
using Base::time_point;
constexpr TimePoint() noexcept;
constexpr TimePoint(const time_point&) noexcept;
constexpr explicit operator bool() const noexcept;
};
///
/// A time duration (difference between two time points)
///
struct Time : A::Time, StdClock::duration {
LANGULUS(ABSTRACT) false;
LANGULUS(POD) true;
LANGULUS_BASES(A::Time);
using Base = duration;
using Base::duration;
constexpr Time() noexcept
: duration {zero()} {
using Representation = typename Base::rep;
static_assert(sizeof(Representation) == sizeof(Time),
"Size mismatch");
}
constexpr Time(const duration& a) noexcept
: duration {a} {}
constexpr explicit operator bool() const noexcept;
template<CT::BuiltinNumber T = Real>
NOD() T Seconds() const noexcept;
Time operator + (auto&& rhs) const {
return ::std::chrono::duration_cast<Base>(
static_cast<const duration&>(*this) + rhs);
}
Time operator * (auto&& rhs) const {
return ::std::chrono::duration_cast<Base>(
static_cast<const duration&>(*this) * rhs);
}
};
///
/// A steady clock used to aquire TimePoint(s)
///
class SteadyClock : public A::Clock, private StdClock {
LANGULUS_BASES(A::Clock);
NOD() static TimePoint Now() noexcept;
};
namespace CT
{
template<class T>
concept Time = SameAsOneOf<T, ::Langulus::TimePoint, ::Langulus::Time>;
} // namespace Langulus::CT
///
/// Manages the framerate by measuring delta-time and sleeping
///
template<int FRAMES_PER_SECOND = 60>
struct Framerate {
static constexpr int FramesPerSecond = FRAMES_PER_SECOND;
protected:
using dsec = ::std::chrono::duration<double>;
using seconds = ::std::chrono::seconds;
const Time mInvFpsLimit;
TimePoint mBegin;
TimePoint mEnd;
TimePoint mPrevTime;
Time mDeltaTime;
public:
Framerate()
: mInvFpsLimit {::std::chrono::round<StdClock::duration>(dsec {1. / FramesPerSecond})}
, mBegin {SteadyClock::Now()}
, mEnd {mBegin + mInvFpsLimit}
, mPrevTime {mBegin} {}
/// Get the time between ticks
/// @return the time period between ticks
Time GetDeltaTime() {
return mDeltaTime;
}
/// Call this from your main loop
/// @attention this may make the current thread sleep!
void Tick() {
const auto now = SteadyClock::Now();
if (now <= mPrevTime)
return;
mDeltaTime = now - mPrevTime;
mPrevTime = now;
if (now < mEnd) {
// We've finished early - sleep for the rest of the time
::std::this_thread::sleep_until(mEnd);
}
mBegin = mEnd;
mEnd = mBegin + mInvFpsLimit;
}
};
} // namespace Langulus
namespace fmt
{
///
/// Extend FMT to be capable of logging Flow::Time
///
template<>
struct formatter<Langulus::Time> {
template<class CONTEXT>
constexpr auto parse(CONTEXT& ctx) {
return ctx.begin();
}
template<class CONTEXT> LANGULUS(INLINED)
auto format(Langulus::Time const& element, CONTEXT& ctx) const {
return fmt::format_to(ctx.out(), "{}",
static_cast<const Langulus::Time::Base&>(element));
}
};
} // namespace fmt