mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
1bb751cb8d
# Objective Fixes #5884 #2879 Alternative to #2988 #5885 #2886 "Immutable" Plugin settings are currently represented as normal ECS resources, which are read as part of plugin init. This presents a number of problems: 1. If a user inserts the plugin settings resource after the plugin is initialized, it will be silently ignored (and use the defaults instead) 2. Users can modify the plugin settings resource after the plugin has been initialized. This creates a false sense of control over settings that can no longer be changed. (1) and (2) are especially problematic and confusing for the `WindowDescriptor` resource, but this is a general problem. ## Solution Immutable Plugin settings now live on each Plugin struct (ex: `WindowPlugin`). PluginGroups have been reworked to support overriding plugin values. This also removes the need for the `add_plugins_with` api, as the `add_plugins` api can use the builder pattern directly. Settings that can be used at runtime continue to be represented as ECS resources. Plugins are now configured like this: ```rust app.add_plugin(AssetPlugin { watch_for_changes: true, ..default() }) ``` PluginGroups are now configured like this: ```rust app.add_plugins(DefaultPlugins .set(AssetPlugin { watch_for_changes: true, ..default() }) ) ``` This is an alternative to #2988, which is similar. But I personally prefer this solution for a couple of reasons: * ~~#2988 doesn't solve (1)~~ #2988 does solve (1) and will panic in that case. I was wrong! * This PR directly ties plugin settings to Plugin types in a 1:1 relationship, rather than a loose "setup resource" <-> plugin coupling (where the setup resource is consumed by the first plugin that uses it). * I'm not a huge fan of overloading the ECS resource concept and implementation for something that has very different use cases and constraints. ## Changelog - PluginGroups can now be configured directly using the builder pattern. Individual plugin values can be overridden by using `plugin_group.set(SomePlugin {})`, which enables overriding default plugin values. - `WindowDescriptor` plugin settings have been moved to `WindowPlugin` and `AssetServerSettings` have been moved to `AssetPlugin` - `app.add_plugins_with` has been replaced by using `add_plugins` with the builder pattern. ## Migration Guide The `WindowDescriptor` settings have been moved from a resource to `WindowPlugin::window`: ```rust // Old (Bevy 0.8) app .insert_resource(WindowDescriptor { width: 400.0, ..default() }) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(WindowPlugin { window: WindowDescriptor { width: 400.0, ..default() }, ..default() })) ``` The `AssetServerSettings` resource has been removed in favor of direct `AssetPlugin` configuration: ```rust // Old (Bevy 0.8) app .insert_resource(AssetServerSettings { watch_for_changes: true, ..default() }) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(AssetPlugin { watch_for_changes: true, ..default() })) ``` `add_plugins_with` has been replaced by `add_plugins` in combination with the builder pattern: ```rust // Old (Bevy 0.8) app.add_plugins_with(DefaultPlugins, |group| group.disable::<AssetPlugin>()); // New (Bevy 0.9) app.add_plugins(DefaultPlugins.build().disable::<AssetPlugin>()); ```
212 lines
7.7 KiB
Rust
212 lines
7.7 KiB
Rust
//! This example illustrates how to run a winit window in a reactive, low power mode.
|
|
//!
|
|
//! This is useful for making desktop applications, or any other program that doesn't need to be
|
|
//! running the event loop non-stop.
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
utils::Duration,
|
|
window::{PresentMode, RequestRedraw},
|
|
winit::WinitSettings,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
// Continuous rendering for games - bevy's default.
|
|
.insert_resource(WinitSettings::game())
|
|
// Power-saving reactive rendering for applications.
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
// You can also customize update behavior with the fields of [`WinitConfig`]
|
|
.insert_resource(WinitSettings {
|
|
focused_mode: bevy::winit::UpdateMode::Continuous,
|
|
unfocused_mode: bevy::winit::UpdateMode::ReactiveLowPower {
|
|
max_wait: Duration::from_millis(10),
|
|
},
|
|
..default()
|
|
})
|
|
.insert_resource(ExampleMode::Game)
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
window: WindowDescriptor {
|
|
// Turn off vsync to maximize CPU/GPU usage
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
..default()
|
|
},
|
|
..default()
|
|
}))
|
|
.add_startup_system(test_setup::setup)
|
|
.add_system(test_setup::cycle_modes)
|
|
.add_system(test_setup::rotate_cube)
|
|
.add_system(test_setup::update_text)
|
|
.add_system(update_winit)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource, Debug)]
|
|
enum ExampleMode {
|
|
Game,
|
|
Application,
|
|
ApplicationWithRedraw,
|
|
}
|
|
|
|
/// Update winit based on the current `ExampleMode`
|
|
fn update_winit(
|
|
mode: Res<ExampleMode>,
|
|
mut event: EventWriter<RequestRedraw>,
|
|
mut winit_config: ResMut<WinitSettings>,
|
|
) {
|
|
use ExampleMode::*;
|
|
*winit_config = match *mode {
|
|
Game => {
|
|
// In the default `WinitConfig::game()` mode:
|
|
// * When focused: the event loop runs as fast as possible
|
|
// * When not focused: the event loop runs as fast as possible
|
|
WinitSettings::game()
|
|
}
|
|
Application => {
|
|
// While in `WinitConfig::desktop_app()` mode:
|
|
// * When focused: the app will update any time a winit event (e.g. the window is
|
|
// moved/resized, the mouse moves, a button is pressed, etc.), a [`RequestRedraw`]
|
|
// event is received, or after 5 seconds if the app has not updated.
|
|
// * When not focused: the app will update when the window is directly interacted with
|
|
// (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
|
|
// updating.
|
|
WinitSettings::desktop_app()
|
|
}
|
|
ApplicationWithRedraw => {
|
|
// 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
|
|
// `WinitConfig::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.
|
|
event.send(RequestRedraw);
|
|
WinitSettings::desktop_app()
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Everything in this module is for setting up and animating the scene, and is not important to the
|
|
/// demonstrated features.
|
|
pub(crate) mod test_setup {
|
|
use crate::ExampleMode;
|
|
use bevy::{prelude::*, window::RequestRedraw};
|
|
|
|
/// Switch between update modes when the mouse is clicked.
|
|
pub(crate) fn cycle_modes(
|
|
mut mode: ResMut<ExampleMode>,
|
|
mouse_button_input: Res<Input<KeyCode>>,
|
|
) {
|
|
if mouse_button_input.just_pressed(KeyCode::Space) {
|
|
*mode = match *mode {
|
|
ExampleMode::Game => ExampleMode::Application,
|
|
ExampleMode::Application => ExampleMode::ApplicationWithRedraw,
|
|
ExampleMode::ApplicationWithRedraw => ExampleMode::Game,
|
|
};
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub(crate) struct Rotator;
|
|
|
|
/// Rotate the cube to make it clear when the app is updating
|
|
pub(crate) fn rotate_cube(
|
|
time: Res<Time>,
|
|
mut cube_transform: Query<&mut Transform, With<Rotator>>,
|
|
) {
|
|
for mut transform in &mut cube_transform {
|
|
transform.rotate_x(time.delta_seconds());
|
|
transform.rotate_local_y(time.delta_seconds());
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct ModeText;
|
|
|
|
pub(crate) fn update_text(
|
|
mut frame: Local<usize>,
|
|
mode: Res<ExampleMode>,
|
|
mut query: Query<&mut Text, With<ModeText>>,
|
|
) {
|
|
*frame += 1;
|
|
let mode = match *mode {
|
|
ExampleMode::Game => "game(), continuous, default",
|
|
ExampleMode::Application => "desktop_app(), reactive",
|
|
ExampleMode::ApplicationWithRedraw => "desktop_app(), reactive, RequestRedraw sent",
|
|
};
|
|
let mut text = query.single_mut();
|
|
text.sections[1].value = mode.to_string();
|
|
text.sections[3].value = frame.to_string();
|
|
}
|
|
|
|
/// Set up a scene with a cube and some text
|
|
pub fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut event: EventWriter<RequestRedraw>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
|
..default()
|
|
},
|
|
Rotator,
|
|
));
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1500.0,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
..default()
|
|
});
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
event.send(RequestRedraw);
|
|
commands.spawn((
|
|
TextBundle::from_sections([
|
|
TextSection::new(
|
|
"Press spacebar to cycle modes\n",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: 50.0,
|
|
color: Color::WHITE,
|
|
},
|
|
),
|
|
TextSection::from_style(TextStyle {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: 50.0,
|
|
color: Color::GREEN,
|
|
}),
|
|
TextSection::new(
|
|
"\nFrame: ",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: 50.0,
|
|
color: Color::YELLOW,
|
|
},
|
|
),
|
|
TextSection::from_style(TextStyle {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: 50.0,
|
|
color: Color::YELLOW,
|
|
}),
|
|
])
|
|
.with_style(Style {
|
|
align_self: AlignSelf::FlexStart,
|
|
position_type: PositionType::Absolute,
|
|
position: UiRect {
|
|
top: Val::Px(5.0),
|
|
left: Val::Px(5.0),
|
|
..default()
|
|
},
|
|
..default()
|
|
}),
|
|
ModeText,
|
|
));
|
|
}
|
|
}
|