//! Lifecycle of the loader for a windowed mission launch. use std::path::Path; #[cfg(windows)] use { fparkan_assets::mission_loader::MissionLoader, fparkan_platform_winit::{ startup_window::StartupWindow, text::StringResources, WinitWindowPlan, }, fparkan_vfs::DirectoryVfs, }; #[derive(Clone, Copy)] pub(super) enum Stage { World, Atmosphere, Objects, Camera, Ready, } #[derive(Default)] pub(super) struct MissionLoading { #[cfg(windows)] presentation: Option, } #[cfg(windows)] struct Presentation { window: StartupWindow, screen: MissionLoader, strings: StringResources, } impl MissionLoading { pub(super) fn start(_root: &Path) -> Self { #[cfg(windows)] { let presentation = Presentation::open(_root) .map_err(|error| eprintln!("startup window: {error}")) .ok(); Self { presentation } } #[cfg(not(windows))] Self::default() } pub(super) fn stage(&mut self, _stage: Stage) { #[cfg(windows)] if let Some(presentation) = &mut self.presentation { if let Err(error) = presentation.update(_stage) { eprintln!("startup window: {error}"); } } } pub(super) fn window_visible(&self) -> bool { #[cfg(windows)] { self.presentation.is_some() } #[cfg(not(windows))] false } pub(super) fn finish(&mut self) { *self = Self::default(); } } #[cfg(windows)] impl Presentation { fn open(root: &Path) -> Result { let strings = StringResources::open(&root.join("iron3d.dll"))?; let plan = WinitWindowPlan::smoke(); let extent = [plan.width, plan.height]; let mut screen = MissionLoader::new( &DirectoryVfs::new(root), extent, &original_text(&strings, 0x13e3)?, )?; screen.append(&original_text(&strings, 0x1194)?)?; let window = StartupWindow::open(extent, screen.frame(10)).map_err(|error| error.to_string())?; Ok(Self { window, screen, strings, }) } fn update(&mut self, stage: Stage) -> Result<(), String> { // The Rust loader prepares several original stages in one operation. // Publish their messages together when that operation has completed. let (ids, percent): (&[u32], u8) = match stage { Stage::World => (&[0x1198, 0x1199, 0x119a], 45), Stage::Atmosphere => (&[0x119b], 48), Stage::Objects => (&[0x119c, 0x119d, 0x119e, 0x119f, 0x11a0, 0x11a2], 90), Stage::Camera => (&[0x11a3], 95), Stage::Ready => (&[0x11a4], 100), }; for &id in ids { self.screen.append(&original_text(&self.strings, id)?)?; } self.window.set_frame(self.screen.frame(percent)); Ok(()) } } #[cfg(windows)] fn original_text(strings: &StringResources, id: u32) -> Result { strings .get(id) .ok_or_else(|| format!("missing iron3d.dll string {id:#06x}")) }