From 2e2dccc033c89d0e3ddcd534ac4f748beb32c48d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 20 Mar 2022 21:28:43 -0700 Subject: [PATCH] Improve pausing --- src/main.rs | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index e796a94..36731e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -163,16 +163,9 @@ fn main() { window.close(); break; } - Event::GainedFocus => { - if paused && paused_from_lost_focus { - toggle_pause = true; - } - } Event::LostFocus => { - if !paused { - toggle_pause = true; - paused_from_lost_focus = true; - } + toggle_pause = true; + paused_from_lost_focus = true; } Event::KeyPressed { code, @@ -182,17 +175,17 @@ fn main() { system: _, } => match code { Key::ESCAPE => toggle_pause = true, - Key::SPACE => snap = !paused, - Key::UP => rotate = !paused, - Key::DOWN => fast_forward = !paused, + Key::SPACE => snap = true, + Key::UP => rotate = true, + Key::DOWN => fast_forward = true, Key::LEFT => { - move_left = !paused; - move_left_immediate = !paused; + move_left = true; + move_left_immediate = true; move_clock.restart(); } Key::RIGHT => { - move_right = !paused; - move_right_immediate = !paused; + move_right = true; + move_right_immediate = true; move_clock.restart(); } _ => {} @@ -241,10 +234,29 @@ fn main() { } if paused { - // window.display() is where SFML implements frame rate limiting - // If we don't run this here, then when paused septadrop will max out the thread - window.display(); - continue; + loop { + match window.wait_event() { + Some(event) => match event { + Event::KeyPressed { + code: Key::ESCAPE, + alt: _, + ctrl: _, + shift: _, + system: _, + } => { + paused = false; + break; + }, + Event::GainedFocus => if paused_from_lost_focus { + paused = false; + paused_from_lost_focus = false; + break; + }, + _ => {} + } + _ => {} + } + } } let is_update_frame = update_clock.elapsed_time().as_milliseconds() - pause_offset as i32