From 72ce79836306c37665da5c2093ab3cbd7607f87b Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 8 Jan 2022 22:51:06 +0000 Subject: [PATCH 1/9] Add an example to test small window sizes --- .github/example-run/expanding_window.ron | 3 ++ Cargo.toml | 4 ++ examples/README.md | 1 + examples/window/expanding_window.rs | 56 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 .github/example-run/expanding_window.ron create mode 100644 examples/window/expanding_window.rs diff --git a/.github/example-run/expanding_window.ron b/.github/example-run/expanding_window.ron new file mode 100644 index 0000000000000..22c7d04d68b82 --- /dev/null +++ b/.github/example-run/expanding_window.ron @@ -0,0 +1,3 @@ +( + exit_after: Some(410) +) diff --git a/Cargo.toml b/Cargo.toml index 3f5c13b5109b7..a15363017ffb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -615,6 +615,10 @@ path = "examples/window/transparent_window.rs" name = "window_settings" path = "examples/window/window_settings.rs" +[[example]] +name = "expanding_window" +path = "examples/window/expanding_window.rs" + # Android [[example]] crate-type = ["cdylib"] diff --git a/examples/README.md b/examples/README.md index 000b0ce4ae858..dfe8339d330e4 100644 --- a/examples/README.md +++ b/examples/README.md @@ -307,6 +307,7 @@ Example | File | Description `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings `transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings +`expanding_window` | [`window/expanding_window.rs`](./window/expanding_window.rs) | Demonstrates changing window settings at runtime. Also used to validate that `bevy` can handle arbitrarily small windows # Platform-Specific Examples diff --git a/examples/window/expanding_window.rs b/examples/window/expanding_window.rs new file mode 100644 index 0000000000000..f8f856e56903c --- /dev/null +++ b/examples/window/expanding_window.rs @@ -0,0 +1,56 @@ +use bevy::prelude::*; + +fn main() { + App::new() + .insert_resource(WindowDescriptor { + width: 200., + height: 200., + scale_factor_override: Some(1.), + ..Default::default() + }) + .add_plugins(DefaultPlugins) + .insert_resource(Phase::ContractingY) + .add_system(change_window_size) + .run(); +} + +enum Phase { + ContractingY, + ContractingX, + ExpandingY, + ExpandingX, +} + +use Phase::*; + +fn change_window_size(mut windows: ResMut, mut phase: ResMut) { + let primary = windows.get_primary_mut().unwrap(); + let height = primary.height(); + let width = primary.width(); + match *phase { + Phase::ContractingY => { + if height <= 0.5 { + *phase = ContractingX; + } + primary.set_resolution(width, (height - 4.).max(0.0)) + } + Phase::ContractingX => { + if width <= 0.5 { + *phase = ExpandingY; + } + primary.set_resolution((width - 4.).max(0.0), height) + } + Phase::ExpandingY => { + if height >= 200. { + *phase = ExpandingX; + } + primary.set_resolution(width, height + 4.) + } + Phase::ExpandingX => { + if width >= 200. { + *phase = ContractingY; + } + primary.set_resolution(width + 4., height) + } + } +} From d399b0eb853f7bc479a6cb909c634cdd6e8e417b Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 8 Jan 2022 23:23:17 +0000 Subject: [PATCH 2/9] Use constants instead Actually add a camera --- examples/window/expanding_window.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/window/expanding_window.rs b/examples/window/expanding_window.rs index f8f856e56903c..968039c3480d0 100644 --- a/examples/window/expanding_window.rs +++ b/examples/window/expanding_window.rs @@ -1,19 +1,27 @@ use bevy::prelude::*; +const MAX_WIDTH: f32 = 400.; +const MAX_HEIGHT: f32 = 400.; + fn main() { App::new() .insert_resource(WindowDescriptor { - width: 200., - height: 200., + width: MAX_WIDTH, + height: MAX_HEIGHT, scale_factor_override: Some(1.), ..Default::default() }) .add_plugins(DefaultPlugins) .insert_resource(Phase::ContractingY) .add_system(change_window_size) + .add_startup_system(setup) .run(); } +fn setup(mut commands: Commands) { + commands.spawn_bundle(OrthographicCameraBundle::new_3d()); +} + enum Phase { ContractingY, ContractingX, @@ -41,13 +49,13 @@ fn change_window_size(mut windows: ResMut, mut phase: ResMut) { primary.set_resolution((width - 4.).max(0.0), height) } Phase::ExpandingY => { - if height >= 200. { + if height >= MAX_HEIGHT { *phase = ExpandingX; } primary.set_resolution(width, height + 4.) } Phase::ExpandingX => { - if width >= 200. { + if width >= MAX_WIDTH { *phase = ContractingY; } primary.set_resolution(width + 4., height) From 5759c6a85b2301ab7747918eba954e3a6409eb31 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Mar 2022 09:37:09 +0000 Subject: [PATCH 3/9] Actually do some rendering for testing --- examples/window/expanding_window.rs | 52 ++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/examples/window/expanding_window.rs b/examples/window/expanding_window.rs index 968039c3480d0..9b9e12481d954 100644 --- a/examples/window/expanding_window.rs +++ b/examples/window/expanding_window.rs @@ -18,10 +18,6 @@ fn main() { .run(); } -fn setup(mut commands: Commands) { - commands.spawn_bundle(OrthographicCameraBundle::new_3d()); -} - enum Phase { ContractingY, ContractingX, @@ -31,7 +27,17 @@ enum Phase { use Phase::*; -fn change_window_size(mut windows: ResMut, mut phase: ResMut) { +fn change_window_size( + mut windows: ResMut, + mut phase: ResMut, + mut first_complete: Local, +) { + // Put off rendering for one frame, as currently for a frame where + // resizing happens, nothing is presented. + if !*first_complete { + *first_complete = true; + return; + } let primary = windows.get_primary_mut().unwrap(); let height = primary.height(); let width = primary.width(); @@ -62,3 +68,39 @@ fn change_window_size(mut windows: ResMut, mut phase: ResMut) { } } } + +/// A simple 3d scene, taken from the `3d_scene` example +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // plane + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), + material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), + ..default() + }); + // cube + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(0.0, 0.5, 0.0), + ..default() + }); + // light + commands.spawn_bundle(PointLightBundle { + point_light: PointLight { + intensity: 1500.0, + shadows_enabled: true, + ..default() + }, + transform: Transform::from_xyz(4.0, 8.0, 4.0), + ..default() + }); + // camera + commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }); +} From 8b2982daf242e0800a346d52be4b497da717a39f Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 14 Apr 2022 19:52:49 +0100 Subject: [PATCH 4/9] No longer test 0x0 windows as they are unsupported on X11 --- examples/window/expanding_window.rs | 53 +++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/examples/window/expanding_window.rs b/examples/window/expanding_window.rs index 9b9e12481d954..fb31bdcadf74b 100644 --- a/examples/window/expanding_window.rs +++ b/examples/window/expanding_window.rs @@ -1,19 +1,30 @@ -use bevy::prelude::*; +use bevy::{input::system::exit_on_esc_system, prelude::*}; -const MAX_WIDTH: f32 = 400.; -const MAX_HEIGHT: f32 = 400.; +const MAX_WIDTH: u16 = 401; +const MAX_HEIGHT: u16 = 401; + +struct Dimensions { + width: u16, + height: u16, +} fn main() { App::new() .insert_resource(WindowDescriptor { - width: MAX_WIDTH, - height: MAX_HEIGHT, + width: MAX_WIDTH.try_into().unwrap(), + height: MAX_HEIGHT.try_into().unwrap(), scale_factor_override: Some(1.), ..Default::default() }) + .insert_resource(Dimensions { + width: MAX_WIDTH, + height: MAX_HEIGHT, + }) .add_plugins(DefaultPlugins) .insert_resource(Phase::ContractingY) .add_system(change_window_size) + .add_system(sync_dimensions) + .add_system(exit_on_esc_system) .add_startup_system(setup) .run(); } @@ -28,7 +39,7 @@ enum Phase { use Phase::*; fn change_window_size( - mut windows: ResMut, + mut windows: ResMut, mut phase: ResMut, mut first_complete: Local, ) { @@ -38,37 +49,49 @@ fn change_window_size( *first_complete = true; return; } - let primary = windows.get_primary_mut().unwrap(); - let height = primary.height(); - let width = primary.width(); + let height = windows.height; + let width = windows.width; match *phase { Phase::ContractingY => { - if height <= 0.5 { + if windows.height <= 1 { *phase = ContractingX; + } else { + windows.height -= 4; } - primary.set_resolution(width, (height - 4.).max(0.0)) } Phase::ContractingX => { - if width <= 0.5 { + if width <= 1 { *phase = ExpandingY; + } else { + windows.width -= 4; } - primary.set_resolution((width - 4.).max(0.0), height) } Phase::ExpandingY => { if height >= MAX_HEIGHT { *phase = ExpandingX; + } else { + windows.height += 4; } - primary.set_resolution(width, height + 4.) } Phase::ExpandingX => { if width >= MAX_WIDTH { *phase = ContractingY; + } else { + windows.width += 4; } - primary.set_resolution(width + 4., height) } } } +fn sync_dimensions(dim: Res, mut windows: ResMut) { + if dim.is_changed() { + windows.get_primary_mut().unwrap().set_resolution( + dim.width.try_into().unwrap(), + dim.height.try_into().unwrap(), + ); + } +} + /// A simple 3d scene, taken from the `3d_scene` example fn setup( mut commands: Commands, From b878e10871cd4559412f3b81a6245362aaeb1c2c Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 14 Apr 2022 20:18:26 +0100 Subject: [PATCH 5/9] Don't 'abuse' 0x0 windows for the example --- .github/example-run/expanding_window.ron | 3 - .github/example-run/minimising.ron | 5 ++ .github/example-run/resizing.ron | 4 ++ Cargo.toml | 9 ++- examples/README.md | 3 +- examples/window/minimising.rs | 67 +++++++++++++++++++ .../{expanding_window.rs => resizing.rs} | 4 ++ 7 files changed, 89 insertions(+), 6 deletions(-) delete mode 100644 .github/example-run/expanding_window.ron create mode 100644 .github/example-run/minimising.ron create mode 100644 .github/example-run/resizing.ron create mode 100644 examples/window/minimising.rs rename examples/window/{expanding_window.rs => resizing.rs} (92%) diff --git a/.github/example-run/expanding_window.ron b/.github/example-run/expanding_window.ron deleted file mode 100644 index 22c7d04d68b82..0000000000000 --- a/.github/example-run/expanding_window.ron +++ /dev/null @@ -1,3 +0,0 @@ -( - exit_after: Some(410) -) diff --git a/.github/example-run/minimising.ron b/.github/example-run/minimising.ron new file mode 100644 index 0000000000000..7df3a888344b2 --- /dev/null +++ b/.github/example-run/minimising.ron @@ -0,0 +1,5 @@ +( + // We have no promised about frame rate. + // TODO: Make this as small as is feasible + exit_after: Some(410) +) diff --git a/.github/example-run/resizing.ron b/.github/example-run/resizing.ron new file mode 100644 index 0000000000000..f4914e281bcd6 --- /dev/null +++ b/.github/example-run/resizing.ron @@ -0,0 +1,4 @@ +( + // Ensures that the full cycle will run + exit_after: Some(410) +) diff --git a/Cargo.toml b/Cargo.toml index a15363017ffb7..c83b3d90f5977 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -616,8 +616,13 @@ name = "window_settings" path = "examples/window/window_settings.rs" [[example]] -name = "expanding_window" -path = "examples/window/expanding_window.rs" +name = "resizing" +path = "examples/window/resizing.rs" + +[[example]] +name = "minimising" +path = "examples/window/minimising.rs" + # Android [[example]] diff --git a/examples/README.md b/examples/README.md index dfe8339d330e4..8fc518641eea5 100644 --- a/examples/README.md +++ b/examples/README.md @@ -307,7 +307,8 @@ Example | File | Description `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings `transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings -`expanding_window` | [`window/expanding_window.rs`](./window/expanding_window.rs) | Demonstrates changing window settings at runtime. Also used to validate that `bevy` can handle arbitrarily small windows +`resizing` | [`window/resizing.rs`](./window/resizing.rs) | Demonstrates changing window size at runtime. Also used to validate that `bevy` can handle arbitrarily small windows +`minimising` | [`window/minimising.rs`](./window/minimising.rs) | Demonstrates minimising a window from within your game. Also used to validate that `bevy` can handle minimised windows # Platform-Specific Examples diff --git a/examples/window/minimising.rs b/examples/window/minimising.rs new file mode 100644 index 0000000000000..1425d88117e22 --- /dev/null +++ b/examples/window/minimising.rs @@ -0,0 +1,67 @@ +use std::time::Duration; + +use bevy::prelude::*; + +#[derive(Deref, DerefMut)] +struct MinimiseTimer(Timer); + +fn main() { + // TODO: Combine this with `resizing` once multiple_windows is simpler than + // it is currently. + App::new() + .insert_resource(WindowDescriptor { + title: "Minimising".into(), + ..Default::default() + }) + .insert_resource(MinimiseTimer(Timer::new(Duration::from_secs(2), false))) + .add_plugins(DefaultPlugins) + .add_system(minimise_automatically) + .add_startup_system(setup) + .run(); +} + +fn minimise_automatically( + mut windows: ResMut, + mut timer: ResMut, + time: Res