basic windowing, passing tonc window demo

This commit is contained in:
Matthew Berry 2021-01-31 22:57:38 -08:00
parent 88405ff36c
commit 686e53ed73

View file

@ -285,40 +285,56 @@ class PPU
end end
def calculate_color(col : Int) : UInt16 def calculate_color(col : Int) : UInt16
enables, effects = if @dispcnt.window_0_display && @win0h.x1 <= col < @win0h.x2 && @win0v.y1 <= @vcount < @win0v.y2 # win0
{bits(@winin.value, 0..4), @winin.window_0_color_special_effect}
elsif @dispcnt.window_1_display && @win1h.x1 <= col < @win1h.x2 && @win1v.y1 <= @vcount < @win1v.y2 # win1
{bits(@winin.value, 8..12), @winin.window_1_color_special_effect}
elsif @dispcnt.obj_window_display && @sprite_pixels[col].window # obj win
{bits(@winout.value, 8..12), @winout.obj_window_color_special_effect}
elsif @dispcnt.window_0_display || @dispcnt.window_1_display || @dispcnt.obj_window_display # winout
{bits(@winout.value, 0..4), @winout.outside_color_special_effect}
else # no windows
{bits(@dispcnt.value, 8..12), true}
end
top_color = nil top_color = nil
4.times do |priority| 4.times do |priority|
sprite_pixel = @sprite_pixels[col] if bit?(enables, 4)
if sprite_pixel.priority == priority sprite_pixel = @sprite_pixels[col]
if sprite_pixel.palette > 0 # todo: abstract out this duplicated work if sprite_pixel.priority == priority
selected_color = (@pram + 0x200).to_unsafe.as(UInt16*)[sprite_pixel.palette] if sprite_pixel.palette > 0 # todo: abstract out this duplicated work
if top_color.nil? # todo: adding the ability to blend sprites _under_ bg broke tonc's bld_demo selected_color = (@pram + 0x200).to_unsafe.as(UInt16*)[sprite_pixel.palette]
top_color = selected_color if top_color.nil? # todo: adding the ability to blend sprites _under_ bg broke tonc's bld_demo
return top_color unless sprite_pixel.blends && @bldcnt.is_bg_target(4, target: 1) top_color = selected_color
else return top_color unless sprite_pixel.blends && @bldcnt.is_bg_target(4, target: 1)
if @bldcnt.is_bg_target(4, target: 2)
color = BGR16.new(top_color) * (Math.min(16, @bldalpha.eva_coefficient) / 16) + BGR16.new(selected_color) * (Math.min(16, @bldalpha.evb_coefficient) / 16)
return color.value
else else
return top_color if @bldcnt.is_bg_target(4, target: 2)
color = BGR16.new(top_color) * (Math.min(16, @bldalpha.eva_coefficient) / 16) + BGR16.new(selected_color) * (Math.min(16, @bldalpha.evb_coefficient) / 16)
return color.value
else
return top_color
end
end end
end end
end end
end end
4.times do |bg| 4.times do |bg|
if @bgcnt[bg].priority == priority if bit?(enables, bg)
palette = @layer_palettes[bg][col] if @bgcnt[bg].priority == priority
next if palette == 0 palette = @layer_palettes[bg][col]
selected_color = @pram.to_unsafe.as(UInt16*)[palette] next if palette == 0
if top_color.nil? selected_color = @pram.to_unsafe.as(UInt16*)[palette]
top_color = selected_color if top_color.nil?
# todo: brightness increase/decrease top_color = selected_color
return top_color if @bldcnt.color_special_effect == 0 || !@bldcnt.is_bg_target(bg, target: 1) # no color effect # todo: brightness increase/decrease
else return top_color if @bldcnt.color_special_effect == 0 || !@bldcnt.is_bg_target(bg, target: 1) # no color effect
if @bldcnt.is_bg_target(bg, target: 2) else
color = BGR16.new(top_color) * (Math.min(16, @bldalpha.eva_coefficient) / 16) + BGR16.new(selected_color) * (Math.min(16, @bldalpha.evb_coefficient) / 16) if @bldcnt.is_bg_target(bg, target: 2)
return color.value color = BGR16.new(top_color) * (Math.min(16, @bldalpha.eva_coefficient) / 16) + BGR16.new(selected_color) * (Math.min(16, @bldalpha.evb_coefficient) / 16)
else # second layer isn't set in bldcnt, don't blend return color.value
return top_color else # second layer isn't set in bldcnt, don't blend
return top_color
end
end end
end end
end end