diff options
| author | Micha White <botahamec@outlook.com> | 2022-10-05 11:11:30 -0400 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2022-10-05 11:11:30 -0400 |
| commit | eac2596771a349ff5a810a7ebdfba0a8a4ed96bc (patch) | |
| tree | 3355f4ff454bd1acbee1f18b67d711e5727d48ab /src | |
| parent | ed5b952b313275e58102770e9dda29a7274c363c (diff) | |
Used macros to make the code shorter
Diffstat (limited to 'src')
| -rw-r--r-- | src/texture.rs | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/src/texture.rs b/src/texture.rs index 4ba460d..37f3a98 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -75,6 +75,15 @@ impl<'a> Default for TextureAtlases<'a> { } } +macro_rules! texture_info { + ($name: ident, $prop: ident) => { + pub fn $name(&self, id: TextureId) -> Option<u32> { + let frame = self.texture_frame(id)?; + Some(frame.frame.$prop) + } + }; +} + impl<'a> TextureAtlases<'a> { /// Creates a new texture atlas, with the given size // TODO why is this u32? @@ -115,25 +124,10 @@ impl<'a> TextureAtlases<'a> { .next()? } - pub fn texture_width(&self, id: TextureId) -> Option<u32> { - let frame = self.texture_frame(id)?; - Some(frame.frame.w) - } - - pub fn texture_height(&self, id: TextureId) -> Option<u32> { - let frame = self.texture_frame(id)?; - Some(frame.frame.h) - } - - pub fn texture_x(&self, id: TextureId) -> Option<u32> { - let frame = self.texture_frame(id)?; - Some(frame.frame.x) - } - - pub fn texture_y(&self, id: TextureId) -> Option<u32> { - let frame = self.texture_frame(id)?; - Some(frame.frame.y) - } + texture_info!(texture_width, w); + texture_info!(texture_height, h); + texture_info!(texture_x, x); + texture_info!(texture_y, y); const fn extent_3d(&self) -> wgpu::Extent3d { wgpu::Extent3d { @@ -197,6 +191,18 @@ pub struct WgpuTextures { diffuse_bind_group: wgpu::BindGroup, } +macro_rules! get_info { + ($name: ident, $divisor: ident) => { + // TODO try to remove this + #[allow(clippy::cast_precision_loss)] + pub fn $name(&self, id: TextureId) -> Option<f32> { + self.atlases + .$name(id) + .map(|u| u as f32 / self.atlases.extent_3d().$divisor as f32) + } + }; +} + impl WgpuTextures { // TODO this is still too large pub fn new(device: &wgpu::Device, width: u32, height: u32) -> (Self, wgpu::BindGroupLayout) { @@ -281,34 +287,10 @@ impl WgpuTextures { self.atlases.load_from_memory(texture, format) } - // TODO try to remove this - #[allow(clippy::cast_precision_loss)] - pub fn texture_width(&self, id: TextureId) -> Option<f32> { - self.atlases - .texture_width(id) - .map(|u| u as f32 / self.atlases.extent_3d().width as f32) - } - - #[allow(clippy::cast_precision_loss)] - pub fn texture_height(&self, id: TextureId) -> Option<f32> { - self.atlases - .texture_height(id) - .map(|u| u as f32 / self.atlases.extent_3d().height as f32) - } - - #[allow(clippy::cast_precision_loss)] - pub fn texture_x(&self, id: TextureId) -> Option<f32> { - self.atlases - .texture_x(id) - .map(|u| u as f32 / self.atlases.extent_3d().width as f32) - } - - #[allow(clippy::cast_precision_loss)] - pub fn texture_y(&self, id: TextureId) -> Option<f32> { - self.atlases - .texture_y(id) - .map(|u| u as f32 / self.atlases.extent_3d().height as f32) - } + get_info!(texture_width, width); + get_info!(texture_height, height); + get_info!(texture_x, width); + get_info!(texture_y, height); pub const fn bind_group(&self) -> &wgpu::BindGroup { &self.diffuse_bind_group |
