From bc6d4253eb0878936e0f04a124708a9157ace708 Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Tue, 28 Sep 2021 19:32:32 -0700 Subject: [PATCH 1/3] change add_texture to return index --- pipelined/bevy_sprite2/src/texture_atlas.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pipelined/bevy_sprite2/src/texture_atlas.rs b/pipelined/bevy_sprite2/src/texture_atlas.rs index 58ded48a3a920..639f1c355092b 100644 --- a/pipelined/bevy_sprite2/src/texture_atlas.rs +++ b/pipelined/bevy_sprite2/src/texture_atlas.rs @@ -119,13 +119,15 @@ impl TextureAtlas { } /// Add a sprite to the list of textures in the `TextureAtlas` + /// returns an index to the texture which can be used with `TextureAtlasSprite` /// /// # Arguments /// /// * `rect` - The section of the atlas that contains the texture to be added, /// from the top-left corner of the texture to the bottom-right corner - pub fn add_texture(&mut self, rect: Rect) { + pub fn add_texture(&mut self, rect: Rect) -> usize { self.textures.push(rect); + self.textures.len() - 1 } /// How many textures are in the `TextureAtlas` From 279c4f3dda8761ba093cd3f9e8c654c2c52363f6 Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Tue, 28 Sep 2021 19:32:45 -0700 Subject: [PATCH 2/3] change sprite indexes to usize --- examples/2d/pipelined_texture_atlas.rs | 2 +- pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs | 4 ++-- pipelined/bevy_sprite2/src/render/mod.rs | 2 +- pipelined/bevy_sprite2/src/texture_atlas.rs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/2d/pipelined_texture_atlas.rs b/examples/2d/pipelined_texture_atlas.rs index a11bad5e7e3ee..ac3ec9858494e 100644 --- a/examples/2d/pipelined_texture_atlas.rs +++ b/examples/2d/pipelined_texture_atlas.rs @@ -81,7 +81,7 @@ fn setup( scale: Vec3::splat(4.0), ..Default::default() }, - sprite: TextureAtlasSprite::new(vendor_index as u32), + sprite: TextureAtlasSprite::new(vendor_index), texture_atlas: atlas_handle, ..Default::default() }); diff --git a/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs b/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs index 9d57adf9e01f3..1fd70add62ab5 100644 --- a/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs +++ b/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs @@ -22,7 +22,7 @@ impl DynamicTextureAtlasBuilder { texture_atlas: &mut TextureAtlas, textures: &mut Assets, texture: &Image, - ) -> Option { + ) -> Option { let allocation = self.atlas_allocator.allocate(size2( texture.texture_descriptor.size.width as i32 + self.padding, texture.texture_descriptor.size.height as i32 + self.padding, @@ -34,7 +34,7 @@ impl DynamicTextureAtlasBuilder { rect.max.x -= self.padding as f32; rect.max.y -= self.padding as f32; texture_atlas.add_texture(rect); - Some((texture_atlas.len() - 1) as u32) + Some(texture_atlas.len() - 1) } else { None } diff --git a/pipelined/bevy_sprite2/src/render/mod.rs b/pipelined/bevy_sprite2/src/render/mod.rs index 9a2f549b4da0a..d44db571ffffe 100644 --- a/pipelined/bevy_sprite2/src/render/mod.rs +++ b/pipelined/bevy_sprite2/src/render/mod.rs @@ -166,7 +166,7 @@ pub fn extract_atlases( let mut sprites = Vec::new(); for (entity, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() { if let Some(texture_atlas) = texture_atlases.get(texture_atlas_handle) { - let rect = texture_atlas.textures[atlas_sprite.index as usize]; + let rect = texture_atlas.textures[atlas_sprite.index]; sprites.push(( entity, (ExtractedSprite { diff --git a/pipelined/bevy_sprite2/src/texture_atlas.rs b/pipelined/bevy_sprite2/src/texture_atlas.rs index 639f1c355092b..5ded3e57dd4ff 100644 --- a/pipelined/bevy_sprite2/src/texture_atlas.rs +++ b/pipelined/bevy_sprite2/src/texture_atlas.rs @@ -24,7 +24,7 @@ pub struct TextureAtlas { #[uuid = "7233c597-ccfa-411f-bd59-9af349432ada"] pub struct TextureAtlasSprite { pub color: Color, - pub index: u32, + pub index: usize, pub flip_x: bool, pub flip_y: bool, } @@ -41,7 +41,7 @@ impl Default for TextureAtlasSprite { } impl TextureAtlasSprite { - pub fn new(index: u32) -> TextureAtlasSprite { + pub fn new(index: usize) -> TextureAtlasSprite { Self { index, ..Default::default() From b259b608213d09ebdbf61f20debbe3980dd1b497 Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Sat, 16 Oct 2021 11:34:59 -0700 Subject: [PATCH 3/3] remove recalculation of index --- pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs b/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs index 1fd70add62ab5..21928ed29785d 100644 --- a/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs +++ b/pipelined/bevy_sprite2/src/dynamic_texture_atlas_builder.rs @@ -33,8 +33,7 @@ impl DynamicTextureAtlasBuilder { let mut rect: Rect = allocation.rectangle.into(); rect.max.x -= self.padding as f32; rect.max.y -= self.padding as f32; - texture_atlas.add_texture(rect); - Some(texture_atlas.len() - 1) + Some(texture_atlas.add_texture(rect)) } else { None }