Mana
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "time.h"
23
24#include <algorithm>
25
26#include <SDL.h>
27
28namespace Time
29{
30
31static uint32_t s_absoluteTimeMs;
32static unsigned s_deltaTimeMs;
33
35{
36 return s_absoluteTimeMs;
37}
38
39unsigned deltaTimeMs()
40{
41 return s_deltaTimeMs;
42}
43
44float deltaTime()
45{
46 return s_deltaTimeMs / 1000.f;
47}
48
49static int32_t getElapsedTime(uint32_t timeMs)
50{
51 return static_cast<int32_t>(s_absoluteTimeMs - timeMs);
52}
53
55{
56 // todo: Use SDL_GetTicks64 once we require SDL 2.0.18 or newer.
57
58 const uint32_t previousTime = s_absoluteTimeMs;
59 s_absoluteTimeMs = SDL_GetTicks();
60 s_deltaTimeMs = std::clamp(getElapsedTime(previousTime), 0, 1000);
61}
62
63} // namespace Time
64
65int32_t Timer::elapsed() const
66{
67 return Time::getElapsedTime(mTimeout);
68}
int32_t elapsed() const
Returns the number of milliseconds elapsed since the set time, or a negative value if the timer hasn'...
Definition time.cpp:65
uint32_t mTimeout
Definition time.h:107
Definition time.cpp:29
unsigned deltaTimeMs()
The time in milliseconds since the last frame, but never more than 1000.
Definition time.cpp:39
float deltaTime()
The time in seconds since the last frame, but never more than 1.
Definition time.cpp:44
void beginFrame()
Called at the start of each frame, updates the above variables.
Definition time.cpp:54
uint32_t absoluteTimeMs()
The time in milliseconds since the program started (wraps around after ~49 days).
Definition time.cpp:34
unsigned int uint32_t
Definition sha256.cpp:82