Added clap options to choose the renderer size

This commit is contained in:
Thomas Forgione 2018-10-08 15:31:50 +02:00
parent 45d301361c
commit 0250e887d4
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 49 additions and 5 deletions

View File

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Thomas Forgione <thomas@forgione.fr>"]
[dependencies]
clap = "2.32.0"
sfml = { git = "https://gitea.tforgione.fr/free-rusty-maker/rust-sfml" }
[build-dependencies]

View File

@ -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::<Vec<_>>();
if split.len() != 2 {
return Err("Resolution should be WIDTHxHEIGHT".to_owned());
}
let width = match split[0].parse::<u32>() {
Ok(w) => w,
Err(_) => return Err("WIDTH must be an integer".to_owned()),
};
let height = match split[1].parse::<u32>() {
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;

View File

@ -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);