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