application::application(settings& settings) :
- next_update_(timer::ticks()),
- total_time_(SCALAR(0.0))
+ last_update_(timer::ticks()),
+ accum_(SCALAR(0.0))
{
unsigned random_seed;
if (settings.get("rngseed", random_seed)) srand(random_seed);
settings.get("framerate", framerate);
framerate = SCALAR(1.0) / framerate;
- update_timer_.init(boost::bind(&application::dispatch_update, this, _1, _2),
- timestep_, timer::repeat, this);
- draw_timer_.init(boost::bind(&application::dispatch_draw, this, _1, _2),
- framerate, timer::repeat, this);
+ //timer::default_source().scale(SCALAR(0.76));
+
+ //update_timer_.init(boost::bind(&application::dispatch_update,
+ //this, _1, _2),
+ //timestep_, timer::repeat);
+ //add_timer(update_timer_);
+
+ game_time_.reset(timestep_);
+
+ draw_timer_.init(boost::bind(&application::dispatch_draw,
+ this, _1, _2), framerate, timer::repeat);
+ add_timer(draw_timer_);
}
}
- const int MAX_FRAMESKIP = 15;
+ const int MAX_CONSECUTIVE_UPDATES = 15;
+
+ log_debug("updating", timer.expiration(), "/", t);
+
+ scalar deltaTime = t - last_update_;
+ accum_ += deltaTime;
int i = 0;
- while (next_update_ < t && ++i < MAX_FRAMESKIP)
+ while (timestep_ <= accum_ && i < MAX_CONSECUTIVE_UPDATES)
{
- total_time_ += timestep_;
- update(total_time_, timestep_);
-
- next_update_ += timestep_;
+ log_debug("UPDATING");
+ update(game_time_.ticks(), timestep_);
+ game_time_.step();
+ accum_ -= timestep_;
}
+
+ last_update_ = t;
}
void application::dispatch_draw(timer& timer, scalar t)
{
- scalar alpha = (t + timestep_ - next_update_) * inverse_timestep_;
- if (alpha < SCALAR(0.0)) log_error("UH OH!!!!! It's NEGATIVE", alpha);
- if (alpha > SCALAR(1.0)) log_error("UH OH!!!!! It's POSITIVE", alpha);
+ log_debug("draw", timer.expiration(), "/", t);
+
+ // XXX temporary
+ thread::main_runloop().run_once();
+
+ dispatch_update(timer, t);
+
+ scalar alpha = accum_ * inverse_timestep_;
+
+ alpha = cml::clamp(alpha, SCALAR(-1.0), SCALAR(2.0));
+ if (alpha < SCALAR(0.0)) log_warning("alpha:", alpha);
+ else if (alpha > SCALAR(1.0)) log_warning("alpha:", alpha);
+ else log_debug("alpha:", alpha);
draw(alpha);
- video::current()->swap();
+
+ scalar begin = timer::ticks();
+ video::current()->swap(t);
+
+ scalar duration = timer::ticks() - begin;
+ log_debug("flip duration:", duration);
}