diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 8fa1c722f1307..31f81714adc17 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -628,10 +628,7 @@ impl NormalizedRenderTarget { .into_iter() .find(|(entity, _)| *entity == window_ref.entity()) .map(|(_, window)| RenderTargetInfo { - physical_size: UVec2::new( - window.resolution.physical_width(), - window.resolution.physical_height(), - ), + physical_size: window.physical_size(), scale_factor: window.resolution.scale_factor(), }), NormalizedRenderTarget::Image(image_handle) => { diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index df3ac93372bb6..9c69a6e0d982b 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -460,7 +460,7 @@ pub fn resolve_outlines_system( ) { let viewport_size = primary_window .get_single() - .map(|window| Vec2::new(window.resolution.width(), window.resolution.height())) + .map(|window| window.size()) .unwrap_or(Vec2::ZERO) / ui_scale.0; diff --git a/crates/bevy_ui/src/render/ui_material_pipeline.rs b/crates/bevy_ui/src/render/ui_material_pipeline.rs index 422410642bacd..d8f443a7eb401 100644 --- a/crates/bevy_ui/src/render/ui_material_pipeline.rs +++ b/crates/bevy_ui/src/render/ui_material_pipeline.rs @@ -383,7 +383,7 @@ pub fn extract_ui_material_nodes( ) { let ui_logical_viewport_size = windows .get_single() - .map(|window| Vec2::new(window.resolution.width(), window.resolution.height())) + .map(|window| window.size()) .unwrap_or(Vec2::ZERO) // The logical window resolution returned by `Window` only takes into account the window scale factor and not `UiScale`, // so we have to divide by `UiScale` to get the size of the UI viewport. diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index b141d388fe128..b6079158615a1 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -2,7 +2,7 @@ use bevy_ecs::{ entity::{Entity, EntityMapper, MapEntities}, prelude::{Component, ReflectComponent}, }; -use bevy_math::{DVec2, IVec2, Vec2}; +use bevy_math::{DVec2, IVec2, UVec2, Vec2}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; #[cfg(feature = "serialize")] @@ -313,6 +313,14 @@ impl Window { self.resolution.height() } + /// The window's client size in logical pixels + /// + /// See [`WindowResolution`] for an explanation about logical/physical sizes. + #[inline] + pub fn size(&self) -> Vec2 { + self.resolution.size() + } + /// The window's client area width in physical pixels. /// /// See [`WindowResolution`] for an explanation about logical/physical sizes. @@ -329,6 +337,14 @@ impl Window { self.resolution.physical_height() } + /// The window's client size in physical pixels + /// + /// See [`WindowResolution`] for an explanation about logical/physical sizes. + #[inline] + pub fn physical_size(&self) -> bevy_math::UVec2 { + self.resolution.physical_size() + } + /// The window's scale factor. /// /// Ratio of physical size to logical size, see [`WindowResolution`]. @@ -664,6 +680,12 @@ impl WindowResolution { self.physical_height() as f32 / self.scale_factor() } + /// The window's client size in logical pixels + #[inline] + pub fn size(&self) -> Vec2 { + Vec2::new(self.width(), self.height()) + } + /// The window's client area width in physical pixels. #[inline] pub fn physical_width(&self) -> u32 { @@ -676,6 +698,12 @@ impl WindowResolution { self.physical_height } + /// The window's client size in physical pixels + #[inline] + pub fn physical_size(&self) -> UVec2 { + UVec2::new(self.physical_width, self.physical_height) + } + /// The ratio of physical pixels to logical pixels. /// /// `physical_pixels = logical_pixels * scale_factor` diff --git a/examples/3d/split_screen.rs b/examples/3d/split_screen.rs index 35b88ae424683..5d9f9c87dbf2d 100644 --- a/examples/3d/split_screen.rs +++ b/examples/3d/split_screen.rs @@ -186,7 +186,7 @@ fn set_camera_viewports( // A resize_event is sent when the window is first created, allowing us to reuse this system for initial setup. for resize_event in resize_events.read() { let window = windows.get(resize_event.window).unwrap(); - let size = UVec2::new(window.physical_width(), window.physical_height()) / 2; + let size = window.physical_size() / 2; for (camera_position, mut camera) in &mut query { camera.viewport = Some(Viewport { diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index 226d09df4958a..52dadf9766652 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -242,7 +242,7 @@ fn collisions( mut query: Query<(&mut Velocity, &mut Transform), With>, ) { let window = windows.single(); - let window_size = Vec2::new(window.width(), window.height()); + let window_size = window.size(); let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.); diff --git a/examples/stress_tests/bevymark.rs b/examples/stress_tests/bevymark.rs index 3d40e6086ec30..ea07492ffd6d7 100644 --- a/examples/stress_tests/bevymark.rs +++ b/examples/stress_tests/bevymark.rs @@ -368,11 +368,7 @@ fn spawn_birds( let bird_x = (primary_window_resolution.width() / -2.) + HALF_BIRD_SIZE; let bird_y = (primary_window_resolution.height() / 2.) - HALF_BIRD_SIZE; - let half_extents = 0.5 - * Vec2::new( - primary_window_resolution.width(), - primary_window_resolution.height(), - ); + let half_extents = 0.5 * primary_window_resolution.size(); let color = counter.color; let current_count = counter.count; @@ -509,7 +505,7 @@ fn handle_collision(half_extents: Vec2, translation: &Vec3, velocity: &mut Vec3) fn collision_system(windows: Query<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) { let window = windows.single(); - let half_extents = 0.5 * Vec2::new(window.width(), window.height()); + let half_extents = 0.5 * window.size(); for (mut bird, transform) in &mut bird_query { handle_collision(half_extents, &transform.translation, &mut bird.velocity);