Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / timer / timer_game_tick.cpp
blobd86992209c6e0d67a914a38f787a4e742681626c
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_tick.cpp
10 * This file implements the timer logic for the tick-based game-timer.
13 #include "../stdafx.h"
14 #include "timer.h"
15 #include "timer_game_tick.h"
17 #include "../safeguards.h"
19 TimerGameTick::TickCounter TimerGameTick::counter = 0;
21 template<>
22 void IntervalTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
24 if (this->period.value == 0) return;
26 this->storage.elapsed += delta;
28 uint count = 0;
29 while (this->storage.elapsed >= this->period.value) {
30 this->storage.elapsed -= this->period.value;
31 count++;
34 if (count > 0) {
35 this->callback(count);
39 template<>
40 void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
42 if (this->fired) return;
43 if (this->period.value == 0) return;
45 this->storage.elapsed += delta;
47 if (this->storage.elapsed >= this->period.value) {
48 this->callback();
49 this->fired = true;
53 template<>
54 bool TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
56 TimerGameTick::counter++;
58 for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
59 timer->Elapsed(delta);
62 return true;
65 #ifdef WITH_ASSERT
66 template<>
67 void TimerManager<TimerGameTick>::Validate(TimerGameTick::TPeriod period)
69 if (period.priority == TimerGameTick::Priority::NONE) return;
71 /* Validate we didn't make a developer error and scheduled more than one
72 * entry on the same priority. There can only be one timer on
73 * a specific priority, to ensure we are deterministic, and to avoid
74 * container sort order invariant issues with timer period saveload. */
75 for (const auto &timer : TimerManager<TimerGameTick>::GetTimers()) {
76 assert(timer->period.priority != period.priority);
79 #endif /* WITH_ASSERT */