FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_clock.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_CLOCK_HPP_INCLUDED
18#define _FGE_C_CLOCK_HPP_INCLUDED
19
20#include <chrono>
21
22namespace fge
23{
24
32class Clock
33{
34public:
41 g_lastTimePoint(std::chrono::steady_clock::now())
42 {}
43
49 auto getElapsedTime() const { return std::chrono::steady_clock::now() - this->g_lastTimePoint; }
50
57 template<typename TD>
58 auto getElapsedTime() const
59 {
60 return std::chrono::duration_cast<TD>(std::chrono::steady_clock::now() - this->g_lastTimePoint).count();
61 }
62
70 auto restart()
71 {
72 auto lastDuration = std::chrono::steady_clock::now() - this->g_lastTimePoint;
73 this->g_lastTimePoint = std::chrono::steady_clock::now();
74 return lastDuration;
75 }
76
85 template<typename TD>
86 auto restart()
87 {
88 auto lastDuration =
89 std::chrono::duration_cast<TD>(std::chrono::steady_clock::now() - this->g_lastTimePoint).count();
90 this->g_lastTimePoint = std::chrono::steady_clock::now();
91 return lastDuration;
92 }
93
100 bool reached(std::chrono::milliseconds const& duration)
101 {
102 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
103 this->g_lastTimePoint) >= duration;
104 }
105
106private:
107 std::chrono::steady_clock::time_point g_lastTimePoint;
108};
109
110} // namespace fge
111
112#endif // _FGE_C_CLOCK_HPP_INCLUDED
A clock that can be used to measure time.
Definition C_clock.hpp:33
auto restart()
Restart the clock and return the time elapsed since the last time point with duration cast to the giv...
Definition C_clock.hpp:86
auto restart()
Restart the clock and return the time elapsed since the last time point.
Definition C_clock.hpp:70
bool reached(std::chrono::milliseconds const &duration)
Check if the clock has elapsed the given duration.
Definition C_clock.hpp:100
auto getElapsedTime() const
Get the time elapsed since the last time point with duration cast to the given type.
Definition C_clock.hpp:58
Clock()
Default constructor.
Definition C_clock.hpp:40
auto getElapsedTime() const
Get the time elapsed since the last time point.
Definition C_clock.hpp:49