From dc95de37d3b16afa4894d9b1e9977f83ba14aa90 Mon Sep 17 00:00:00 2001 From: nenikitov Date: Thu, 25 Jan 2024 09:14:04 -0500 Subject: [PATCH] refactor: split texture things into multiple files --- engine/src/asset/texture/dat/mod.rs | 3 + engine/src/asset/texture/dat/offset.rs | 45 +++++++++ engine/src/asset/texture/dat/size.rs | 25 +++++ engine/src/asset/texture/dat/texture.rs | 60 +++++++++++ engine/src/asset/texture/mod.rs | 127 +----------------------- 5 files changed, 137 insertions(+), 123 deletions(-) create mode 100644 engine/src/asset/texture/dat/mod.rs create mode 100644 engine/src/asset/texture/dat/offset.rs create mode 100644 engine/src/asset/texture/dat/size.rs create mode 100644 engine/src/asset/texture/dat/texture.rs diff --git a/engine/src/asset/texture/dat/mod.rs b/engine/src/asset/texture/dat/mod.rs new file mode 100644 index 0000000..f4c3dfb --- /dev/null +++ b/engine/src/asset/texture/dat/mod.rs @@ -0,0 +1,3 @@ +pub mod offset; +pub mod size; +pub mod texture; diff --git a/engine/src/asset/texture/dat/offset.rs b/engine/src/asset/texture/dat/offset.rs new file mode 100644 index 0000000..c241c80 --- /dev/null +++ b/engine/src/asset/texture/dat/offset.rs @@ -0,0 +1,45 @@ +use crate::{ + asset::{extension::*, AssetParser}, + utils::nom::*, +}; + +pub struct TextureOffset { + pub width: u16, + pub height: u16, + pub offset: u32, + pub size_compressed: u32, + pub size_decompressed: u32, + pub animation_frames: u32, + pub next_animation_texture_id: u32, +} + +impl AssetParser for TextureOffset { + type Context<'ctx> = (); + + type Output = Self; + + fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result { + move |input| { + let (input, width) = number::le_u16(input)?; + let (input, height) = number::le_u16(input)?; + let (input, offset) = number::le_u32(input)?; + let (input, size_compressed) = number::le_u32(input)?; + let (input, size_decompressed) = number::le_u32(input)?; + let (input, animation_frames) = number::le_u32(input)?; + let (input, next_animation_texture_id) = number::le_u32(input)?; + + Ok(( + input, + Self { + width, + height, + offset, + size_compressed, + size_decompressed, + animation_frames, + next_animation_texture_id, + }, + )) + } + } +} diff --git a/engine/src/asset/texture/dat/size.rs b/engine/src/asset/texture/dat/size.rs new file mode 100644 index 0000000..e5d93d8 --- /dev/null +++ b/engine/src/asset/texture/dat/size.rs @@ -0,0 +1,25 @@ +use std::ops::Div; + +pub struct TextureSize { + pub width: u16, + pub height: u16, +} + +impl Div for TextureSize { + type Output = Self; + + fn div(self, rhs: u16) -> Self::Output { + &self / rhs + } +} + +impl Div for &TextureSize { + type Output = TextureSize; + + fn div(self, rhs: u16) -> Self::Output { + TextureSize { + width: self.width / rhs, + height: self.height / rhs, + } + } +} diff --git a/engine/src/asset/texture/dat/texture.rs b/engine/src/asset/texture/dat/texture.rs new file mode 100644 index 0000000..0f046f2 --- /dev/null +++ b/engine/src/asset/texture/dat/texture.rs @@ -0,0 +1,60 @@ +use super::size::TextureSize; +use crate::{ + asset::{extension::*, AssetParser}, + utils::nom::*, +}; +use itertools::Itertools; + +pub struct Texture { + pub colors: Vec>, +} + +impl AssetParser for Texture { + type Output = Self; + + type Context<'ctx> = &'ctx TextureSize; + + fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result { + let width = size.width as usize; + let height = size.height as usize; + + move |input| { + let (input, colors) = multi::count!(number::le_u8, width * height)(input)?; + + let colors = colors + .into_iter() + .chunks(width) + .into_iter() + .map(Iterator::collect) + .collect(); + + Ok((input, Self { colors })) + } + } +} + +pub struct MippedTexture { + pub mips: [Texture; 4], +} + +impl AssetParser for MippedTexture { + type Output = Self; + + type Context<'ctx> = TextureSize; + + fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result { + move |input| { + let (input, mip_1) = Texture::parser(&size)(&input)?; + let (input, mip_2) = Texture::parser(&(&size / 2))(&input)?; + let (input, mip_3) = Texture::parser(&(&size / 4))(&input)?; + let (input, mip_4) = Texture::parser(&(&size / 8))(&input)?; + + Ok(( + &[], + Self { + mips: [mip_1, mip_2, mip_3, mip_4], + }, + )) + } + } +} diff --git a/engine/src/asset/texture/mod.rs b/engine/src/asset/texture/mod.rs index ffab3c8..636848b 100644 --- a/engine/src/asset/texture/mod.rs +++ b/engine/src/asset/texture/mod.rs @@ -1,129 +1,10 @@ -use std::ops::Div; +mod dat; use super::{extension::*, AssetParser}; use crate::utils::{compression::decompress, nom::*}; -use itertools::Itertools; - -pub struct TextureOffset { - pub width: u16, - pub height: u16, - pub offset: u32, - pub size_compressed: u32, - pub size_decompressed: u32, - pub animation_frames: u32, - pub next_animation_texture_id: u32, -} - -impl AssetParser for TextureOffset { - type Context<'ctx> = (); - - type Output = Self; - - fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result { - move |input| { - let (input, width) = number::le_u16(input)?; - let (input, height) = number::le_u16(input)?; - let (input, offset) = number::le_u32(input)?; - let (input, size_compressed) = number::le_u32(input)?; - let (input, size_decompressed) = number::le_u32(input)?; - let (input, animation_frames) = number::le_u32(input)?; - let (input, next_animation_texture_id) = number::le_u32(input)?; - - Ok(( - input, - Self { - width, - height, - offset, - size_compressed, - size_decompressed, - animation_frames, - next_animation_texture_id, - }, - )) - } - } -} - -pub struct Texture { - pub colors: Vec>, -} - -pub struct TextureSize { - width: u16, - height: u16, -} - -impl Div for TextureSize { - type Output = Self; - - fn div(self, rhs: u16) -> Self::Output { - &self / rhs - } -} - -impl Div for &TextureSize { - type Output = TextureSize; - - fn div(self, rhs: u16) -> Self::Output { - TextureSize { - width: self.width / rhs, - height: self.height / rhs, - } - } -} - -impl AssetParser for Texture { - type Output = Self; - - type Context<'ctx> = &'ctx TextureSize; - - fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result { - let width = size.width as usize; - let height = size.height as usize; - - move |input| { - let (input, colors) = multi::count!(number::le_u8, width * height)(input)?; - - let colors = colors - .into_iter() - .chunks(width) - .into_iter() - .map(Iterator::collect) - .collect(); - - Ok((input, Self { colors })) - } - } -} - -pub struct MippedTexture { - pub mips: [Texture; 4], -} - -impl AssetParser for MippedTexture { - type Output = Self; - - type Context<'ctx> = TextureSize; - - fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result { - move |input| { - let (input, mip_1) = Texture::parser(&size)(&input)?; - let (input, mip_2) = Texture::parser(&(&size / 2))(&input)?; - let (input, mip_3) = Texture::parser(&(&size / 4))(&input)?; - let (input, mip_4) = Texture::parser(&(&size / 8))(&input)?; - - Ok(( - &[], - Self { - mips: [mip_1, mip_2, mip_3, mip_4], - }, - )) - } - } -} +use dat::{offset::TextureOffset, size::TextureSize, texture::MippedTexture}; -pub struct TextureOffsetCollection {} +pub struct TextureOffsetCollection; impl AssetParser for TextureOffsetCollection { type Output = Vec; @@ -139,7 +20,7 @@ impl AssetParser for TextureOffsetCollection { } } -pub struct MippedTextureCollection {} +pub struct MippedTextureCollection; impl AssetParser for MippedTextureCollection { type Output = Vec;