blob: 58b8164aac9fc2f96e5f9fb3c4fb156be257c5e7 (
plain) (
blame)
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
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_TIME_H
#define UTILS_TIME_H
#include <cstdint>
/**
* The amount of milliseconds in a logic tick.
*/
static constexpr unsigned MILLISECONDS_IN_A_TICK = 10;
namespace Time
{
/**
* The time in milliseconds since the program started (wraps around after ~49
* days).
*/
uint32_t absoluteTimeMs();
/**
* The time in milliseconds since the last frame, but never more than 1000.
*/
unsigned deltaTimeMs();
/**
* The time in seconds since the last frame, but never more than 1.
*/
float deltaTime();
/**
* Called at the start of each frame, updates the above variables.
*/
void beginFrame();
} // namespace Time
/**
* Simple timer that can be used to check if a certain amount of time
* has passed.
*/
class Timer
{
public:
Timer() = default;
/**
* Sets the timer with an optional duration in milliseconds.
*/
void set(uint32_t ms = 0)
{ mTimeout = Time::absoluteTimeMs() + ms; }
/**
* Reset the timer.
*/
void reset()
{ mTimeout = 0; }
/**
* Returns whether the timer has been set.
*/
bool isSet() const
{ return mTimeout != 0; }
/**
* Returns whether the timer has passed.
* A timer that wasn't set will always return true.
*/
bool passed() const
{ return elapsed() >= 0; }
/**
* Extend the timer by the given number of milliseconds.
*/
void extend(uint32_t ms)
{ mTimeout += ms; }
/**
* Returns the number of milliseconds elapsed since the set time, or a
* negative value if the timer hasn't passed.
*
* Due to wrapping, this function is not suitable for measuring long
* periods of time (tens of days).
*/
int32_t elapsed() const;
private:
uint32_t mTimeout = 0;
};
#endif // UTILS_TIME_H
|