diff options
Diffstat (limited to 'crates/fparkan-assets')
| -rw-r--r-- | crates/fparkan-assets/src/satellite_map.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/satellite_map.rs b/crates/fparkan-assets/src/satellite_map.rs index d1d11ce3..c9c1b086 100644 --- a/crates/fparkan-assets/src/satellite_map.rs +++ b/crates/fparkan-assets/src/satellite_map.rs @@ -16,6 +16,81 @@ pub const MAP_RECT: [u32; 4] = [374, 0, 640, 266]; /// Inset between the frame and the mission image (iron3d+0x74cf0). pub const MAP_INSET: u32 = 5; +/// Initial satellite background opacity in the GOG HUD. +pub const MAP_INITIAL_ALPHA: u8 = 128; + +/// Steps the background opacity as in iron3d+0x749a0 and +0x74a00. +pub fn step_map_alpha(alpha: u8, increase: bool) -> u8 { + let step = if increase { 12 } else { -12 }; + (i16::from(alpha) + step).clamp(30, 255) as u8 +} + +fn displayed_map_alpha(alpha: u8) -> u16 { + (u16::from(alpha) * 100 / 255 / 5 * 5).max(10) +} + +const ALPHA_LABELS: usize = 19; +const FONT_EXTENTS: [[u32; 2]; 3] = [[640, 480], [800, 600], [1024, 768]]; + +/// Precomposed percent strings from the original GAME_FONT profiles. +pub struct MapOpacityLabels { + pub atlas_size: [u32; 2], + frames: [[[u32; 3]; ALPHA_LABELS]; FONT_EXTENTS.len()], +} + +impl MapOpacityLabels { + pub fn load(vfs: &dyn Vfs) -> Result<(Self, RgbaImage), String> { + let mut labels = Vec::new(); + let mut frames = [[[0; 3]; ALPHA_LABELS]; FONT_EXTENTS.len()]; + let (mut width, mut height) = (0, 0); + for (profile, (extent, frames)) in FONT_EXTENTS.into_iter().zip(&mut frames).enumerate() { + let [font] = + crate::game_font::load_profile(vfs, (profile + 1) as u8, extent, &["GAME_FONT"])?; + for (index, frame) in frames.iter_mut().enumerate() { + let text = format!("{}%", 10 + index * 5); + let [w, h] = font.line_metrics(&text)?; + let label = font.centered_line(&text, w, h)?; + *frame = [height, label.width, label.height]; + width = width.max(label.width); + // A transparent row keeps linear sampling away from the next label. + height += label.height + 1; + labels.push(label); + } + } + let mut rgba8 = vec![0; (width * height * 4) as usize]; + for ([top, w, _], label) in frames.iter().flatten().copied().zip(labels) { + let rows = rgba8.chunks_exact_mut((width * 4) as usize); + for (dest, source) in rows + .skip(top as usize) + .zip(label.rgba8.chunks_exact((w * 4) as usize)) + { + dest[..source.len()].copy_from_slice(source); + } + } + Ok(( + Self { + atlas_size: [width, height], + frames, + }, + RgbaImage { + width, + height, + rgba8, + }, + )) + } + + /// Returns the atlas row and unscaled glyph extent for the active resolution. + pub fn frame(&self, extent: [u32; 2], alpha: u8) -> [u32; 3] { + let profile = FONT_EXTENTS + .iter() + .rposition(|&[width, height]| extent[0] >= width && extent[1] >= height) + .unwrap_or(0); + let index = usize::from((displayed_map_alpha(alpha) - 10) / 5); + self.frames[profile][index] + } +} + /// The mission map, compass and contact atlas from the installed game. pub struct SatelliteMapAssets { pub frame: RgbaImage, @@ -102,6 +177,25 @@ fn texture(bytes: &[u8]) -> Result<RgbaImage, String> { decode_mip_rgba8(&document, 0).map_err(|error| error.to_string()) } +#[cfg(test)] +mod opacity_tests { + use super::*; + + #[test] + fn satellite_opacity_uses_native_steps_and_rounded_label() { + assert_eq!(step_map_alpha(MAP_INITIAL_ALPHA, true), 140); + assert_eq!(step_map_alpha(MAP_INITIAL_ALPHA, false), 116); + assert_eq!(step_map_alpha(30, false), 30); + assert_eq!(step_map_alpha(32, false), 30); + assert_eq!(step_map_alpha(248, true), 255); + assert_eq!(step_map_alpha(255, true), 255); + assert_eq!( + [30, 116, 128, 140, 255].map(displayed_map_alpha), + [10, 45, 50, 50, 100] + ); + } +} + fn crop( image: &RgbaImage, [x, y]: [u32; 2], |
