//! 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::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 through the log facade. fn log_startup( config: &Config, view: &View, renderer: &WindowRenderer, window: &Window, simulation: &sim::Simulation, ) { 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 cart = simulation.cart_info(); let info = StartupInfo { window: (size.width, size.height), scale: window.scale_factor(), terrain: view.terrain_config, cart_mass_kg: cart.mass_kg, wheels: cart.wheels, wheel_radius_m: cart.wheel_radius_m, spawn: cart.spawn, 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, }; devlog::startup::write(&info); } fn aspect(size: winit::dpi::PhysicalSize) -> f32 { if size.height == 0 { 1.0 } else { size.width as f32 / size.height as f32 } }