public function drawSpritesForLine($line)
{
if (!$this->gfxSpriteShow) {
return;
}
$minSpriteY = $line - ($this->gfxSpriteDouble ? 15 : 7);
// either only do priorityFlag == 0 (all foreground),
// or first 0x80 (background) and then 0 (foreground)
$priorityFlag = $this->spritePriorityEnabled ? 0x80 : 0;
for (; $priorityFlag >= 0; $priorityFlag -= 0x80) {
$oamIx = 159;
while ($oamIx >= 0) {
$attributes = 0xff & $this->memory[0xfe00 + $oamIx--];
if (($attributes & 0x80) == $priorityFlag || !$this->spritePriorityEnabled) {
$tileNum = 0xff & $this->memory[0xfe00 + $oamIx--];
$spriteX = (0xff & $this->memory[0xfe00 + $oamIx--]) - 8;
$spriteY = (0xff & $this->memory[0xfe00 + $oamIx--]) - 16;
$offset = $line - $spriteY;
if ($spriteX >= 160 || $spriteY < $minSpriteY || $offset < 0) {
continue;
}
if ($this->gfxSpriteDouble) {
$tileNum = $tileNum & 0xfe;
}
$spriteAttrib = $attributes >> 5 & 0x3;
// flipx: from bit 0x20 to 0x01, flipy: from bit 0x40 to 0x02
if ($this->cGBC) {
$spriteAttrib += 0x20 + (($attributes & 0x7) << 2);
// palette
$tileNum += (384 >> 3) * ($attributes & 0x8);
// tile vram bank
} else {
// attributes 0x10: 0x00 = OBJ1 palette, 0x10 = OBJ2 palette
// spriteAttrib: 0x04: OBJ1 palette, 0x08: OBJ2 palette
$spriteAttrib += 0x4 + (($attributes & 0x10) >> 2);
}
if ($priorityFlag == 0x80) {
// background
if ($this->gfxSpriteDouble) {
if (($spriteAttrib & 2) != 0) {
$this->drawPartBgSprite(($tileNum | 1) - ($offset >> 3), $spriteX, $line, $offset & 7, $spriteAttrib);
} else {
$this->drawPartBgSprite(($tileNum & -2) + ($offset >> 3), $spriteX, $line, $offset & 7, $spriteAttrib);
}
} else {
$this->drawPartBgSprite($tileNum, $spriteX, $line, $offset, $spriteAttrib);
}
} else {
// foreground
if ($this->gfxSpriteDouble) {
if (($spriteAttrib & 2) != 0) {
$this->drawPartFgSprite(($tileNum | 1) - ($offset >> 3), $spriteX, $line, $offset & 7, $spriteAttrib);
} else {
$this->drawPartFgSprite(($tileNum & -2) + ($offset >> 3), $spriteX, $line, $offset & 7, $spriteAttrib);
}
} else {
$this->drawPartFgSprite($tileNum, $spriteX, $line, $offset, $spriteAttrib);
}
}
} else {
$oamIx -= 3;
}
}
}
}