use super::state::{State, StateMachine};
use super::timing::{Duration, SteadyTime, Stopwatch};
pub struct Application {
delta_time: Duration,
fixed_step: Duration,
last_fixed_update: SteadyTime,
states: StateMachine,
timer: Stopwatch,
}
impl Application {
pub fn new<T: 'static>(initial_state: T) -> Application
where T: State
{
Application {
delta_time: Duration::zero(),
fixed_step: Duration::microseconds(16666),
last_fixed_update: SteadyTime::now(),
states: StateMachine::new(initial_state),
timer: Stopwatch::new(),
}
}
pub fn run(&mut self) {
self.initialize();
while self.states.is_running() {
self.timer.restart();
self.advance_frame();
self.timer.stop();
self.delta_time = self.timer.elapsed()
}
self.shutdown();
}
fn initialize(&mut self) {
self.states.start();
}
fn advance_frame(&mut self) {
while SteadyTime::now() - self.last_fixed_update > self.fixed_step {
self.states.fixed_update(self.fixed_step);
self.last_fixed_update = self.last_fixed_update + self.fixed_step;
}
self.states.update(self.delta_time);
}
fn shutdown(&mut self) {
}
}