Codechange: Make OverflowSafeInt ConvertibleThroughBase. (#13449)
[openttd-github.git] / src / timer / timer_game_realtime.cpp
blob0a9b7a9aa01b8923d8f7884ae416562f42cff3da
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 */
8 /**
9 * @file timer_game_realtime.cpp
10 * This file implements the timer logic for the real time game-timer.
13 #include "../stdafx.h"
14 #include "../openttd.h"
15 #include "timer.h"
16 #include "timer_game_realtime.h"
18 #include "../safeguards.h"
20 template <>
21 void IntervalTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
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;
27 this->storage.elapsed += delta;
29 uint count = 0;
30 while (this->storage.elapsed >= this->period.period) {
31 this->storage.elapsed -= this->period.period;
32 count++;
35 if (count > 0) {
36 this->callback(count);
40 template <>
41 void TimeoutTimer<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
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;
48 this->storage.elapsed += delta;
50 if (this->storage.elapsed >= this->period.period) {
51 this->callback();
52 this->fired = true;
56 template <>
57 bool TimerManager<TimerGameRealtime>::Elapsed(TimerGameRealtime::TElapsed delta)
59 for (auto timer : TimerManager<TimerGameRealtime>::GetTimers()) {
60 timer->Elapsed(delta);
63 return true;
66 #ifdef WITH_ASSERT
67 template <>
68 void TimerManager<TimerGameRealtime>::Validate(TimerGameRealtime::TPeriod)
71 #endif /* WITH_ASSERT */