OpenTTD Source 20260206-master-g4d4e37dbf1
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "timer.h"
13#include "timer_game_realtime.h"
14
15#include "../safeguards.h"
16
17template <>
18void IntervalTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
19{
20 if (this->period.period == std::chrono::milliseconds::zero()) return;
21 if (this->period.trigger == TimerGameRealtime::Trigger::Autosave && _pause_mode.Any() && !_pause_mode.Test(PauseMode::CommandDuringPause)) return;
22 if (this->period.trigger == TimerGameRealtime::Trigger::Unpaused && _pause_mode.Any()) return;
23
24 this->storage.elapsed += delta;
25
26 uint count = 0;
27 while (this->storage.elapsed >= this->period.period) {
28 this->storage.elapsed -= this->period.period;
29 count++;
30 }
31
32 if (count > 0) {
33 this->callback(count);
34 }
35}
36
37template <>
38void TimeoutTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
39{
40 if (this->fired) return;
41 if (this->period.period == std::chrono::milliseconds::zero()) return;
42 if (this->period.trigger == TimerGameRealtime::Trigger::Autosave && _pause_mode.Any() && _pause_mode.Test(PauseMode::CommandDuringPause)) return;
43 if (this->period.trigger == TimerGameRealtime::Trigger::Unpaused && _pause_mode.Any()) return;
44
45 this->storage.elapsed += delta;
46
47 if (this->storage.elapsed >= this->period.period) {
48 this->callback();
49 this->fired = true;
50 }
51}
52
53template <>
54bool TimerManager<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
55{
57 timer->Elapsed(delta);
58 }
59
60 return true;
61}
62
63#ifdef WITH_ASSERT
64template <>
66{
67}
68#endif /* WITH_ASSERT */
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.
@ Autosave
Only run when not paused or there was a Command executed recently.
@ Unpaused
Only run when not paused.
The TimerManager manages a single Timer-type.
static std::set< BaseTimer< TTimerType > *, base_timer_sorter > & GetTimers()
Singleton list, to store all the active timers.
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:51
Some generic types.
@ CommandDuringPause
A game paused, and a command executed during the pause; resets on autosave.
Definition openttd.h:76
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Definition of Interval and OneShot timers.
Definition of the real time game-timer.