From 3892867a2cfda3b5a68993e389beb8abd0f81155 Mon Sep 17 00:00:00 2001 From: Cameron <51241057+maniwani@users.noreply.github.com> Date: Fri, 10 Feb 2023 12:32:09 -0800 Subject: [PATCH] add `RateLimited` option --- crates/bevy_winit/src/lib.rs | 6 +++++- crates/bevy_winit/src/winit_config.rs | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 5d8e00a5cb1d6e..f6dfb903809922 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -578,6 +578,9 @@ pub fn winit_runner(mut app: App) { let focused = windows.iter().any(|window| window.focused); let should_update = match config.update_mode(focused) { UpdateMode::Continuous => true, + UpdateMode::RateLimited { .. } => { + runner_state.timeout_elapsed || runner_state.redraw_requested + } UpdateMode::Reactive { .. } => { runner_state.timeout_elapsed || runner_state.redraw_requested @@ -606,7 +609,8 @@ pub fn winit_runner(mut app: App) { let focused = windows.iter().any(|window| window.focused); match config.update_mode(focused) { UpdateMode::Continuous => *control_flow = ControlFlow::Poll, - UpdateMode::Reactive { wait } + UpdateMode::RateLimited { wait } + | UpdateMode::Reactive { wait } | UpdateMode::ReactiveLowPower { wait } => { if let Some(next) = runner_state.last_update.checked_add(*wait) { runner_state.scheduled_update = Some(next); diff --git a/crates/bevy_winit/src/winit_config.rs b/crates/bevy_winit/src/winit_config.rs index d411f6e7110435..24741c4e0a95bb 100644 --- a/crates/bevy_winit/src/winit_config.rs +++ b/crates/bevy_winit/src/winit_config.rs @@ -37,8 +37,17 @@ pub struct WinitSettings { impl WinitSettings { /// Default settings for games. + /// + /// [`Continuous`](UpdateMode::Continuous) if windows have focus, + /// [`RateLimited`](UpdateMode::RateLimited) otherwise. pub fn game() -> Self { - WinitSettings::default() + WinitSettings { + focused_mode: UpdateMode::Continuous, + unfocused_mode: UpdateMode::RateLimited { + wait: Duration::from_millis(50), // 20Hz + }, + ..Default::default() + } } /// Default settings for desktop applications. @@ -94,6 +103,17 @@ pub enum UpdateMode { /// until an [`AppExit`](bevy_app::AppExit) event appears: /// - enough time has elapsed since the previous update /// - a redraw is requested + RateLimited { + /// The minimum time to wait from the start of one update to the next. + /// + /// **Note:** This has no upper limit. + /// Bevy will wait forever if you set this to [`Duration::MAX`]. + wait: Duration, + }, + /// The [`App`](bevy_app::App) will update in response to the following, + /// until an [`AppExit`](bevy_app::AppExit) event appears: + /// - enough time has elapsed since the previous update + /// - a redraw is requested /// - new window or device events have appeared Reactive { /// The minimum time from the start of one update to the next.