OpenTTD Source  20240919-master-gdf0233f4c2
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 
20 template<>
21 void IntervalTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
22 {
23  if (this->period.period == std::chrono::milliseconds::zero()) return;
24  if (this->period.flag == TimerGameRealtime::PeriodFlags::AUTOSAVE && _pause_mode != PM_UNPAUSED && (_pause_mode & PM_COMMAND_DURING_PAUSE) == 0) return;
25  if (this->period.flag == TimerGameRealtime::PeriodFlags::UNPAUSED && _pause_mode != PM_UNPAUSED) 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 
40 template<>
41 void TimeoutTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
42 {
43  if (this->fired) return;
44  if (this->period.period == std::chrono::milliseconds::zero()) return;
45  if (this->period.flag == TimerGameRealtime::PeriodFlags::AUTOSAVE && _pause_mode != PM_UNPAUSED && (_pause_mode & PM_COMMAND_DURING_PAUSE) == 0) return;
46  if (this->period.flag == TimerGameRealtime::PeriodFlags::UNPAUSED && _pause_mode != PM_UNPAUSED) 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 
56 template<>
57 bool 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
67 template<>
69 {
70 }
71 #endif /* WITH_ASSERT */
PM_COMMAND_DURING_PAUSE
@ PM_COMMAND_DURING_PAUSE
A game paused, and a command executed during the pause; resets on autosave.
Definition: openttd.h:77
TimeoutTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
IntervalTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
timer_game_realtime.h
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
timer.h
TimerManager
The TimerManager manages a single Timer-type.
Definition: timer_manager.h:27
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
TimerGameRealtime::TPeriod
Definition: timer_game_realtime.h:36
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:69