Skip to content

Commit

Permalink
add RateLimited option
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Jul 25, 2023
1 parent c3a38a9 commit 0b73439
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 21 additions & 1 deletion crates/bevy_winit/src/winit_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -93,6 +102,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.
Expand Down

0 comments on commit 0b73439

Please sign in to comment.