use bevy::{
prelude::*,
window::{PresentMode, RequestRedraw, WindowPlugin},
winit::{EventLoopProxyWrapper, WakeUp, WinitSettings},
};
use core::time::Duration;
fn main() {
App::new()
.insert_resource(WinitSettings::game())
.insert_resource(WinitSettings::desktop_app())
.insert_resource(WinitSettings {
focused_mode: bevy::winit::UpdateMode::Continuous,
unfocused_mode: bevy::winit::UpdateMode::reactive_low_power(Duration::from_millis(10)),
})
.insert_resource(ExampleMode::Game)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
present_mode: PresentMode::AutoNoVsync,
..default()
}),
..default()
}))
.add_systems(Startup, test_setup::setup)
.add_systems(
Update,
(
test_setup::cycle_modes,
test_setup::rotate_cube,
test_setup::update_text,
update_winit,
),
)
.run();
}
#[derive(Resource, Debug)]
enum ExampleMode {
Game,
Application,
ApplicationWithRequestRedraw,
ApplicationWithWakeUp,
}
fn update_winit(
mode: Res<ExampleMode>,
mut winit_config: ResMut<WinitSettings>,
event_loop_proxy: Res<EventLoopProxyWrapper<WakeUp>>,
mut redraw_request_events: EventWriter<RequestRedraw>,
) {
use ExampleMode::*;
*winit_config = match *mode {
Game => {
WinitSettings::game()
}
Application => {
WinitSettings::desktop_app()
}
ApplicationWithRequestRedraw => {
redraw_request_events.write(RequestRedraw);
WinitSettings::desktop_app()
}
ApplicationWithWakeUp => {
let _ = event_loop_proxy.send_event(WakeUp);
WinitSettings::desktop_app()
}
};
}
pub(crate) mod test_setup {
use crate::ExampleMode;
use bevy::{
color::palettes::basic::{LIME, YELLOW},
prelude::*,
window::RequestRedraw,
};
pub(crate) fn cycle_modes(
mut mode: ResMut<ExampleMode>,
button_input: Res<ButtonInput<KeyCode>>,
) {
if button_input.just_pressed(KeyCode::Space) {
*mode = match *mode {
ExampleMode::Game => ExampleMode::Application,
ExampleMode::Application => ExampleMode::ApplicationWithRequestRedraw,
ExampleMode::ApplicationWithRequestRedraw => ExampleMode::ApplicationWithWakeUp,
ExampleMode::ApplicationWithWakeUp => ExampleMode::Game,
};
}
}
#[derive(Component)]
pub(crate) struct Rotator;
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_secs());
transform.rotate_local_y(time.delta_secs());
}
}
#[derive(Component)]
pub struct ModeText;
pub(crate) fn update_text(
mut frame: Local<usize>,
mode: Res<ExampleMode>,
text: Single<Entity, With<ModeText>>,
mut writer: TextUiWriter,
) {
*frame += 1;
let mode = match *mode {
ExampleMode::Game => "game(), continuous, default",
ExampleMode::Application => "desktop_app(), reactive",
ExampleMode::ApplicationWithRequestRedraw => {
"desktop_app(), reactive, RequestRedraw sent"
}
ExampleMode::ApplicationWithWakeUp => "desktop_app(), reactive, WakeUp sent",
};
*writer.text(*text, 2) = mode.to_string();
*writer.text(*text, 4) = frame.to_string();
}
pub fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut event: EventWriter<RequestRedraw>,
) {
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
Rotator,
));
commands.spawn((
DirectionalLight::default(),
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
));
event.write(RequestRedraw);
commands.spawn((
Text::default(),
Node {
align_self: AlignSelf::FlexStart,
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
ModeText,
children![
TextSpan::new("Press space bar to cycle modes\n"),
(TextSpan::default(), TextColor(LIME.into())),
(TextSpan::new("\nFrame: "), TextColor(YELLOW.into())),
(TextSpan::new(""), TextColor(YELLOW.into())),
],
));
}
}