update example low_power (#14224)

# Objective

- Show both `RequestRedraw` and `WakeUp`
- Partly adresses #14214

---------

Co-authored-by: Aevyrie <aevyrie@gmail.com>
This commit is contained in:
François Mockers 2024-07-14 17:42:07 +02:00 committed by GitHub
parent 6c9ec88e54
commit d008227553
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,13 +3,11 @@
//! This is useful for making desktop applications, or any other program that doesn't need to be //! This is useful for making desktop applications, or any other program that doesn't need to be
//! running the event loop non-stop. //! running the event loop non-stop.
use bevy::window::WindowResolution;
use bevy::winit::WakeUp;
use bevy::{ use bevy::{
prelude::*, prelude::*,
utils::Duration, utils::Duration,
window::{PresentMode, WindowPlugin}, window::{PresentMode, RequestRedraw, WindowPlugin},
winit::{EventLoopProxy, WinitSettings}, winit::{EventLoopProxy, WakeUp, WinitSettings},
}; };
fn main() { fn main() {
@ -28,7 +26,6 @@ fn main() {
primary_window: Some(Window { primary_window: Some(Window {
// Turn off vsync to maximize CPU/GPU usage // Turn off vsync to maximize CPU/GPU usage
present_mode: PresentMode::AutoNoVsync, present_mode: PresentMode::AutoNoVsync,
resolution: WindowResolution::new(800., 640.).with_scale_factor_override(1.),
..default() ..default()
}), }),
..default() ..default()
@ -50,7 +47,8 @@ fn main() {
enum ExampleMode { enum ExampleMode {
Game, Game,
Application, Application,
ApplicationWithRedraw, ApplicationWithRequestRedraw,
ApplicationWithWakeUp,
} }
/// Update winit based on the current `ExampleMode` /// Update winit based on the current `ExampleMode`
@ -58,6 +56,7 @@ fn update_winit(
mode: Res<ExampleMode>, mode: Res<ExampleMode>,
mut winit_config: ResMut<WinitSettings>, mut winit_config: ResMut<WinitSettings>,
event_loop_proxy: NonSend<EventLoopProxy<WakeUp>>, event_loop_proxy: NonSend<EventLoopProxy<WakeUp>>,
mut redraw_request_events: EventWriter<RequestRedraw>,
) { ) {
use ExampleMode::*; use ExampleMode::*;
*winit_config = match *mode { *winit_config = match *mode {
@ -79,18 +78,24 @@ fn update_winit(
// (e.g. the mouse hovers over a visible part of the out of focus window), a // (e.g. the mouse hovers over a visible part of the out of focus window), a
// [`RequestRedraw`] event is received, or one minute has passed without the app // [`RequestRedraw`] event is received, or one minute has passed without the app
// updating. // updating.
WinitSettings { WinitSettings::desktop_app()
focused_mode: bevy::winit::UpdateMode::reactive(Duration::from_secs(1)),
unfocused_mode: bevy::winit::UpdateMode::reactive_low_power(Duration::from_secs(5)),
}
} }
ApplicationWithRedraw => { ApplicationWithRequestRedraw => {
// Sending a `RequestRedraw` event is useful when you want the app to update the next // Sending a `RequestRedraw` event is useful when you want the app to update the next
// frame regardless of any user input. For example, your application might use // frame regardless of any user input. For example, your application might use
// `WinitSettings::desktop_app()` to reduce power use, but UI animations need to play even // `WinitSettings::desktop_app()` to reduce power use, but UI animations need to play even
// when there are no inputs, so you send redraw requests while the animation is playing. // when there are no inputs, so you send redraw requests while the animation is playing.
// Note that in this example the RequestRedraw winit event will make the app run in the same // Note that in this example the RequestRedraw winit event will make the app run in the same
// way as continuous // way as continuous
redraw_request_events.send(RequestRedraw);
WinitSettings::desktop_app()
}
ApplicationWithWakeUp => {
// Sending a `WakeUp` event is useful when you want the app to update the next
// frame regardless of any user input. This can be used from outside Bevy, see example
// `window/custom_user_event.rs` for an example usage from outside.
// Note that in this example the Wakeup winit event will make the app run in the same
// way as continuous
let _ = event_loop_proxy.send_event(WakeUp); let _ = event_loop_proxy.send_event(WakeUp);
WinitSettings::desktop_app() WinitSettings::desktop_app()
} }
@ -115,8 +120,9 @@ pub(crate) mod test_setup {
if button_input.just_pressed(KeyCode::Space) { if button_input.just_pressed(KeyCode::Space) {
*mode = match *mode { *mode = match *mode {
ExampleMode::Game => ExampleMode::Application, ExampleMode::Game => ExampleMode::Application,
ExampleMode::Application => ExampleMode::ApplicationWithRedraw, ExampleMode::Application => ExampleMode::ApplicationWithRequestRedraw,
ExampleMode::ApplicationWithRedraw => ExampleMode::Game, ExampleMode::ApplicationWithRequestRedraw => ExampleMode::ApplicationWithWakeUp,
ExampleMode::ApplicationWithWakeUp => ExampleMode::Game,
}; };
} }
} }
@ -147,7 +153,10 @@ pub(crate) mod test_setup {
let mode = match *mode { let mode = match *mode {
ExampleMode::Game => "game(), continuous, default", ExampleMode::Game => "game(), continuous, default",
ExampleMode::Application => "desktop_app(), reactive", ExampleMode::Application => "desktop_app(), reactive",
ExampleMode::ApplicationWithRedraw => "desktop_app(), reactive, RequestRedraw sent", ExampleMode::ApplicationWithRequestRedraw => {
"desktop_app(), reactive, RequestRedraw sent"
}
ExampleMode::ApplicationWithWakeUp => "desktop_app(), reactive, WakeUp sent",
}; };
let mut text = query.single_mut(); let mut text = query.single_mut();
text.sections[1].value = mode.to_string(); text.sections[1].value = mode.to_string();
@ -183,26 +192,20 @@ pub(crate) mod test_setup {
TextBundle::from_sections([ TextBundle::from_sections([
TextSection::new( TextSection::new(
"Press space bar to cycle modes\n", "Press space bar to cycle modes\n",
TextStyle { TextStyle { ..default() },
font_size: 50.0,
..default()
},
), ),
TextSection::from_style(TextStyle { TextSection::from_style(TextStyle {
font_size: 50.0,
color: LIME.into(), color: LIME.into(),
..default() ..default()
}), }),
TextSection::new( TextSection::new(
"\nFrame: ", "\nFrame: ",
TextStyle { TextStyle {
font_size: 50.0,
color: YELLOW.into(), color: YELLOW.into(),
..default() ..default()
}, },
), ),
TextSection::from_style(TextStyle { TextSection::from_style(TextStyle {
font_size: 50.0,
color: YELLOW.into(), color: YELLOW.into(),
..default() ..default()
}), }),
@ -210,8 +213,8 @@ pub(crate) mod test_setup {
.with_style(Style { .with_style(Style {
align_self: AlignSelf::FlexStart, align_self: AlignSelf::FlexStart,
position_type: PositionType::Absolute, position_type: PositionType::Absolute,
top: Val::Px(5.0), top: Val::Px(12.0),
left: Val::Px(5.0), left: Val::Px(12.0),
..default() ..default()
}), }),
ModeText, ModeText,