Skip to content

Commit

Permalink
Detect sprite 0 hit
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed Nov 26, 2023
1 parent 83ab1d7 commit 58db1e8
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/ppu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 58db1e8

Please sign in to comment.