aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/game_font.rs23
-rw-r--r--crates/fparkan-assets/src/lib.rs1
-rw-r--r--crates/fparkan-assets/src/mission_loader.rs174
-rw-r--r--crates/fparkan-font/src/bitmap.rs12
4 files changed, 201 insertions, 9 deletions
diff --git a/crates/fparkan-assets/src/game_font.rs b/crates/fparkan-assets/src/game_font.rs
index 3b88c47e..12625e90 100644
--- a/crates/fparkan-assets/src/game_font.rs
+++ b/crates/fparkan-assets/src/game_font.rs
@@ -9,19 +9,20 @@ use std::sync::Arc;
/// Load the `GAME_FONT` for the 800×600 pager profile witnessed in the original.
pub fn load_game_font(vfs: &dyn Vfs) -> Result<BitmapFont, String> {
- load_game_font_profile(vfs, 2, [800, 600])
+ load_profile(vfs, 2, [800, 600], &["GAME_FONT"]).map(|[font]| font)
}
/// Load the font for composing controls in the original 640×480 layout space.
pub fn load_layout_font(vfs: &dyn Vfs) -> Result<BitmapFont, String> {
- load_game_font_profile(vfs, 1, [640, 480])
+ load_profile(vfs, 1, [640, 480], &["GAME_FONT"]).map(|[font]| font)
}
-fn load_game_font_profile(
+pub(crate) fn load_profile<const N: usize>(
vfs: &dyn Vfs,
profile: u8,
dimensions: [u32; 2],
-) -> Result<BitmapFont, String> {
+ names: &[&str; N],
+) -> Result<[BitmapFont; N], String> {
let bytes = read(vfs, "ui/game_resources.cfg")?;
let cfg = String::from_utf8_lossy(&bytes);
let aliases =
@@ -33,14 +34,18 @@ fn load_game_font_profile(
{
return Err("unexpected dimensions for the game font profile".into());
}
- let key = field(&aliases, "GAME_FONT")?.to_ascii_uppercase();
let fonts = object(&cfg, "fonts").ok_or("missing font catalog")?;
- let id = EntryId(number(&fonts, &key)?);
let archive = decode(read(vfs, field(&fonts, "LIBRARY")?)?, ReadProfile::Strict)
.map_err(|error| error.to_string())?;
- let payload = archive.payload(id).map_err(|error| error.to_string())?;
- let font = decode_tfnt(Arc::from(payload)).map_err(|error| error.to_string())?;
- BitmapFont::prepare(&font)
+ let load = |name: &&str| {
+ let key = field(&aliases, name)?.to_ascii_uppercase();
+ let id = EntryId(number(&fonts, &key)?);
+ let payload = archive.payload(id).map_err(|error| error.to_string())?;
+ let font = decode_tfnt(Arc::from(payload)).map_err(|error| error.to_string())?;
+ BitmapFont::prepare(&font)
+ };
+ let fonts = names.iter().map(load).collect::<Result<Vec<_>, _>>()?;
+ fonts.try_into().map_err(|_| "invalid font count".into())
}
#[cfg(test)]
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index f2601b44..d55c9f83 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -36,6 +36,7 @@ mod control_drive;
mod control_weapon_sound;
mod explosion_profile;
pub mod game_font;
+pub mod mission_loader;
pub mod object_descriptions;
pub mod satellite_map;
mod turret_elevation;
diff --git a/crates/fparkan-assets/src/mission_loader.rs b/crates/fparkan-assets/src/mission_loader.rs
new file mode 100644
index 00000000..842c416d
--- /dev/null
+++ b/crates/fparkan-assets/src/mission_loader.rs
@@ -0,0 +1,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]
+ );
+ }
+}
diff --git a/crates/fparkan-font/src/bitmap.rs b/crates/fparkan-font/src/bitmap.rs
index 039e3b9f..6360b367 100644
--- a/crates/fparkan-font/src/bitmap.rs
+++ b/crates/fparkan-font/src/bitmap.rs
@@ -131,6 +131,18 @@ impl BitmapFont {
self.centered_line_bytes(&bytes, width, height)
}
+ /// Text extent in native atlas pixels, including character spacing.
+ pub fn line_metrics(&self, text: &str) -> Result<[u16; 2], String> {
+ let (bytes, _, unmappable) = WINDOWS_1251.encode(text);
+ if unmappable {
+ return Err("game text contains characters outside CP1251".into());
+ }
+ let width = bytes.iter().map(|&byte| self.advance(byte)).sum::<usize>();
+ let width = u16::try_from(width).map_err(|_| "game text is too wide")?;
+ let height = u16::try_from(self.glyph_height).map_err(|_| "game font is too tall")?;
+ Ok([width, height])
+ }
+
/// Composes a single line from a game resource's original CP1251 bytes.
pub fn centered_line_bytes(
&self,