From 6367a9ba5a549b62f01da61fb50323877b9f52ff Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:24 +0100 Subject: refactor(all): move types to -skeleton --- stockton-levels/src/parts/mod.rs | 2 +- stockton-levels/src/parts/textures.rs | 37 ++++++++++++++++++++++++++++++++++- stockton-levels/src/parts/visdata.rs | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) (limited to 'stockton-levels/src/parts') diff --git a/stockton-levels/src/parts/mod.rs b/stockton-levels/src/parts/mod.rs index 05697f0..d164bc3 100644 --- a/stockton-levels/src/parts/mod.rs +++ b/stockton-levels/src/parts/mod.rs @@ -13,5 +13,5 @@ pub mod data { pub use entities::{HasEntities, IsEntity}; pub use faces::{HasFaces, IsFace}; -pub use textures::{HasTextures, IsTexture}; +pub use textures::{HasTextures, IsTexture, FsResolver}; pub use visdata::HasVisData; diff --git a/stockton-levels/src/parts/textures.rs b/stockton-levels/src/parts/textures.rs index 8e6cb9a..ae946fd 100644 --- a/stockton-levels/src/parts/textures.rs +++ b/stockton-levels/src/parts/textures.rs @@ -11,7 +11,9 @@ // You should have received a copy of the GNU General Public License along // with this program. If not, see . -use std::iter::Iterator; +use std::{iter::Iterator, path::Path, sync::{Arc, RwLock}}; +use image::{RgbaImage, io::Reader}; +use stockton_skeleton::texture::TextureResolver; pub type TextureRef = u32; @@ -46,3 +48,36 @@ impl<'a, T: HasTextures> Iterator for Textures<'a, T> { res } } + + +/// A basic filesystem resolver which gets the texture name from any HasTextures Object. +pub struct FsResolver<'a, T: HasTextures> { + path: &'a Path, + map_lock: Arc>, +} + +impl<'a, T: HasTextures> FsResolver<'a, T> { + pub fn new(path: &'a Path, map_lock: Arc>) -> Self { + FsResolver { path, map_lock } + } +} + +impl<'a, T: HasTextures> TextureResolver for FsResolver<'a, T> { + type Image = RgbaImage; + + fn resolve(&mut self, tex: u32) -> Option { + let map = self.map_lock.read().unwrap(); + let tex = map.get_texture(tex)?; + let path = self.path.join(&tex.name()); + + if let Ok(file) = Reader::open(path) { + if let Ok(guessed) = file.with_guessed_format() { + if let Ok(decoded) = guessed.decode() { + return Some(decoded.into_rgba8()); + } + } + } + + None + } +} diff --git a/stockton-levels/src/parts/visdata.rs b/stockton-levels/src/parts/visdata.rs index aa2ec3d..3e58d4c 100644 --- a/stockton-levels/src/parts/visdata.rs +++ b/stockton-levels/src/parts/visdata.rs @@ -1,6 +1,6 @@ use super::faces::FaceRef; use std::iter::Iterator; -use stockton_types::components::{CameraSettings, Transform}; +use stockton_skeleton::components::{CameraSettings, Transform}; pub trait HasVisData<'a> { type Faces: Iterator; -- cgit v1.2.3