//! winit application loop for the M4c vertical slice. mod application; mod camera; mod config; mod devlog; mod error; mod gpu; mod metrics; mod scene; mod sim; pub use error::AppError; use devlog::DevLog; use devlog::startup::StartupInfo; use gpu::WindowRenderer; use scene::View; use winit::window::Window; use config::Config; use metrics::CsvLog; /// Runs the window loop until the user closes the window or presses Escape. pub fn run() -> Result<(), AppError> { application::run() } /// Parses the flags and environment, printing usage or the error. fn parse_config() -> Config { match Config::from_process() { Ok(config::Outcome::Run(config)) => config, Ok(config::Outcome::Help) => { print!("{}", config::USAGE); std::process::exit(0); } Err(error) => { eprintln!("fabrilis: {error}"); eprintln!("{}", config::USAGE); std::process::exit(2); } } } /// Opens the metrics CSV when a path was configured. fn open_csv(config: &Config) -> Result, AppError> { let Some(path) = &config.metrics_log else { return Ok(None); }; CsvLog::open(path, config.append) .map(Some) .map_err(|error| AppError::Log(format!("{}: {error}", path.display()))) } /// Writes the startup banner, reporting a write failure without stopping the app. fn log_startup( dev: Option<&mut DevLog>, config: &Config, view: &View, renderer: &WindowRenderer, window: &Window, ) { let Some(dev) = dev else { return; }; let gpu = renderer.info(); let size = window.inner_size(); let metrics_path = config .metrics_log .as_ref() .map(|path| path.display().to_string()); let log_path = config.log.as_ref().map(|path| path.display().to_string()); let info = StartupInfo { window: (size.width, size.height), scale: window.scale_factor(), terrain: view.terrain_config, boxes: sim::DROPS.len(), box_mass_kg: sim::BOX_MASS, box_size_m: sim::BOX_SIZE, eye: view.camera.eye(), target: view.camera.target(), adapter: &gpu.adapter, driver: &gpu.driver, surface: &gpu.surface, metrics_path: metrics_path.as_deref(), log_path: log_path.as_deref(), dev: config.dev, append: config.append, }; if let Err(error) = devlog::startup::write(dev, &info) { eprintln!("fabrilis: dev log write failed: {error}"); } } fn aspect(size: winit::dpi::PhysicalSize) -> f32 { if size.height == 0 { 1.0 } else { size.width as f32 / size.height as f32 } }