Skip to content

Commit 3892867

Browse files
committed
add RateLimited option
1 parent d99faa2 commit 3892867

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

crates/bevy_winit/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ pub fn winit_runner(mut app: App) {
578578
let focused = windows.iter().any(|window| window.focused);
579579
let should_update = match config.update_mode(focused) {
580580
UpdateMode::Continuous => true,
581+
UpdateMode::RateLimited { .. } => {
582+
runner_state.timeout_elapsed || runner_state.redraw_requested
583+
}
581584
UpdateMode::Reactive { .. } => {
582585
runner_state.timeout_elapsed
583586
|| runner_state.redraw_requested
@@ -606,7 +609,8 @@ pub fn winit_runner(mut app: App) {
606609
let focused = windows.iter().any(|window| window.focused);
607610
match config.update_mode(focused) {
608611
UpdateMode::Continuous => *control_flow = ControlFlow::Poll,
609-
UpdateMode::Reactive { wait }
612+
UpdateMode::RateLimited { wait }
613+
| UpdateMode::Reactive { wait }
610614
| UpdateMode::ReactiveLowPower { wait } => {
611615
if let Some(next) = runner_state.last_update.checked_add(*wait) {
612616
runner_state.scheduled_update = Some(next);

crates/bevy_winit/src/winit_config.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ pub struct WinitSettings {
3737

3838
impl WinitSettings {
3939
/// Default settings for games.
40+
///
41+
/// [`Continuous`](UpdateMode::Continuous) if windows have focus,
42+
/// [`RateLimited`](UpdateMode::RateLimited) otherwise.
4043
pub fn game() -> Self {
41-
WinitSettings::default()
44+
WinitSettings {
45+
focused_mode: UpdateMode::Continuous,
46+
unfocused_mode: UpdateMode::RateLimited {
47+
wait: Duration::from_millis(50), // 20Hz
48+
},
49+
..Default::default()
50+
}
4251
}
4352

4453
/// Default settings for desktop applications.
@@ -94,6 +103,17 @@ pub enum UpdateMode {
94103
/// until an [`AppExit`](bevy_app::AppExit) event appears:
95104
/// - enough time has elapsed since the previous update
96105
/// - a redraw is requested
106+
RateLimited {
107+
/// The minimum time to wait from the start of one update to the next.
108+
///
109+
/// **Note:** This has no upper limit.
110+
/// Bevy will wait forever if you set this to [`Duration::MAX`].
111+
wait: Duration,
112+
},
113+
/// The [`App`](bevy_app::App) will update in response to the following,
114+
/// until an [`AppExit`](bevy_app::AppExit) event appears:
115+
/// - enough time has elapsed since the previous update
116+
/// - a redraw is requested
97117
/// - new window or device events have appeared
98118
Reactive {
99119
/// The minimum time from the start of one update to the next.

0 commit comments

Comments
 (0)