X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.cc;fp=src%2Fmoof%2Ftimer.cc;h=82f8cfa3ec4dbf13a5a1962936b35f9d4295c226;hp=cd898ddd1c62b6b437f889e80741991b7142ff2e;hb=574af38ed616d1adfa5e6ce35f67cda1f707f89d;hpb=6c9943707d4f33035830eba0587a61a34eaecbc2 diff --git a/src/moof/timer.cc b/src/moof/timer.cc index cd898dd..82f8cfa 100644 --- a/src/moof/timer.cc +++ b/src/moof/timer.cc @@ -1,15 +1,15 @@ -/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +/*] Copyright (c) 2009-2011, Charles McGarvey [***************************** **] All rights reserved. * -* vi:ts=4 sw=4 tw=75 -* * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * -**************************************************************************/ +*****************************************************************************/ +#if HAVE_CONFIG_H #include "config.h" +#endif #include #include @@ -26,9 +26,7 @@ namespace moof { void timer::init(const function& function, - scalar seconds, - enum mode mode, - timer_source& source) + scalar seconds, enum mode mode, timer_source& source) { source_ = &source; @@ -60,7 +58,6 @@ void timer::invalidate() absolute_ = SCALAR(0.0); } - void timer::added_to_runloop(runloop& runloop) { detach_from_runloop(); @@ -76,29 +73,30 @@ void timer::detach_from_runloop() } } - void timer::fire(scalar t) { if (function_) function_(*this, t); if (mode_ == repeat) { - if (is_equal(absolute_, t, 1.0)) absolute_ += interval_; - else absolute_ = interval_ + t; + if (is_equal(absolute_, t, 1.0)) + absolute_ += interval_; + else + absolute_ = interval_ + t; + // TODO error accumulates in absolute var + } + else + { + invalidate(); } - else invalidate(); } - scalar timer::ticks() { return default_source().ticks(); } - - #if ENABLE_CLOCK_GETTIME - class real_time : public timer_source { public: @@ -109,7 +107,6 @@ public: reset(); } - scalar ticks() const { struct timespec ts; @@ -117,9 +114,9 @@ public: ASSERT(result == 0 && "monotonic clock not available"); return reference_ + - (scalar(ts.tv_sec - start_.tv_sec) + - scalar(ts.tv_nsec - start_.tv_nsec) * - SCALAR(0.000000001)) * scale_; + (scalar(ts.tv_sec - start_.tv_sec) + + scalar(ts.tv_nsec - start_.tv_nsec) * + SCALAR(0.000000001)) * scale_; } void reset() @@ -135,15 +132,13 @@ public: scale_ = factor; } - private: - scalar reference_; + scalar reference_; struct timespec start_; - scalar scale_; + scalar scale_; }; - void timer::sleep(scalar seconds, enum mode mode) { if (mode == absolute) seconds -= ticks(); @@ -154,24 +149,21 @@ void timer::sleep(scalar seconds, enum mode mode) ts.tv_nsec = (seconds - scalar(ts.tv_sec)) * SCALAR(1000000000.0); int ret; - do ret = nanosleep(&ts, &ts); while (ret == -1 && errno == EINTR); + do ret = nanosleep(&ts, &ts); + while (ret == -1 && errno == EINTR); } - #else // ! ENABLE_CLOCK_GETTIME - - class real_time : public timer_source { public: real_time() : - scale_(SCALAR(1.0)) + scale_(SCALAR(0.001)) { reset(); } - scalar ticks() const { return reference_ + scalar(SDL_GetTicks() - start_) * scale_; @@ -190,7 +182,6 @@ public: scale_ = factor * SCALAR(0.001); } - private: scalar reference_; @@ -198,17 +189,14 @@ private: scalar scale_; }; - void timer::sleep(scalar seconds, enum mode mode) { if (mode == absolute) seconds -= ticks(); if (seconds < SCALAR(0.0)) return; SDL_Delay(seconds * SCALAR(1000.0)); } - #endif // ENABLE_CLOCK_GETTIME - timer_source& timer::default_source() { static real_time t;