Skip to content
Snippets Groups Projects
ppu.rs 36.4 KiB
Newer Older
  • Learn to ignore specific revisions
  •             &self.palette_colors,
                self.palettes[1],
            );
            Self::compute_palette(
                &mut self.palette_obj_1,
                &self.palette_colors,
                self.palettes[2],
            );
        }
    
        /// Static method used for the base logic of computation of RGB
        /// based palettes from the internal Game Boy color indexes.
        /// This method should be called whenever the palette indexes
        /// are changed.
        fn compute_palette(palette: &mut Palette, palette_colors: &Palette, value: u8) {
    
            for (index, palette_item) in palette.iter_mut().enumerate() {
    
                let color_index: usize = (value as usize >> (index * 2)) & 3;
                match color_index {
    
                    0..=3 => *palette_item = palette_colors[color_index],
    
                    color_index => panic!("Invalid palette color index {:04x}", color_index),
                }
            }
        }
    
    
    impl Default for Ppu {
        fn default() -> Self {
            Self::new()
        }
    }
    
    João Magalhães's avatar
    João Magalhães committed
    
    #[cfg(test)]
    mod tests {
    
        use super::Ppu;
    
    João Magalhães's avatar
    João Magalhães committed
    
        #[test]
        fn test_update_tile_simple() {
            let mut ppu = Ppu::new();
            ppu.vram[0x0000] = 0xff;
            ppu.vram[0x0001] = 0xff;
    
            let result = ppu.tiles()[0].get(0, 0);
            assert_eq!(result, 0);
    
            ppu.update_tile(0x8000, 0x00);
            let result = ppu.tiles()[0].get(0, 0);
            assert_eq!(result, 3);
        }
    
        #[test]
        fn test_update_tile_upper() {
            let mut ppu = Ppu::new();
            ppu.vram[0x1000] = 0xff;
            ppu.vram[0x1001] = 0xff;
    
            let result = ppu.tiles()[256].get(0, 0);
            assert_eq!(result, 0);
    
            ppu.update_tile(0x9000, 0x00);
            let result = ppu.tiles()[256].get(0, 0);
            assert_eq!(result, 3);
        }