From 729e6be59dc5ab6b5b3f99b36b107902d60966a0 Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 25 Apr 2024 03:23:30 +0200 Subject: [PATCH] add command line options for help, and for setting window mode --- src/main.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 611cc4a..f7e2975 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,11 +32,32 @@ use bevy::prelude::*; use bevy::pbr::ExtendedMaterial; use std::env; +const HELP: &str = "./outfly [options] + +Options: + --version, -v show this program's version number and exit + --help, -h show this help message and exit + --gl use GL rendering instead of Vulkan + --fs-legacy use 'legacy' (non-borderless) fullscreen mode + --fs-sized use 'sized' (non-borderless) fullscreen mode + --windowed start in non-fullscreen mode + +Note: borderless fullscreen is the default, but it crashes on some systems."; + fn main() { + let mut opt = CommandLineOptions { + window_mode_fullscreen: WindowMode::BorderlessFullscreen, + window_mode_initial: WindowMode::BorderlessFullscreen, + }; + let args: Vec = env::args().collect(); if args.len() > 1 { for arg in &args[1..] { - if arg == "--version" || arg == "-v" { + if arg == "--help" || arg == "-h" { + println!("{}", HELP); + return; + } + else if arg == "--version" || arg == "-v" { let version = option_env!("CARGO_PKG_VERSION").unwrap(); let name = option_env!("CARGO_PKG_NAME").unwrap(); let homepage = option_env!("CARGO_PKG_HOMEPAGE").unwrap(); @@ -48,13 +69,38 @@ fn main() { else if arg == "--gl" { env::set_var("WGPU_BACKEND", "gl"); } + else if arg == "--windowed" { + opt.window_mode_initial = WindowMode::Windowed; + } + else if arg == "--fs-legacy" { + let mode = WindowMode::Fullscreen; + if opt.window_mode_initial == opt.window_mode_fullscreen { + opt.window_mode_initial = mode; + } + opt.window_mode_fullscreen = mode; + } + else if arg == "--fs-sized" { + let mode = WindowMode::SizedFullscreen; + if opt.window_mode_initial == opt.window_mode_fullscreen { + opt.window_mode_initial = mode; + } + opt.window_mode_fullscreen = mode; + } + else { + println!("{}", HELP); + println!("\nERROR: no such option: `{}`", arg); + return; + } } } let mut app = App::new(); + app.insert_resource(opt); + #[cfg(feature = "embed_assets")] app.add_plugins(bevy_embedded_assets::EmbeddedAssetPlugin { mode: bevy_embedded_assets::PluginMode::ReplaceDefault }); + app.add_plugins(OutFlyPlugin).run(); } @@ -84,13 +130,20 @@ impl Plugin for OutFlyPlugin { } } +#[derive(Resource, Default)] +pub struct CommandLineOptions { + window_mode_fullscreen: WindowMode, + window_mode_initial: WindowMode, +} + fn setup( mut windows: Query<&mut Window, With>, + opt: Res, ) { for mut window in &mut windows { window.cursor.grab_mode = CursorGrabMode::Locked; window.cursor.visible = false; - window.mode = WindowMode::BorderlessFullscreen; + window.mode = opt.window_mode_initial; window.title = "OutFly".to_string(); } } @@ -98,6 +151,7 @@ fn setup( fn handle_input( keyboard_input: Res>, settings: Res, + opt: Res, mut app_exit_events: ResMut>, mut windows: Query<&mut Window, With>, ) { @@ -107,7 +161,7 @@ fn handle_input( if keyboard_input.just_pressed(settings.key_fullscreen) { for mut window in &mut windows { window.mode = if window.mode == WindowMode::Windowed { - WindowMode::BorderlessFullscreen + opt.window_mode_fullscreen } else { WindowMode::Windowed }