C++ifyied SuperTux ticks.
[supertux.git] / lib / special / timer.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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
21 #ifndef SUPERTUX_TIMER_H
22 #define SUPERTUX_TIMER_H
23
24 namespace SuperTux
25   {
26
27   class Ticks
28     {
29     public:
30       /// Time a game is running. (Non-pause mode, etc.)
31       static unsigned int get(void);
32
33       static void pause_init(void);
34       static void pause_start(void);
35       static void pause_stop(void);
36       static bool pause_started(void);
37
38     private:
39       static unsigned int pause_ticks;
40       static unsigned int pause_count;
41
42     };
43
44   /// Timer
45   /** This class can be used as stop watch
46       for example. It's also possible to calculate
47       frames per seconds and things like that with it.
48       It's a general timing class, but it
49       can esspecially be used together with Ticks::get(). */
50   class Timer
51     {
52     public:
53       unsigned int period;
54       unsigned int time;
55       unsigned int (*get_ticks) (void);
56
57     public:
58       Timer();
59
60       /// Initialize the timer.
61       /** @param st_ticks: If true internally Ticks::get() is used, else SDL_GetTicks() is used. */
62       void init(bool game_ticks);
63
64       /// Start the timer with the given period (in ms).
65       void start(unsigned int period);
66
67       /// Stop the timer.
68       void stop();
69
70       /// Check if the timer is started and within its period.
71       /** If one of these requirements isn't the case the timer
72           is automatically reseted. */
73       int check();
74
75       /// Is the timer started?
76       int started();
77
78       /// Get time left until the last timing period is finished.
79       /** The returned value can be negative. */
80       int get_left();
81
82       /// Get the gone time, since last timer start.
83       /** The returned value can be negative. */
84       int  get_gone();
85
86       /// Write the timer value to a file (For save games in example).
87       void fwrite(FILE* fi);
88       /// Read a timer value from a file (From save games in example).
89       void fread(FILE* fi);
90     };
91
92 } //namespace SuperTux
93
94 #endif /*SUPERTUX_TIMER_H*/
95
96 /* Local Variables: */
97 /* mode:c++ */
98 /* End: */