OpenTTD Source 20250312-master-gcdcc6b491d
timer_game_realtime.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
13#include "../stdafx.h"
14#include "../openttd.h"
15#include "timer.h"
16#include "timer_game_realtime.h"
17
18#include "../safeguards.h"
19
20template <>
21void IntervalTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
22{
23 if (this->period.period == std::chrono::milliseconds::zero()) return;
25 if (this->period.flag == TimerGameRealtime::PeriodFlags::UNPAUSED && _pause_mode.Any()) return;
26
27 this->storage.elapsed += delta;
28
29 uint count = 0;
30 while (this->storage.elapsed >= this->period.period) {
31 this->storage.elapsed -= this->period.period;
32 count++;
33 }
34
35 if (count > 0) {
36 this->callback(count);
37 }
38}
39
40template <>
41void TimeoutTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
42{
43 if (this->fired) return;
44 if (this->period.period == std::chrono::milliseconds::zero()) return;
46 if (this->period.flag == TimerGameRealtime::PeriodFlags::UNPAUSED && _pause_mode.Any()) return;
47
48 this->storage.elapsed += delta;
49
50 if (this->storage.elapsed >= this->period.period) {
51 this->callback();
52 this->fired = true;
53 }
54}
55
56template <>
57bool TimerManager<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
58{
59 for (auto timer : TimerManager<TimerGameRealtime>::GetTimers()) {
60 timer->Elapsed(delta);
61 }
62
63 return true;
64}
65
66#ifdef WITH_ASSERT
67template <>
69{
70}
71#endif /* WITH_ASSERT */
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
Timer that represents real time for game-related purposes.
@ UNPAUSED
Only run when not paused.
@ AUTOSAVE
Only run when not paused or there was a Command executed recently.
The TimerManager manages a single Timer-type.
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:50
@ CommandDuringPause
A game paused, and a command executed during the pause; resets on autosave.
Definition of Interval and OneShot timers.
Definition of the real time game-timer.