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  void pause();
43 
45  void resume();
46 
48  float get_period() const { return m_period; }
49  float get_timeleft() const { return m_period - (g_game_time - m_cycle_start); }
50  float get_timegone() const { return g_game_time - m_cycle_start; }
51  float get_progress() const { return get_timegone() / get_period(); }
52  bool started() const { return (m_period != 0 && get_timeleft() > 0); }
53  bool paused() const { return m_cycle_pause != 0; }
54 
55 private:
56  float m_period;
57  float m_cycle_start;
58  float m_cycle_pause;
59  bool m_cyclic;
60 
61 private:
62  Timer(const Timer&) = delete;
63  Timer& operator=(const Timer&) = delete;
64 };
65 
66 #endif
67 
68 /* EOF */
float get_period() const
returns the period of the timer or 0 if it isn&#39;t started
Definition: timer.hpp:48
void resume()
resume (unpause) the timer
Definition: timer.cpp:65
void start(float period, bool cyclic=false)
start the timer with the given period (in seconds).
Definition: timer.cpp:30
bool check()
returns true if a period (or more) passed since start call or last successful check ...
Definition: timer.cpp:39
void pause()
pause the timer
Definition: timer.cpp:57
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