summaryrefslogtreecommitdiff
path: root/src/gfx/media.rs
diff options
context:
space:
mode:
authorLizzy Fleckenstein <eliasfleckenstein@web.de>2023-05-24 03:43:26 +0200
committerLizzy Fleckenstein <eliasfleckenstein@web.de>2023-05-24 05:32:41 +0200
commit03c459695021b41f4986707a0fd5d42cd2d31169 (patch)
treef0b4738915cc3edad35685b54459cdd78547bf0d /src/gfx/media.rs
parent46ef7a60b3c88799fd8dfb2faf7c9f1dfb22b964 (diff)
downloadmt_client-03c459695021b41f4986707a0fd5d42cd2d31169.tar.xz
Move texture creation to media.rs
Diffstat (limited to 'src/gfx/media.rs')
-rw-r--r--src/gfx/media.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/gfx/media.rs b/src/gfx/media.rs
index ac5d158..eca2fcd 100644
--- a/src/gfx/media.rs
+++ b/src/gfx/media.rs
@@ -1,3 +1,4 @@
+use rand::Rng;
use std::collections::HashMap;
#[derive(rust_embed::RustEmbed)]
@@ -39,4 +40,49 @@ impl MediaMgr {
.find_map(|pack| pack.get(file))
.map(Vec::as_slice)
}
+
+ pub fn texture(&self, texture: &str) -> image::RgbaImage {
+ match match self.get(texture) {
+ Some(payload) => image::load_from_memory(payload)
+ .or_else(|_| image::load_from_memory_with_format(payload, image::ImageFormat::Tga))
+ .map_err(|e| eprintln!("while loading {texture}: {e}"))
+ .ok(),
+ None => {
+ eprintln!("unknown texture: {texture}");
+ None
+ }
+ } {
+ Some(v) => image::imageops::flip_vertical(&v),
+ None => {
+ let mut img = image::RgbImage::new(1, 1);
+ rand::thread_rng().fill(&mut img.get_pixel_mut(0, 0).0);
+
+ image::DynamicImage::from(img).to_rgba8()
+ }
+ }
+ }
+
+ pub fn texture_string(&self, texture: &str) -> image::RgbaImage {
+ texture
+ .split('^')
+ .fold(None, |mut base, next| {
+ if let Some(overlay) = match next {
+ "" => Some(self.texture("no_texture.png")),
+ texmod if matches!(texmod.chars().next(), Some('[')) => {
+ eprintln!("unknown texture modifier: {texmod}");
+ None
+ }
+ texture => Some(self.texture(texture)),
+ } {
+ if let Some(base) = &mut base {
+ image::imageops::overlay(base, &overlay, 0, 0);
+ } else {
+ base = Some(overlay);
+ }
+ }
+
+ base
+ })
+ .unwrap()
+ }
}