Skip to content

Commit

Permalink
Re-calculate delta
Browse files Browse the repository at this point in the history
  • Loading branch information
juchiast committed Mar 18, 2017
1 parent 9ffc32f commit 8f16317
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ impl Game {
vec3(0., config.camera_height, -config.camera_distance) + player.pos()
)
}
// Re-calculate fps
fn update_fps(&mut self) {
let d = self.state.last_frame.elapsed();
self.state.last_frame = Instant::now();
self.state.fps = 1. / (d.as_secs() as f64 + 1e-9*d.subsec_nanos() as f64);
}

pub fn run(&mut self) {
while let Some(e) = self.window.next() {
match e {
Input::Press(key) => self.press(key),
Input::Release(key) => self.release(key),
Input::Render(_) => {
// Calculate fps
let d = self.state.last_frame.elapsed();
self.state.last_frame = Instant::now();
self.state.fps = 1. / (d.as_secs() as f64 + 1e-9*d.subsec_nanos() as f64);
self.update_fps();
self.draw(&e);
},
Input::Update(args) => self.update(args.dt),
Expand Down Expand Up @@ -254,6 +257,9 @@ impl Game {
}
// `dt` stands for delta, duration since the last update
fn update(&mut self, dt: f64) {
// Re-calculate delta according to fps
let dt = if self.state.fps != 0. { 1./self.state.fps}
else { dt };
let old = self.world.player.position;
if self.state.bullets <= 0 {
self.state.recharge -= dt;
Expand Down

0 comments on commit 8f16317

Please sign in to comment.