summaryrefslogtreecommitdiff
path: root/src/utils/time.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-10-01 17:59:04 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-10-08 21:01:45 +0200
commit59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb (patch)
tree536f6b80e359922172e671fbbbeb2b7bc5d3f165 /src/utils/time.cpp
parente115390f18734d9e3e0e2fc5e1ed05f44c9fdb60 (diff)
downloadmana-59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb.tar.gz
mana-59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb.tar.bz2
mana-59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb.tar.xz
mana-59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb.zip
Added convenient and efficient Timer class
The Timer is efficient because it does not depend on incrementing a counter to keep track of time, nor does it call SDL_GetTicks every time its state is checked (this happens once per frame instead). Along with global functions Time::absoluteTimeMs() and Time::deltaTimeMs(), this replaces previous globals tick_time, cur_time and get_elapsed_time(). For now, there is still a fixed 100 times per second logic call rate, but the new Time::deltaTimeMs() function should allow getting rid of this.
Diffstat (limited to 'src/utils/time.cpp')
-rw-r--r--src/utils/time.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/utils/time.cpp b/src/utils/time.cpp
new file mode 100644
index 00000000..c89914fa
--- /dev/null
+++ b/src/utils/time.cpp
@@ -0,0 +1,63 @@
+/*
+ * 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/>.
+ */
+
+#include "time.h"
+
+#include <algorithm>
+
+#include <SDL.h>
+
+namespace Time
+{
+
+static uint32_t s_absoluteTimeMs;
+static unsigned s_deltaTimeMs;
+
+uint32_t absoluteTimeMs()
+{
+ return s_absoluteTimeMs;
+}
+
+unsigned deltaTimeMs()
+{
+ return s_deltaTimeMs;
+}
+
+static int32_t getElapsedTime(uint32_t timeMs)
+{
+ return static_cast<int32_t>(s_absoluteTimeMs - timeMs);
+}
+
+void beginFrame()
+{
+ // todo: Use SDL_GetTicks64 once we require SDL 2.0.18 or newer.
+
+ const uint32_t previousTime = s_absoluteTimeMs;
+ s_absoluteTimeMs = SDL_GetTicks();
+ s_deltaTimeMs = std::clamp(getElapsedTime(previousTime), 0, 1000);
+}
+
+} // namespace Time
+
+int32_t Timer::elapsed() const
+{
+ return Time::getElapsedTime(mTimeout);
+}