From 511d3873f5f567c97eecd69d186bb4f93f927d58 Mon Sep 17 00:00:00 2001 From: Micha White Date: Sun, 2 Oct 2022 13:49:47 -0400 Subject: Hacked in textures --- src/texture.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) (limited to 'src/texture.rs') diff --git a/src/texture.rs b/src/texture.rs index 8a36334..9184304 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -12,9 +12,10 @@ use thiserror::Error; static NEXT_TEXTURE_ID: AtomicUsize = AtomicUsize::new(0); #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -struct TextureId(usize); +pub struct TextureId(usize); impl TextureId { + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self(NEXT_TEXTURE_ID.fetch_add(1, Ordering::Relaxed)) } @@ -58,15 +59,16 @@ impl From for TextureError { } } -struct TextureAtlases<'a> { +// TODO make this Debug +pub struct TextureAtlases<'a> { packer: MultiTexturePacker<'a, image::RgbaImage, TextureId>, + width: u32, + height: u32, } impl<'a> Default for TextureAtlases<'a> { fn default() -> Self { - Self { - packer: MultiTexturePacker::new_skyline(TexturePackerConfig::default()), - } + Self::new(1024, 1024) } } @@ -78,8 +80,11 @@ impl<'a> TextureAtlases<'a> { packer: MultiTexturePacker::new_skyline(TexturePackerConfig { max_width: width, max_height: height, + //trim: false, ..Default::default() }), + width, + height, } } @@ -98,6 +103,50 @@ impl<'a> TextureAtlases<'a> { Ok(id) } + fn texture_frame(&self, id: TextureId) -> Option<&texture_packer::Frame> { + self.packer + .get_pages() + .iter() + .map(|a| a.get_frame(&id)) + .next()? + } + + pub fn texture_width(&self, id: TextureId) -> Option { + let frame = self.texture_frame(id)?; + Some(frame.frame.w) + } + + pub fn texture_height(&self, id: TextureId) -> Option { + let frame = self.texture_frame(id)?; + Some(frame.frame.h) + } + + pub fn texture_x(&self, id: TextureId) -> Option { + let frame = self.texture_frame(id)?; + Some(frame.frame.x) + } + + pub fn texture_y(&self, id: TextureId) -> Option { + let frame = self.texture_frame(id)?; + Some(frame.frame.y) + } + + pub(crate) const fn bytes_per_row(&self) -> u32 { + self.width * 4 + } + + pub(crate) const fn height(&self) -> u32 { + self.height + } + + pub(crate) const fn extent_3d(&self) -> wgpu::Extent3d { + wgpu::Extent3d { + width: self.width, + height: self.height, + depth_or_array_layers: 1, + } + } + pub(crate) fn atlases(&self) -> ExportResult> { self.packer .get_pages() -- cgit v1.2.3