diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 00:04:24 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 00:04:24 +0300 |
| commit | 752d34af83acf9a818e22c621fb034913079acff (patch) | |
| tree | 65b0f47859be8f8a33b8e37fecad81cfd660739c /crates/fparkan-render | |
| parent | 029297446ec0b235602b4f4d9197786a3466a6fe (diff) | |
| download | fparkan-752d34af83acf9a818e22c621fb034913079acff.tar.xz fparkan-752d34af83acf9a818e22c621fb034913079acff.zip | |
feat(ui): plot local detections on the cockpit radar
Draw each current detection from the occupied unit sensor in camera-relative coordinates, with the original range scale, clan color and screen-pixel marks. Check stock Two Fortresses before and after a free-look turn and exercise the native rectangle and line callbacks.
Diffstat (limited to 'crates/fparkan-render')
| -rw-r--r-- | crates/fparkan-render/src/cockpit_course.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-render/src/cockpit_radar.rs | 120 | ||||
| -rw-r--r-- | crates/fparkan-render/src/lib.rs | 1 |
3 files changed, 122 insertions, 1 deletions
diff --git a/crates/fparkan-render/src/cockpit_course.rs b/crates/fparkan-render/src/cockpit_course.rs index 974249b0..713c9ca6 100644 --- a/crates/fparkan-render/src/cockpit_course.rs +++ b/crates/fparkan-render/src/cockpit_course.rs @@ -2,7 +2,7 @@ use std::f32::consts::{FRAC_PI_2, PI}; -const CENTER: [f32; 2] = [320.0, 396.0]; +pub(crate) const CENTER: [f32; 2] = [320.0, 396.0]; const HALF_MARKER_ANGLE: f32 = 0.08726647; /// Front and rear marker corners in the original triangle-strip order. diff --git a/crates/fparkan-render/src/cockpit_radar.rs b/crates/fparkan-render/src/cockpit_radar.rs new file mode 100644 index 00000000..f14fe3c6 --- /dev/null +++ b/crates/fparkan-render/src/cockpit_radar.rs @@ -0,0 +1,120 @@ +//! Native cockpit radar projection and screen-pixel marks. + +use crate::cockpit_course::CENTER; +use std::f64::consts::FRAC_PI_2; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ContactMark { + Building, + Unit, + Cross, +} + +/// Project a horizontal world offset into the cockpit radar's screen pixels. +#[must_use] +pub fn contact_pixel( + [x, y]: [f32; 2], + [forward_x, forward_y]: [f32; 2], + range: f32, + viewport: [f32; 2], +) -> [f32; 2] { + let [x, y] = [x, y].map(f64::from); + let bearing = y.atan2(x) - f64::from(forward_y).atan2(f64::from(forward_x)) + FRAC_PI_2; + let (sin, cos) = bearing.sin_cos(); + let radius = f64::from(x.hypot(y) as f32) * f64::from(range.recip()) * 60.0; + let [sx, sy] = [viewport[0] / 640.0, viewport[1] / 480.0].map(f64::from); + [ + ((f64::from(CENTER[0]) + cos * radius) * sx) as f32, + ((f64::from(CENTER[1]) - sin * radius) * sy) as f32, + ] +} + +/// Emit the filled rectangles that make up a contact and its optional outline. +pub fn mark_rectangles( + mark: ContactMark, + [x, y]: [f32; 2], + [width, height]: [f32; 2], + selected: bool, + mut paint: impl FnMut([i32; 4]), +) { + let design_size = match mark { + ContactMark::Building => 5.0, + ContactMark::Unit => 3.0, + ContactMark::Cross => 4.0, + }; + let round = |value: f32| value.round_ties_even() as i32; + let w = round(width / 640.0 * design_size); + let h = round(height / 480.0 * design_size); + let [left, top, right, bottom] = if mark == ContactMark::Cross { + let [cx, cy] = [round(x), round(y)]; + let [dx, dy] = [w / 2, h / 2]; + let bounds = [ + cx.saturating_sub(dx), + cy.saturating_sub(dy), + cx.saturating_add(dx), + cy.saturating_add(dy), + ]; + paint([bounds[0], cy, bounds[2], cy.saturating_add(1)]); + paint([cx, bounds[1], cx.saturating_add(1), bounds[3]]); + bounds + } else { + let left = round(x - (w / 2) as f32); + let top = round(y - (h / 2) as f32); + let bounds = [left, top, left.saturating_add(w), top.saturating_add(h)]; + paint(bounds); + bounds + }; + if selected { + let [l, t, r, b] = [ + left.saturating_sub(2), + top.saturating_sub(2), + right.saturating_add(if mark == ContactMark::Cross { 2 } else { 1 }), + bottom.saturating_add(if mark == ContactMark::Cross { 2 } else { 1 }), + ]; + paint([l, t, r, t.saturating_add(1)]); + paint([l, b.saturating_sub(1), r, b]); + paint([l, t, l.saturating_add(1), b]); + paint([r.saturating_sub(1), t, r, b]); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cockpit_radar_rotates_with_view_and_maps_range_to_sixty_design_pixels() { + let viewport = [800.0, 600.0]; + assert_eq!( + contact_pixel([300.0, 0.0], [1.0, 0.0], 300.0, viewport), + [400.0, 420.0] + ); + assert_eq!( + contact_pixel([0.0, -300.0], [1.0, 0.0], 300.0, viewport), + [475.0, 495.0] + ); + assert_eq!( + contact_pixel([300.0, 0.0], [0.0, 1.0], 300.0, viewport), + [475.0, 495.0] + ); + } + + #[test] + fn projected_contact_reproduces_native_800_by_600_draw_coordinates() { + // Native callbacks produced these bounds for a point 110 units east and 80 north. + let viewport = [800.0, 600.0]; + let point = contact_pixel([110.0, 80.0], [0.7673624, -0.64121366], 400.0, viewport); + for (mark, expected) in [ + (ContactMark::Building, vec![[372, 486, 378, 492]]), + (ContactMark::Unit, vec![[373, 487, 377, 491]]), + ( + ContactMark::Cross, + vec![[373, 489, 377, 490], [375, 487, 376, 491]], + ), + ] { + let mut actual = Vec::new(); + mark_rectangles(mark, point, viewport, false, |rect| actual.push(rect)); + assert_eq!(actual, expected); + } + } +} diff --git a/crates/fparkan-render/src/lib.rs b/crates/fparkan-render/src/lib.rs index b684b787..645cb25d 100644 --- a/crates/fparkan-render/src/lib.rs +++ b/crates/fparkan-render/src/lib.rs @@ -2,6 +2,7 @@ //! Camera mathematics and graphics pipeline state. pub mod cockpit_course; +pub mod cockpit_radar; pub mod water; /// A 64-byte transform block returned by the original Terrain camera ABI. |
