From 71ce7ed846f05352b63458d4a63a97cca36169dd Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Tue, 11 Apr 2023 16:36:59 -0400 Subject: [PATCH] spritesheets --- Cargo.lock | 301 +++++++++++++++++++++++++++++++- assets/dist/spritesheet.ron | 147 +--------------- client/Cargo.toml | 3 + client/src/lib.rs | 12 +- client/src/rendering/mod.rs | 1 - client/src/rendering/texture.rs | 0 client/src/textures/loader.rs | 71 ++++++++ client/src/textures/mod.rs | 8 + 8 files changed, 393 insertions(+), 150 deletions(-) delete mode 100644 client/src/rendering/texture.rs create mode 100644 client/src/textures/loader.rs create mode 100644 client/src/textures/mod.rs diff --git a/Cargo.lock b/Cargo.lock index c95a9f37ba43ee421f308f3571805ba1dd4b4d3e..75b72c40b192a12768e9ef1d81762d2eb5eb1adc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "approx" version = "0.5.1" @@ -73,12 +79,24 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "base64" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" + [[package]] name = "bit-vec" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + [[package]] name = "bitflags" version = "1.3.2" @@ -156,6 +174,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "colored" version = "2.0.0" @@ -197,6 +221,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam" version = "0.8.2" @@ -264,6 +297,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-common" version = "0.1.6" @@ -296,6 +335,54 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "exr" +version = "1.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd2162b720141a91a054640662d3edce3d50a944a50ffca5313cd951abb35b4" +dependencies = [ + "bit_field", + "flume", + "half", + "lebe", + "miniz_oxide 0.6.2", + "rayon-core", + "smallvec", + "zune-inflate", +] + +[[package]] +name = "fdeflate" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d329bdeac514ee06249dabc27877490f17f5d371ec693360768b838e19f3ae10" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "flate2" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" +dependencies = [ + "crc32fast", + "miniz_oxide 0.6.2", +] + +[[package]] +name = "flume" +version = "0.10.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577" +dependencies = [ + "futures-core", + "futures-sink", + "nanorand", + "pin-project", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -417,8 +504,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", +] + +[[package]] +name = "gif" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80792593675e051cf94a4b111980da2ba60d4a83e43e0048c5693baab3977045" +dependencies = [ + "color_quant", + "weezl", ] [[package]] @@ -440,6 +539,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "half" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0" +dependencies = [ + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -532,6 +640,25 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "image" +version = "0.24.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527909aa81e20ac3a44803521443a765550f09b5130c2c2fa1ea59c2f8f50a3a" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "exr", + "gif", + "jpeg-decoder", + "num-rational", + "num-traits", + "png", + "qoi", + "tiff", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -548,6 +675,15 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +[[package]] +name = "jpeg-decoder" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e" +dependencies = [ + "rayon", +] + [[package]] name = "js-sys" version = "0.3.61" @@ -563,6 +699,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lebe" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" + [[package]] name = "libc" version = "0.2.141" @@ -575,6 +717,16 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.17" @@ -617,6 +769,25 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" +dependencies = [ + "adler", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", + "simd-adler32", +] + [[package]] name = "mio" version = "0.8.6" @@ -656,6 +827,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "num-complex" version = "0.4.3" @@ -781,6 +961,26 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "pin-project" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -793,6 +993,19 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "png" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaeebc51f9e7d2c150d3f3bfeb667f2aa985db5ef1e3d212847bdedb488beeaa" +dependencies = [ + "bitflags", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.7.1", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -808,6 +1021,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "quote" version = "1.0.26" @@ -874,6 +1096,28 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "rmp" version = "0.8.11" @@ -902,6 +1146,17 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5864e7ef1a6b7bcf1d6ca3f655e65e724ed3b52546a0d0a663c991522f552ea" +[[package]] +name = "ron" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300a51053b1cb55c80b7a9fde4120726ddf25ca241a1cbb926626f62fb136bff" +dependencies = [ + "base64 0.13.1", + "bitflags", + "serde", +] + [[package]] name = "rustc-hash" version = "1.1.0" @@ -1008,6 +1263,12 @@ dependencies = [ "wide", ] +[[package]] +name = "simd-adler32" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f" + [[package]] name = "simple_logger" version = "4.1.0" @@ -1058,20 +1319,32 @@ dependencies = [ "smallvec", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + [[package]] name = "starkingdoms-client" version = "0.1.0" dependencies = [ "async-recursion", "async-trait", + "base64 0.21.0", "console_error_panic_hook", "console_log", "futures", + "image", "js-sys", "lazy_static", "log", "markdown", "rmp-serde", + "ron", "serde", "starkingdoms-protocol", "url", @@ -1151,6 +1424,17 @@ dependencies = [ "syn 2.0.13", ] +[[package]] +name = "tiff" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7449334f9ff2baf290d55d73983a7d6fa15e01198faef72af07e2a8db851e471" +dependencies = [ + "flate2", + "jpeg-decoder", + "weezl", +] + [[package]] name = "time" version = "0.3.20" @@ -1287,7 +1571,7 @@ version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30ee6ab729cd4cf0fd55218530c4522ed30b7b6081752839b68fcec8d0960788" dependencies = [ - "base64", + "base64 0.13.1", "byteorder", "bytes", "http", @@ -1454,6 +1738,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "weezl" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" + [[package]] name = "wide" version = "0.7.8" @@ -1585,3 +1875,12 @@ dependencies = [ "wasm-bindgen-futures", "web-sys", ] + +[[package]] +name = "zune-inflate" +version = "0.2.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "440a08fd59c6442e4b846ea9b10386c38307eae728b216e1ab2c305d1c9daaf8" +dependencies = [ + "simd-adler32", +] diff --git a/assets/dist/spritesheet.ron b/assets/dist/spritesheet.ron index 33cd4d4eb066908988a06e025477d6d7742dae8c..da9eea7b20f205cbd8c44ce22a2a85d82c2b488b 100644 --- a/assets/dist/spritesheet.ron +++ b/assets/dist/spritesheet.ron @@ -1,146 +1 @@ -( - texture_width: 3584, - texture_height: 4128, - sprites: [ - ( - x: 0, - y: 0, - width: 2048, - height: 2048, - offsets: None, - ), - ( - x: 0, - y: 2048, - width: 512, - height: 544, - offsets: None, - ), - ( - x: 0, - y: 2592, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 0, - y: 3104, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 0, - y: 3616, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 512, - y: 2048, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 512, - y: 2560, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 512, - y: 3072, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 512, - y: 3584, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1024, - y: 2048, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1024, - y: 2560, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1024, - y: 3072, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1024, - y: 3584, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1536, - y: 2048, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1536, - y: 2560, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1536, - y: 3072, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 1536, - y: 3584, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 2048, - y: 0, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 2560, - y: 0, - width: 512, - height: 512, - offsets: None, - ), - ( - x: 3072, - y: 0, - width: 512, - height: 512, - offsets: None, - ), - ], -) \ No newline at end of file +(texture_width:3584,texture_height:4128,sprites:[(name:"earth",x:0,y:0,width:2048,height:2048,offsets:None,),(name:"icon_landingthruster",x:0,y:2048,width:512,height:544,offsets:None,),(name:"autoplr_cfg",x:0,y:2592,width:512,height:512,offsets:None,),(name:"autoplr_error",x:0,y:3104,width:512,height:512,offsets:None,),(name:"autoplr_on",x:0,y:3616,width:512,height:512,offsets:None,),(name:"cargo_off",x:512,y:2048,width:512,height:512,offsets:None,),(name:"cargo_on",x:512,y:2560,width:512,height:512,offsets:None,),(name:"ecothruster_on",x:512,y:3072,width:512,height:512,offsets:None,),(name:"hearty",x:512,y:3584,width:512,height:512,offsets:None,),(name:"hub_off",x:1024,y:2048,width:512,height:512,offsets:None,),(name:"hub_on",x:1024,y:2560,width:512,height:512,offsets:None,),(name:"landingleg",x:1024,y:3072,width:512,height:512,offsets:None,),(name:"landingthruster_off",x:1024,y:3584,width:512,height:512,offsets:None,),(name:"landingthruster_on",x:1536,y:2048,width:512,height:512,offsets:None,),(name:"powerhub_off",x:1536,y:2560,width:512,height:512,offsets:None,),(name:"powerhub_on",x:1536,y:3072,width:512,height:512,offsets:None,),(name:"superthruster_off",x:1536,y:3584,width:512,height:512,offsets:None,),(name:"superthruster_on",x:2048,y:0,width:512,height:512,offsets:None,),(name:"thruster_off",x:2560,y:0,width:512,height:512,offsets:None,),(name:"thruster_on",x:3072,y:0,width:512,height:512,offsets:None,),],) \ No newline at end of file diff --git a/client/Cargo.toml b/client/Cargo.toml index 9cef67cf4d187c5c3d3beb5cd1301ad93edb0117..8fb3e83c76335f0abe5cbaac517aea9dd5323f08 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -25,6 +25,9 @@ markdown = "1.0.0-alpha.7" # DO NOT DOWNGRADE async-recursion = "1" console_error_panic_hook = "0.1" async-trait = "0.1.68" +image = "0.24.6" +ron = "0.8" +base64 = "0.21.0" [dependencies.web-sys] version = "0.3.4" diff --git a/client/src/lib.rs b/client/src/lib.rs index 54cec1817aca27c51e9020a648bdf19657f61518..96d6e3c419e315307159acea7cc7d8835a87c894 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -20,12 +20,15 @@ use wasm_bindgen_futures::JsFuture; use web_sys::{Window}; use starkingdoms_protocol::GoodbyeReason::PingPongTimeout; use crate::rendering::renderer::WebRenderer; +use crate::textures::loader::TextureLoader; +use crate::textures::TextureManager; #[macro_use] pub mod macros; pub mod chat; pub mod rendering; +pub mod textures; #[wasm_bindgen] extern { @@ -71,6 +74,13 @@ pub async fn rust_init(gateway: &str, username: &str) -> Result<(), JsValue> { info!("Logger setup successfully"); + info!("Loading sprites..."); + set_status("Loading sprites.."); + + //let textures = TextureLoader::load().map_err(|e| e.to_string())?; + + + match main(gateway, username, 1).await { Ok(c) => c, Err(e) => { @@ -79,8 +89,6 @@ pub async fn rust_init(gateway: &str, username: &str) -> Result<(), JsValue> { } }; - let mut client = CLIENT.write().unwrap(); - info!("StarKingdoms client set up successfully"); Ok(()) diff --git a/client/src/rendering/mod.rs b/client/src/rendering/mod.rs index a025096e54804ead549cd3d4ea451c2b1d193991..13b95d4d3ff8650ddb128fcfcd157198403d15a1 100644 --- a/client/src/rendering/mod.rs +++ b/client/src/rendering/mod.rs @@ -2,7 +2,6 @@ use std::error::Error; use async_trait::async_trait; pub mod renderer; -pub mod texture; #[async_trait] pub trait Renderer { diff --git a/client/src/rendering/texture.rs b/client/src/rendering/texture.rs deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/client/src/textures/loader.rs b/client/src/textures/loader.rs new file mode 100644 index 0000000000000000000000000000000000000000..883cbda6c6a04bd2799adfa6526904154d85daba --- /dev/null +++ b/client/src/textures/loader.rs @@ -0,0 +1,71 @@ +use std::collections::HashMap; +use std::error::Error; +use std::io::Cursor; +use base64::Engine; +use image::ImageOutputFormat; +use log::debug; +use serde::{Deserialize, Serialize}; +use crate::textures::TextureManager; + +pub const SPRITESHEET_IMAGE_FILE: &[u8] = include_bytes!("../../../assets/dist/spritesheet.png"); +pub const SPRITESHEET_DATA_FILE: &str = include_str!("../../../assets/dist/spritesheet.ron"); + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct SpritePosition { + pub name: String, + pub x: f32, + pub y: f32, + pub width: f32, + pub height: f32, + pub offsets: Option<[f32; 2]>, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct SerializedSpriteSheet { + pub texture_width: f32, + pub texture_height: f32, + pub sprites: Vec, +} + +#[derive(Debug)] +pub struct TextureLoader { + pub sprites: HashMap +} +impl TextureManager for TextureLoader { + fn load() -> Result> where Self: Sized { + // load the generated spritesheet data + let spritesheet_data: SerializedSpriteSheet = ron::from_str(SPRITESHEET_DATA_FILE)?; + + // load the generated spritesheet image + let spritesheet_image = image::load_from_memory(SPRITESHEET_IMAGE_FILE)?; + + if spritesheet_image.width() as f32 != spritesheet_data.texture_width { + return Err("Image width mismatch between spritesheet and data file".into()); + } + if spritesheet_image.height() as f32 != spritesheet_data.texture_height { + return Err("Image height mismatch between spritesheet and data file".into()); + } + + let mut sprites = HashMap::new(); + + for sprite in spritesheet_data.sprites { + debug!("Loading texture {} ({}x{}, start at {}, {})", sprite.name, sprite.width, sprite.height, sprite.x, sprite.y); + let sprite_img = spritesheet_image.crop_imm(sprite.x as u32, sprite.y as u32, sprite.width as u32, sprite.height as u32); + let mut image_data: Vec = Vec::new(); + sprite_img.write_to(&mut Cursor::new(&mut image_data), ImageOutputFormat::Png) + .unwrap(); + let res_base64 = base64::engine::general_purpose::STANDARD.encode(image_data); + sprites.insert(sprite.name, res_base64); + } + + debug!("Loaded {} sprites from spritesheet", sprites.len()); + + Ok(Self { + sprites, + }) + } + + fn get_texture(&self, texture_id: &str) -> Option<&String> { + self.sprites.get(texture_id) + } +} \ No newline at end of file diff --git a/client/src/textures/mod.rs b/client/src/textures/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..c33441b79bf57f5bb750ab9a9ede16d509bbf30e --- /dev/null +++ b/client/src/textures/mod.rs @@ -0,0 +1,8 @@ +use std::error::Error; + +pub mod loader; + +pub trait TextureManager { + fn load() -> Result> where Self: Sized; + fn get_texture(&self, texture_id: &str) -> Option<&String>; +} \ No newline at end of file