"""Composing one line: windows, layer priority, mosaic, blending, forced blank and green swap. For each dot the compositor picks the two front-most visible layers (BGs by priority then number, OBJs in front of a BG of equal priority, the backdrop behind everything), then applies the colour effect selected by BLDCNT if the dot's window allows it. A semi-transparent OBJ blends with the layer behind it whenever that layer is a second target, whatever BLDCNT's mode and the window say (GBATEK "LCD I/O Color Special Effects"). The arithmetic follows NanoBoyAdvance's ``merge.cpp`` (emulator-derived): blending works on 6-bit green, whose low bit is the colour's bit 15, and rounds (C14 in docs/undocumented-behavior.md). GBATEK states the formulas without the rounding. """ from __future__ import annotations from gba_python.ppu.background import DIRECT from gba_python.ppu.objects import ObjLine OBJ_LAYER, BACKDROP_LAYER = 4, 5 # BLDCNT/WININ bit numbers; BGs are 0-3 EFFECT_BIT = 0x20 # WININ/WINOUT bit 5: colour effects allowed in this window def _split(color: int) -> tuple[int, int, int]: """Red 0-31, green 0-63 (the low bit is bit 15), blue 0-31.""" return color & 31, (color >> 4 & 62) | (color >> 15 & 1), color >> 10 & 31 def blend(top: int, below: int, eva: int, evb: int) -> int: eva, evb = min(16, eva), min(16, evb) r1, g1, b1 = _split(top) r2, g2, b2 = _split(below) r = min((r1 * eva + r2 * evb + 8) >> 4, 31) g = min((g1 * eva + g2 * evb + 8) >> 4, 63) >> 1 b = min((b1 * eva + b2 * evb + 8) >> 4, 31) return b << 10 | g << 5 | r def brighten(color: int, evy: int) -> int: evy = min(16, evy) r, g, b = _split(color) r += ((31 - r) * evy + 8) >> 4 g += ((63 - g) * evy + 8) >> 4 b += ((31 - b) * evy + 8) >> 4 return b << 10 | (g >> 1) << 5 | r def darken(color: int, evy: int) -> int: evy = min(16, evy) r, g, b = _split(color) r -= (r * evy + 7) >> 4 g -= (g * evy + 7) >> 4 b -= (b * evy + 7) >> 4 return b << 10 | (g >> 1) << 5 | r def compose( *, palette: bytearray, layers: list[tuple[int, int, bool, list[int]]], objects: ObjLine | None, window_masks: list[int] | None, bldcnt: int, bldalpha: int, bldy: int, mosaic: int, out: list[int], ) -> None: """Write 240 colours (BGR555) into ``out``. ``layers`` holds the enabled BGs front to back as (number, priority, mosaic, pixels); ``objects`` is the OBJ layer if it is shown; ``window_masks`` gives each dot's six layer and effect bits (WININ/WINOUT) when any window is on, else None. """ effect = bldcnt >> 6 & 3 # 0 none, 1 alpha blend, 2 brighten, 3 darken first_targets = bldcnt & 0x3F second_targets = bldcnt >> 8 & 0x3F eva, evb, evy = bldalpha & 31, bldalpha >> 8 & 31, bldy & 31 bg_mosaic_x = (mosaic & 15) + 1 obj_mosaic_x = (mosaic >> 8 & 15) + 1 def color_of(value: int, layer: int) -> int: if value & DIRECT: return value & 0xFFFF address = (value + 256 if layer == OBJ_LAYER else value) << 1 return palette[address] | palette[address + 1] << 8 # The OBJ mosaic holds a dot's OBJ pixel across the mosaic block, but a pixel that is not # mosaic, or is in front of the held one, replaces it at once (hw-test/ppu/sprite-hmosaic). held_color = held_priority = 0 held_alpha = held_mosaic = False for x in range(240): enable = window_masks[x] if window_masks is not None else 0x3F top_layer = bottom_layer = BACKDROP_LAYER top_value = bottom_value = 0 top_priority = bottom_priority = 3 found = 0 bg_x = x - x % bg_mosaic_x for number, priority, bg_mosaic, pixels in layers: if not enable >> number & 1: continue value = pixels[bg_x if bg_mosaic else x] if not value: continue if found == 0: top_layer, top_value, top_priority = number, value, priority else: bottom_layer, bottom_value, bottom_priority = number, value, priority found += 1 if found == 2: break alpha = False if objects is not None: color = objects.color[x] priority = objects.priority[x] mosaic_here = objects.mosaic[x] if ( not mosaic_here or not held_mosaic or priority < held_priority or (x % obj_mosaic_x == 0) ): held_color, held_priority = color, priority held_alpha, held_mosaic = objects.alpha[x], mosaic_here if held_color and enable >> OBJ_LAYER & 1: if held_priority <= top_priority: bottom_layer, bottom_value = top_layer, top_value top_layer, top_value = OBJ_LAYER, held_color alpha = held_alpha elif held_priority <= bottom_priority: bottom_layer, bottom_value = OBJ_LAYER, held_color top = color_of(top_value, top_layer) below_is_target = second_targets >> bottom_layer & 1 if alpha and below_is_target: top = blend(top, color_of(bottom_value, bottom_layer), eva, evb) elif enable & EFFECT_BIT and effect and first_targets >> top_layer & 1: if effect == 1: if below_is_target: top = blend(top, color_of(bottom_value, bottom_layer), eva, evb) elif effect == 2: top = brighten(top, evy) else: top = darken(top, evy) out[x] = top & 0x7FFF