X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.cc;h=9b105b4c991fcb73582ff0ad86ca73091ef5c85d;hp=5bb2092af68591a8dc6fa3df03dd5d0ecd736b3e;hb=d6990468d297a6cbee98e4d0d33ab37e1b2352c9;hpb=51069fee9139ab8d14ecc80dffbe5deecb73d9e0 diff --git a/src/moof/timer.cc b/src/moof/timer.cc index 5bb2092..9b105b4 100644 --- a/src/moof/timer.cc +++ b/src/moof/timer.cc @@ -24,24 +24,15 @@ namespace moof { -scalar timer::next_expiration_ = std::numeric_limits::max(); -hash timer::timers_; - - -unsigned timer::new_identifier() -{ - static unsigned id = 1; - return id++; -} - - -void timer::init(const function& function, scalar seconds, mode mode) +void timer::init(const function& function, + scalar seconds, + mode mode, + runloop* runloop) { invalidate(); + ASSERT(runloop && "can't schedule timer without a runloop"); - mode_ = mode; - - if (mode_ != invalid) + if ((mode_ = mode) != invalid) { function_ = function; @@ -55,109 +46,39 @@ void timer::init(const function& function, scalar seconds, mode mode) interval_ = seconds; } - id_ = new_identifier(); - timers_.insert(std::pair(id_, this)); - - if (absolute_ < next_expiration_) next_expiration_ = absolute_; + runloop->add_timer(this); + runloop_ = runloop; } } -bool timer::is_valid() const -{ - return mode_ != invalid; -} - void timer::invalidate() { if (mode_ != invalid) { - timers_.erase(id_); mode_ = invalid; + absolute_ = SCALAR(0.0); - if (is_equal(absolute_, next_expiration_)) - { - next_expiration_ = find_next_expiration(); - } + runloop_->remove_timer(this); + runloop_ = 0; } } -void timer::fire() +void timer::fire(scalar t) { - scalar t = ticks(); - if (function_) function_(*this, t); if (is_repeating()) { - scalar absolute = absolute_; - if (is_equal(absolute_, t, 1.0)) absolute_ += interval_; else absolute_ = interval_ + t; - - if (is_equal(absolute, next_expiration_)) - { - next_expiration_ = find_next_expiration(); - } - } - else - { - invalidate(); } + else invalidate(); } -scalar timer::find_next_expiration() -{ - scalar next_fire = std::numeric_limits::max(); - - hash::iterator it; - for (it = timers_.begin(); it != timers_.end(); ++it) - { - scalar absolute = (*it).second->absolute_; - if (absolute < next_fire) next_fire = absolute; - } - - return next_fire; -} - - -scalar timer::seconds_remaining() const -{ - return absolute_ - ticks(); -} - -bool timer::is_expired() const -{ - return seconds_remaining() < 0.0; -} - -bool timer::is_repeating() const -{ - return mode_ == repeat; -} - - -void timer::fire_expired_timers() -{ - fire_expired_timers(ticks()); -} - -void timer::fire_expired_timers(scalar t) -{ - if (next_expiration_ > t) return; - - hash::iterator it; - for (it = timers_.begin(); it != timers_.end(); ++it) - { - timer* timer = (*it).second; - if (timer->is_expired()) timer->fire(); - } -} - - -#if USE_CLOCK_GETTIME +#if ENABLE_CLOCK_GETTIME // Since the monotonic clock will provide us with the time since the // computer started, the number of seconds since that time could easily @@ -189,46 +110,43 @@ scalar timer::ticks() ASSERT(result == 0 && "cannot access clock"); return scalar(ts.tv_sec - reference_) + - scalar(ts.tv_nsec) / 1000000000.0; + scalar(ts.tv_nsec) * SCALAR(0.000000001); } void timer::sleep(scalar seconds, mode mode) { - struct timespec ts; - int ret; - if (mode == absolute) seconds -= ticks(); - ts.tv_sec = time_t(seconds); - ts.tv_nsec = long((seconds - scalar(ts.tv_sec)) * 1000000000.0); + if (seconds < SCALAR(0.0)) return; - do - { - ret = nanosleep(&ts, &ts); - } - while (ret == -1 && errno == EINTR); + struct timespec ts; + ts.tv_sec = seconds; + ts.tv_nsec = (seconds - scalar(ts.tv_sec)) * SCALAR(1000000000.0); + + int ret; + do ret = nanosleep(&ts, &ts); while (ret == -1 && errno == EINTR); } -#else // ! USE_CLOCK_GETTIME +#else // ! ENABLE_CLOCK_GETTIME // If we don't have posix timers, we'll have to use a different timing // method. SDL only promises centisecond accuracy, but that's better than -// a kick in the pants. +// a kick in the pants. It could end up being just as good anyway. scalar timer::ticks() { - Uint32 ms = SDL_GetTicks(); - return scalar(ms / 1000) + scalar(ms % 1000) / 1000.0; + return scalar(SDL_GetTicks()) * SCALAR(0.001); } void timer::sleep(scalar seconds, mode mode) { if (mode == absolute) seconds -= ticks(); - SDL_Delay(Uint32(clamp(int(seconds * 1000.0), 0, 1000))); + if (seconds < SCALAR(0.0)) return; + SDL_Delay(seconds * SCALAR(1000.0)); } -#endif // USE_CLOCK_GETTIME +#endif // ENABLE_CLOCK_GETTIME } // namespace moof