aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/mission_loader.rs
blob: 842c416da705d68f61cb781be80fbbd2094a6ece (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Composition of the original mission loading screen.

use crate::{game_font, RgbaImage};
use fparkan_vfs::Vfs;

/// Screen content assembled from the installed game's fonts and strings.
pub struct MissionLoader {
    extent: [u32; 2],
    title: RgbaImage,
    log_font: game_font::BitmapFont,
    lines: Vec<RgbaImage>,
}

impl MissionLoader {
    /// Picks a font profile that fits the drawable and renders the title.
    pub fn new(vfs: &dyn Vfs, extent: [u32; 2], title: &str) -> Result<Self, String> {
        let [width, height] = extent;
        let (profile, dimensions) = if width >= 1024 && height >= 768 {
            (3, [1024, 768])
        } else if width >= 800 && height >= 600 {
            (2, [800, 600])
        } else {
            (1, [640, 480])
        };
        let [title_font, log_font] =
            game_font::load_profile(vfs, profile, dimensions, &["MENU_FONT", "GAME_FONT"])?;
        Ok(Self {
            extent,
            title: line(&title_font, title)?,
            log_font,
            lines: Vec::new(),
        })
    }

    /// Adds a completed loading operation to the rolling log.
    pub fn append(&mut self, text: &str) -> Result<(), String> {
        self.lines.push(line(&self.log_font, text)?);
        Ok(())
    }

    /// Composes top-down RGB pixels (0x00RRGGBB) for the current native percentage.
    pub fn frame(&self, percent: u8) -> Vec<u32> {
        let [width, height] = self.extent;
        let mut pixels = vec![0; width as usize * height as usize];
        let scale = |pixels: u32, design: u32, drawable: u32| {
            (f64::from(pixels) * f64::from(design) / f64::from(drawable)).round_ties_even() as u32
        };
        let project = |value: u32, design: u32, drawable: u32| {
            (f64::from(value) * f64::from(drawable) / f64::from(design)).round_ties_even() as u32
        };
        let mut text = |image: &RgbaImage, x, y, color| {
            let origin = [project(x, 640, width), project(y, 480, height)];
            draw_line(&mut pixels, self.extent, image, origin, color);
        };
        text(
            &self.title,
            320_u32.saturating_sub(scale(self.title.width, 640, width) / 2),
            240_u32.saturating_sub(scale(self.title.height + 7, 480, height)),
            [100, 255, 100],
        );
        let lines = &self.lines[self.lines.len().saturating_sub(17)..];
        if let Some(first) = lines.first() {
            let step = scale(first.height + 2, 480, height);
            for (index, image) in lines.iter().enumerate() {
                let fade = index as u8 * 2;
                let y = 460_u32.saturating_sub((lines.len() - index) as u32 * step);
                text(image, 5, y, [100 - fade, 255 - fade, 100 - fade]);
            }
        }
        let bar = ProgressBar::new(self.extent, self.title.width);
        let [_, top, _, bottom] = bar.inner;
        for x in bar.cells(percent) {
            fill(
                &mut pixels,
                self.extent,
                [x, top, x + bar.step - 2, bottom],
                0x32_e1_32,
            );
        }
        let [left, top, right, bottom] = bar.outline();
        for rect in [
            [left, top, right, top + 1],
            [left, bottom, right, bottom + 1],
            [left, top, left + 1, bottom + 1],
            [right, top, right + 1, bottom + 1],
        ] {
            fill(&mut pixels, self.extent, rect, 0x32_ff_32);
        }
        pixels
    }
}

fn line(font: &game_font::BitmapFont, text: &str) -> Result<RgbaImage, String> {
    let [width, height] = font.line_metrics(text)?;
    font.centered_line(text, width, height)
}

fn draw_line(
    pixels: &mut [u32],
    [width, height]: [u32; 2],
    image: &RgbaImage,
    [x, y]: [u32; 2],
    color: [u8; 3],
) {
    for row in 0..image.height.min(height.saturating_sub(y)) {
        for col in 0..image.width.min(width.saturating_sub(x)) {
            let i = ((row * image.width + col) * 4) as usize;
            let [r, g, b] =
                std::array::from_fn(|c| u32::from(image.rgba8[i + c]) * u32::from(color[c]) / 255);
            pixels[((y + row) * width + x + col) as usize] = (r << 16) | (g << 8) | b;
        }
    }
}

struct ProgressBar {
    inner: [i32; 4],
    step: i32,
}

impl ProgressBar {
    fn new([width, height]: [u32; 2], title_width: u32) -> Self {
        let (width, height) = (width as i32, height as i32);
        let span = (title_width as i32 + 8) / 8 * 8 + 8;
        Self {
            inner: [
                (width - span) / 2,
                height / 2,
                (width + span) / 2,
                height / 2 + height / 30,
            ],
            step: span / 8,
        }
    }

    fn cells(&self, percent: u8) -> impl Iterator<Item = i32> {
        let [left, _, right, _] = self.inner;
        let end = (f64::from(right - left) * f64::from(0.01_f32) * f64::from(percent)
            + f64::from(left))
        .round_ties_even() as i32;
        (left..end).step_by(self.step as usize)
    }

    fn outline(&self) -> [i32; 4] {
        let [left, top, right, bottom] = self.inner;
        [left - 3, top - 3, right, bottom + 2]
    }
}

fn fill(pixels: &mut [u32], [width, height]: [u32; 2], rect: [i32; 4], color: u32) {
    let [left, top, right, bottom] = rect.map(|edge| edge.max(0) as u32);
    for y in top..bottom.min(height) {
        let base = y as usize * width as usize;
        pixels[base + left.min(width) as usize..base + right.min(width) as usize].fill(color);
    }
}

#[cfg(test)]
mod tests {
    use super::ProgressBar;

    #[test]
    fn native_progress_rounds_the_title_span_and_draws_eight_segments() {
        let bar = ProgressBar::new([800, 600], 73);
        assert_eq!(bar.inner, [356, 300, 444, 320]);
        assert_eq!(bar.outline(), [353, 297, 444, 322]);
        assert_eq!(bar.cells(0).count(), 0);
        assert_eq!(bar.cells(10).collect::<Vec<_>>(), [356]);
        assert_eq!(bar.cells(45).collect::<Vec<_>>(), [356, 367, 378, 389]);
        assert_eq!(
            bar.cells(100).collect::<Vec<_>>(),
            [356, 367, 378, 389, 400, 411, 422, 433]
        );
    }
}