supertux
timer.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_SUPERTUX_TIMER_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_TIMER_HPP
19 
20 #include "supertux/globals.hpp"
21 
24 class Timer final
25 {
26 public:
27  Timer();
28 
32  void start(float period, bool cyclic = false);
33 
36  bool check();
37 
39  void stop() { start(0); }
40 
42  float get_period() const { return m_period; }
43  float get_timeleft() const{ return m_period - (g_game_time - m_cycle_start); }
44  float get_timegone() const { return g_game_time - m_cycle_start; }
45  bool started() const { return m_period != 0 && get_timeleft() > 0; }
46 
47 private:
48  float m_period;
49  float m_cycle_start;
50  bool m_cyclic;
51 
52 private:
53  Timer(const Timer&) = delete;
54  Timer& operator=(const Timer&) = delete;
55 };
56 
57 #endif
58 
59 /* EOF */
float get_period() const
returns the period of the timer or 0 if it isn&#39;t started
Definition: timer.hpp:42
void start(float period, bool cyclic=false)
start the timer with the given period (in seconds).
Definition: timer.cpp:29
bool check()
returns true if a period (or more) passed since start call or last successful check ...
Definition: timer.cpp:37
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
void stop()
stop the timer
Definition: timer.hpp:39