From ea5db5846bc700f0da912225ddcb4be372359044 Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 7 Oct 2024 20:45:14 -0400 Subject: Finish basic windowing stuff --- src/main.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 22 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c8d4618..100cfea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use alligator_scripts::ScriptManager; use alligator_sprites::SpriteManager; use alligator_sys::{Renderer, RendererConfig, Window, WindowConfig, WindowEvent}; +use clap::Parser; use pollster::FutureExt; use serde::Deserialize; use sha3::{Digest, Sha3_256}; @@ -67,6 +68,19 @@ pub struct Config { vsync: bool, } +#[derive(Debug, Clone, Parser)] +#[command(version, about, long_about = None)] +pub struct CommandLineArguments { + /// The configuration, represented as a JSON object. If not specified, + /// this is retrieved from the game.json in the executable's directory. + #[arg(short, long)] + config: Option>, + + /// Send and receive console commands through the stdio + #[arg(short, long)] + debug: bool, +} + fn sprite_manager(config: &Config) -> SpriteManager { SpriteManager::with_capacity(config.sprite_manager_capacity as usize) } @@ -98,10 +112,11 @@ fn script_manager(config: &Config) -> ScriptManager { fn window(config: &Config) -> Window { let config = WindowConfig { title: config.window_title.clone(), - default_width: config.default_window_width.get(), - default_height: config.default_window_height.get(), - default_x: 100, - default_y: 100, + // TODO set window size properly + default_width: config.default_window_width.unwrap().get(), + default_height: config.default_window_height.unwrap().get(), + default_x: 200, + default_y: 200, borderless_fullscreen: config.default_window_mode == ConfigWindowMode::BorderlessFullscreen, visible: false, }; @@ -111,8 +126,9 @@ fn window(config: &Config) -> Window { fn renderer(config: &Config, window: &Window) -> Renderer { let config = RendererConfig { - width: config.default_window_width.get(), - height: config.default_window_height.get(), + // TODO set window size properly + width: config.default_window_width.unwrap().get(), + height: config.default_window_height.unwrap().get(), instance_capacity: config.sprite_manager_capacity, fullscreen: false, vsync: config.vsync, @@ -122,14 +138,30 @@ fn renderer(config: &Config, window: &Window) -> Renderer { } fn main() { - let console = Console::new(26009) - .block_on() - .expect("The console should not fail to start"); - profile::set_profiler(console.logger()); + let args = CommandLineArguments::parse(); + + let console = if args.debug { + let console = Console::new(26009) + .block_on() + .expect("The console should not fail to start"); + log::set_logger(Box::leak(Box::new(console.logger()))) + .expect("this should be the only logger"); + profile::set_profiler(console.logger()); + + Some(console) + } else { + None + }; - std::env::set_current_dir(std::env::current_exe().unwrap().parent().unwrap()).unwrap(); - let config = File::open("game.json").unwrap(); - let config: Config = serde_json::from_reader(config).unwrap(); + //std::env::set_current_dir(std::env::current_exe().unwrap().parent().unwrap()).unwrap(); + + let config = match args.config { + Some(config) => serde_json::from_str(&config).unwrap(), + None => { + let config = File::open("game.json").unwrap(); + serde_json::from_reader(config).unwrap() + } + }; let sprites = sprite_manager(&config); let scripts = script_manager(&config); @@ -140,15 +172,21 @@ fn main() { let mut renderer = renderer(&config, &window); window.set_visible(true); - window.run(move |window, event| match event { - Some(WindowEvent::RedrawRequest) => { - renderer.render_frame(); - profile::finish_frame(); - } - Some(WindowEvent::CloseRequest) => { - std::process::exit(0); + window.run(move |window, event| { + match event { + Some(WindowEvent::RedrawRequest) => { + renderer.render_frame(); + profile::finish_frame(); + } + Some(WindowEvent::CloseRequest) => { + std::process::exit(0); + } + Some(_) => (), + None => window.request_redraw(), + }; + + if let Some(c) = &console { + c.flush().expect("why would this error?"); } - Some(_) => (), - None => window.request_redraw(), }) } -- cgit v1.2.3