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