Introduction of SuperTux namespace.
[supertux.git] / lib / special / timer.cpp
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 #include "SDL.h"
22 #include "special/timer.h"
23
24 unsigned int st_pause_ticks, st_pause_count;
25
26 unsigned int st_get_ticks(void)
27 {
28   if(st_pause_count != 0)
29     return /*SDL_GetTicks()*/ - st_pause_ticks /*- SDL_GetTicks()*/ + st_pause_count;
30   else
31     return SDL_GetTicks() - st_pause_ticks;
32 }
33
34 void st_pause_ticks_init(void)
35 {
36   st_pause_ticks = 0;
37   st_pause_count = 0;
38 }
39
40 void st_pause_ticks_start(void)
41 {
42   if(st_pause_count == 0)
43     st_pause_count = SDL_GetTicks();
44 }
45
46 void st_pause_ticks_stop(void)
47 {
48 if(st_pause_count == 0)
49 return;
50
51   st_pause_ticks += SDL_GetTicks() - st_pause_count;
52   st_pause_count = 0;
53 }
54
55 bool st_pause_ticks_started(void)
56 {
57 if(st_pause_count == 0)
58 return false;
59 else
60 return true;
61 }
62
63 Timer::Timer()
64 {
65   init(true);
66 }
67
68 void
69 Timer::init(bool st_ticks)
70 {
71   period    = 0;
72   time      = 0;
73   get_ticks = st_ticks ? st_get_ticks : SDL_GetTicks;
74 }
75
76 void
77 Timer::start(unsigned int period_)
78 {
79   time   = get_ticks();
80   period = period_;
81 }
82
83 void
84 Timer::stop()
85 {
86   if(get_ticks == st_get_ticks)
87     init(true);
88   else
89     init(false);
90 }
91
92 int
93 Timer::check()
94 {
95   if((time != 0) && (time + period > get_ticks()))
96     return true;
97   else
98     {
99       time = 0;
100       return false;
101     }
102 }
103
104 int
105 Timer::started()
106 {
107   if(time != 0)
108     return true;
109   else
110     return false;
111 }
112
113 int
114 Timer::get_left()
115 {
116   return (period - (get_ticks() - time));
117 }
118
119 int
120 Timer::get_gone()
121 {
122   return (get_ticks() - time);
123 }
124
125 void
126 Timer::fwrite(FILE* fi)
127 {
128   unsigned int diff_ticks;
129   int tick_mode;
130   if(time != 0)
131     diff_ticks = get_ticks() - time;
132   else
133     diff_ticks = 0;
134
135   ::fwrite(&period,sizeof(unsigned int),1,fi);
136   ::fwrite(&diff_ticks,sizeof(unsigned int),1,fi);
137   if(get_ticks == st_get_ticks)
138       tick_mode = true;
139   else
140       tick_mode = false;
141   ::fwrite(&tick_mode,sizeof(unsigned int),1,fi);
142 }
143
144 void
145 Timer::fread(FILE* fi)
146 {
147   unsigned int diff_ticks;
148   int tick_mode;
149
150   ::fread(&period,sizeof(unsigned int),1,fi);
151   ::fread(&diff_ticks,sizeof(unsigned int),1,fi);
152   ::fread(&tick_mode,sizeof(unsigned int),1,fi);
153
154   if (tick_mode)
155     get_ticks = st_get_ticks;
156   else
157     get_ticks = SDL_GetTicks;
158
159   if (diff_ticks != 0)
160     time = get_ticks() - diff_ticks;
161   else
162     time = 0;
163
164 }
165
166 /* EOF */