diff --git a/Cargo.lock b/Cargo.lock index bbfdff7..2f411fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,54 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +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.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.75" @@ -52,10 +100,57 @@ name = "chip8" version = "0.1.0" dependencies = [ "anyhow", + "clap", "minifb", "rand", ] +[[package]] +name = "clap" +version = "4.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "cty" version = "0.2.2" @@ -204,6 +299,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "instant" version = "0.1.12" @@ -513,6 +614,12 @@ version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "2.0.37" @@ -543,6 +650,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version-compare" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index b0cb19c..5244ef7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] anyhow = "1.0.75" +clap = { version = "4.4.6", features = ["derive"] } minifb = "0.25.0" rand = "0.8.5" diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..58f9e7c --- /dev/null +++ b/src/args.rs @@ -0,0 +1,17 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Args { + /// Path to a file containing a CHIP-8 program. + pub program: std::path::PathBuf, + + #[arg(short = 'W', long, default_value_t = 640)] + pub window_width: usize, + + #[arg(short = 'H', long, default_value_t = 320)] + pub window_height: usize, + + #[arg(short = 's', long, default_value_t = 700)] + pub cpu_speed: u64, +} diff --git a/src/main.rs b/src/main.rs index 8ece320..5abe10d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,27 @@ use anyhow::Result; +use clap::Parser; use minifb::{ScaleMode, Window, WindowOptions}; use std::io::Read; use std::{fs::File, path::Path}; +use args::Args; use state::State; +pub mod args; pub mod operation; pub mod state; pub mod window; fn main() -> Result<()> { + let args = Args::parse(); + let window = Window::new( - "chip8", - 640, - 320, + match args.program.file_name() { + Some(s) => s.to_str().unwrap_or("CHIP-8"), + None => "CHIP-8", + }, + args.window_width, + args.window_height, WindowOptions { resize: true, scale_mode: ScaleMode::AspectRatioStretch, @@ -22,9 +30,9 @@ fn main() -> Result<()> { ) .unwrap_or_else(|err| panic!("Failed to create window: {}", err)); - let mut state = State::new(window, 700); + let mut state = State::new(window, args.cpu_speed); - let program = read_program_from_file(Path::new("./roms/games/Tic-Tac-Toe [David Winter].ch8"))?; + let program = read_program_from_file(args.program.as_path())?; state.load_program(&program); while state.display_open() {