Skip to content

Commit

Permalink
Force render in batches of at minimum roughly 100ms
Browse files Browse the repository at this point in the history
  • Loading branch information
fredizzimo committed Nov 27, 2023
1 parent 22976fb commit 9e7795f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/window/update_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ enum FocusedState {
}

const MAX_ANIMATION_DT: f32 = 1.0 / 120.0;
const MIN_ANIMATION_LENGTH: f32 = 0.1;

pub struct UpdateLoop {
idle: bool,
previous_frame_start: Instant,
last_dt: f32,
frame_dt_avg: NoSumSMA<f64, f64, 10>,
should_render: bool,
new_updates: bool,
num_consecutive_rendered: u32,
num_frames_since_update: u32,
focused: FocusedState,
}

Expand All @@ -41,7 +44,9 @@ impl UpdateLoop {
let last_dt = 0.0;
let frame_dt_avg = NoSumSMA::new();
let should_render = true;
let new_updates = true;
let num_consecutive_rendered = 0;
let num_frames_since_update = 0;
let focused = FocusedState::Focused;

Self {
Expand All @@ -50,7 +55,9 @@ impl UpdateLoop {
last_dt,
frame_dt_avg,
should_render,
new_updates,
num_consecutive_rendered,
num_frames_since_update,
focused,
}
}
Expand Down Expand Up @@ -85,7 +92,15 @@ impl UpdateLoop {
self.last_dt
}
.min(1.0);
self.should_render = window_wrapper.prepare_frame();
if self.new_updates {
self.num_frames_since_update = 0;
} else {
self.num_frames_since_update += 1;
}
self.new_updates = window_wrapper.prepare_frame();
self.should_render = self.new_updates;
let required_animation_frames = (MIN_ANIMATION_LENGTH / dt) as u32;
self.should_render |= self.num_frames_since_update < required_animation_frames;
let num_steps = (dt / MAX_ANIMATION_DT).ceil();
let step = dt / num_steps;
for _ in 0..num_steps as usize {
Expand Down Expand Up @@ -133,7 +148,8 @@ impl UpdateLoop {
return Err(());
}
Ok(Event::AboutToWait) | Err(false) => {
self.should_render |= window_wrapper.prepare_frame();
self.new_updates |= window_wrapper.prepare_frame();
self.should_render |= self.new_updates;
if self.should_render || !self.idle {
if window_wrapper.vsync.uses_winit_throttling() {
window_wrapper.windowed_context.window().request_redraw();
Expand All @@ -158,7 +174,7 @@ impl UpdateLoop {
window_wrapper.handle_window_settings_changed_events();

if let Ok(event) = event {
self.should_render |= window_wrapper.handle_event(event);
self.new_updates |= window_wrapper.handle_event(event);
}

let (_, deadline) = self.get_event_wait_time(&window_wrapper.vsync);
Expand Down

0 comments on commit 9e7795f

Please sign in to comment.