add escape and borderless

This commit is contained in:
Matthew Deville 2025-08-31 12:17:57 +02:00
parent 68763ea15c
commit b037f9150b
2 changed files with 27 additions and 7 deletions

View file

@ -5,4 +5,4 @@ edition = "2024"
[dependencies]
bevy = { version = "0.16.1", features = ["shader_format_glsl"] }
bevy = "0.16.1"

View file

@ -1,11 +1,12 @@
//! A shader that uses the GLSL shading language.
//! A shader that uses the WGSL shading language.
use bevy::{
input::common_conditions::*,
prelude::*,
reflect::TypePath,
render::render_resource::{AsBindGroup, ShaderRef},
sprite::{AlphaMode2d, Material2d, Material2dPlugin},
window::PrimaryWindow,
window::{PrimaryWindow, WindowMode},
};
const SHADER_ASSET_PATH: &str = "shaders/default.wgsl";
@ -17,7 +18,14 @@ fn main() {
Material2dPlugin::<CustomMaterial>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, resize_fullscreen_quad)
.add_systems(
Update,
(
resize_fullscreen_quad,
exit_app.run_if(input_just_pressed(KeyCode::Escape)),
toggle_fullscreen.run_if(input_just_pressed(KeyCode::F11)),
),
)
.run();
}
@ -26,9 +34,10 @@ fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
primary_window: Single<&Window, With<PrimaryWindow>>,
mut primary_window: Single<&mut Window, With<PrimaryWindow>>,
) {
// camera
primary_window.decorations = false;
commands.spawn(Camera2d);
// quad that fills the whole window
@ -36,7 +45,6 @@ fn setup(
primary_window.resolution.width(),
primary_window.resolution.height(),
);
commands.spawn((
Mesh2d(meshes.add(Rectangle::default())),
MeshMaterial2d(materials.add(CustomMaterial {})),
@ -61,6 +69,18 @@ fn resize_fullscreen_quad(
transform.scale = Vec3::new(size.x, size.y, 1.0);
}
fn exit_app(mut exit: EventWriter<AppExit>) {
exit.write(AppExit::Success);
}
fn toggle_fullscreen(mut primary_window: Single<&mut Window, With<PrimaryWindow>>) {
primary_window.mode = match primary_window.mode {
WindowMode::BorderlessFullscreen(_) => WindowMode::Windowed,
WindowMode::Windowed => WindowMode::BorderlessFullscreen(MonitorSelection::Current),
WindowMode::Fullscreen(_, _) => WindowMode::BorderlessFullscreen(MonitorSelection::Current),
};
}
// This is the struct that will be passed to your shader
#[derive(Asset, TypePath, AsBindGroup, Clone)]
struct CustomMaterial {}