OpenTTD Source: src/guitimer_func.h Source File

OpenTTD Source 12.2

guitimer_func.h
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
10 #ifndef GUITIMER_FUNC_H
11 #define GUITIMER_FUNC_H
12
13 class GUITimer
14 {
15 protected:
16 uint timer;
17 uint interval;
18
19 public:
20 GUITimer() : timer(0), interval(0) { }
21 explicit GUITimer(uint interval) : timer(0), interval(interval) { }
22
23 inline bool HasElapsed() const
24 {
25 return this->interval == 0;
26 }
27
28 inline void SetInterval(uint interval)
29 {
30 this->timer = 0;
31 this->interval = interval;
32 }
33
40 inline uint CountElapsed(uint delta)
41 {
42 if (this->interval == 0) return 0;
43 uint count = delta / this->interval;
44 if (this->timer + (delta % this->interval) >= this->interval) count++;
45 this->timer = (this->timer + delta) % this->interval;
46 return count;
47 }
48
55 inline bool Elapsed(uint delta)
56 {
57 if (this->CountElapsed(delta) == 0) return false;
58 this->SetInterval(0);
59 return true;
60 }
61 };
62
63 #endif /* GUITIMER_FUNC_H */
GUITimer
Definition: guitimer_func.h:13
GUITimer::CountElapsed
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:40
GUITimer::Elapsed
bool Elapsed(uint delta)
Test if a timer has elapsed.
Definition: guitimer_func.h:55