- added standard copyright header to every file
[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 void
57 Timer::init(bool st_ticks)
58 {
59   period    = 0;
60   time      = 0;
61   get_ticks = st_ticks ? st_get_ticks : SDL_GetTicks;
62 }
63
64 void
65 Timer::start(unsigned int period_)
66 {
67   time   = get_ticks();
68   period = period_;
69 }
70
71 void
72 Timer::stop()
73 {
74   if(get_ticks == st_get_ticks)
75     init(true);
76   else
77     init(false);
78 }
79
80 int
81 Timer::check()
82 {
83   if((time != 0) && (time + period > get_ticks()))
84     return true;
85   else
86     {
87       time = 0;
88       return false;
89     }
90 }
91
92 int
93 Timer::started()
94 {
95   if(time != 0)
96     return true;
97   else
98     return false;
99 }
100
101 int
102 Timer::get_left()
103 {
104   return (period - (get_ticks() - time));
105 }
106
107 int
108 Timer::get_gone()
109 {
110   return (get_ticks() - time);
111 }
112
113 void
114 Timer::fwrite(FILE* fi)
115 {
116   unsigned int diff_ticks;
117   int tick_mode;
118   if(time != 0)
119     diff_ticks = get_ticks() - time;
120   else
121     diff_ticks = 0;
122
123   ::fwrite(&period,sizeof(unsigned int),1,fi);
124   ::fwrite(&diff_ticks,sizeof(unsigned int),1,fi);
125   if(get_ticks == st_get_ticks)
126       tick_mode = true;
127   else
128       tick_mode = false;
129   ::fwrite(&tick_mode,sizeof(unsigned int),1,fi);
130 }
131
132 void
133 Timer::fread(FILE* fi)
134 {
135   unsigned int diff_ticks;
136   int tick_mode;
137
138   ::fread(&period,sizeof(unsigned int),1,fi);
139   ::fread(&diff_ticks,sizeof(unsigned int),1,fi);
140   ::fread(&tick_mode,sizeof(unsigned int),1,fi);
141
142   if (tick_mode)
143     get_ticks = st_get_ticks;
144   else
145     get_ticks = SDL_GetTicks;
146
147   if (diff_ticks != 0)
148     time = get_ticks() - diff_ticks;
149   else
150     time = 0;
151
152 }
153
154 /* EOF */