]> Dogcows Code - chaz/yoink/blob - src/Moof/Timer.cc
new timer class
[chaz/yoink] / src / Moof / Timer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cerrno>
30 #include <ctime>
31 #include <limits>
32 #include <stdexcept>
33
34 #include "Log.hh"
35 #include "Timer.hh"
36
37 #if HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <SDL/SDL.h>
42
43
44 namespace Mf {
45
46
47 Scalar Timer::nextFire_ = std::numeric_limits<Scalar>::max();
48 std::map<unsigned,Timer&> Timer::timers_;
49
50
51 unsigned Timer::getNewID()
52 {
53 static unsigned id = 1;
54 return id++;
55 }
56
57
58 void Timer::init(const Function& function, Scalar seconds, Mode mode)
59 {
60 invalidate();
61
62 mode_ = mode;
63
64 if (mode_ != INVALID)
65 {
66 function_ = function;
67
68 if (mode == ABSOLUTEE)
69 {
70 absolute_ = seconds;
71 }
72 else
73 {
74 absolute_ = seconds - getTicks();
75 interval_ = seconds;
76 }
77
78 id_ = getNewID();
79 timers_.insert(std::pair<unsigned,Timer&>(id_, *this));
80
81 if (absolute_ < nextFire_) nextFire_ = absolute_;
82 }
83 }
84
85
86 bool Timer::isValid() const
87 {
88 return mode_ != INVALID;
89 }
90
91 void Timer::invalidate()
92 {
93 if (mode_ != INVALID)
94 {
95 timers_.erase(id_);
96 mode_ = INVALID;
97
98 if (isEqual(absolute_, nextFire_)) nextFire_ = findNextFire();
99 }
100 }
101
102
103 void Timer::fire()
104 {
105 Scalar t = getTicks();
106
107 if (function_) function_(*this, t);
108
109 if (isRepeating())
110 {
111 Scalar absolute = absolute_;
112
113 if (isEqual(absolute_, t, 1.0)) absolute_ += interval_;
114 else absolute_ = interval_ + t;
115
116 if (isEqual(absolute, nextFire_)) nextFire_ = findNextFire();
117 }
118 else
119 {
120 invalidate();
121 }
122 }
123
124
125 Scalar Timer::findNextFire()
126 {
127 std::map<unsigned,Timer&>::iterator it;
128 Scalar nextFire = std::numeric_limits<Scalar>::max();
129
130 for (it = timers_.begin(); it != timers_.end(); ++it)
131 {
132 Scalar absolute = (*it).second.absolute_;
133 if (absolute < nextFire) nextFire = absolute;
134 }
135
136 return nextFire;
137 }
138
139
140 Scalar Timer::getSecondsRemaining() const
141 {
142 return absolute_ - getTicks();
143 }
144
145 bool Timer::isExpired() const
146 {
147 return getSecondsRemaining() < 0.0;
148 }
149
150 bool Timer::isRepeating() const
151 {
152 return mode_ == REPEAT;
153 }
154
155
156 void Timer::fireIfExpired(Scalar t)
157 {
158 std::map<unsigned,Timer&>::iterator it;
159
160 if (nextFire_ > t) return;
161
162 for (it = timers_.begin(); it != timers_.end(); ++it)
163 {
164 Timer& timer = (*it).second;
165 if (timer.isExpired()) timer.fire();
166 }
167 }
168
169
170 #if HAVE_CLOCK_GETTIME
171
172 // Since the monotonic clock will provide us with the timer since the computer
173 // started, the number of seconds since that time could easily become so large
174 // that it cannot be accurately stored in a float (even with as little two days
175 // uptime), therefore we need to start from a more recent reference (when the
176 // program starts). Of course this isn't much of an issue if scalar is a
177 // double-precision number.
178
179 static time_t setReference_()
180 {
181 struct timespec ts;
182
183 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
184 {
185 return 0;
186 }
187
188 return ts.tv_sec;
189 }
190
191 static const time_t reference = setReference_();
192
193
194 Scalar Timer::getTicks()
195 {
196 struct timespec ts;
197
198 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
199 {
200 throw std::runtime_error("cannot access monotonic clock");
201 }
202
203 return Scalar(ts.tv_sec - reference) + Scalar(ts.tv_nsec) / 1000000000.0;
204 }
205
206 void Timer::sleep(Scalar seconds, bool absolute)
207 {
208 struct timespec ts;
209 int ret;
210
211 if (absolute) seconds -= getTicks();
212 ts.tv_sec = time_t(seconds);
213 ts.tv_nsec = long((seconds - Scalar(ts.tv_sec)) * 1000000000.0);
214
215 do
216 {
217 ret = nanosleep(&ts, &ts);
218 }
219 while (ret == -1 && errno == EINTR);
220 }
221
222
223 #else // ! HAVE_CLOCK_GETTIME
224
225
226 // If we don't have posix timers, we'll have to use a different timing method.
227 // SDL only promises centisecond accuracy, but that's better than a kick in the
228 // butt.
229
230 Scalar Timer::getTicks()
231 {
232 Uint32 ms = SDL_GetTicks();
233 return Scalar(ms / 1000) + Scalar(ms % 1000) / 1000.0;
234 }
235
236 void Timer::sleep(Scalar seconds, bool absolute)
237 {
238 if (absolute) seconds -= getTicks();
239 SDL_Delay(Uint32(cml::clamp(int(seconds * 1000.0), 0, 1000)));
240 }
241
242 #endif // HAVE_CLOCK_GETTIME
243
244
245 } // namespace Mf
246
247 /** vim: set ts=4 sw=4 tw=80: *************************************************/
248
This page took 0.039907 seconds and 4 git commands to generate.