Attributes
Sprite Cycling

NES Programming Tutorial

Published 6/4/2014 - Download Example ROM at Bottom

This tutorial is kind of a side note to the others. It just shows how to do sprite cycling and better attribute handling.

This is fairly involved, so make sure you go slowly and think about stuff. If the position stuff doesn’t make sense, it won’t end well for you.

First, when you have more than 8 sprites per scanline, some will dissapear. This is pretty simple to fix. You just flip-flop the order in which they are drawn every frame…keeping in mind that ones which are stored lower in RAM are drawn first (i.e. one at $0204 is drawn on top of $0208). So, by flipping the order they are stored in RAM, we can make them flicker so they won’t dissapear completely.

Start by reserving the set $0700 to be your working area for your sprites. Keep the set $0200 (or whatever) for your DMA memory. Then simply copy them “forward” one frame and then “backwards” the next…like so:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Sprite_Cycling:

LDA Sprite_Rotation
BNE .Backwards

.loop1
LDA sprite_RAM,x ;copy forwards
STA sprite_DMA,x
INX
BNE .loop1

INC Sprite_Rotation
RTS

.Backwards
LDY #$FC
.loop2 ;copy backwards
LDA sprite_RAM,x
STA sprite_DMA,y
INX
INY
LDA sprite_RAM,x
STA sprite_DMA,y
INX
INY
LDA sprite_RAM,x
STA sprite_DMA,y
INX
INY
LDA sprite_RAM,x
STA sprite_DMA,y
TYA
SEC
SBC #$08
TAY
INY
INX
BNE .loop2

LDA #$00
STA Sprite_Rotation

RTS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Pretty simple. You can cycle them all (like here) or just some (if you have a sprite zero hit, you don’t want that bitch moving around). Or you can make it so your PC is always on top.

Next, we will look at how to better handle attributes, at least in my opinion. If you figure this out, you’ll never have to fuck with them again! It will be automatic.

So, first we need to modify our metatiles.

MetaTileSeta00
.db $44,$49,$5F,$7A,%00000000,%00001111

The first 4 bytes are the tiles, like always.
The sixth byte is the collision data, like always.
The fifth byte is the attribute data for that particular metatile. If you want it to draw from pallet “01” you’d use “%01010101”, pallet “11”, “%11111111”, and so on.

Do this for all of them. Note that if you have two tiles that use the same bg tiles but different colors, you’ll have to make a duplicate.

It’s probably better that you look through the routine as you read. It’s pretty simple once you figure it out.

Next, we need to look at our position in the background data table. If you think in hex, you always have $10 meta tiles across the screen. So, row 0 would be $00-$0F, row 1 is $10-$1F, 2 is $20-$2F.

So, the attribute bytes are 2 metatiles across. This means that for each row, we just divide by 2 to get the position in the table…we hope that the NES has a convenient command for this. Just use LSR A!!

Next, there is the same thing with the vertical. Row 0 and 1 use the first 8 attribute bytes, 2 and 3 use the second, and so on. So, to figure out a row, we AND the room table counter with %11100000 and LSR A twice…I’ll leave it to you to work this out.

The combo of these two steps will produce your position within the attribute table.

Next, you simply use the table position to work out which quadrant you are within in the attribute byte, and Bob’s your uncle.

I kinda glazed over this finding the position thing. So if you can’t figure it out, I can delve into it further. But it is important that you work it out and understand what it’s doing. Take a pen and paper, draw out a screen, label each metatile (in hex), and manually decode the routine to see what it’s doing. It will click and life will be titties and rainbows.

I hope you enjoyed this. Post questions!! See below for the program.

Download Example Stuff