From 58db1e8d1dff8ed7e2ba843384c316291ae85c44 Mon Sep 17 00:00:00 2001 From: Keith Leonardo Date: Sun, 26 Nov 2023 22:46:35 +1100 Subject: [PATCH] Detect sprite 0 hit --- src/ppu.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ppu.c b/src/ppu.c index 164b875..78cec5e 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -553,17 +553,29 @@ void draw_dot_ppu(ppu_t *ppu) { unsigned char sp_order = 0xFF; for (int i = 0; i < ppu->sprite_count_latch; i++) { + unsigned char sprite_index = ppu->sprite_indices[i]; + unsigned char sprite_attr = ppu->sprite_latches[i]; + if (ppu->sprite_counters[i] == 0) { bool sp_pt0 = ppu->sprite_shift[i * 2] & 0x100; bool sp_pt1 = ppu->sprite_shift[i * 2 + 1] & 0x100; unsigned char sp_color_tmp = sp_pt0 | (sp_pt1 << 1); + // Detect sprite 0 hit + bool collision = bg_color && sp_color_tmp; + bool clip_left = ppu->mask & (PPU_MASK_SHOW_BG_LEFT | + PPU_MASK_SHOW_SPRITES_LEFT); + if (sprite_index == 0 && collision && ppu->dot != 255 && + !(ppu->dot <= 7 && clip_left)) { + ppu->status |= PPU_STATUS_S0_HIT; + } + // Update palette index depending on pixel priority - if (ppu->sprite_indices[i] <= sp_order && sp_color_tmp) { - sp_palette = (ppu->sprite_latches[i] & 0x3) + 4; + if (sprite_index <= sp_order && sp_color_tmp) { + sp_order = sprite_index; + sp_palette = (sprite_attr & 0x3) + 4; sp_color = sp_color_tmp; - sp_behind_bg = ppu->sprite_latches[i] & 0x20; - sp_order = ppu->sprite_indices[i]; + sp_behind_bg = sprite_attr & 0x20; } } }