fix tile rendering

This commit is contained in:
Alex Janka 2023-02-09 11:10:49 +11:00
parent d6b95e6c6c
commit 09b1cb1e57

View file

@ -330,6 +330,7 @@ impl CPU {
palette, palette,
scroll_x, scroll_x,
scroll_y, scroll_y,
true,
); );
} }
@ -345,6 +346,7 @@ impl CPU {
palette, palette,
pos_x, pos_x,
pos_y, pos_y,
false,
) )
} }
} }
@ -429,28 +431,39 @@ impl CPU {
tilemap: &TilemapArea, tilemap: &TilemapArea,
tiledata: &TiledataArea, tiledata: &TiledataArea,
palette: Palette, palette: Palette,
_offset_x: u8, offset_x: u8,
offset_y: u8, offset_y: u8,
wrap: bool,
) { ) {
let tile_line = (scanline as usize) + (offset_y as usize); let tile_line_y = scanline.wrapping_add(offset_y);
let tilemap_row = tile_line / 8; if (scanline as usize + offset_y as usize) % 0xFF > 0 && !wrap {
let tile_px = (tile_line) % 8; return;
let tiledata_offset = tile_px * 2; }
let row_addr = ((tilemap_row * 32) % 0x400) as u16; let tilemap_row = tile_line_y / 8;
for x in 0..32_u16 { let tile_px_y = (tile_line_y) % 8;
let tile_addr = tiledata.get_addr(self.memory.get(tilemap.get_addr(row_addr + x))) let tiledata_offset = tile_px_y * 2;
+ tiledata_offset as u16; let row_addr = ((tilemap_row as usize * 32) % 0x400) as u16;
for x in 0..WIDTH {
let tile_line_x = (x as u8).wrapping_add(offset_x);
if ((x + offset_x as usize) / 0xFF) > 0 && !wrap {
continue;
}
let tilemap_column = (tile_line_x / 8) as u16;
let tile_px_x = (tile_line_x % 8) as u8;
let tile_addr = tiledata.get_addr(
self.memory
.get(tilemap.get_addr(row_addr + (tilemap_column))),
) + tiledata_offset as u16;
let lsbs = self.memory.get(tile_addr); let lsbs = self.memory.get(tile_addr);
let msbs = self.memory.get(tile_addr + 1); let msbs = self.memory.get(tile_addr + 1);
for px_x in 0..8 { let lsb = get_bit(lsbs, 7 - tile_px_x);
let lsb = get_bit(lsbs, 7 - px_x); let msb = get_bit(msbs, 7 - tile_px_x);
let msb = get_bit(msbs, 7 - px_x);
let colour = bits_to_mapped_colour(lsb, msb, palette); let colour = bits_to_mapped_colour(lsb, msb, palette);
let x_coord = ((x * 8) + (px_x as u16)) as usize;
if x_coord < WIDTH { self.gpu.buffer[(scanline as usize * WIDTH) + x] = colour.to_rgb();
self.gpu.buffer[(scanline as usize * WIDTH) + x_coord] = colour.to_rgb();
}
}
} }
} }