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
22/** Simple timer designed to be used in the update functions of
23 objects */
24class Timer final
25{
26public:
27 Timer();
28
29 /** start the timer with the given period (in seconds). If
30 cyclic=true then the timer will be reset after each period. Set
31 period to zero if you want to disable the timer. */
32 void start(float period, bool cyclic = false);
33
34 /** returns true if a period (or more) passed since start call or last
35 successful check */
36 bool check();
37
38 /** stop the timer */
39 void stop() { start(0); }
40
41 /** returns the period of the timer or 0 if it isn't started */
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
47private:
48 float m_period;
49 float m_cycle_start;
50 bool m_cyclic;
51
52private:
53 Timer(const Timer&) = delete;
54 Timer& operator=(const Timer&) = delete;
55};
56
57#endif
58
59/* EOF */
60