Skip to content

Commit

Permalink
feat: implemented CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoof committed Oct 6, 2023
1 parent 3fad9c9 commit 1c79cdd
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 5 deletions.
113 changes: 113 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 17 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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,
}
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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() {
Expand Down

0 comments on commit 1c79cdd

Please sign in to comment.