aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/weapon_hud.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 23:14:47 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 23:14:47 +0300
commitc3e2add9a189879b5e800344e07645af591a2cbd (patch)
treea2ac87f7cba43bc28f913a667524f01423a47c4a /apps/fparkan-game/src/weapon_hud.rs
parenta6cb9f56ab34bc435ccf7dad613a2c2ce8b417e6 (diff)
downloadfparkan-c3e2add9a189879b5e800344e07645af591a2cbd.tar.xz
fparkan-c3e2add9a189879b5e800344e07645af591a2cbd.zip
feat(ui): show live weapon readings in the hero cockpit
Diffstat (limited to 'apps/fparkan-game/src/weapon_hud.rs')
-rw-r--r--apps/fparkan-game/src/weapon_hud.rs259
1 files changed, 259 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/weapon_hud.rs b/apps/fparkan-game/src/weapon_hud.rs
new file mode 100644
index 00000000..68144b28
--- /dev/null
+++ b/apps/fparkan-game/src/weapon_hud.rs
@@ -0,0 +1,259 @@
+use super::{
+ append_environment_gradient_material, append_environment_range, environment_vertex,
+ 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_sprites,
+ RgbaImage,
+};
+use fparkan_platform_winit::text::StringResources;
+use fparkan_runtime::{PlayerWeapons, WeaponIndicator, WeaponLamp};
+use fparkan_world::ObjectHandle;
+use std::path::Path;
+
+const ROW_HEIGHT: u16 = 19;
+const AMMO_WIDTH: u16 = 24;
+const SLOT_WIDTH: u16 = 12;
+const NAME_WIDTH: u16 = 120;
+const ROW_COUNT: usize = 4;
+// The native renderer lays out cells from the right edge, with a one-pixel overlap.
+const WIDTH: u32 = 202;
+const HEIGHT: u32 = ROW_COUNT as u32 * ROW_HEIGHT as u32;
+const UV: [[f32; 2]; 4] = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]];
+
+pub(super) struct WeaponHud {
+ hero: ObjectHandle,
+ range: EnvironmentGpuRange,
+ labels: [RgbaImage; ROW_COUNT],
+ font: BitmapFont,
+ unlimited: String,
+ lamps: [RgbaImage; 5],
+ end: RgbaImage,
+ digits: RgbaImage,
+ separator: RgbaImage,
+ emitter: RgbaImage,
+ body: RgbaImage,
+ cap: RgbaImage,
+ previous: [Option<WeaponIndicator>; ROW_COUNT],
+}
+
+impl WeaponHud {
+ pub(super) fn load(
+ root: &Path,
+ hero: ObjectHandle,
+ preview: &mut StaticPreviewScene,
+ ) -> Result<Self, String> {
+ let vfs = DirectoryVfs::new(root);
+ let [red, yellow1, yellow2, green, black, end, digits, separator, emitter, body, cap] =
+ load_ui_sprites(
+ &vfs,
+ "ui/compaund.cfg",
+ [
+ "ccres_red_lamp",
+ "ccres_yellow1_lamp",
+ "ccres_yellow2_lamp",
+ "ccres_green_lamp",
+ "ccres_black_lamp",
+ "ccres_ending_text",
+ "ccres_body_text",
+ "ccres_separator_left_text",
+ "ccres_ray_emitter_off",
+ "ccres_ray_body",
+ "ccres_ray_ending",
+ ],
+ )?;
+ let lamps = [red, yellow1, yellow2, green, black];
+ if lamps
+ .iter()
+ .chain([&end, &digits, &separator, &emitter, &body, &cap])
+ .any(|sprite| sprite.height != u32::from(ROW_HEIGHT))
+ || lamps.iter().any(|lamp| lamp.width != 19)
+ || [end.width, separator.width, emitter.width, cap.width] != [5, 5, 10, 6]
+ {
+ return Err("inconsistent original weapon HUD sprite dimensions".into());
+ }
+ let font = load_layout_font(&vfs)?;
+ let strings = StringResources::open(&root.join("iron3d.dll"))?;
+ let mut labels = Vec::with_capacity(ROW_COUNT);
+ // The hero uses the ordered labels loaded by iron3d+0x74f40.
+ for id in 0x0bff..=0x0c02 {
+ let text = strings
+ .get(id)
+ .ok_or_else(|| format!("missing weapon label {id:#x}"))?;
+ labels.push(font.centered_line(&text, NAME_WIDTH, ROW_HEIGHT)?);
+ }
+ let unlimited = strings
+ .get(0x13e6)
+ .ok_or("missing unlimited-ammunition label")?;
+ 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 = blank_texture();
+ 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 {
+ hero,
+ range,
+ font,
+ unlimited,
+ end,
+ digits,
+ separator,
+ emitter,
+ body,
+ cap,
+ labels: labels
+ .try_into()
+ .map_err(|_| "unexpected weapon label count")?,
+ lamps,
+ previous: [None; ROW_COUNT],
+ })
+ }
+
+ 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],
+ current: Option<(ObjectHandle, &PlayerWeapons)>,
+ ) -> Result<(), String> {
+ let bank =
+ current.filter(|(owner, bank)| *owner == self.hero && !bank.weapons().is_empty());
+ renderer
+ .set_draw_range_index_count(self.range_index(), if bank.is_some() { 6 } else { 0 })
+ .map_err(|error| error.to_string())?;
+ let Some((_, bank)) = bank else { return Ok(()) };
+ let mut slots = bank.indicators();
+ let state = std::array::from_fn(|_| slots.next());
+ if state != self.previous {
+ let mut image = blank_texture();
+ for (index, indicator) in state.iter().enumerate() {
+ if let Some(indicator) = indicator {
+ self.draw_row(&mut image, index, *indicator)?;
+ }
+ }
+ renderer
+ .update_material_texture(self.range.material_index, 0, &image)
+ .map_err(|error| error.to_string())?;
+ self.previous = state;
+ }
+ let width = WIDTH as f32 * viewport[0] / 640.0;
+ let height = HEIGHT as f32 * viewport[1] / 480.0;
+ let start = self.range.vertex_start;
+ mesh.vertices[start..start + 4].copy_from_slice(&screen_tl_quad_vertices_pixels(
+ [viewport[0] - width * 0.5, height * 0.5],
+ width * 0.5,
+ height * 0.5,
+ viewport,
+ [1.0; 3],
+ 1.0,
+ UV,
+ ));
+ Ok(())
+ }
+
+ fn draw_row(
+ &self,
+ image: &mut VulkanStaticTexture,
+ index: usize,
+ state: WeaponIndicator,
+ ) -> Result<(), String> {
+ let y = index as u32 * u32::from(ROW_HEIGHT);
+ let lamp = &self.lamps[match state.lamp {
+ WeaponLamp::Unavailable => 0,
+ WeaponLamp::Reloading => 1,
+ WeaponLamp::Cycling => 2,
+ WeaponLamp::Ready => 3,
+ WeaponLamp::Unselected => 4,
+ }];
+ let mut right = WIDTH;
+ let mut cell = |sprite: &RgbaImage, width: u32| {
+ right -= width;
+ paint(image, sprite, right - 1, y, width + 1);
+ right
+ };
+ cell(&self.end, self.end.width);
+ let slot_x = cell(&self.digits, u32::from(SLOT_WIDTH));
+ cell(lamp, lamp.width);
+ let ammo_x = cell(&self.digits, u32::from(AMMO_WIDTH));
+ cell(&self.separator, self.separator.width);
+ cell(&self.emitter, self.emitter.width);
+ let name_x = cell(&self.body, u32::from(NAME_WIDTH));
+ cell(&self.cap, self.cap.width);
+ let tint = match state.charge_percent {
+ 0..=19 => [128, 0, 0, 128],
+ 20..=79 => [128, 128, 0, 128],
+ _ => [0, 128, 0, 128],
+ };
+ let charged = (f64::from(state.charge_percent) * f64::from(NAME_WIDTH) / 100.0)
+ .round_ties_even() as u32;
+ for row in y + 3..y + u32::from(ROW_HEIGHT) - 3 {
+ for x in name_x + u32::from(NAME_WIDTH) - charged..name_x + u32::from(NAME_WIDTH) - 2 {
+ paint_pixel(image, x, row, &tint);
+ }
+ }
+ let slot = self
+ .font
+ .centered_line(&(index + 1).to_string(), SLOT_WIDTH, ROW_HEIGHT)?;
+ let rounds = state
+ .rounds
+ .map_or_else(|| self.unlimited.clone(), |count| count.to_string());
+ let ammo = self.font.centered_line(&rounds, AMMO_WIDTH, ROW_HEIGHT)?;
+ for (x, glyphs) in [
+ (slot_x, &slot),
+ (ammo_x, &ammo),
+ (name_x, &self.labels[index]),
+ ] {
+ paint(image, glyphs, x, y, glyphs.width);
+ }
+ Ok(())
+ }
+}
+
+fn blank_texture() -> VulkanStaticTexture {
+ VulkanStaticTexture {
+ width: WIDTH,
+ height: HEIGHT,
+ rgba8: vec![0; (WIDTH * HEIGHT * 4) as usize],
+ mip_levels: Vec::new(),
+ }
+}
+
+fn paint(image: &mut VulkanStaticTexture, sprite: &RgbaImage, left: u32, top: u32, width: u32) {
+ for y in 0..sprite.height {
+ for x in 0..width {
+ let source_x = x * sprite.width / width;
+ let offset = ((y * sprite.width + source_x) * 4) as usize;
+ paint_pixel(image, left + x, top + y, &sprite.rgba8[offset..offset + 4]);
+ }
+ }
+}
+
+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;
+}