Thursday, November 30, 2017

VDP sprite masking

Particularly astute readers will notice there's something off about my previous post. If the Super gumball is a sprite, and it's being displayed behind the level blocks...


...then that means the level blocks must have their priority bit set. If that's the case, though, then the only way to display other sprites over them is to set their priority bit as well, which causes them to be displayed over all the level blocks:


So how do we get around this? It's time to break out the big guns.

Remember how the VDP implements the sprite attribute table as a linked list of sprite data? There's an obscure feature where if, in the process of rendering a row of pixels, the VDP encounters a sprite whose X coordinate is exactly zero, it will not render any sprites that appear further down the list.


The trick is to push the player sprites and the machine's handle into the table first, then push a garbage sprite with an X coordinate of zero, and finally push the gumball sprites which make up the contents of the machine. Coordinate zero is far left of the visible region of the screen, so the garbage sprite is never actually drawn, but it masks the gumball sprites because they appear further down the list.


The same trick is used in Sonic 2's title screen animation. When Sonic and Tails rise from inside the wing emblem, their sprites are actually fully constructed from the start and are simply moved up in order to gradually reveal them. A couple of garbage sprites at X coordinate zero prevent them from peeking out from the underside of the emblem.


Sonic 1's title screen behaves very similarly, but without actually using the sprite masking trick. Instead, it just places an absurd amount of garbage sprites immediately off screen, causing the sprite limit to kick in early, which produces a nigh indistinguishable effect.

2 comments:

  1. Sure enough, I mentioned the gumball graphics not workly properly on certain Flash-based online emulators, and I get this. Cool.

    ReplyDelete
    Replies
    1. It's particularly uncommon to find this feature properly emulated, because apparently even SEGA's own VDP documentation did not accurately describe the conditions which produce the sprite masking effect.

      Delete