ppu: Fix window

This commit is contained in:
Kevin Hamacher 2020-02-21 00:15:09 +01:00
parent bdf51448f7
commit b7c1d9b8a0

View File

@ -843,87 +843,95 @@ impl Display {
if (self.control & CTRL_WND_DISPLAY_ENABLE) == CTRL_WND_DISPLAY_ENABLE {
// Draw 'window' over the background.
// Screen coordinates of the top left corner are WX-7, WY
// Quick check if the window is visible at all.
if self.windowx < 167 && self.windowy < 144 {
//let rx = self.windowx.wrapping_add(7);
let rx = 7u8.wrapping_sub(self.windowx);
let ry = render_y.wrapping_add(self.windowy);
for r_x in 0..160u8 {
let render_x = r_x.wrapping_add(rx);
// Absolute render coordinates
let tile_index_x: u8 = render_x >> 3;
let tile_index_y: u8 = ry >> 3;
let tile_offset_x: u8 = render_x & 7;
let tile_offset_y: u8 = ry & 7;
let window_y_offset = render_y as i16 - self.windowy as i16;
if self.windowx < 7 {
eprintln!("Window X position < 7, bailing out");
} else if window_y_offset >= 144 || window_y_offset < 0 {
// Not visible
} else {
let window_y_offset = window_y_offset as u8;
let window_x_offset = self.windowx - 7;
let vram_offset: usize =
window_map + (tile_index_y as usize) * 32 + tile_index_x as usize;
for r_x in 0..(160u8 - window_x_offset) {
let render_x = r_x.wrapping_add(window_x_offset);
// Absolute render coordinates
let tile_index_x: u8 = render_x >> 3;
let tile_index_y: u8 = window_y_offset >> 3;
let tile_offset_x: u8 = render_x & 7;
let tile_offset_y: u8 = window_y_offset & 7;
// Obtain tile ID in this area
let tile_id = self.vram0[vram_offset];
let vram_offset: usize =
window_map + (tile_index_y as usize) * 32 + tile_index_x as usize;
let bg_attribs = self.get_background_attribute(
//tile_index_x as usize, tile_index_y as usize
vram_offset,
);
// Obtain tile ID in this area
let tile_id = self.vram0[vram_offset];
// Get BG map attributes
let vram = if bg_attribs.vram_bank_number() == 0 {
&*self.vram0
} else {
&*self.vram1
};
let bg_attribs = self.get_background_attribute(
//tile_index_x as usize, tile_index_y as usize
vram_offset,
);
// TODO: Priority
let tile_offset_x = if bg_attribs.vertical_flip() {
7 ^ tile_offset_x
} else {
tile_offset_x
};
let tile_offset_y = if bg_attribs.horizontal_flip() {
7 ^ tile_offset_y
} else {
tile_offset_y
};
// Get BG map attributes
let vram = if bg_attribs.vram_bank_number() == 0 {
&*self.vram0
} else {
&*self.vram1
};
// Obtain tile information
let tile_base_addr: usize = self.get_bg_window_tile_addr(tile_id);
let tile_addr = tile_base_addr + (tile_offset_y as usize) * 2;
let tile_line_1 = vram[tile_addr];
let tile_line_2 = vram[tile_addr + 1];
// TODO: Priority
let tile_offset_x = if bg_attribs.vertical_flip() {
7 ^ tile_offset_x
} else {
tile_offset_x
};
let tile_offset_y = if bg_attribs.horizontal_flip() {
7 ^ tile_offset_y
} else {
tile_offset_y
};
// Get the correct bit
let b1: bool = (tile_line_1 & 1 << (7 - tile_offset_x)) > 0;
let b2: bool = (tile_line_2 & 1 << (7 - tile_offset_x)) > 0;
// Obtain tile information
let tile_base_addr: usize = self.get_bg_window_tile_addr(tile_id);
let tile_addr = tile_base_addr + (tile_offset_y as usize) * 2;
let tile_line_1 = vram[tile_addr];
let tile_line_2 = vram[tile_addr + 1];
let c = (b2 as u8) * 2 + b1 as u8;
let origin = match c {
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Window,
};
let c = self.background_palette_cgb[bg_attribs.palette_number()]
.get_color(c as usize);
self.set_pixel(render_x, render_y, c, origin);
/*
let lookup: [u8; 4] = [
self.background_palette & 3,
(self.background_palette >> 2) & 3,
(self.background_palette >> 4) & 3,
(self.background_palette >> 6) & 3,
];
let entry = MONOCHROME_PALETTE[lookup[c as usize] as usize];
// Get the correct bit
let b1: bool = (tile_line_1 & 1 << (7 - tile_offset_x)) > 0;
let b2: bool = (tile_line_2 & 1 << (7 - tile_offset_x)) > 0;
// Draw stuff. We're currently only in monochrome mode
let origin = match c {
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Window,
};
self.set_pixel(
render_x,
render_y,
sdl2::pixels::Color::RGB(entry[0], entry[1], entry[2]),
origin,
);
*/
let c = (b2 as u8) * 2 + b1 as u8;
let origin = match c {
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Window,
};
let c = self.background_palette_cgb[bg_attribs.palette_number()]
.get_color(c as usize);
self.set_pixel(render_x, render_y, c, origin);
/*
let lookup: [u8; 4] = [
self.background_palette & 3,
(self.background_palette >> 2) & 3,
(self.background_palette >> 4) & 3,
(self.background_palette >> 6) & 3,
];
let entry = MONOCHROME_PALETTE[lookup[c as usize] as usize];
// Draw stuff. We're currently only in monochrome mode
let origin = match c {
0 => PixelOrigin::Empty, // Hack so that objects will be in front of it.
_ => PixelOrigin::Window,
};
self.set_pixel(
render_x,
render_y,
sdl2::pixels::Color::RGB(entry[0], entry[1], entry[2]),
origin,
);
*/
}
}
}
}