playdate_rs/
display.rs

1use crate::math::{Size, Vec2};
2
3pub struct PlaydateDisplay {
4    handle: *const sys::playdate_display,
5}
6
7impl PlaydateDisplay {
8    pub(crate) fn new(handle: *const sys::playdate_display) -> Self {
9        Self { handle }
10    }
11
12    /// Returns the height of the display, taking the current scale into account; e.g., if the scale is 2, this function returns 120 instead of 240.
13    pub fn get_width(&self) -> u32 {
14        unsafe { (*self.handle).getWidth.unwrap()() as _ }
15    }
16
17    /// Returns the width of the display, taking the current scale into account; e.g., if the scale is 2, this function returns 200 instead of 400.
18    pub fn get_height(&self) -> u32 {
19        unsafe { (*self.handle).getHeight.unwrap()() as _ }
20    }
21
22    /// Sets the nominal refresh rate in frames per second. Default is 20 fps, the maximum rate supported by the hardware for full-frame updates.
23    pub fn set_refresh_rate(&self, rate: f32) {
24        unsafe { (*self.handle).setRefreshRate.unwrap()(rate) }
25    }
26
27    /// If flag evaluates to true, the frame buffer is drawn inverted—black instead of white, and vice versa.
28    pub fn set_inverted(&self, flag: bool) {
29        unsafe { (*self.handle).setInverted.unwrap()(flag as _) }
30    }
31
32    /// Sets the display scale factor. Valid values for scale are 1, 2, 4, and 8.
33    ///
34    /// The top-left corner of the frame buffer is scaled up to fill the display; e.g., if the scale is set to 4, the pixels in rectangle \[0,100\] x \[0,60\] are drawn on the screen as 4 x 4 squares.
35    pub fn set_scale(&self, s: u32) {
36        unsafe { (*self.handle).setScale.unwrap()(s) }
37    }
38
39    /// Adds a mosaic effect to the display. Valid x and y values are between 0 and 3, inclusive.
40    pub fn set_mosaic(&self, effect: Vec2<u32>) {
41        debug_assert!(
42            effect.x < 4 && effect.y < 4,
43            "invalid mosaic effect: {:?}",
44            effect
45        );
46        unsafe { (*self.handle).setMosaic.unwrap()(effect.x, effect.y) }
47    }
48
49    /// Flips the display on the x or y axis, or both.
50    pub fn set_flipped(&self, x: bool, y: bool) {
51        unsafe { (*self.handle).setFlipped.unwrap()(x as _, y as _) }
52    }
53
54    /// Offsets the display by the given amount. Areas outside of the displayed area are filled with the current background color.
55    pub fn set_offset(&self, delta: Vec2<i32>) {
56        unsafe { (*self.handle).setOffset.unwrap()(delta.x, delta.y) }
57    }
58}
59
60pub const DISPLAY_WIDTH: u32 = 400;
61pub const DISPLAY_HEIGHT: u32 = 240;
62pub const DISPLAY_SIZE: Size<u32> = size!(DISPLAY_WIDTH, DISPLAY_HEIGHT);