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

OpenTTD Source 14.0-RC3

timer_game_tick.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 "timer.h"
15 #include "timer_game_tick.h"
16
17 #include "../safeguards.h"
18
19 TimerGameTick::TickCounter TimerGameTick::counter = 0;
20
21 template<>
22 void IntervalTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
23 {
24 if (this->period == 0) return;
25
26 this->storage.elapsed += delta;
27
28 uint count = 0;
29 while (this->storage.elapsed >= this->period) {
30 this->storage.elapsed -= this->period;
31 count++;
32 }
33
34 if (count > 0) {
35 this->callback(count);
36 }
37 }
38
39 template<>
40 void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
41 {
42 if (this->fired) return;
43 if (this->period == 0) return;
44
45 this->storage.elapsed += delta;
46
47 if (this->storage.elapsed >= this->period) {
48 this->callback();
49 this->fired = true;
50 }
51 }
52
53 template<>
54 bool TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
55 {
56 TimerGameTick::counter++;
57
58 for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
59 timer->Elapsed(delta);
60 }
61
62 return true;
63 }
64
65 #ifdef WITH_ASSERT
66 template<>
67 void TimerManager<TimerGameTick>::Validate(TimerGameTick::TPeriod)
68 {
69 }
70 #endif /* WITH_ASSERT */
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition: timer_game_tick.h:33
BaseTimer::period
TPeriod period
The period of the timer.
Definition: timer.h:49
TimerGameTick::TickCounter
uint64_t TickCounter
The type that the tick counter is stored in.
Definition: timer_game_tick.h:25
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_tick.h
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.
BaseTimer::storage
TStorage storage
The storage of the timer.
Definition: timer.h:50