diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 23:31:29 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 23:31:29 +0300 |
| commit | b3d2b95b8dd5e4578e2889845809fe7c21c5886b (patch) | |
| tree | 97c106a587c350cb82f05fc20c477c3e89f6aba9 /apps/fparkan-game | |
| parent | c3e2add9a189879b5e800344e07645af591a2cbd (diff) | |
| download | fparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.tar.xz fparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.zip | |
feat(ui): show cockpit altitude above mission sea level
Diffstat (limited to 'apps/fparkan-game')
| -rw-r--r-- | apps/fparkan-game/src/altimeter_hud.rs | 182 | ||||
| -rw-r--r-- | apps/fparkan-game/src/main.rs | 1 | ||||
| -rw-r--r-- | apps/fparkan-game/src/mission_overlay.rs | 46 | ||||
| -rw-r--r-- | apps/fparkan-game/src/weapon_hud.rs | 14 |
4 files changed, 226 insertions, 17 deletions
diff --git a/apps/fparkan-game/src/altimeter_hud.rs b/apps/fparkan-game/src/altimeter_hud.rs new file mode 100644 index 00000000..48114bf1 --- /dev/null +++ b/apps/fparkan-game/src/altimeter_hud.rs @@ -0,0 +1,182 @@ +use super::{ + append_environment_gradient_material, append_environment_range, environment_vertex, + mission_overlay::source_over, quad_indices, screen_tl_quad_vertices_pixels, + world_pipeline_state, DirectoryVfs, EnvironmentGpuRange, LegacyBlendMode, LegacyDepthMode, + StaticPreviewScene, VulkanSmokeRenderer, VulkanStaticMesh, VulkanStaticTexture, +}; +use fparkan_assets::{ + game_font::{load_layout_font, BitmapFont}, + ui_sprite::load_ui_page, + RgbaImage, +}; +use fparkan_runtime::cockpit_altimeter::Altimeter; +use std::path::Path; + +const LEFT: u32 = 221; +const TOP: u32 = 347; +const WIDTH: u32 = 50; +const HEIGHT: u32 = 110; +const WHITE: [u8; 3] = [255; 3]; +const UV: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]; + +pub(super) struct AltimeterHud { + range: EnvironmentGpuRange, + backing: VulkanStaticTexture, + page: RgbaImage, + font: BitmapFont, + previous: Option<Altimeter>, +} + +impl AltimeterHud { + pub(super) fn load(root: &Path, preview: &mut StaticPreviewScene) -> Result<Self, String> { + let vfs = DirectoryVfs::new(root); + let page = load_ui_page(&vfs, "page6")?; + let icons = load_ui_page(&vfs, "ui_menu3")?; + if [page.width, page.height, icons.width, icons.height] != [256; 4] { + return Err("unexpected native altimeter atlas dimensions".into()); + } + let font = load_layout_font(&vfs)?; + let mut backing = VulkanStaticTexture { + width: WIDTH, + height: HEIGHT, + rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize], + mip_levels: Vec::new(), + }; + // The gauge bracket and number well are part of the mirrored radar frame. + blit( + &mut backing, + &page, + [228, 52, 21, 63], + [227, 379], + true, + WHITE, + ); + blit( + &mut backing, + &page, + [219, 115, 36, 14], + [221, 442], + true, + WHITE, + ); + // iron3d+0x3f6a0 and +0x3ff00 select and position the altitude icon. + blit( + &mut backing, + &icons, + [215, 0, 15, 28], + [230, 383], + false, + WHITE, + ); + + let material = append_environment_gradient_material(&mut preview.materials)?; + preview.materials[material].screen_tl = true; + preview.materials[material].sky_far_depth = false; + preview.materials[material].texture = backing.clone(); + let range = append_environment_range( + &mut preview.mesh, + material, + vec![environment_vertex([0.0; 3], [1.0; 3], [0.0; 2]); 4], + quad_indices(1)?, + world_pipeline_state(LegacyBlendMode::SourceAlpha, LegacyDepthMode::Disabled), + )?; + Ok(Self { + range, + backing, + page, + font, + previous: None, + }) + } + + pub(super) fn range_index(&self) -> usize { + self.range.range_index + } + + pub(super) fn draw( + &mut self, + renderer: &mut VulkanSmokeRenderer, + mesh: &mut VulkanStaticMesh, + viewport: [f32; 2], + state: Option<Altimeter>, + ) -> Result<(), String> { + renderer + .set_draw_range_index_count(self.range_index(), if state.is_some() { 6 } else { 0 }) + .map_err(|error| error.to_string())?; + let Some(state) = state else { return Ok(()) }; + if self.previous != Some(state) { + let mut sprite = self.backing.clone(); + let text = self + .font + .centered_line(&state.altitude.to_string(), 28, 11)?; + blit( + &mut sprite, + &text, + [0, 0, text.width, text.height], + [226, 445], + false, + WHITE, + ); + // iron3d+0x3fc18 crops the same rows from the texture and the screen; + // the lower end stays fixed and the top follows the indicated altitude. + blit( + &mut sprite, + &self.page, + [ + 121, + state.scale_top, + 31, + Altimeter::SCALE_HEIGHT - state.scale_top, + ], + [240, TOP + state.scale_top], + true, + [0xff, 0xb4, 0x50], + ); + renderer + .update_material_texture(self.range.material_index, 0, &sprite) + .map_err(|error| error.to_string())?; + self.previous = Some(state); + } + + let [sx, sy] = [viewport[0] / 640.0, viewport[1] / 480.0]; + let half_width = WIDTH as f32 * 0.5; + let half_height = HEIGHT as f32 * 0.5; + let center = [ + (LEFT as f32 + half_width) * sx, + (TOP as f32 + half_height) * sy, + ]; + let start = self.range.vertex_start; + mesh.vertices[start..start + 4].copy_from_slice(&screen_tl_quad_vertices_pixels( + center, + half_width * sx, + half_height * sy, + viewport, + [1.0; 3], + 1.0, + UV, + )); + Ok(()) + } +} + +fn blit( + target: &mut VulkanStaticTexture, + source: &RgbaImage, + [x, y, width, height]: [u32; 4], + [left, top]: [u32; 2], + flip_x: bool, + tint: [u8; 3], +) { + for row in 0..height { + for col in 0..width { + let sx = x + if flip_x { width - 1 - col } else { col }; + let src = (((y + row) * source.width + sx) * 4) as usize; + let mut pixel: [u8; 4] = source.rgba8[src..src + 4].try_into().expect("RGBA pixel"); + for (channel, tint) in pixel.iter_mut().zip(tint) { + *channel = ((u16::from(*channel) * u16::from(tint) + 127) / 255) as u8; + } + let dst = (((top - TOP + row) * WIDTH + left - LEFT + col) * 4) as usize; + source_over(&mut target.rgba8[dst..dst + 4], &pixel); + } + } +} diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index afb553e4..4e044abe 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -4,6 +4,7 @@ use fparkan_runtime::initial_component_node_times as static_preview_node_times; +mod altimeter_hud; mod camera_capture; mod chassis_animation; mod cockpit_geometry; diff --git a/apps/fparkan-game/src/mission_overlay.rs b/apps/fparkan-game/src/mission_overlay.rs index 6c87492c..a3b37d50 100644 --- a/apps/fparkan-game/src/mission_overlay.rs +++ b/apps/fparkan-game/src/mission_overlay.rs @@ -1,12 +1,31 @@ +use super::{ + altimeter_hud::AltimeterHud, satellite_map::SatelliteMap, StaticPreviewScene, StaticVulkanApp, +}; #[cfg(windows)] use super::{mission_hints::MissionHints, weapon_hud::WeaponHud}; -use super::{satellite_map::SatelliteMap, StaticPreviewScene, StaticVulkanApp}; -use fparkan_runtime::{Engine, StartingCockpit}; +use fparkan_runtime::{cockpit_altimeter::cockpit_altimeter, Engine, StartingCockpit}; use std::path::Path; +/// Source-over composition for original sprite texels and their alpha masks. +pub(super) fn source_over(pixel: &mut [u8], source: &[u8]) { + let alpha = u32::from(source[3]); + if alpha == 0 { + return; + } + let under = (u32::from(pixel[3]) * (255 - alpha) + 127) / 255; + let combined = alpha + under; + for index in 0..3 { + pixel[index] = + ((u32::from(source[index]) * alpha + u32::from(pixel[index]) * under + combined / 2) + / combined) as u8; + } + pixel[3] = combined as u8; +} + #[derive(Default)] pub(super) struct MissionOverlay { pub(super) satellite_map: Option<SatelliteMap>, + altimeter: Option<AltimeterHud>, #[cfg(windows)] pub(super) hints: Option<MissionHints>, #[cfg(windows)] @@ -24,6 +43,15 @@ impl MissionOverlay { let Some(engine) = engine else { return Self::default(); }; + // The original draws the cockpit instruments before the satellite map. + let altimeter = cockpit.and_then(|_| { + AltimeterHud::load(root, preview) + .map(Some) + .unwrap_or_else(|error| { + eprintln!("altimeter_unavailable reason={error}"); + None + }) + }); #[cfg(windows)] let hints = MissionHints::load(root, mission, preview) .map(Some) @@ -45,10 +73,9 @@ impl MissionOverlay { None }) }); - #[cfg(not(windows))] - let _ = cockpit; Self { satellite_map, + altimeter, #[cfg(windows)] hints, #[cfg(windows)] @@ -72,6 +99,9 @@ impl MissionOverlay { remove(range, ranges, counts); } } + if let Some(altimeter) = &self.altimeter { + remove(altimeter.range_index(), ranges, counts); + } #[cfg(windows)] if let Some(hints) = &self.hints { remove(hints.range_index(), ranges, counts); @@ -96,6 +126,14 @@ impl StaticVulkanApp { .map(|(engine, cockpit)| (engine, cockpit, self.boarding_target)); map.draw(renderer, &mut self.mesh, frame.viewport, player)?; } + if let Some(altimeter) = &mut self.overlay.altimeter { + let state = self + .mission_engine + .as_ref() + .zip(self.cockpit) + .and_then(|(engine, cockpit)| cockpit_altimeter(engine, cockpit)); + altimeter.draw(renderer, &mut self.mesh, frame.viewport, state)?; + } #[cfg(windows)] if let Some(hints) = &mut self.overlay.hints { let outcome = self diff --git a/apps/fparkan-game/src/weapon_hud.rs b/apps/fparkan-game/src/weapon_hud.rs index 68144b28..93db51db 100644 --- a/apps/fparkan-game/src/weapon_hud.rs +++ b/apps/fparkan-game/src/weapon_hud.rs @@ -242,18 +242,6 @@ fn paint(image: &mut VulkanStaticTexture, sprite: &RgbaImage, left: u32, top: u3 } fn paint_pixel(image: &mut VulkanStaticTexture, x: u32, y: u32, source: &[u8]) { - let alpha = u32::from(source[3]); - if alpha == 0 { - return; - } let start = ((y * WIDTH + x) * 4) as usize; - let pixel = &mut image.rgba8[start..start + 4]; - let under = (u32::from(pixel[3]) * (255 - alpha) + 127) / 255; - let combined = alpha + under; - for index in 0..3 { - pixel[index] = - ((u32::from(source[index]) * alpha + u32::from(pixel[index]) * under + combined / 2) - / combined) as u8; - } - pixel[3] = combined as u8; + super::mission_overlay::source_over(&mut image.rgba8[start..start + 4], source); } |
