Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

psx_display/
offset.rs

1// SPDX-License-Identifier: GPL-2.0-or-later
2//! The SCREEN X and SCREEN Y options: where the picture sits on the
3//! television.
4//!
5//! A CRT's overscan differs from set to set, so the picture is moved in the
6//! video signal (the GPU's display range, which [`DisplayConfig`] already
7//! writes), not in VRAM: nothing is cropped and no draw call changes. The rows
8//! read `CENTRE` at zero, `LEFT n` / `RIGHT n` for X and `UP n` / `DOWN n`
9//! for Y, in pixels.
10
11use psx_gpu::display::DisplayConfig;
12
13use crate::label::Label;
14
15/// The picture position: pixels right of and below centre, each within
16/// `-RANGE..=RANGE`. `RANGE` defaults to 16, WipEout's and Hollow Knight's.
17///
18/// ```
19/// use psx_display::ScreenOffset;
20/// let offset = ScreenOffset::<16>::CENTRE.stepped_x(-3).stepped_y(2);
21/// assert_eq!(offset.label_x().as_str(), "LEFT 3");
22/// assert_eq!(offset.label_y().as_str(), "DOWN 2");
23/// assert_eq!(offset.pixels(), (-3, 2));
24/// ```
25#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
26pub struct ScreenOffset<const RANGE: i8 = 16> {
27    x: i8,
28    y: i8,
29}
30
31impl<const RANGE: i8> ScreenOffset<RANGE> {
32    /// The standard, centred picture.
33    pub const CENTRE: Self = Self { x: 0, y: 0 };
34
35    /// The offset `x` pixels right and `y` down, each clamped to the range.
36    pub const fn new(x: i8, y: i8) -> Self {
37        Self {
38            x: clamp(x, RANGE),
39            y: clamp(y, RANGE),
40        }
41    }
42
43    /// Whether the picture is centred.
44    pub const fn is_centre(self) -> bool {
45        self.x == 0 && self.y == 0
46    }
47
48    /// Pixels right of centre (negative is left).
49    pub const fn x(self) -> i8 {
50        self.x
51    }
52
53    /// Pixels below centre (negative is up).
54    pub const fn y(self) -> i8 {
55        self.y
56    }
57
58    /// This offset with X moved by `delta` pixels, stopping at the ends.
59    pub const fn stepped_x(self, delta: i8) -> Self {
60        Self::new(add(self.x, delta), self.y)
61    }
62
63    /// This offset with Y moved by `delta` pixels, stopping at the ends.
64    pub const fn stepped_y(self, delta: i8) -> Self {
65        Self::new(self.x, add(self.y, delta))
66    }
67
68    /// What the SCREEN X row shows: `CENTRE`, `LEFT n` or `RIGHT n`.
69    pub const fn label_x(self) -> Label {
70        if self.x == 0 {
71            Label::word(b"CENTRE")
72        } else if self.x < 0 {
73            Label::toward(b"LEFT", self.x.unsigned_abs())
74        } else {
75            Label::toward(b"RIGHT", self.x.unsigned_abs())
76        }
77    }
78
79    /// What the SCREEN Y row shows: `CENTRE`, `UP n` or `DOWN n`.
80    pub const fn label_y(self) -> Label {
81        if self.y == 0 {
82            Label::word(b"CENTRE")
83        } else if self.y < 0 {
84            Label::toward(b"UP", self.y.unsigned_abs())
85        } else {
86            Label::toward(b"DOWN", self.y.unsigned_abs())
87        }
88    }
89
90    /// The save bytes, X then Y, each in two's complement; zero is centred,
91    /// which is also what a save from before the setting reads as.
92    pub const fn to_bytes(self) -> [u8; 2] {
93        [self.x as u8, self.y as u8]
94    }
95
96    /// The offset two save bytes hold. Any bytes decode: a value outside the
97    /// range is clamped to the nearest end.
98    pub const fn from_bytes(bytes: [u8; 2]) -> Self {
99        Self::new(bytes[0] as i8, bytes[1] as i8)
100    }
101
102    /// The offset as `(x, y)` pixels, the form [`DisplayConfig::with_offset`]
103    /// takes.
104    pub const fn pixels(self) -> (i16, i16) {
105        (self.x as i16, self.y as i16)
106    }
107
108    /// `display` with the picture moved to this offset. Hand the result to
109    /// the GPU's display setter to apply it at once; it is a video-signal
110    /// change and costs nothing per frame.
111    pub const fn apply_to(self, display: DisplayConfig) -> DisplayConfig {
112        display.with_offset(self.pixels())
113    }
114}
115
116const fn clamp(value: i8, range: i8) -> i8 {
117    let range = if range < 0 { 0 } else { range };
118    if value < -range {
119        -range
120    } else if value > range {
121        range
122    } else {
123        value
124    }
125}
126
127const fn add(value: i8, delta: i8) -> i8 {
128    let sum = value as i16 + delta as i16;
129    if sum < i8::MIN as i16 {
130        i8::MIN
131    } else if sum > i8::MAX as i16 {
132        i8::MAX
133    } else {
134        sum as i8
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::ScreenOffset;
141    use psx_gpu::display::{DisplayConfig, Resolution, VideoMode};
142
143    #[test]
144    fn labels_read_centre_at_zero() {
145        let offset = ScreenOffset::<16>::CENTRE;
146        assert_eq!(offset.label_x().as_str(), "CENTRE");
147        assert_eq!(offset.label_y().as_str(), "CENTRE");
148        assert!(offset.is_centre());
149        let offset = ScreenOffset::<16>::new(16, -16);
150        assert_eq!(offset.label_x().as_str(), "RIGHT 16");
151        assert_eq!(offset.label_y().as_str(), "UP 16");
152        let offset = ScreenOffset::<16>::new(-1, 1);
153        assert_eq!(offset.label_x().as_str(), "LEFT 1");
154        assert_eq!(offset.label_y().as_str(), "DOWN 1");
155    }
156
157    #[test]
158    fn stepping_and_new_clamp_to_the_range() {
159        let mut offset = ScreenOffset::<16>::CENTRE;
160        for _ in 0..40 {
161            offset = offset.stepped_x(1).stepped_y(-1);
162        }
163        assert_eq!((offset.x(), offset.y()), (16, -16));
164        assert_eq!(
165            ScreenOffset::<16>::new(i8::MAX, i8::MIN).pixels(),
166            (16, -16)
167        );
168        assert_eq!(ScreenOffset::<16>::new(0, 0).stepped_x(i8::MAX).x(), 16);
169        assert_eq!(ScreenOffset::<24>::new(100, -100).pixels(), (24, -24));
170        assert_eq!(ScreenOffset::<0>::new(5, 5), ScreenOffset::<0>::CENTRE);
171    }
172
173    #[test]
174    fn the_save_bytes_match_wipeouts_and_survive_any_byte() {
175        for x in -16..=16i8 {
176            for y in [-16, -1, 0, 1, 16] {
177                let offset = ScreenOffset::<16>::new(x, y);
178                // WipEout saves `screen_x as u8` then `screen_y as u8`.
179                assert_eq!(offset.to_bytes(), [x as u8, y as u8]);
180                assert_eq!(ScreenOffset::<16>::from_bytes(offset.to_bytes()), offset);
181            }
182        }
183        for byte in 0..=u8::MAX {
184            let want = (byte as i8).clamp(-16, 16);
185            assert_eq!(
186                ScreenOffset::<16>::from_bytes([byte, byte]).pixels(),
187                (want as i16, want as i16)
188            );
189        }
190    }
191
192    #[test]
193    fn the_offset_moves_the_display_config() {
194        let display = DisplayConfig::new(VideoMode::Ntsc, Resolution::R320X240);
195        let moved = ScreenOffset::<16>::new(-4, 3).apply_to(display);
196        assert_eq!(moved.offset, (-4, 3));
197        assert_eq!(moved.resolution, display.resolution);
198        assert_eq!(ScreenOffset::<16>::CENTRE.apply_to(display), display);
199    }
200}