gb-emu/src/processor/gpu/tile_window.rs

95 lines
2.7 KiB
Rust
Raw Normal View History

use minifb::Window;
2023-02-06 12:14:51 +11:00
use crate::{
2023-02-07 09:12:39 +11:00
processor::{
get_bit,
gpu::{
bits_to_mapped_colour, scale_buffer, Palette, TiledataArea, TILE_WINDOW_HEIGHT,
TILE_WINDOW_HEIGHT_SCALED, TILE_WINDOW_WIDTH, TILE_WINDOW_WIDTH_SCALED,
2023-02-07 09:12:39 +11:00
},
memory::Memory,
2023-02-07 09:12:39 +11:00
},
2023-02-06 12:14:51 +11:00
FACTOR,
};
pub(super) struct TileWindow {
sprite_buffer: Vec<u32>,
sprite_buffer_scaled: Vec<u32>,
sprite_window: Window,
}
2023-02-06 12:14:51 +11:00
impl TileWindow {
pub(super) fn new(window: Window) -> Self {
Self {
sprite_buffer: vec![0; TILE_WINDOW_WIDTH * TILE_WINDOW_HEIGHT],
sprite_buffer_scaled: vec![0; TILE_WINDOW_WIDTH_SCALED * TILE_WINDOW_HEIGHT_SCALED],
sprite_window: window,
2023-02-06 12:14:51 +11:00
}
}
}
2023-02-06 12:14:51 +11:00
impl TileWindow {
pub(super) fn draw_sprite_window(&mut self, palette: Palette, memory: &Memory) {
for tile_y in 0..16 {
self.draw_row(
tile_y,
tile_y as usize,
TiledataArea::D8000,
palette,
memory,
);
}
for tile_y in 0..8 {
self.draw_row(
tile_y,
(tile_y as usize) + 16,
TiledataArea::D9000,
palette,
memory,
);
}
self.sprite_buffer_scaled = scale_buffer(
&self.sprite_buffer,
TILE_WINDOW_WIDTH,
TILE_WINDOW_HEIGHT,
2023-02-06 12:14:51 +11:00
FACTOR,
);
self.sprite_window
2023-02-06 12:14:51 +11:00
.update_with_buffer(
&self.sprite_buffer_scaled,
TILE_WINDOW_WIDTH_SCALED,
TILE_WINDOW_HEIGHT_SCALED,
2023-02-06 12:14:51 +11:00
)
.unwrap();
}
fn draw_row(
&mut self,
tile_y: u8,
display_y: usize,
area: TiledataArea,
palette: Palette,
memory: &Memory,
) {
for tile_x in 0..16 {
let tile_num = (tile_y * 16) + tile_x;
let data_begin = area.get_addr(tile_num);
for px_y in 0..8 {
let lsbs = memory.get((px_y * 2) + data_begin);
let msbs = memory.get((px_y * 2) + data_begin + 1);
for px_x in 0..8 {
let real_px_y = (display_y * 8) + px_y as usize;
let real_px_x = (tile_x as usize * 8) + px_x as usize;
let lsb = get_bit(lsbs, 7 - px_x);
let msb = get_bit(msbs, 7 - px_x);
let colour = bits_to_mapped_colour(lsb, msb, palette);
self.sprite_buffer[real_px_x + (real_px_y * TILE_WINDOW_WIDTH)] =
colour.to_rgb();
}
}
}
}
2023-02-06 12:14:51 +11:00
}