OpenTTD Source: src/timer/timer_game_realtime.cpp Source File

OpenTTD Source 15.0-RC2

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
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;
24 if (this->period.flag == TimerGameRealtime::PeriodFlags::AUTOSAVE && _pause_mode.Any() && !_pause_mode.Test(PauseMode::CommandDuringPause)) 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;
45 if (this->period.flag == TimerGameRealtime::PeriodFlags::AUTOSAVE && _pause_mode.Any() && _pause_mode.Test(PauseMode::CommandDuringPause)) 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 <>
68void TimerManager<TimerGameRealtime>::Validate(TimerGameRealtime::TPeriod)
69{
70}
71#endif /* WITH_ASSERT */
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
IntervalTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimeoutTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimerGameRealtime
Timer that represents real time for game-related purposes.
Definition timer_game_realtime.h:28
TimerGameRealtime::UNPAUSED
@ UNPAUSED
Only run when not paused.
Definition timer_game_realtime.h:32
TimerGameRealtime::AUTOSAVE
@ AUTOSAVE
Only run when not paused or there was a Command executed recently.
Definition timer_game_realtime.h:33
TimerManager
The TimerManager manages a single Timer-type.
Definition timer_manager.h:25
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
_pause_mode
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:50
PauseMode::CommandDuringPause
@ CommandDuringPause
A game paused, and a command executed during the pause; resets on autosave.
TimerGameRealtime::TPeriod
Definition timer_game_realtime.h:36
timer.h
Definition of Interval and OneShot timers.
timer_game_realtime.h
Definition of the real time game-timer.