removed outdated rendering methods

This commit is contained in:
Matthew Berry 2021-01-31 15:32:57 -08:00
parent 0ba9da018e
commit 8e9eac5b6f

View file

@ -326,156 +326,6 @@ class PPU
end
end
def render_background(scanline : Slice(UInt16), row : Int, bg : Int) : Nil
return unless bit?(@dispcnt.value, 8 + bg)
tw, th = case @bgcnt[bg].screen_size
when 0b00 then {32, 32} # 32x32
when 0b01 then {64, 32} # 64x32
when 0b10 then {32, 64} # 32x64
when 0b11 then {64, 64} # 64x64
else raise "Impossible bgcnt screen size: #{@bgcnt[bg].screen_size}"
end
screen_base = 0x800_u32 * @bgcnt[bg].screen_base_block
character_base = @bgcnt[bg].character_base_block.to_u32 * 0x4000
effective_row = (row + @bgvofs[bg].value) % (th << 3)
ty = effective_row >> 3
240.times do |col|
next if scanline[col] > 0
effective_col = (col + @bghofs[bg].value) % (tw << 3)
tx = effective_col >> 3
se_idx = se_index(tx, ty, @bgcnt[bg].screen_size)
screen_entry = @vram[screen_base + se_idx * 2 + 1].to_u16 << 8 | @vram[screen_base + se_idx * 2]
tile_id = bits(screen_entry, 0..9)
y = (effective_row & 7) ^ (7 * (screen_entry >> 11 & 1))
x = (effective_col & 7) ^ (7 * (screen_entry >> 10 & 1))
if @bgcnt[bg].color_mode # 8bpp
pal_idx = @vram[character_base + tile_id * 0x40 + y * 8 + x]
else # 4bpp
palette_bank = bits(screen_entry, 12..15)
palettes = @vram[character_base + tile_id * 0x20 + y * 4 + (x >> 1)]
pal_idx = ((palettes >> ((x & 1) * 4)) & 0xF)
pal_idx = (palette_bank << 4) + pal_idx if pal_idx > 0
end
scanline[col] = pal_idx.to_u16
end
end
def render_sprites(scanline : Slice(UInt16), row : Int, priority : Int) : Nil
base = 0x10000_u32
sprites = Slice(Sprite).new(@oam.to_unsafe.as(Sprite*), 128)
sprites.each do |sprite|
next unless sprite.priority == priority
next if sprite.obj_shape == 3 # prohibited
x_coord, y_coord = (sprite.x_coord << 7).to_i16! >> 7, sprite.y_coord.to_i8!.to_i16!
orig_width, orig_height = SIZES[sprite.obj_shape][sprite.obj_size]
width, height = orig_width, orig_height
center_x, center_y = x_coord + width // 2, y_coord + height // 2 # off of center
if sprite.affine
oam_affine_entry = sprite.attr1_bits_9_13
# signed 8.8 fixed-point numbers, need to shr 8
pa = sprites[oam_affine_entry * 4].aff_param.to_i32
pb = sprites[oam_affine_entry * 4 + 1].aff_param.to_i32
pc = sprites[oam_affine_entry * 4 + 2].aff_param.to_i32
pd = sprites[oam_affine_entry * 4 + 3].aff_param.to_i32
if sprite.attr0_bit_9 # double-size (rotated sprites won't clip unless scaled)
center_x += width >> 1
center_y += height >> 1
width <<= 1
height <<= 1
end
else # identity matrix if sprite isn't affine (shifted left 8 to match the 8.8 fixed-point)
pa, pb, pc, pd = 0x100, 0, 0, 0x100
end
if y_coord <= row < y_coord + height
iy = row.to_i16 - center_y
min_x, max_x = Math.max(0, x_coord), Math.min(240, x_coord + width)
(-center_x...center_x).each do |ix|
col = center_x + ix
next unless min_x <= col < max_x
next if scanline[col] > 0
# transform to texture coordinates
px = (pa * ix + pb * iy) >> 8
py = (pc * ix + pd * iy) >> 8
# bring origin back to top-left of the sprite
px += (orig_width // 2)
py += (orig_height // 2)
next unless 0 <= px < orig_width && 0 <= py < orig_height
px = orig_width - px - 1 if bit?(sprite.attr1, 12) && !sprite.affine
py = orig_height - py - 1 if bit?(sprite.attr1, 13) && !sprite.affine
x = px & 7
y = py & 7
tile_id = sprite.character_name
offset = py >> 3
if @dispcnt.obj_character_vram_mapping
offset *= orig_width >> 3
else
if sprite.color_mode
offset *= 0x10
else
offset *= 0x20
end
end
offset += px >> 3
if sprite.color_mode # 8bpp
tile_id >>= 1
tile_id += offset
pal_idx = @vram[base + tile_id * 0x40 + y * 8 + x]
else # 4bpp
tile_id += offset
palettes = @vram[base + tile_id * 0x20 + y * 4 + (x >> 1)]
pal_idx = ((palettes >> ((x & 1) * 4)) & 0xF)
pal_idx += (sprite.palette_number << 4) if pal_idx > 0
end
scanline[col] = pal_idx.to_u16 + 0x100 if pal_idx > 0 # 0x100 vs 0x200 because palette is read as 16 bit
end
end
end
end
def render_affine(scanline : Slice(UInt16), row : Int, bg : Int) : Nil
return unless bit?(@dispcnt.value, 8 + bg)
pa, pb, pc, pd = @bgaff[bg - 2].map { |p| p.value.to_i16!.to_i32! }
dx, dy = @bgref[bg - 2].map { |p| (p.value << 4).to_i32! >> 4 }
size = 16 << @bgcnt[bg].screen_size # tiles, always a square
size_pixels = size << 3
screen_base = 0x800_u32 * @bgcnt[bg].screen_base_block
character_base = @bgcnt[bg].character_base_block.to_u32 * 0x4000
240.times do |col|
next if scanline[col] > 0
x = ((pa * col + pb * row) + dx) >> 8
y = ((pc * col + pd * row) + dy) >> 8
if @bgcnt[bg].affine_wrap
x %= size_pixels
y %= size_pixels
end
next unless 0 <= x < size_pixels && 0 <= y < size_pixels
screen_entry = @vram[screen_base + (y >> 3) * size + (x >> 3)].to_u16
tile_id = bits(screen_entry, 0..9)
y = (y & 7) ^ (7 * (screen_entry >> 11 & 1))
x = (x & 7) ^ (7 * (screen_entry >> 10 & 1))
pal_idx = @vram[character_base + tile_id * 0x40 + y * 8 + x]
scanline[col] = pal_idx.to_u16
end
end
def read_io(io_addr : Int) : Byte
case io_addr
when 0x000..0x001 then @dispcnt.read_byte(io_addr & 1)