diff --git a/src/editor.rs b/src/editor.rs index 0cb0d7c..ef162d5 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -31,7 +31,7 @@ const MAX_ZOOM_IN: i32 = 3; const BOARD_MARGIN: isize = 3; const MAX_SPEED_POWER: u8 = 16; const SPEED_DIGITS: u8 = 5; -const MAX_FRAME_TIME_MICROS: u128 = 1000_000 / 30; +const MAX_FRAME_TIME_MICROS: u128 = 1_000_000 / 30; #[derive(Debug)] pub struct Editor { @@ -180,9 +180,7 @@ impl Editor { } fn step(&mut self) { - let start_time = Instant::now(); self.machine.step(); - self.step_time = start_time.elapsed().as_micros(); if self.complete_popup == Popup::Visible { self.complete_popup = Popup::Dismissed; @@ -334,9 +332,10 @@ impl Editor { if self.sim_state == SimState::Running { self.time_since_step += rl.get_frame_time(); let step_size = 1. / (1 << self.sim_speed) as f32; + let mut steps_taken = 0; + let start_time = Instant::now(); if self.time_since_step > step_size { let step_count = (self.time_since_step / step_size) as u32; - let start_time = Instant::now(); for _ in 0..step_count { if self.sim_state != SimState::Running { // pause on level completion @@ -346,9 +345,16 @@ impl Editor { break; } self.step(); + steps_taken += 1; } self.time_since_step -= step_count as f32 * step_size; } + let avg_step_time = start_time + .elapsed() + .as_micros() + .checked_div(steps_taken) + .unwrap_or_default(); + self.step_time = avg_step_time; } if rl.is_key_pressed(KeyboardKey::KEY_SPACE) { self.step_pressed()