From 8ae584b87e51cca2654a5c5b47b7757dad9efa4d Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 22 Jan 2024 08:49:42 -0800 Subject: [PATCH] Fixed texture loader --- crates/content/src/lib.rs | 5 +++++ crates/packer/src/lib.rs | 5 +++++ crates/render/src/texturearray.rs | 31 +++++++++---------------------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/content/src/lib.rs b/crates/content/src/lib.rs index 8b57a39..df881a6 100644 --- a/crates/content/src/lib.rs +++ b/crates/content/src/lib.rs @@ -303,6 +303,11 @@ impl Content { return &self.sprite_atlas.atlas_list; } + /// Get sprite atlas metadata + pub fn get_atlas(&self) -> &SpriteAtlas { + return &self.sprite_atlas; + } + /// Get a texture by its index pub fn get_image(&self, idx: NonZeroU32) -> &SpriteAtlasImage { &self.sprite_atlas.get_by_idx(idx) diff --git a/crates/packer/src/lib.rs b/crates/packer/src/lib.rs index 7a6673f..25f6477 100644 --- a/crates/packer/src/lib.rs +++ b/crates/packer/src/lib.rs @@ -76,6 +76,11 @@ impl SpriteAtlas { self.path_map.get(path).map(|x| *x) } + /// Iterate all images in this atlas + pub fn iter_images(&self) -> impl Iterator { + self.index.iter() + } + /// Get the number of images in this atlas pub fn len(&self) -> u32 { self.index.len() as u32 diff --git a/crates/render/src/texturearray.rs b/crates/render/src/texturearray.rs index f16af59..e117f60 100644 --- a/crates/render/src/texturearray.rs +++ b/crates/render/src/texturearray.rs @@ -109,28 +109,15 @@ impl TextureArray { let mut image_locations = AtlasArray::zeroed(); println!("sending to gpu"); - for sprite in &ct.sprites { - for section in sprite.iter_sections() { - for idx in §ion.frames { - // Some atlas entries may be written twice here, - // but that's not really a problem. They're all the same! - // - // This happens rarely---only when two different sections - // use the same frame. - let idx = NonZeroU32::new(*idx); - if idx.is_some() { - let image = ct.get_image(idx.unwrap()); - image_locations.data[idx.unwrap().get() as usize] = AtlasImageLocation { - xpos: image.x, - ypos: image.y, - width: image.w, - height: image.h, - atlas_texture: image.atlas, - _padding: Default::default(), - }; - } - } - } + for image in ct.get_atlas().iter_images() { + image_locations.data[image.idx.get() as usize] = AtlasImageLocation { + xpos: image.x, + ypos: image.y, + width: image.w, + height: image.h, + atlas_texture: image.atlas, + _padding: Default::default(), + }; } let sampler = device.create_sampler(&wgpu::SamplerDescriptor {