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