aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/mission_loading.rs
blob: 41f75c87f7cc9673c51d92fabf666274a717cca8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! 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<Presentation>,
}

#[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<Self, String> {
        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<String, String> {
    strings
        .get(id)
        .ok_or_else(|| format!("missing iron3d.dll string {id:#06x}"))
}