9b44b304d388fff1f9b1aa608a676db26783eda1
[supertux.git] / src / timer.h
1 #ifndef __SUPERTUX_TIMER_H__
2 #define __SUPERTUX_TIMER_H__
3
4 extern float global_time;
5
6 /**
7  * new simpler timer designed to be used in the update functions of objects
8  */
9 class Timer2 // TODO rename later
10 {
11 public:
12   Timer2();
13   ~Timer2();
14
15   /** start the timer with the given period. If cyclic=true then the timer willl
16    * be reset after each period.
17    * Set period to zero if you want to disable the timer.
18    */
19   void start(float period, bool cyclic = false);
20   /** returns true if a period (or more) passed during the last tick command */
21   bool check();
22
23   /** returns the period of the timer or 0 if it isn't started */
24   float get_period() const
25   { return period; }
26   float get_timeleft() const
27   { return period - (global_time - cycle_start); }
28   float get_timegone() const
29   { return global_time - cycle_start; }
30   bool started() const
31   { return period != 0 && get_timeleft() > 0; }
32
33 private:
34   float period;
35   float cycle_start;
36   bool cyclic;
37 };
38
39 #endif
40