//! 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, } 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 { 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 { 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 { 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 { 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::>(), [356]); assert_eq!(bar.cells(45).collect::>(), [356, 367, 378, 389]); assert_eq!( bar.cells(100).collect::>(), [356, 367, 378, 389, 400, 411, 422, 433] ); } }