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