Skip to content

Commit

Permalink
refactor: split texture things into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
nenikitov committed Jan 25, 2024
1 parent 4a43477 commit dc95de3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 123 deletions.
3 changes: 3 additions & 0 deletions engine/src/asset/texture/dat/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod offset;
pub mod size;
pub mod texture;
45 changes: 45 additions & 0 deletions engine/src/asset/texture/dat/offset.rs
Original file line number Diff line number Diff line change
@@ -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<Wildcard> for TextureOffset {
type Context<'ctx> = ();

type Output = Self;

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
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,
},
))
}
}
}
25 changes: 25 additions & 0 deletions engine/src/asset/texture/dat/size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::ops::Div;

pub struct TextureSize {
pub width: u16,
pub height: u16,
}

impl Div<u16> for TextureSize {
type Output = Self;

fn div(self, rhs: u16) -> Self::Output {
&self / rhs
}
}

impl Div<u16> for &TextureSize {
type Output = TextureSize;

fn div(self, rhs: u16) -> Self::Output {
TextureSize {
width: self.width / rhs,
height: self.height / rhs,
}
}
}
60 changes: 60 additions & 0 deletions engine/src/asset/texture/dat/texture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::size::TextureSize;
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use itertools::Itertools;

pub struct Texture {
pub colors: Vec<Vec<u8>>,
}

impl AssetParser<Wildcard> for Texture {
type Output = Self;

type Context<'ctx> = &'ctx TextureSize;

fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
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<Wildcard> for MippedTexture {
type Output = Self;

type Context<'ctx> = TextureSize;

fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
move |input| {
let (input, mip_1) = Texture::parser(&size)(&input)?;

Check warning on line 47 in engine/src/asset/texture/dat/texture.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> engine/src/asset/texture/dat/texture.rs:47:57 | 47 | let (input, mip_1) = Texture::parser(&size)(&input)?; | ^^^^^^ help: change this to: `input` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
let (input, mip_2) = Texture::parser(&(&size / 2))(&input)?;

Check warning on line 48 in engine/src/asset/texture/dat/texture.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> engine/src/asset/texture/dat/texture.rs:48:64 | 48 | let (input, mip_2) = Texture::parser(&(&size / 2))(&input)?; | ^^^^^^ help: change this to: `input` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
let (input, mip_3) = Texture::parser(&(&size / 4))(&input)?;

Check warning on line 49 in engine/src/asset/texture/dat/texture.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> engine/src/asset/texture/dat/texture.rs:49:64 | 49 | let (input, mip_3) = Texture::parser(&(&size / 4))(&input)?; | ^^^^^^ help: change this to: `input` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
let (input, mip_4) = Texture::parser(&(&size / 8))(&input)?;

Check warning on line 50 in engine/src/asset/texture/dat/texture.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> engine/src/asset/texture/dat/texture.rs:50:64 | 50 | let (input, mip_4) = Texture::parser(&(&size / 8))(&input)?; | ^^^^^^ help: change this to: `input` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

Ok((
&[],
Self {
mips: [mip_1, mip_2, mip_3, mip_4],
},
))
}
}
}
127 changes: 4 additions & 123 deletions engine/src/asset/texture/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Wildcard> for TextureOffset {
type Context<'ctx> = ();

type Output = Self;

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
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<Vec<u8>>,
}

pub struct TextureSize {
width: u16,
height: u16,
}

impl Div<u16> for TextureSize {
type Output = Self;

fn div(self, rhs: u16) -> Self::Output {
&self / rhs
}
}

impl Div<u16> for &TextureSize {
type Output = TextureSize;

fn div(self, rhs: u16) -> Self::Output {
TextureSize {
width: self.width / rhs,
height: self.height / rhs,
}
}
}

impl AssetParser<Wildcard> for Texture {
type Output = Self;

type Context<'ctx> = &'ctx TextureSize;

fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
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<Wildcard> for MippedTexture {
type Output = Self;

type Context<'ctx> = TextureSize;

fn parser(size: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
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<Pack> for TextureOffsetCollection {
type Output = Vec<TextureOffset>;
Expand All @@ -139,7 +20,7 @@ impl AssetParser<Pack> for TextureOffsetCollection {
}
}

pub struct MippedTextureCollection {}
pub struct MippedTextureCollection;

impl AssetParser<Pack> for MippedTextureCollection {
type Output = Vec<MippedTexture>;
Expand Down

0 comments on commit dc95de3

Please sign in to comment.