tetanes_core/ppu/
sprite.rs

1//! PPU OAM Sprite implementation.
2//!
3//! See: <https://www.nesdev.org/wiki/PPU_OAM>
4
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8/// PPU OAM Sprite entry.
9///
10/// See: <https://www.nesdev.org/wiki/PPU_OAM>
11#[derive(Copy, Clone, Serialize, Deserialize)]
12#[must_use]
13pub struct Sprite {
14    pub x: u32,
15    pub y: u32,
16    pub tile_addr: u16,
17    pub tile_lo: u8,
18    pub tile_hi: u8,
19    pub palette: u8,
20    pub bg_priority: bool,
21    pub flip_horizontal: bool,
22    pub flip_vertical: bool,
23}
24
25impl Sprite {
26    pub const fn new() -> Self {
27        Self {
28            x: 0xFF,
29            y: 0xFF,
30            tile_addr: 0x0000,
31            tile_lo: 0x00,
32            tile_hi: 0x00,
33            palette: 0x07,
34            bg_priority: true,
35            flip_horizontal: true,
36            flip_vertical: true,
37        }
38    }
39}
40
41impl Default for Sprite {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl fmt::Debug for Sprite {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.debug_struct("Sprite")
50            .field("x", &self.x)
51            .field("y", &self.y)
52            .field("tile_addr", &format_args!("${:04X}", &self.tile_addr))
53            .field("tile_lo", &format_args!("${:02X}", &self.tile_lo))
54            .field("tile_hi", &format_args!("${:02X}", &self.tile_hi))
55            .field("palette", &format_args!("${:02X}", &self.palette))
56            .field("bg_priority", &self.bg_priority)
57            .field("flip_horizontal", &self.flip_horizontal)
58            .field("flip_vertical", &self.flip_vertical)
59            .finish()
60    }
61}