Skip to content

Commit 2beffb8

Browse files
committed
add RateLimited option
1 parent 4afe6be commit 2beffb8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

crates/bevy_winit/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ pub fn winit_runner(mut app: App) {
580580
let focused = windows.iter().any(|window| window.focused);
581581
let should_update = match config.update_mode(focused) {
582582
UpdateMode::Continuous => true,
583+
UpdateMode::RateLimited { .. } => {
584+
runner_state.timeout_elapsed || runner_state.redraw_requested
585+
}
583586
UpdateMode::Reactive { .. } => {
584587
runner_state.timeout_elapsed
585588
|| runner_state.redraw_requested
@@ -608,7 +611,8 @@ pub fn winit_runner(mut app: App) {
608611
let focused = windows.iter().any(|window| window.focused);
609612
match config.update_mode(focused) {
610613
UpdateMode::Continuous => *control_flow = ControlFlow::Poll,
611-
UpdateMode::Reactive { wait }
614+
UpdateMode::RateLimited { wait }
615+
| UpdateMode::Reactive { wait }
612616
| UpdateMode::ReactiveLowPower { wait } => {
613617
if let Some(next) = runner_state.last_update.checked_add(*wait) {
614618
runner_state.scheduled_update = Some(next);

crates/bevy_winit/src/winit_config.rs

+21-1
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)