From 0250e887d480ec20137c4ed96564b0b7652eb031 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 8 Oct 2018 15:31:50 +0200 Subject: [PATCH] Added clap options to choose the renderer size --- Cargo.toml | 1 + src/app/game.rs | 49 +++++++++++++++++++++++++++++++++++--- src/engine/renderer/mod.rs | 4 ++-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e65a71f..4a08ca3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Thomas Forgione "] [dependencies] +clap = "2.32.0" sfml = { git = "https://gitea.tforgione.fr/free-rusty-maker/rust-sfml" } [build-dependencies] diff --git a/src/app/game.rs b/src/app/game.rs index 3879bb8..fa5363a 100644 --- a/src/app/game.rs +++ b/src/app/game.rs @@ -1,8 +1,15 @@ +#[macro_use] +extern crate clap; extern crate sfml; extern crate rusty; use std::time::Instant; +use clap::{ + Arg, + App, +}; + use sfml::window::{ Event, Key, @@ -13,9 +20,45 @@ use rusty::engine::character::Character; use rusty::engine::controls::Controls; use rusty::engine::renderer::Renderer; +fn parse_resolution(res: &str) -> Result<(u32, u32), String> { + let split = res.split('x').collect::>(); + + if split.len() != 2 { + return Err("Resolution should be WIDTHxHEIGHT".to_owned()); + } + + let width = match split[0].parse::() { + Ok(w) => w, + Err(_) => return Err("WIDTH must be an integer".to_owned()), + }; + + let height = match split[1].parse::() { + Ok(h) => h, + Err(_) => return Err("HEIGHT must be an integer".to_owned()), + }; + + Ok((width, height)) +} + fn main() { - let game_width = 800; - let game_height = 450; + + let matches = App::new("Rusty Maker") + .version(crate_version!()) + .arg(Arg::with_name("resolution") + .short("r") + .long("resolution") + .value_name("WIDTHxHEIGHT") + .takes_value(true) + .default_value("800x450") + .validator(|x| parse_resolution(&x).map(|_| ()))) + .arg(Arg::with_name("fullscreen") + .short("f") + .long("fullscreen") + .takes_value(false)) + .get_matches(); + + let resolution = parse_resolution(matches.value_of("resolution").unwrap()).unwrap(); + let fullscreen = matches.is_present("fullscreen"); let mut character = Character::with_controls(Controls::new()); character.set_position((10.0, 0.0)); @@ -23,7 +66,7 @@ fn main() { let mut scene = Scene::new(); scene.add(character); - let mut renderer = Renderer::new(game_width, game_height); + let mut renderer = Renderer::new(resolution.0, resolution.1, fullscreen); let mut after_loop = Instant::now(); let mut running = true; diff --git a/src/engine/renderer/mod.rs b/src/engine/renderer/mod.rs index eb383d7..6d9b861 100644 --- a/src/engine/renderer/mod.rs +++ b/src/engine/renderer/mod.rs @@ -50,12 +50,12 @@ pub struct Renderer { impl Renderer { /// Creates a new renderer. - pub fn new(width: u32, height: u32) -> Renderer { + pub fn new(width: u32, height: u32, fullscreen: bool) -> Renderer { let mut window = RenderWindow::new( (width, height), "Free Rusty Maker", - Style::CLOSE, + if fullscreen { Style::FULLSCREEN } else { Style::CLOSE }, &Default::default(), ); window.set_vertical_sync_enabled(true);