Skip to content

Commit

Permalink
PPU: implement emphasis coloring
Browse files Browse the repository at this point in the history
- code copied from Mesen
  • Loading branch information
Amjad50 committed Aug 23, 2020
1 parent da7048c commit 69c70be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions display/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone, Copy)]
pub struct Color {
pub r: u8,
pub g: u8,
Expand Down
37 changes: 35 additions & 2 deletions ppu2c02/src/ppu2c02.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::ppu2c02_registers::Register;
use crate::sprite::{Sprite, SpriteAttribute};
use common::{interconnection::PPUCPUConnection, Bus, Device};
use display::{COLORS, TV};
use display::{Color, COLORS, TV};
use std::cell::Cell;
use std::cmp::min;

bitflags! {
pub struct ControlReg: u8 {
Expand Down Expand Up @@ -909,6 +910,38 @@ where
color
}

fn emphasis_color(&self, color: Color) -> Color {
let is_red_emph = self.reg_mask.intersects(MaskReg::EMPHASIZE_RED);
let is_green_emph = self.reg_mask.intersects(MaskReg::EMPHASIZE_GREEN);
let is_blue_emph = self.reg_mask.intersects(MaskReg::EMPHASIZE_BLUE);

let mut red = 1.;
let mut green = 1.;
let mut blue = 1.;

if is_red_emph {
red *= 1.1;
green *= 0.9;
blue *= 0.9;
}
if is_green_emph {
red *= 0.9;
green *= 1.1;
blue *= 0.9;
}
if is_blue_emph {
red *= 0.9;
green *= 0.9;
blue *= 1.1;
}

Color {
r: min((color.r as f32 * red) as u8, 255),
g: min((color.g as f32 * green) as u8, 255),
b: min((color.b as f32 * blue) as u8, 255),
}
}

fn render_pixel(&mut self) {
// fix overflowing colors
let mut color = self.generate_pixel() & 0x3F;
Expand All @@ -922,7 +955,7 @@ where
self.tv.set_pixel(
self.cycle as u32,
self.scanline as u32,
&COLORS[color as usize],
&self.emphasis_color(COLORS[color as usize]),
);
}

Expand Down

0 comments on commit 69c70be

Please sign in to comment.