aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/texture/resolver.rs
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:23 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:23 +0100
commit0353181306702c40ad0fe482b5c2b159b46794a4 (patch)
tree33acc6a9e8ea4705884cf93b78cf869008f71832 /stockton-skeleton/src/texture/resolver.rs
parent664f0b0777ba96298b29f0c753d52a81cbb233f1 (diff)
refactor(all): rename some crates
Diffstat (limited to 'stockton-skeleton/src/texture/resolver.rs')
-rw-r--r--stockton-skeleton/src/texture/resolver.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/stockton-skeleton/src/texture/resolver.rs b/stockton-skeleton/src/texture/resolver.rs
new file mode 100644
index 0000000..f66b724
--- /dev/null
+++ b/stockton-skeleton/src/texture/resolver.rs
@@ -0,0 +1,55 @@
+//! Resolves a texture in a BSP File to an image
+
+use crate::texture::image::LoadableImage;
+use stockton_levels::{parts::IsTexture, prelude::HasTextures};
+
+use std::{
+ path::Path,
+ sync::{Arc, RwLock},
+};
+
+use image::{io::Reader, RgbaImage};
+
+/// An object that can be used to resolve a texture from a BSP File
+pub trait TextureResolver {
+ type Image: LoadableImage;
+
+ /// Get the given texture, or None if it's corrupt/not there.
+ fn resolve(&mut self, texture_id: u32) -> Option<Self::Image>;
+}
+
+/// 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<RwLock<T>>,
+}
+
+impl<'a, T: HasTextures> FsResolver<'a, T> {
+ pub fn new(path: &'a Path, map_lock: Arc<RwLock<T>>) -> Self {
+ FsResolver { path, map_lock }
+ }
+}
+
+impl<'a, T: HasTextures> TextureResolver for FsResolver<'a, T> {
+ type Image = RgbaImage;
+
+ fn resolve(&mut self, tex: u32) -> Option<Self::Image> {
+ let map = self.map_lock.read().unwrap();
+ let tex = map.get_texture(tex)?;
+ let path = self.path.join(&tex.name());
+
+ // drop(tex);
+ // drop(map);
+
+ 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());
+ }
+ }
+ }
+
+ log::warn!("Couldn't resolve texture {:?}", tex.name());
+ None
+ }
+}