From fd4839057f3924fa4adccb3d95cdecf6827b0e8f Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 23 Jan 2024 16:56:56 -0800 Subject: [PATCH] Added basic cli --- Cargo.lock | 182 ++++++++++++++++++ Cargo.toml | 1 + crates/galactica/Cargo.toml | 4 + crates/galactica/src/cli/mod.rs | 1 + .../main.rs => galactica/src/cli/packer.rs} | 15 +- crates/galactica/src/main.rs | 55 +++++- crates/packer/Cargo.toml | 4 - crates/packer/src/atlasset.rs | 6 +- crates/packer/src/lib.rs | 3 + 9 files changed, 246 insertions(+), 25 deletions(-) create mode 100644 crates/galactica/src/cli/mod.rs rename crates/{packer/src/main.rs => galactica/src/cli/packer.rs} (90%) diff --git a/Cargo.lock b/Cargo.lock index ed0c50a..e278dd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,6 +84,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.76" @@ -267,6 +315,46 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.42", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + [[package]] name = "codespan-reporting" version = "0.11.1" @@ -283,6 +371,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "com-rs" version = "0.2.1" @@ -622,14 +716,18 @@ name = "galactica" version = "0.0.0" dependencies = [ "anyhow", + "clap", "galactica-content", + "galactica-packer", "galactica-playeragent", "galactica-render", "galactica-system", "galactica-util", + "image", "nalgebra", "pollster", "rand", + "walkdir", "wgpu", "winit", ] @@ -875,6 +973,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hexf-parse" version = "0.2.1" @@ -1903,6 +2007,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "svg_fmt" version = "0.4.1" @@ -2153,6 +2263,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "vec_map" version = "0.8.2" @@ -2522,6 +2638,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-targets" version = "0.42.2" @@ -2552,6 +2677,21 @@ dependencies = [ "windows_x86_64_msvc 0.48.5", ] +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -2564,6 +2704,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" @@ -2576,6 +2722,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.42.2" @@ -2588,6 +2740,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.42.2" @@ -2600,6 +2758,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" @@ -2612,6 +2776,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" @@ -2624,6 +2794,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" @@ -2636,6 +2812,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + [[package]] name = "winit" version = "0.28.7" diff --git a/Cargo.toml b/Cargo.toml index 57b3816..6841f3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,3 +71,4 @@ walkdir = "2.4.0" toml = "0.8.8" glyphon = "0.4.1" lazy_static = "1.4.0" +clap = { version = "4.4.18", features = ["derive"] } diff --git a/crates/galactica/Cargo.toml b/crates/galactica/Cargo.toml index dddaaa3..d61ffe2 100644 --- a/crates/galactica/Cargo.toml +++ b/crates/galactica/Cargo.toml @@ -26,6 +26,7 @@ galactica-render = { workspace = true } galactica-util = { workspace = true } galactica-system = { workspace = true } galactica-playeragent = { workspace = true } +galactica-packer = { workspace = true } rand = { workspace = true } winit = { workspace = true } @@ -33,3 +34,6 @@ wgpu = { workspace = true } pollster = { workspace = true } anyhow = { workspace = true } nalgebra = { workspace = true } +clap = { workspace = true } +walkdir = { workspace = true } +image = { workspace = true } diff --git a/crates/galactica/src/cli/mod.rs b/crates/galactica/src/cli/mod.rs new file mode 100644 index 0000000..2ae55b9 --- /dev/null +++ b/crates/galactica/src/cli/mod.rs @@ -0,0 +1 @@ +pub mod packer; diff --git a/crates/packer/src/main.rs b/crates/galactica/src/cli/packer.rs similarity index 90% rename from crates/packer/src/main.rs rename to crates/galactica/src/cli/packer.rs index 5a3d8c3..6226565 100644 --- a/crates/packer/src/main.rs +++ b/crates/galactica/src/cli/packer.rs @@ -1,10 +1,7 @@ -mod atlasset; - -use atlasset::AtlasSet; - use anyhow::{bail, Context, Result}; +use galactica_packer::AtlasSet; use image::io::Reader; -use std::path::{Path, PathBuf}; +use std::path::Path; use walkdir::WalkDir; // TODO: warning when images have extra transparency @@ -13,11 +10,9 @@ use walkdir::WalkDir; // TODO: randomly assign sprites to textures, for efficiency // TODO: group images by use case (dynamic loading algorithm) -fn main() -> Result<()> { +pub fn run(asset_root: &Path, cache_target: &Path) -> Result<()> { let mut files = Vec::new(); - let asset_root = Path::new("./assets/render"); - // Total number of pixels we want to add let mut total_dim = 0f64; @@ -88,8 +83,8 @@ fn main() -> Result<()> { println!("Saving files..."); atlas_set.save_files( - |x| PathBuf::from(format!("cache/atlas-{x:0.2}.bmp")), - &PathBuf::from("cache/spriteatlas.toml"), + |x| cache_target.join(format!("atlas-{x:0.2}.bmp")), + &cache_target.join("spriteatlas.toml"), )?; return Ok(()); diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index 31f25e6..9d9d29f 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -1,6 +1,7 @@ mod game; use anyhow::{bail, Result}; +use clap::Parser; use galactica_content::{Content, SystemHandle}; use galactica_playeragent::{PlayerAgent, PlayerStatus}; use galactica_render::RenderInput; @@ -20,17 +21,51 @@ use winit::{ window::WindowBuilder, }; -fn main() { - if let Err(err) = try_main() { - eprintln!("Galactica failed with an error"); +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Run the asset packer + #[arg(long, action, group = "run")] + pack: bool, - let mut i = 0; - for e in err.chain().rev() { - eprintln!("{i:02}: {}", e); - i += 1; + /// Check content in the default directory + #[arg(long, action, group = "run")] + check_content: bool, +} + +mod cli; + +fn main() -> Result<()> { + let args = Args::parse(); + + let cache_dir = Path::new(ASSET_CACHE); + fs::create_dir_all(cache_dir)?; + + if args.pack { + cli::packer::run(Path::new("./assets/render"), cache_dir)?; + } else if args.check_content { + let atlas_index = cache_dir.join("spriteatlas.toml"); + let _ = Content::load_dir( + PathBuf::from("./content"), + PathBuf::from("./assets"), + atlas_index.clone(), + )?; + println!("Content was loaded without errors.") + } else { + // Run game + if let Err(err) = try_main() { + eprintln!("Galactica failed with an error"); + + let mut i = 0; + for e in err.chain().rev() { + eprintln!("{i:02}: {}", e); + i += 1; + } + std::process::exit(1); } - std::process::exit(1); } + + return Ok(()); } fn try_main() -> Result<()> { @@ -43,11 +78,11 @@ fn try_main() -> Result<()> { bail!("Could not find sprite atlas!"); } - // TODO: pretty error if missing + // TODO: pretty error if missing (also in cli) let content = Content::load_dir( PathBuf::from("./content"), PathBuf::from("./assets"), - atlas_index.clone(), + atlas_index, )?; let event_loop = EventLoop::new(); diff --git a/crates/packer/Cargo.toml b/crates/packer/Cargo.toml index 84826a6..9e90500 100644 --- a/crates/packer/Cargo.toml +++ b/crates/packer/Cargo.toml @@ -1,7 +1,3 @@ -[[bin]] -name = "galactica-packer" -path = "src/main.rs" - [package] name = "galactica-packer" description = "Galactica's sprite packer" diff --git a/crates/packer/src/atlasset.rs b/crates/packer/src/atlasset.rs index d1989b0..9108cc6 100644 --- a/crates/packer/src/atlasset.rs +++ b/crates/packer/src/atlasset.rs @@ -1,5 +1,5 @@ +use crate::{SpriteAtlas, SpriteAtlasImage}; use anyhow::{bail, Context, Result}; -use galactica_packer::{SpriteAtlas, SpriteAtlasImage}; use image::{imageops, GenericImageView, ImageBuffer, Rgba, RgbaImage}; use std::{ fs::File, @@ -16,6 +16,7 @@ use std::{ // image: a single file // sprite: a possibly animated texture +/// Builds a set of atlas textures from input image files pub struct AtlasSet { /// The width of each atlas texture_width: u32, @@ -56,6 +57,7 @@ pub struct AtlasSet { } impl AtlasSet { + /// Make a new AtlasSet pub fn new( texture_width: u32, texture_height: u32, @@ -223,6 +225,7 @@ impl AtlasSet { return Ok(atlas_idx); } + /// Save the index and all atlas files pub fn save_files(mut self, atlas_path: F, index_path: &Path) -> Result<()> where F: Fn(usize) -> PathBuf, @@ -265,6 +268,7 @@ impl AtlasSet { } */ + /// Get the current packing efficiency of this set, over all atlases. pub fn get_efficiency(&self) -> f64 { self.used_area / (self.texture_height as f64 diff --git a/crates/packer/src/lib.rs b/crates/packer/src/lib.rs index 25f6477..37e46cb 100644 --- a/crates/packer/src/lib.rs +++ b/crates/packer/src/lib.rs @@ -2,6 +2,9 @@ //! This crate creates texture atlases from an asset tree. +mod atlasset; +pub use atlasset::*; + use std::{ collections::HashMap, num::NonZeroU32,