measure step time over an entire frame

This commit is contained in:
Crispy 2024-12-07 20:37:33 +01:00
parent e6d8c1246f
commit 1d001be403

View file

@ -31,7 +31,7 @@ const MAX_ZOOM_IN: i32 = 3;
const BOARD_MARGIN: isize = 3; const BOARD_MARGIN: isize = 3;
const MAX_SPEED_POWER: u8 = 16; const MAX_SPEED_POWER: u8 = 16;
const SPEED_DIGITS: u8 = 5; 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)] #[derive(Debug)]
pub struct Editor { pub struct Editor {
@ -180,9 +180,7 @@ impl Editor {
} }
fn step(&mut self) { fn step(&mut self) {
let start_time = Instant::now();
self.machine.step(); self.machine.step();
self.step_time = start_time.elapsed().as_micros();
if self.complete_popup == Popup::Visible { if self.complete_popup == Popup::Visible {
self.complete_popup = Popup::Dismissed; self.complete_popup = Popup::Dismissed;
@ -334,9 +332,10 @@ impl Editor {
if self.sim_state == SimState::Running { if self.sim_state == SimState::Running {
self.time_since_step += rl.get_frame_time(); self.time_since_step += rl.get_frame_time();
let step_size = 1. / (1 << self.sim_speed) as f32; 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 { if self.time_since_step > step_size {
let step_count = (self.time_since_step / step_size) as u32; let step_count = (self.time_since_step / step_size) as u32;
let start_time = Instant::now();
for _ in 0..step_count { for _ in 0..step_count {
if self.sim_state != SimState::Running { if self.sim_state != SimState::Running {
// pause on level completion // pause on level completion
@ -346,9 +345,16 @@ impl Editor {
break; break;
} }
self.step(); self.step();
steps_taken += 1;
} }
self.time_since_step -= step_count as f32 * step_size; 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) { if rl.is_key_pressed(KeyboardKey::KEY_SPACE) {
self.step_pressed() self.step_pressed()