Sprite Limitation/Correct Sprite Priorities

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Sprite Limitation/Correct Sprite Priorities
by on (#4567)
I am a little confused with the 8 sprite limit per scanline rule.

If sprites 57-63 are drawn in the first 10 pixels of a scanline and sprite 5 is drawn at 100 pixels in and sprite 6 is drawn at 200 pixels then which sprite is drawn? 5 or 6? i.e. does the limit go from sprite 63.0 or what is drawn from left to right on a scanline?

Also, if sprite 10 (bg sprite) is drawn on pixel 123 of a scanline and sprite 11 (fg sprite) is drawn on the same pixel then the latter wont get drawn. Do any games exist so that we can test this?
Re: Sprite Limitation/Correct Sprite Priorities
by on (#4569)
james123 wrote:
If sprites 57-63 are drawn in the first 10 pixels of a scanline and sprite 5 is drawn at 100 pixels in and sprite 6 is drawn at 200 pixels then which sprite is drawn? 5 or 6?


Both - in that setup, you will see sprites 5, 6, 57, 58, 59, 60, 61, and 62. X position has nothing to do with sprite priority, since it doesn't even get checked until after the PPU has chosen its 8 sprites to render (based entirely on Y coordinates, choosing the first 8 sprites it finds starting at sprite #0 and ending at sprite #63).

Quote:
Also, if sprite 10 (bg sprite) is drawn on pixel 123 of a scanline and sprite 11 (fg sprite) is drawn on the same pixel then the latter wont get drawn. Do any games exist so that we can test this?

Yes - Super Mario Bros. 3 uses this exact trick whenever an item comes out of a [?] block or whenever something enters or exits a pipe (either Mario or an enemy).

by on (#4570)
Thanks for the second one, but WedNESday can only run NROM games at the moment...

What i mean by the first one is if sprites 64,64,63,62,61,60,59 are drawn at the start of a scanline, and sprite 57 is drawn at pixel 100 and sprite 58 is drawn at pixel 200 BOTH can't be drawn because that would be 9 sprites. What I mean is does the NES increment from sprite 64th...1st or increment when it finds a sprite on a scanline? (kinda hard to explain)

by on (#4572)
The LOWEST numbered sprites are the HIGHEST priority - this means that if all 64 sprites are on the first scanline, you will only see sprites 0, 1, 2, 3, 4, 5, 6, and 7 (SPR-RAM $00-1F).

by on (#4573)
I'm pretty sure Castlevania uses this "lower-numbered sprite behind the background to mask a sprite in front" technique when you walk through the door at the beginning and when a hidden money bag ascends from the floor (the first of which is hidden within a few hundred pixels of the door mentioned above). These didn't appear correct in my emulator until I implemented the "background pushed in front of front sprites" behavior you're referring to.

As far as sprites per scanline, it's really very simple. For each scanline, scan sprites from 0 to 63, in that order. Examine each sprite's Y coordinate in combination with the global sprite height (8 or 16) to determine whether any of its lines fall on this scanline. Once eight sprites are found for the scanline (or all sprites have been examined), stop and draw them.

by on (#4607)
blargg wrote:
I'm pretty sure Castlevania uses this "lower-numbered sprite behind the background to mask a sprite in front" technique when you walk through the door at the beginning and when a hidden money bag ascends from the floor

I'm absolutely sure, myself. It's a shame that games such as Just Breed didn't use this techniques to allow the player to pass behind trees, chimneys, etc...