My piece of metasprite code:
Code:
; Metasprite addon lib for PPU.s
; (c) 2014-2015 Peter Santing
;
;
; OAM is always 0x0500 in my engine so place meta sprites there.
; also NEVER TOUCH 0x00 (SPRITE #0)
WRITEBUF EQU $C ; zeropage address counters.
XBUF EQU $D;
add_meta_sprite: ; ($00) = holds metasprite addr.
; $02 = x pos, $03 = y pos.
; meta data is as follows. x, y, tile, bgcolor and attr
; sprite is as follows: y, tile, attr, x
ldx WRITEBUF
ldy #0
- lda $02 ; load x pos.
clc
adc ($00), Y ; add meta x pos.
sta XBUF
iny
lda $03 ; load y pos
clc
adc ($00), Y ; add meta y pos.
sta ($0500), X ; save byte Y
inx
iny
lda ($00), Y ; load tile number.
sta ($0500), x ; save tile number.
inx
iny
lda ($00), Y ; load attribute.
sta ($0500), X ; save attribute.
inx
lda XBUF
sta ($0500), X ; save x position.
iny
inx
lda ($00), Y ; load x pos?
cmp #128 ; if x = 128
bne - ; then end loop
stx WRITEBUF
rts
prep_write_meta_sprites:
lda #$4 ; always ignore spr #0 (hard set for scrollbars, etc)
sta WRITEBUF
rts
done_write_meta_sprites:
lda #0
ldx WRITEBUF
inx ; goto tile.
- sta ($0500), X
inx
inx
inx
inx
cpx #$01
bne -
rts
How a meta-sprite table is built:
Code:
spr0:
.db -8, -16, 1, 2, 0, -16, 2, 2, -8, -8, $11, 2
.db 0, -8, $12, 2, -8, 0, $21, 2, 0, 0, $22, 2, 0, 8, $32, 2, 128
the table works as follows: delta-x, delta-y, sprite-tile, attributes&color. thus SPR0 is like:
sprite 1: x=-8, y=-16, tile=1, attributes&color =2
sprite 2: x=0, y=-16, tile=2, attributes&color = 2
sprite 3, x=-8, y=-8, tile=$11, attributes&color = 2
........
sprite 7, x=0, y=8, tile=$32, attributes&color= = 2
sprite_end_marker = 128
to render the meta sprite use the following code:
Code:
jsr prep_write_meta_sprites
lda #<spr0
sta $00
iny
lda #>spr0
sta $01
lda PLAYERX
sta $02
lda PLAYERY
sta $03
jsr add_meta_sprite
jsr done_write_meta_sprites
then DMA $500 to your OAM buffer.