OpenTTD
guitimer_func.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef GUITIMER_FUNC_H
13 #define GUITIMER_FUNC_H
14 
15 class GUITimer
16 {
17 protected:
18  uint timer;
19  uint interval;
20 
21 public:
22  GUITimer() : timer(0), interval(0) { }
23  explicit GUITimer(uint interval) : timer(0), interval(interval) { }
24 
25  inline bool HasElapsed() const
26  {
27  return this->interval == 0;
28  }
29 
30  inline void SetInterval(uint interval)
31  {
32  this->timer = 0;
33  this->interval = interval;
34  }
35 
42  inline uint CountElapsed(uint delta)
43  {
44  if (this->interval == 0) return 0;
45  uint count = delta / this->interval;
46  if (this->timer + (delta % this->interval) >= this->interval) count++;
47  this->timer = (this->timer + delta) % this->interval;
48  return count;
49  }
50 
57  inline bool Elapsed(uint delta)
58  {
59  if (this->CountElapsed(delta) == 0) return false;
60  this->SetInterval(0);
61  return true;
62  }
63 };
64 
65 #endif /* GUITIMER_FUNC_H */
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:42
bool Elapsed(uint delta)
Test if a timer has elapsed.
Definition: guitimer_func.h:57