Debian Codegraph @

src/engine/timing.h

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2021 - 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #pragma once
22
23 #include <chrono>
24 #include <cstdint>
25
26 namespace fheroes2
27 {
28 // IMPORTANT!!! According to https://en.cppreference.com/w/cpp/chrono/high_resolution_clock we should never use high_resolution_clock for time internal measurements
29 // because for high_resolution_clock the time may go backwards.
30
31 class Time
32 {
33 public:
34 Time()
35 {
36 reset();
37 }
38
39 void reset()
40 {
41 _startTime = std::chrono::steady_clock::now();
42 }
43
44 // Returns time in seconds.
45 double getS() const
46 {
47 const std::chrono::duration<double> time = std::chrono::steady_clock::now() - _startTime;
48 return time.count();
49 }
50
51 // Returns rounded time in milliseconds.
52 uint64_t getMs() const
53 {
54 const auto time = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - _startTime );
55 return time.count();
56 }
57
58 private:
59 std::chrono::time_point<std::chrono::steady_clock> _startTime;
60 };
61
62 class TimeDelay
63 {
64 public:
65 TimeDelay() = delete;
66
67 explicit TimeDelay( const uint64_t delayMs )
68 : _delayMs( delayMs )
69 {
70 reset();
71 }
72
73 void setDelay( const uint64_t delayMs )
74 {
75 _delayMs = delayMs;
76 }
77
78 uint64_t getDelay() const
79 {
80 return _delayMs;
81 }
82
83 bool isPassed() const
84 {
85 return isPassed( _delayMs );
86 }
87
88 bool isPassed( const uint64_t delayMs ) const
89 {
90 const auto time = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - _prevTime );
91 const uint64_t passedMs = time.count();
92 return passedMs >= delayMs;
93 }
94
95 // Reset delay by starting the count from the current time.
96 void reset()
97 {
98 _prevTime = std::chrono::steady_clock::now();
99 }
100
101 // Explicitly set delay to passed state. Can be used in cases when first call of isPassed() must return true.
102 void pass()
103 {
104 _prevTime = std::chrono::steady_clock::now() - std::chrono::milliseconds( 2 * _delayMs );
105 }
106
107 private:
108 std::chrono::time_point<std::chrono::steady_clock> _prevTime;
109 uint64_t _delayMs;
110 };
111
112 void delayforMs( const uint32_t delayMs );
113 }