From 9accab710daaffa699b8403e441451ac31bd97f2 Mon Sep 17 00:00:00 2001 From: Boris Boutillier Date: Sun, 16 Jan 2022 14:15:13 +0100 Subject: [PATCH 1/2] Add a size_2d method on Image. Add simple associated test. --- crates/bevy_render/src/texture/image.rs | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 1bb5e8935d599..f5911822234e8 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -7,7 +7,7 @@ use crate::{ }; use bevy_asset::HandleUntyped; use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem}; -use bevy_math::Size; +use bevy_math::{Size, Vec2}; use bevy_reflect::TypeUuid; use thiserror::Error; use wgpu::{ @@ -118,6 +118,14 @@ impl Image { self.texture_descriptor.size.height as f32 / self.texture_descriptor.size.width as f32 } + /// Returns the size of a 2D image. + pub fn size_2d(&self) -> Vec2 { + Vec2::new( + self.texture_descriptor.size.width as f32, + self.texture_descriptor.size.height as f32, + ) + } + /// Resizes the image to the new size, by removing information or appending 0 to the `data`. /// Does not properly resize the contents of the image, but only its internal `data` buffer. pub fn resize(&mut self, size: Extent3d) { @@ -440,3 +448,33 @@ impl RenderAsset for Image { }) } } + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn image_size_2d() { + let size = Extent3d { + width: 200, + height: 100, + depth_or_array_layers: 1, + }; + let image = Image::new_fill( + size, + TextureDimension::D2, + &[0, 0, 0, 255], + TextureFormat::Rgba8Unorm, + ); + assert_eq!( + Vec2::new(size.width as f32, size.height as f32), + image.size_2d() + ); + } + #[test] + fn image_default_size_2d() { + let image = Image::default(); + assert_eq!(Vec2::new(1.0, 1.0), image.size_2d()); + } +} From c70966cb6c1494213b7eb9d48746fcc1d4e1dcfd Mon Sep 17 00:00:00 2001 From: Boris Boutillier Date: Sun, 16 Jan 2022 18:34:28 +0100 Subject: [PATCH 2/2] Rename method to size() instead of size_2d() --- crates/bevy_render/src/texture/image.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index f5911822234e8..e6c1343a4aa91 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -119,7 +119,7 @@ impl Image { } /// Returns the size of a 2D image. - pub fn size_2d(&self) -> Vec2 { + pub fn size(&self) -> Vec2 { Vec2::new( self.texture_descriptor.size.width as f32, self.texture_descriptor.size.height as f32, @@ -455,7 +455,7 @@ mod test { use super::*; #[test] - fn image_size_2d() { + fn image_size() { let size = Extent3d { width: 200, height: 100, @@ -469,12 +469,12 @@ mod test { ); assert_eq!( Vec2::new(size.width as f32, size.height as f32), - image.size_2d() + image.size() ); } #[test] - fn image_default_size_2d() { + fn image_default_size() { let image = Image::default(); - assert_eq!(Vec2::new(1.0, 1.0), image.size_2d()); + assert_eq!(Vec2::new(1.0, 1.0), image.size()); } }