Skip to content

Commit

Permalink
Clean code:
Browse files Browse the repository at this point in the history
* Add structs for the fifo fetchers
* remove the colors module
  • Loading branch information
alloncm committed Dec 25, 2022
1 parent 0acf351 commit 5ac877c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 40 deletions.
5 changes: 5 additions & 0 deletions lib_gb/src/ppu/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use super::gfx_device::Pixel;

pub const WHITE:Color = Color {r: 255,g: 255,b: 255};
pub const LIGHT_GRAY:Color = Color {r: 160,g: 160,b: 160};
pub const DARK_GRAY:Color = Color {r: 64,g: 64,b: 64};
pub const BLACK:Color = Color {r: 0,g: 0,b: 0};

pub struct Color{
pub r:u8,
pub g:u8,
Expand Down
6 changes: 0 additions & 6 deletions lib_gb/src/ppu/colors.rs

This file was deleted.

11 changes: 7 additions & 4 deletions lib_gb/src/ppu/fifo/background_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::{mmu::vram::VRam, utils::{bit_masks::*, fixed_size_queue::FixedSizeQueue, vec2::Vec2}, ppu::attributes::GbcBackgroundAttributes, machine::Mode};
use super::{FIFO_SIZE, SPRITE_WIDTH, fetcher_state_machine::FetcherStateMachine, fetching_state::*};

const EMPTY_FIFO_BUFFER:[(u8, GbcBackgroundAttributes);FIFO_SIZE] = [(0, DEFAULT_GBC_ATTRIBUTES);FIFO_SIZE];
#[derive(Clone, Copy, Default)]
pub struct BackgroundPixel{pub color_index:u8, pub attributes:GbcBackgroundAttributes}

const EMPTY_FIFO_BUFFER:[BackgroundPixel;FIFO_SIZE] = [BackgroundPixel{color_index:0, attributes:DEFAULT_GBC_ATTRIBUTES};FIFO_SIZE];
const DEFAULT_GBC_ATTRIBUTES: GbcBackgroundAttributes = GbcBackgroundAttributes::new(0);

pub struct BackgroundFetcher{
pub fifo:FixedSizeQueue<(u8, GbcBackgroundAttributes), FIFO_SIZE>,
pub fifo:FixedSizeQueue<BackgroundPixel, FIFO_SIZE>,
pub window_line_counter:u8,
pub has_wy_reached_ly:bool,

Expand All @@ -24,7 +27,7 @@ impl BackgroundFetcher{
fetcher_state_machine:FetcherStateMachine::new(state_machine),
cgb_attribute:DEFAULT_GBC_ATTRIBUTES,
current_x_pos:0,
fifo:FixedSizeQueue::<(u8, GbcBackgroundAttributes), FIFO_SIZE>::new(),
fifo:FixedSizeQueue::<BackgroundPixel, FIFO_SIZE>::new(),
window_line_counter:0,
rendering_window:false,
has_wy_reached_ly:false,
Expand Down Expand Up @@ -124,7 +127,7 @@ impl BackgroundFetcher{
let mask = 1 << i;
let mut pixel = (low_data & mask) >> i;
pixel |= ((high_data & mask) >> i) << 1;
self.fifo.push((pixel, self.cgb_attribute));
self.fifo.push(BackgroundPixel { color_index: pixel, attributes: self.cgb_attribute});
self.current_x_pos += 1;
i += step;

Expand Down
13 changes: 8 additions & 5 deletions lib_gb/src/ppu/fifo/sprite_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ pub const NORMAL_SPRITE_HIGHT:u8 = 8;
pub const EXTENDED_SPRITE_HIGHT:u8 = 16;
pub const MAX_SPRITES_PER_LINE:usize = 10;

#[derive(Clone, Copy, Default)]
pub struct SpritePixel {pub color_index:u8, pub oam_entry:u8}

pub struct SpriteFetcher{
pub fifo:FixedSizeQueue<(u8, u8), FIFO_SIZE>,
pub fifo:FixedSizeQueue<SpritePixel, FIFO_SIZE>,
pub oam_entries:[SpriteAttributes; 10],
pub oam_entries_len:u8,
pub rendering:bool,
Expand All @@ -25,7 +28,7 @@ impl SpriteFetcher{
current_oam_entry:0,
oam_entries_len:0,
oam_entries,
fifo:FixedSizeQueue::<(u8,u8), 8>::new(),
fifo:FixedSizeQueue::<SpritePixel, 8>::new(),
rendering:false,
}
}
Expand Down Expand Up @@ -85,13 +88,13 @@ impl SpriteFetcher{
if self.fifo.len() >= start_x{
for i in 0..start_x{
// pixel 0 is transparent
if self.fifo[i].0 == 0{
self.fifo[i] = (pixels[i], self.current_oam_entry);
if self.fifo[i].color_index == 0{
self.fifo[i] = SpritePixel{color_index: pixels[i], oam_entry:self.current_oam_entry};
}
}
}
for i in start_x..end_x{
self.fifo.push((pixels[i], self.current_oam_entry));
self.fifo.push(SpritePixel{color_index:pixels[i], oam_entry:self.current_oam_entry});
}

self.current_oam_entry += 1;
Expand Down
43 changes: 20 additions & 23 deletions lib_gb/src/ppu/gb_ppu.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::machine::Mode;
use crate::mmu::vram::VRam;
use crate::utils::{vec2::Vec2, bit_masks::*};
use crate::ppu::{gfx_device::GfxDevice, ppu_state::PpuState, attributes::SpriteAttributes, colors::*, color::*};

use super::fifo::SPRITE_WIDTH;
use super::fifo::{FIFO_SIZE, sprite_fetcher::*, background_fetcher::BackgroundFetcher};
use super::gfx_device::Pixel;
use super::attributes::GbcBackgroundAttributes;
use super::fifo::{SPRITE_WIDTH, background_fetcher::{BackgroundPixel, BackgroundFetcher}, FIFO_SIZE, sprite_fetcher::*};
use super::{gfx_device::*, ppu_state::PpuState, attributes::SpriteAttributes, color::*};

pub const SCREEN_HEIGHT: usize = 144;
pub const SCREEN_WIDTH: usize = 160;
Expand Down Expand Up @@ -370,51 +367,51 @@ impl<GFX:GfxDevice> GbPpu<GFX>{
}
}

let (bg_pixel_color_num, bg_cgb_attribute) = self.bg_fetcher.fifo.remove();
let pixel = self.get_pixel_color(bg_cgb_attribute, bg_pixel_color_num);
let bg_pixel = self.bg_fetcher.fifo.remove();
let pixel = self.get_pixel_color(bg_pixel);
self.push_pixel(Color::into(pixel));
self.pixel_x_pos += 1;
}

fn get_pixel_color(&mut self, bg_cgb_attribute: GbcBackgroundAttributes, bg_pixel_color_num: u8) -> Color {
fn get_pixel_color(&mut self, bg_pixel:BackgroundPixel) -> Color {
if self.sprite_fetcher.fifo.len() == 0{
return self.get_bg_pixel(bg_cgb_attribute, bg_pixel_color_num);
return self.get_bg_pixel(bg_pixel);
}
let (oam_pixel, oam_attribute_index) = self.sprite_fetcher.fifo.remove();
if oam_pixel == 0{
return self.get_bg_pixel(bg_cgb_attribute, bg_pixel_color_num);
let sprite_pixel = self.sprite_fetcher.fifo.remove();
if sprite_pixel.color_index == 0{
return self.get_bg_pixel(bg_pixel);
}
let pixel_oam_attribute = &self.sprite_fetcher.oam_entries[oam_attribute_index as usize];
let pixel_oam_attribute = &self.sprite_fetcher.oam_entries[sprite_pixel.oam_entry as usize];
if self.mode == Mode::CGB{
return if bg_pixel_color_num == 0 || self.lcd_control & BIT_0_MASK == 0 || (!pixel_oam_attribute.attributes.bg_priority && !bg_cgb_attribute.attribute.bg_priority){
Self::get_color_from_color_ram(&self.obj_color_ram, pixel_oam_attribute.gbc_palette_number, oam_pixel)
return if bg_pixel.color_index == 0 || self.lcd_control & BIT_0_MASK == 0 || (!pixel_oam_attribute.attributes.bg_priority && !bg_pixel.attributes.attribute.bg_priority){
Self::get_color_from_color_ram(&self.obj_color_ram, pixel_oam_attribute.gbc_palette_number, sprite_pixel.color_index)
}
else{
Self::get_color_from_color_ram(&self.bg_color_ram, bg_cgb_attribute.cgb_pallete_number, bg_pixel_color_num)
Self::get_color_from_color_ram(&self.bg_color_ram, bg_pixel.attributes.cgb_pallete_number, bg_pixel.color_index)
};
}
else{
if pixel_oam_attribute.attributes.bg_priority && bg_pixel_color_num != 0{
return self.bg_color_mapping[bg_pixel_color_num as usize];
if pixel_oam_attribute.attributes.bg_priority && bg_pixel.color_index != 0{
return self.bg_color_mapping[bg_pixel.color_index as usize];
}
else{
let sprite_pixel = if pixel_oam_attribute.gb_palette_number{
self.obj_color_mapping1[oam_pixel as usize]
self.obj_color_mapping1[sprite_pixel.color_index as usize]
}
else{
self.obj_color_mapping0[oam_pixel as usize]
self.obj_color_mapping0[sprite_pixel.color_index as usize]
};
return sprite_pixel.expect("Corruption in the object color pallete");
}
}
}

fn get_bg_pixel(&mut self, bg_cgb_attribute: GbcBackgroundAttributes, bg_pixel_color_num: u8) -> Color {
fn get_bg_pixel(&mut self, bg_pixel:BackgroundPixel) -> Color {
return if self.mode == Mode::CGB{
Self::get_color_from_color_ram(&self.bg_color_ram, bg_cgb_attribute.cgb_pallete_number, bg_pixel_color_num)
Self::get_color_from_color_ram(&self.bg_color_ram, bg_pixel.attributes.cgb_pallete_number, bg_pixel.color_index)
}
else{
self.bg_color_mapping[bg_pixel_color_num as usize]
self.bg_color_mapping[bg_pixel.color_index as usize]
};
}

Expand Down
1 change: 0 additions & 1 deletion lib_gb/src/ppu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod gb_ppu;
pub mod ppu_state;
pub mod color;
pub mod colors;
pub mod ppu_register_updater;
pub mod fifo;
pub mod gfx_device;
Expand Down
2 changes: 1 addition & 1 deletion lib_gb/src/ppu/ppu_register_updater.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::bit_masks::*;
use super::{color::*, colors::*, gb_ppu::GbPpu, gfx_device::GfxDevice, ppu_state::PpuState};
use super::{color::*, gb_ppu::GbPpu, gfx_device::GfxDevice, ppu_state::PpuState};

const WX_OFFSET:u8 = 7;

Expand Down

0 comments on commit 5ac877c

Please sign in to comment.