8dd9400f9cc5fa28c800497ad55975141115dacc
[supertux.git] / src / timer.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #ifndef __SUPERTUX_TIMER_H__
21 #define __SUPERTUX_TIMER_H__
22
23 extern float game_time;
24 extern float real_time;
25
26 /**
27  * Simple timer designed to be used in the update functions of objects
28  */
29 class Timer
30 {
31 public:
32   Timer();
33   ~Timer();
34
35   /** start the timer with the given period (in seconds).
36    * If cyclic=true then the timer willl be reset after each period.
37    * Set period to zero if you want to disable the timer.
38    */
39   void start(float period, bool cyclic = false);
40   /** returns true if a period (or more) passed since start call or last
41    * successfull check
42    */
43   bool check();
44   /** stop the timer */
45   void stop()
46   { start(0); }
47
48   /** returns the period of the timer or 0 if it isn't started */
49   float get_period() const
50   { return period; }
51   float get_timeleft() const
52   { return period - (game_time - cycle_start); }
53   float get_timegone() const
54   { return game_time - cycle_start; }
55   bool started() const
56   { return period != 0 && get_timeleft() > 0; }
57
58 private:
59   float period;
60   float cycle_start;
61   bool cyclic;
62 };
63
64 #endif