fix: resolve relative sprite priority issues

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:27 -03:00
parent d7354cca33
commit 74cda6a1d0
1 changed files with 17 additions and 18 deletions

View File

@ -121,37 +121,36 @@ pub const Ppu = struct {
/// Draw all relevant sprites on a scanline /// Draw all relevant sprites on a scanline
fn drawSprites(self: *Self, prio: u2) void { fn drawSprites(self: *Self, prio: u2) void {
// Object VRAM 3rd and 4th (0-indexed) charblocks
const char_base = 0x4000 * 4; const char_base = 0x4000 * 4;
const y = @bitCast(i8, self.vcount.scanline.read()); const y = @bitCast(i8, self.vcount.scanline.read());
// Loop over every fetched sprite
sprite_loop: for (self.scanline_sprites) |maybe_sprites| {
if (maybe_sprites) |sprite| {
// Move on to the next sprite If its of a different priority
if (sprite.priority() != prio) continue :sprite_loop;
var i: u9 = 0; var i: u9 = 0;
while (i < width) : (i += 1) { px_loop: while (i < sprite.width) : (i += 1) {
// Exit early if a pixel is already here const x = (sprite.x() +% i) % 240;
if (self.scanline_buf[i] != null) continue;
const x = i;
for (self.scanline_sprites) |maybe_sprite| {
if (maybe_sprite) |sprite| {
if (sprite.priority() != prio) continue;
const ix = @bitCast(i9, x); const ix = @bitCast(i9, x);
// If We've already rendered a pixel here don't overwrite it
if (self.scanline_buf[x] != null) continue :px_loop;
const start = sprite.x(); const start = sprite.x();
const istart = @bitCast(i9, start); const istart = @bitCast(i9, start);
const end = start +% sprite.width; const end = start +% sprite.width;
const iend = @bitCast(i9, end); const iend = @bitCast(i9, end);
// Sprites are expected to wrap, by performing the same check on both // By comparing with both signed and unsigned values we ensure that sprites
// signed and unsigned values we ensure that sprites are properly displayed // are displayed in all valid (AFAIK) configuration
// in all valid scenarios
if ((start <= x and x < end) or (istart <= ix and ix < iend)) { if ((start <= x and x < end) or (istart <= ix and ix < iend)) {
self.drawSpritePixel(char_base, sprite, ix, y); self.drawSpritePixel(char_base, sprite, ix, y);
} }
} else break;
} }
} else break;
} }
} }
@ -275,12 +274,12 @@ pub const Ppu = struct {
0x0 => { 0x0 => {
const start = framebuf_pitch * @as(usize, scanline); const start = framebuf_pitch * @as(usize, scanline);
self.fetchSprites(); if (obj_enable) self.fetchSprites();
var i: usize = 0; var i: usize = 0;
while (i < 4) : (i += 1) { while (i < 4) : (i += 1) {
// Draw Sprites Here // Draw Sprites Here
if (obj_enable) self.drawSprites(@truncate(u2, i)); self.drawSprites(@truncate(u2, i));
if (i == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0); if (i == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0);
if (i == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1); if (i == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1);
if (i == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackround(2); if (i == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackround(2);