| 1 |
void PPU::render_objects() { |
| 2 |
if(regs.control.enable[OBJ] == false) return; |
| 3 |
for(unsigned n = 0; n < 128; n++) render_object(object[n]); |
| 4 |
} |
| 5 |
|
| 6 |
//px,py = pixel coordinates within sprite [0,0 - width,height) |
| 7 |
//fx,fy = affine pixel coordinates |
| 8 |
//pa,pb,pc,pd = affine pixel adjustments |
| 9 |
//x,y = adjusted coordinates within sprite (linear = vflip/hflip, affine = rotation/zoom) |
| 10 |
void PPU::render_object(Object& obj) { |
| 11 |
uint8 py = regs.vcounter - obj.y; |
| 12 |
if(obj.affine == 0 && obj.affinesize == 1) return; //hidden |
| 13 |
if(py >= obj.height << obj.affinesize) return; //offscreen |
| 14 |
|
| 15 |
auto& output = layer[OBJ]; |
| 16 |
unsigned rowsize = regs.control.objmapping == 0 ? 32 >> obj.colors : obj.width / 8; |
| 17 |
unsigned baseaddr = obj.character * 32; |
| 18 |
|
| 19 |
if(obj.mosaic && regs.mosaic.objvsize) { |
| 20 |
signed mosaicy = (regs.vcounter / (1 + regs.mosaic.objvsize)) * (1 + regs.mosaic.objvsize); |
| 21 |
py = obj.y >= 160 || mosaicy - obj.y >= 0 ? mosaicy - obj.y : 0; |
| 22 |
} |
| 23 |
|
| 24 |
int16 pa = objectparam[obj.affineparam].pa; |
| 25 |
int16 pb = objectparam[obj.affineparam].pb; |
| 26 |
int16 pc = objectparam[obj.affineparam].pc; |
| 27 |
int16 pd = objectparam[obj.affineparam].pd; |
| 28 |
|
| 29 |
//center-of-sprite coordinates |
| 30 |
int16 centerx = obj.width / 2; |
| 31 |
int16 centery = obj.height / 2; |
| 32 |
|
| 33 |
//origin coordinates (top-left of sprite) |
| 34 |
int28 originx = -(centerx << obj.affinesize); |
| 35 |
int28 originy = -(centery << obj.affinesize) + py; |
| 36 |
|
| 37 |
//fractional pixel coordinates |
| 38 |
int28 fx = originx * pa + originy * pb; |
| 39 |
int28 fy = originx * pc + originy * pd; |
| 40 |
|
| 41 |
for(unsigned px = 0; px < (obj.width << obj.affinesize); px++) { |
| 42 |
unsigned x, y; |
| 43 |
if(obj.affine == 0) { |
| 44 |
x = px; |
| 45 |
y = py; |
| 46 |
if(obj.hflip) x ^= obj.width - 1; |
| 47 |
if(obj.vflip) y ^= obj.height - 1; |
| 48 |
} else { |
| 49 |
x = (fx >> 8) + centerx; |
| 50 |
y = (fy >> 8) + centery; |
| 51 |
} |
| 52 |
|
| 53 |
uint9 ox = obj.x + px; |
| 54 |
if(ox < 240 && x < obj.width && y < obj.height) { |
| 55 |
unsigned offset = (y / 8) * rowsize + (x / 8); |
| 56 |
offset = offset * 64 + (y & 7) * 8 + (x & 7); |
| 57 |
|
| 58 |
uint8 color = object_vram_read(baseaddr + (offset >> !obj.colors)); |
| 59 |
if(obj.colors == 0) color = (x & 1) ? color >> 4 : color & 15; |
| 60 |
if(color) { |
| 61 |
if(obj.mode & 2) { |
| 62 |
windowmask[Obj][ox] = true; |
| 63 |
} else if(output[ox].enable == false || obj.priority < output[ox].priority) { |
| 64 |
if(obj.colors == 0) color = obj.palette * 16 + color; |
| 65 |
output[ox].write(true, obj.priority, pram[256 + color], obj.mode == 1, obj.mosaic); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
fx += pa; |
| 71 |
fy += pc; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
uint8 PPU::object_vram_read(unsigned addr) const { |
| 76 |
if(regs.control.bgmode == 3 || regs.control.bgmode == 4 || regs.control.bgmode == 5) { |
| 77 |
if(addr <= 0x3fff) return 0u; |
| 78 |
} |
| 79 |
return vram[0x10000 + (addr & 0x7fff)]; |
| 80 |
} |