Sprite Loop Macro [4.5.9]

crazygrouptrio

Active member
This is a simple macro I came up with to draw sprites to resemble an object, if only to make my life easier. So figured I would share. It's easy to use but requires some setup for code around it, mostly for the sake of space.

First of all, here is the macro. Just copy and paste into a new file and save it in your macros folder.
Code:
MACRO SpriteLoop arg0, arg1
; arg0 = how wide is it, including 0
; arg1 = how many tiles is it, including 0

LDA #$00
STA temp
LDA tempA
STA temp1

doSpriteDrawLoop:
JSR +drawTheSprite
LDA temp
CMP arg0
BEQ +nextLine
CMP arg1
BEQ +done

LDA tempA
CLC
ADC #$08
STA tempA

INC tempC
INC temp
JMP doSpriteDrawLoop

+nextLine
LDA temp1
STA tempA
LDA tempB
CLC
ADC #$08
STA tempB
INC tempC
INC temp
JMP doSpriteDrawLoop

+done
ENDM

Use the arg0 to set how wide your sprite is (including 0, so the total sprite tiles minus 1) and arg1 for how many sprite tiles it is total (again including 0)

Now this macro is dependent on a few things.
1. Sprites need to be set up in order from left to right on your sprite sheet, regardless of how many tiles tall it is. Here's mine for an example.
tutorial3.png
It's 3 tiles wide so we set the first value to 2, and in total it's 6 sprites, so the second value is 5.

2. Before the macro in your script, setup the initial position, tile, palette for the sprites. Like so:
Code:
LDA #112 ; x coordinate for first sprite
STA tempA
LDA #80 ; y coordinate for first sprite
STA tempB
LDA #$40 ; first sprite location on sprite sheet
STA tempC
LDA #$00 ; palette for all sprites
STA tempD

SpriteLoop #$02, #$05
All values are just examples, and you should change them to wherever you want the sprite. It's worth noting using this all sprites will use the same palette.

3. Somewhere in the same script after your macros/other code, place the draw sprite macro, like this:
Code:
+drawTheSprite:
DrawSprite tempA, tempB, tempC, tempD
RTS
If you are using it in the static bank, the "+" may need to be removed from here and inside the macro. I just keep this bit of code at the bottom of the script where I'm drawing sprites.
The reason this is separate is to save on space. We JSR to this macro rather than use it in every instance of the SpriteLoop macro, which will save several lines of code.
tutorial4.png
Here is the end result, as you can see the sprites are placed where I want, drop down a line once they reach the end of the row and finish the other sprites.
Maybe this is a niche thing but it really helped me to simply put this into a macro, so hopefully is helpful for someone else. Enjoy!

Some caveats:
- All sprites will use the same palette.
- The result will always be square/rectangle. Unless the tile it's said to draw is blank.
 
Top Bottom