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