reduce simulation steps in a frame if time exceeds 30fps, to avoid freezing the application
This commit is contained in:
parent
c1094980a6
commit
e0d0a297b9
1 changed files with 7 additions and 1 deletions
|
@ -2,6 +2,7 @@ use std::{
|
|||
fs::{read_dir, read_to_string},
|
||||
mem::transmute,
|
||||
ops::Rem,
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use raylib::prelude::*;
|
||||
|
@ -30,6 +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;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Editor {
|
||||
|
@ -178,7 +180,7 @@ impl Editor {
|
|||
}
|
||||
|
||||
fn step(&mut self) {
|
||||
let start_time = std::time::Instant::now();
|
||||
let start_time = Instant::now();
|
||||
self.machine.step();
|
||||
self.step_time = start_time.elapsed().as_micros();
|
||||
|
||||
|
@ -334,11 +336,15 @@ impl Editor {
|
|||
let step_size = 1. / (1 << self.sim_speed) as f32;
|
||||
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
|
||||
break;
|
||||
}
|
||||
if start_time.elapsed().as_micros() > MAX_FRAME_TIME_MICROS {
|
||||
break;
|
||||
}
|
||||
self.step();
|
||||
}
|
||||
self.time_since_step -= step_count as f32 * step_size;
|
||||
|
|
Loading…
Reference in a new issue