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