Skip to content

Commit

Permalink
Do not re-draw when out of focus
Browse files Browse the repository at this point in the history
Some optimization, no need to refresh GUI when app is out of focus
  • Loading branch information
adamws committed Dec 20, 2023
1 parent 8a28631 commit 9209e83
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,18 +942,13 @@ pub fn app<P: AsRef<Path>>(
status_bar.reposition(canvas.viewport().bottom_left());

// app logic handling:
let mut redraw: bool = true;
let mut drag = drag_module::Drag::new();
let mut event_pump = sdl_context.event_pump()?;

'running: loop {
let frame_start = std::time::Instant::now();

canvas.set_draw_color(Color::RGBA(255, 255, 255, 255));
canvas.clear();

let viewport = canvas.viewport();
let mut center = viewport.center();

for event in event_pump.poll_iter() {
match event {
Event::Quit { .. } => break 'running,
Expand All @@ -964,6 +959,11 @@ pub fn app<P: AsRef<Path>>(
Some(sdl2::keyboard::Keycode::Escape) => break 'running,
_ => {}
},
Event::Window { win_event, .. } => match win_event {
sdl2::event::WindowEvent::FocusGained => redraw = true,
sdl2::event::WindowEvent::FocusLost => redraw = false,
_ => {}
},
Event::MouseWheel { y, .. } => {
let new_scale = if y > 0 { scale * 2.0 } else { scale / 2.0 };

Expand Down Expand Up @@ -1000,32 +1000,38 @@ pub fn app<P: AsRef<Path>>(
_ => {}
}
}
let mouse_state = event_pump.mouse_state();

drag.update(&mouse_state);
center += drag.get();
if redraw || testing.is_some() {
canvas.set_draw_color(Color::RGBA(255, 255, 255, 255));
canvas.clear();

let viewport = canvas.viewport();
let mut center = viewport.center();

workarea.center_on(center);
workarea.draw(&mut canvas)?;
let mouse_state = event_pump.mouse_state();

diff.center_on(center);
diff.update(&mouse_state);
diff.draw(&mut canvas)?;
drag.update(&mouse_state);
center += drag.get();

status_bar.update(
mouse_state.x() - workarea.position.x(),
mouse_state.y() - workarea.position.y(),
diff.split as i32,
scale,
);
status_bar
.reposition(viewport.bottom_left() - Point::new(0, status_bar.size().1 as i32));
status_bar.draw(&mut canvas)?;
workarea.center_on(center);
workarea.draw(&mut canvas)?;

canvas.present();
diff.center_on(center);
diff.update(&mouse_state);
diff.draw(&mut canvas)?;

let frame_duration = frame_start.elapsed().as_micros() as u64;
trace!("Frame duration: {}us", frame_duration);
status_bar.update(
mouse_state.x() - workarea.position.x(),
mouse_state.y() - workarea.position.y(),
diff.split as i32,
scale,
);
status_bar
.reposition(viewport.bottom_left() - Point::new(0, status_bar.size().1 as i32));
status_bar.draw(&mut canvas)?;

canvas.present();
}

match testing {
Some(val) => {
Expand All @@ -1042,7 +1048,14 @@ pub fn app<P: AsRef<Path>>(
_ => {}
}

::std::thread::sleep(std::time::Duration::new(0, 1_000_000_000u32 / 30));
let frame_duration = frame_start.elapsed();
trace!("Frame duration: {}us", frame_duration.as_micros() as u64);

// cap to ~30 FPS
match std::time::Duration::from_millis(33).checked_sub(frame_duration) {
Some(t) => ::std::thread::sleep(t),
_ => {}
}
}

Ok(())
Expand Down

0 comments on commit 9209e83

Please sign in to comment.