fabrilis/crates/app/src/runtime/mod.rs
loki5512344 1042269d7e
Finish M4: cart, log facade, idle diagnostics and terrain LOD
Add the core::log facade with five levels and a sink; migrate the app dev log and enable trace in --dev. Replace the dropped cubes with a four-wheel cart on revolute joints driven by velocity motors, add WASD controls, a golden trajectory and a slope test. Instrument idle frame spikes and throttle the window title. Add chunked terrain LOD with skirts (2368 vs 8192 triangles) while physics keeps the full mesh. Pin Rust 1.100.0; edition stays 2024.
2026-11-17 14:32:52 +01:00

97 lines
2.3 KiB
Rust

//! 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<Option<CsvLog>, 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<u32>) -> f32 {
if size.height == 0 {
1.0
} else {
size.width as f32 / size.height as f32
}
}