mirror of
https://github.com/bevyengine/bevy
synced 2024-11-29 08:00:20 +00:00
69d08c5ef4
# Objective - Reconfigure surface after present mode changes. It seems that this is not done currently at runtime. It's pretty common for games to change such graphical settings at runtime. - Fixes present mode issue in #5111 ## Solution - Exactly like resolution change gets tracked when extracting window, do the same for present mode. Additionally, I added present mode (vsync) toggling to window settings example.
84 lines
2.7 KiB
Rust
84 lines
2.7 KiB
Rust
//! Illustrates how to change window settings and shows how to affect
|
|
//! the mouse pointer in various ways.
|
|
|
|
use bevy::{prelude::*, window::PresentMode};
|
|
use bevy_internal::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(WindowDescriptor {
|
|
title: "I am a window!".to_string(),
|
|
width: 500.,
|
|
height: 300.,
|
|
present_mode: PresentMode::AutoVsync,
|
|
..default()
|
|
})
|
|
.add_plugins(DefaultPlugins)
|
|
.add_plugin(LogDiagnosticsPlugin::default())
|
|
.add_plugin(FrameTimeDiagnosticsPlugin)
|
|
.add_system(change_title)
|
|
.add_system(toggle_cursor)
|
|
.add_system(toggle_vsync)
|
|
.add_system(cycle_cursor_icon)
|
|
.run();
|
|
}
|
|
|
|
/// This system toggles the vsync mode when pressing the button V.
|
|
/// You'll see fps increase displayed in the console.
|
|
fn toggle_vsync(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
|
|
if input.just_pressed(KeyCode::V) {
|
|
let window = windows.primary_mut();
|
|
|
|
window.set_present_mode(if matches!(window.present_mode(), PresentMode::AutoVsync) {
|
|
PresentMode::AutoNoVsync
|
|
} else {
|
|
PresentMode::AutoVsync
|
|
});
|
|
info!("PRESENT_MODE: {:?}", window.present_mode());
|
|
}
|
|
}
|
|
|
|
/// This system will then change the title during execution
|
|
fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
|
|
let window = windows.primary_mut();
|
|
window.set_title(format!(
|
|
"Seconds since startup: {}",
|
|
time.seconds_since_startup().round()
|
|
));
|
|
}
|
|
|
|
/// This system toggles the cursor's visibility when the space bar is pressed
|
|
fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
|
|
let window = windows.primary_mut();
|
|
if input.just_pressed(KeyCode::Space) {
|
|
window.set_cursor_lock_mode(!window.cursor_locked());
|
|
window.set_cursor_visibility(!window.cursor_visible());
|
|
}
|
|
}
|
|
|
|
/// This system cycles the cursor's icon through a small set of icons when clicking
|
|
fn cycle_cursor_icon(
|
|
input: Res<Input<MouseButton>>,
|
|
mut windows: ResMut<Windows>,
|
|
mut index: Local<usize>,
|
|
) {
|
|
const ICONS: &[CursorIcon] = &[
|
|
CursorIcon::Default,
|
|
CursorIcon::Hand,
|
|
CursorIcon::Wait,
|
|
CursorIcon::Text,
|
|
CursorIcon::Copy,
|
|
];
|
|
let window = windows.primary_mut();
|
|
if input.just_pressed(MouseButton::Left) {
|
|
*index = (*index + 1) % ICONS.len();
|
|
window.set_cursor_icon(ICONS[*index]);
|
|
} else if input.just_pressed(MouseButton::Right) {
|
|
*index = if *index == 0 {
|
|
ICONS.len() - 1
|
|
} else {
|
|
*index - 1
|
|
};
|
|
window.set_cursor_icon(ICONS[*index]);
|
|
}
|
|
}
|