2021-10-21 13:50:07 +00:00
|
|
|
; Functions for sprie handling. Mostly maintains the sprite list and provides
|
|
|
|
; utility functions to calculate sprite/tile intersections
|
2021-08-25 14:38:02 +00:00
|
|
|
;
|
2021-10-21 13:50:07 +00:00
|
|
|
; The sprite plane actually covers two banks so that more than 32K can be used as a virtual
|
|
|
|
; screen buffer. In order to be able to draw sprites offscreen, the virtual screen must be
|
|
|
|
; wider and taller than the physical graphics screen.
|
|
|
|
;
|
|
|
|
; Initialize the sprite plane data and mask banks (all data = $0000, all masks = $FFFF)
|
2021-10-24 03:31:38 +00:00
|
|
|
InitSprites
|
2021-10-21 13:50:07 +00:00
|
|
|
ldx #$FFFE
|
|
|
|
lda #0
|
2021-10-24 03:31:38 +00:00
|
|
|
:loop1 stal spritedata,x
|
2021-10-21 13:50:07 +00:00
|
|
|
dex
|
|
|
|
dex
|
2021-10-29 02:52:48 +00:00
|
|
|
cpx #$FFFE
|
|
|
|
bne :loop1
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
ldx #$FFFE
|
|
|
|
lda #$FFFF
|
2021-10-24 03:31:38 +00:00
|
|
|
:loop2 stal spritemask,x
|
2021-10-21 13:50:07 +00:00
|
|
|
dex
|
|
|
|
dex
|
2021-10-29 02:52:48 +00:00
|
|
|
cpx #$FFFE
|
|
|
|
bne :loop2
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
; This function looks at the sprite list and renders the sprite plane data into the appropriate
|
|
|
|
; tiles in the code field
|
|
|
|
_RenderSprites
|
|
|
|
ldx #0
|
|
|
|
:loop lda _Sprites+SPRITE_STATUS,x
|
|
|
|
beq :out
|
2021-10-29 02:41:01 +00:00
|
|
|
; cmp #SPRITE_STATUS_DIRTY
|
|
|
|
; beq :render
|
|
|
|
bra :render
|
2021-10-21 13:50:07 +00:00
|
|
|
:next inx
|
|
|
|
inx
|
|
|
|
bra :loop
|
|
|
|
:out rts
|
|
|
|
|
2021-10-27 05:14:19 +00:00
|
|
|
; This is the complicated part; we need to draw the sprite into the sprite plane, but then
|
2021-10-21 13:50:07 +00:00
|
|
|
; calculate the code field tiles that this sprite potentially overlaps with and mark those
|
2021-10-27 05:14:19 +00:00
|
|
|
; tiles as dirty and store the appropriate sprite plane address that those tiles need to copy
|
|
|
|
; from.
|
2021-10-24 03:31:38 +00:00
|
|
|
:render
|
2021-10-29 02:41:01 +00:00
|
|
|
stx tmp0 ; stash the X register
|
2021-10-29 03:18:33 +00:00
|
|
|
|
|
|
|
txy
|
|
|
|
ldx _Sprites+OLD_VBUFF_ADDR,y
|
|
|
|
jsr _EraseTileSprite ; erase from the old position
|
|
|
|
|
|
|
|
ldx _Sprites+VBUFF_ADDR,y
|
|
|
|
lda _Sprites+TILE_DATA_OFFSET,y
|
|
|
|
tay
|
2021-10-21 13:50:07 +00:00
|
|
|
jsr _DrawTileSprite ; draw the sprite into the sprite plane
|
2021-10-29 03:18:33 +00:00
|
|
|
ldx tmp0
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
ldy #0 ; flags to mark if the sprite is aligned to the code field grid or not
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
lda _Sprites+SPRITE_X,x ; Will need some special handling for X < 0
|
2021-10-29 02:41:01 +00:00
|
|
|
sta tmp3
|
|
|
|
|
2021-10-21 13:50:07 +00:00
|
|
|
clc
|
|
|
|
adc StartXMod164
|
|
|
|
|
|
|
|
bit #$0003 ; If the botton bit are zero, then we're aligned
|
2021-10-29 02:41:01 +00:00
|
|
|
beq :aligned_x
|
|
|
|
ldy #4
|
|
|
|
:aligned_x
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
cmp #164
|
|
|
|
bcc *+5
|
|
|
|
sbc #164
|
|
|
|
lsr
|
|
|
|
lsr
|
2021-10-24 03:31:38 +00:00
|
|
|
pha ; Save the tile column
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
lda _Sprites+SPRITE_Y,x
|
2021-10-29 02:41:01 +00:00
|
|
|
sta tmp2
|
|
|
|
|
2021-10-21 13:50:07 +00:00
|
|
|
clc
|
|
|
|
adc StartYMod208
|
|
|
|
|
|
|
|
bit #$0007
|
2021-10-29 02:41:01 +00:00
|
|
|
beq :aligned_y
|
|
|
|
iny
|
|
|
|
iny
|
|
|
|
:aligned_y
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
cmp #208
|
|
|
|
bcc *+5
|
|
|
|
sbc #208
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
tyx ; stash the alignment in the x register for dispatch
|
|
|
|
jmp (:mark_dirty,x)
|
|
|
|
; :mark_dirty dw :corner,:column,:row,:square
|
|
|
|
:mark_dirty dw :corner,:corner,:corner,:corner
|
|
|
|
|
|
|
|
; Just mark the square with the sprite as dirty
|
|
|
|
:corner tay
|
|
|
|
plx
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
ldx tmp0
|
|
|
|
brl :next
|
|
|
|
|
|
|
|
; Mark the left column (x, y) and (x, y+1) as dirty
|
|
|
|
:column tay
|
|
|
|
plx
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
|
|
|
|
iny
|
|
|
|
cpy #26
|
|
|
|
bcc *+5
|
|
|
|
ldy #0
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #8
|
|
|
|
sta tmp2
|
|
|
|
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
ldx tmp0
|
|
|
|
brl :next
|
|
|
|
|
|
|
|
; Mark the top row (x, y) and (x+1, y) as dirty
|
|
|
|
:row tay
|
|
|
|
plx
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
|
|
|
|
inx
|
|
|
|
cpx #41
|
|
|
|
bcc *+5
|
|
|
|
ldx #0
|
|
|
|
lda tmp3
|
|
|
|
clc
|
|
|
|
adc #4
|
|
|
|
sta tmp3
|
|
|
|
|
|
|
|
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
ldx tmp0
|
|
|
|
brl :next
|
|
|
|
|
|
|
|
; Mark all four squares as dirty
|
|
|
|
:square tay
|
|
|
|
lda 1,s
|
|
|
|
tax
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
|
|
|
|
inx
|
|
|
|
cpx #41
|
|
|
|
bcc *+5
|
|
|
|
ldx #0
|
|
|
|
lda tmp3
|
|
|
|
clc
|
|
|
|
adc #4
|
|
|
|
sta tmp3
|
|
|
|
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
|
|
|
|
iny
|
|
|
|
cpy #26
|
|
|
|
bcc *+5
|
|
|
|
ldy #0
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #8
|
|
|
|
sta tmp2
|
|
|
|
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
|
|
|
|
plx
|
|
|
|
lda tmp3
|
|
|
|
sec
|
|
|
|
sbc #4
|
|
|
|
sta tmp3
|
|
|
|
|
|
|
|
jsr _MarkAsDirty
|
|
|
|
ldx tmp0
|
|
|
|
brl :next
|
|
|
|
|
|
|
|
_MarkAsDirty
|
|
|
|
phx
|
|
|
|
phy
|
2021-10-22 05:56:12 +00:00
|
|
|
|
2021-10-27 05:14:19 +00:00
|
|
|
jsr _GetTileStoreOffset ; Get the tile store value
|
|
|
|
jsr _PushDirtyTile ; Enqueue for processing (Returns offset in Y-register)
|
2021-10-24 03:31:38 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
lda TileStore+TS_SPRITE_FLAG,y ; If this tile has already been flagged on this frame, avoid recalculating the address
|
|
|
|
beq :early_out
|
|
|
|
|
2021-10-27 05:14:19 +00:00
|
|
|
lda #TILE_SPRITE_BIT ; Mark this tile as having a sprite, regardless of whether it was already enqueued
|
2021-10-24 03:31:38 +00:00
|
|
|
sta TileStore+TS_SPRITE_FLAG,y
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
jsr _SetSpriteAddr
|
2021-10-27 05:14:19 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
:early_out
|
|
|
|
ply
|
|
|
|
plx
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Set the TileStore+TS_SPRITE_ADDR for tile that a sprite is on.
|
|
|
|
;
|
2021-10-27 05:14:19 +00:00
|
|
|
; To calculate the sprite plane coordinate for this tile column. We really just have to compensate
|
|
|
|
; for the StartXMod164 mod 4 value, so the final value is (SPRITE_X + (StartXMod164 mod 4)) & 0xFFFC
|
|
|
|
; for the horizontal and (SPRITE_Y + (StartYMod208 mod 8)) & 0xFFF8
|
|
|
|
;
|
|
|
|
; The final address is (Y + NUM_BUFF_LINES) * 256 + X
|
2021-10-29 02:41:01 +00:00
|
|
|
;
|
|
|
|
; tmp2 = sprite Y coordinate
|
|
|
|
; tmp3 = sprite X coordinate
|
|
|
|
; Y = tile record index
|
|
|
|
_SetSpriteAddr
|
2021-10-27 05:14:19 +00:00
|
|
|
lda StartYMod208
|
|
|
|
and #$0007
|
|
|
|
clc
|
2021-10-29 02:41:01 +00:00
|
|
|
adc tmp2
|
2021-10-27 05:14:19 +00:00
|
|
|
and #$00F8
|
|
|
|
clc
|
|
|
|
adc #NUM_BUFF_LINES
|
|
|
|
xba
|
2021-10-29 02:41:01 +00:00
|
|
|
sta tmp4
|
|
|
|
|
2021-10-27 05:14:19 +00:00
|
|
|
lda StartXMod164
|
|
|
|
and #$0003
|
|
|
|
clc
|
2021-10-29 02:41:01 +00:00
|
|
|
adc tmp3
|
2021-10-27 05:14:19 +00:00
|
|
|
and #$00FC
|
|
|
|
clc
|
2021-10-29 02:41:01 +00:00
|
|
|
adc tmp4
|
2021-10-27 05:14:19 +00:00
|
|
|
sta TileStore+TS_SPRITE_ADDR,y
|
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
rts
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
; _GetTileAt
|
|
|
|
;
|
|
|
|
; Given a relative playfield coordinate [0, ScreenWidth), [0, ScreenHeight) return the
|
|
|
|
; X = horizontal point [0, ScreenTileWidth]
|
|
|
|
; Y = vertical point [0, ScreenTileHeight]
|
|
|
|
;
|
|
|
|
; Return
|
|
|
|
; C = 1, out of range
|
|
|
|
; C = 0, X = column, Y = row
|
|
|
|
_GetTileAt
|
|
|
|
cpx ScreenWidth
|
|
|
|
bcc *+3
|
|
|
|
rts
|
|
|
|
|
|
|
|
cpy ScreenHeight
|
|
|
|
bcc *+3
|
|
|
|
rts
|
|
|
|
|
|
|
|
tya ; carry is clear here
|
|
|
|
adc StartYMod208 ; This is the code field line that is at the top of the screen
|
|
|
|
cmp #208
|
|
|
|
bcc *+5
|
|
|
|
sbc #208
|
|
|
|
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
tay ; This is the code field row for this point
|
|
|
|
|
|
|
|
clc
|
|
|
|
txa
|
|
|
|
adc StartXMod164
|
|
|
|
cmp #164
|
|
|
|
bcc *+5
|
|
|
|
sbc #164
|
|
|
|
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
tax ; Could call _CopyBG0Tile with these arguments
|
|
|
|
|
|
|
|
clc
|
|
|
|
rts
|
|
|
|
|
|
|
|
; _DrawSprite
|
|
|
|
;
|
|
|
|
; Draw the sprites on the _Sprite list into the Sprite Plane data and mask buffers. This is using the
|
|
|
|
; tile data right now, but could be replaced with compiled sprite routines.
|
|
|
|
_DrawSprites
|
|
|
|
ldx #0
|
|
|
|
:loop lda _Sprites+SPRITE_STATUS,x
|
2021-10-29 02:41:01 +00:00
|
|
|
beq :out ; The first open slot is the end of the list
|
|
|
|
cmp #SPRITE_STATUS_DIRTY
|
|
|
|
bne :skip
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
phx
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
lda _Sprites+VBUFF_ADDR,x ; Load the address in the sprite plane
|
|
|
|
ldy _Sprites+TILE_DATA_OFFSET,x ; Load the address in the tile data bank
|
|
|
|
tax
|
2021-10-21 13:50:07 +00:00
|
|
|
jsr _DrawTileSprite
|
2021-10-29 02:41:01 +00:00
|
|
|
plx
|
|
|
|
:skip
|
|
|
|
inx
|
|
|
|
inx
|
2021-10-21 13:50:07 +00:00
|
|
|
bra :loop
|
2021-10-29 02:41:01 +00:00
|
|
|
:out rts
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
DrawTileSprite ENT
|
|
|
|
jsr _DrawTileSprite
|
|
|
|
rtl
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
_DrawTileSprite
|
2021-10-21 13:50:07 +00:00
|
|
|
phb
|
|
|
|
pea #^tiledata ; Set the bank to the tile data
|
|
|
|
plb
|
|
|
|
|
|
|
|
]line equ 0
|
|
|
|
lup 8
|
|
|
|
lda: tiledata+32+{]line*4},y
|
|
|
|
andl spritemask+{]line*256},x
|
|
|
|
stal spritemask+{]line*256},x
|
|
|
|
|
|
|
|
ldal spritedata+{]line*SPRITE_PLANE_SPAN},x
|
|
|
|
and: tiledata+32+{]line*4},y
|
2021-10-22 05:56:12 +00:00
|
|
|
ora: tiledata+{]line*4},y
|
2021-10-21 13:50:07 +00:00
|
|
|
stal spritedata+{]line*SPRITE_PLANE_SPAN},x
|
|
|
|
|
|
|
|
lda: tiledata+32+{]line*4}+2,y
|
|
|
|
andl spritemask+{]line*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
stal spritemask+{]line*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
|
|
|
|
ldal spritedata+{]line*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
and: tiledata+32+{]line*4}+2,y
|
2021-10-22 05:56:12 +00:00
|
|
|
ora: tiledata+{]line*4}+2,y
|
2021-10-21 13:50:07 +00:00
|
|
|
stal spritedata+{]line*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
]line equ ]line+1
|
|
|
|
--^
|
|
|
|
|
|
|
|
plb ; pop extra byte
|
|
|
|
plb
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Erase is easy -- set an 8x8 area of the data region to all $0000 and the corresponding mask
|
|
|
|
; resgion to all $FFFF
|
|
|
|
;
|
2021-10-29 03:18:33 +00:00
|
|
|
; X = address is sprite plane -- erases an 8x8 region
|
2021-10-21 13:50:07 +00:00
|
|
|
SPRITE_PLANE_SPAN equ 256
|
|
|
|
|
2021-10-29 03:18:33 +00:00
|
|
|
EraseTileSprite ENT
|
|
|
|
jsr _EraseTileSprite
|
|
|
|
rtl
|
2021-10-21 13:50:07 +00:00
|
|
|
|
2021-10-29 03:18:33 +00:00
|
|
|
_EraseTileSprite
|
2021-10-29 02:41:01 +00:00
|
|
|
phb ; Save the bank to switch to the sprite plane
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
pea #^spritedata
|
|
|
|
plb
|
|
|
|
|
|
|
|
lda #0
|
|
|
|
sta: {0*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {0*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {1*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {1*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {2*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {2*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {3*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {3*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {4*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {4*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {5*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {5*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {6*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {6*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {7*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {7*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
|
|
|
|
pea #^spritemask
|
|
|
|
plb
|
|
|
|
|
|
|
|
lda #$FFFF
|
|
|
|
sta: {0*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {0*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {1*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {1*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {2*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {2*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {3*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {3*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {4*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {4*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {5*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {5*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {6*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {6*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
sta: {7*SPRITE_PLANE_SPAN}+0,x
|
|
|
|
sta: {7*SPRITE_PLANE_SPAN}+2,x
|
|
|
|
|
|
|
|
pla
|
|
|
|
plb
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Add a new sprite to the rendering pipeline
|
|
|
|
;
|
2021-10-31 00:24:23 +00:00
|
|
|
; The tile id ithe range 0 - 511. The top 7 bits are used as sprite control bits
|
|
|
|
;
|
|
|
|
; Bit 9 : Horizontal flip.
|
|
|
|
; Bit 10 : Vertical flip.
|
|
|
|
; Bits 11 - 13 : Sprite Size Selector
|
|
|
|
; 000 - 8x8 (1x1 tile)
|
|
|
|
; 001 - 8x16 (1x2 tiles)
|
|
|
|
; 010 - 16x8 (2x1 tiles)
|
|
|
|
; 011 - 16x16 (2x2 tiles)
|
|
|
|
; 100 - 24x16 (3x2 tiles)
|
|
|
|
; 101 - 16x24 (2x3 tiles)
|
|
|
|
; 110 - 24x24 (3x3 tiles)
|
|
|
|
; 111 - 32x24 (4x3 tiles)
|
|
|
|
; Bit 14 : Low Sprite priority. Draws behind high priority tiles.
|
|
|
|
; Bit 15 : Reserved. Must be zero.
|
|
|
|
;
|
|
|
|
; When a sprite has a size > 8x8, the horizontal tiles are taken from the next tile index and
|
|
|
|
; the vertical tiles are taken from tileId + 32. This is why tile sheets should be saved
|
|
|
|
; with a width of 256 pixels.
|
|
|
|
;
|
|
|
|
; Single sprite are limited to 24 lines high because there are 28 lines of padding above and below the
|
|
|
|
; sprite plane buffers, so a sprite that is 32 lines high could overflow the drawing area.
|
|
|
|
;
|
|
|
|
; A = tileId + flags
|
2021-10-21 13:50:07 +00:00
|
|
|
; X = x position
|
|
|
|
; Y = y position
|
|
|
|
AddSprite ENT
|
|
|
|
phb
|
|
|
|
phk
|
|
|
|
plb
|
|
|
|
jsr _AddSprite
|
|
|
|
plb
|
|
|
|
rtl
|
|
|
|
|
|
|
|
_AddSprite
|
2021-10-24 03:31:38 +00:00
|
|
|
phx ; Save the horizontal position and tile ID
|
2021-10-21 13:50:07 +00:00
|
|
|
pha
|
|
|
|
|
|
|
|
ldx #0
|
|
|
|
:loop lda _Sprites+SPRITE_STATUS,x ; Look for an open slot
|
|
|
|
beq :open
|
|
|
|
inx
|
|
|
|
inx
|
|
|
|
cpx #MAX_SPRITES*2
|
|
|
|
bcc :loop
|
|
|
|
|
|
|
|
pla ; Early out
|
|
|
|
pla
|
2021-10-24 03:31:38 +00:00
|
|
|
sec ; Signal that no sprite slot was available
|
2021-10-21 13:50:07 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
:open lda #SPRITE_STATUS_DIRTY
|
|
|
|
sta _Sprites+SPRITE_STATUS,x ; Mark this sprite slot as occupied and that it needs to be drawn
|
|
|
|
pla
|
|
|
|
jsr _GetTileAddr
|
|
|
|
sta _Sprites+TILE_DATA_OFFSET,x
|
|
|
|
|
|
|
|
tya
|
|
|
|
clc
|
|
|
|
adc #NUM_BUFF_LINES ; The virtual buffer has 24 lines of off-screen space
|
|
|
|
xba ; Each virtual scan line is 256 bytes wide for overdraw space
|
|
|
|
clc
|
2021-10-24 03:31:38 +00:00
|
|
|
adc 1,s ; Add the horizontal position
|
2021-10-21 13:50:07 +00:00
|
|
|
sta _Sprites+VBUFF_ADDR,x
|
|
|
|
|
2021-10-24 03:31:38 +00:00
|
|
|
pla ; Pop off the saved value
|
|
|
|
clc ; Mark that the sprite was successfully added
|
|
|
|
txa ; And return the sprite ID
|
|
|
|
rts
|
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
; X = x coordinate
|
|
|
|
; Y = y coordinate
|
|
|
|
GetSpriteVBuffAddr ENT
|
|
|
|
tya
|
|
|
|
clc
|
|
|
|
adc #NUM_BUFF_LINES ; The virtual buffer has 24 lines of off-screen space
|
|
|
|
xba ; Each virtual scan line is 256 bytes wide for overdraw space
|
|
|
|
phx
|
|
|
|
clc
|
|
|
|
adc 1,s
|
|
|
|
plx
|
|
|
|
rtl
|
|
|
|
|
2021-10-24 03:31:38 +00:00
|
|
|
; Move a sprite to a new location. If the tile ID of the sprite needs to be changed, then
|
|
|
|
; a full remove/add cycle needs to happen
|
|
|
|
;
|
|
|
|
; A = sprite ID
|
|
|
|
; X = x position
|
|
|
|
; Y = y position
|
|
|
|
UpdateSprite ENT
|
|
|
|
phb
|
|
|
|
phk
|
|
|
|
plb
|
|
|
|
jsr _UpdateSprite
|
|
|
|
plb
|
|
|
|
rtl
|
|
|
|
|
|
|
|
_UpdateSprite
|
|
|
|
cmp #MAX_SPRITES*2 ; Make sure we're in bounds
|
|
|
|
bcc :ok
|
|
|
|
rts
|
|
|
|
|
|
|
|
:ok
|
2021-10-29 02:41:01 +00:00
|
|
|
stx tmp0 ; Save the horizontal position
|
|
|
|
and #$FFFE ; Defensive
|
2021-10-24 03:31:38 +00:00
|
|
|
tax ; Get the sprite index
|
|
|
|
|
|
|
|
lda #SPRITE_STATUS_DIRTY ; Position is changing, mark as dirty
|
|
|
|
sta _Sprites+SPRITE_STATUS,x ; Mark this sprite slot as occupied and that it needs to be drawn
|
|
|
|
|
2021-10-29 02:41:01 +00:00
|
|
|
lda _Sprites+VBUFF_ADDR,x ; Save the previous draw location for erasing
|
|
|
|
sta _Sprites+OLD_VBUFF_ADDR,x
|
|
|
|
|
|
|
|
lda tmp0 ; Update the X coordinate
|
|
|
|
sta _Sprites+SPRITE_X,x
|
|
|
|
|
|
|
|
tya ; Update the Y coordinate
|
|
|
|
sta _Sprites+SPRITE_Y,x
|
|
|
|
|
2021-10-24 03:31:38 +00:00
|
|
|
clc
|
|
|
|
adc #NUM_BUFF_LINES ; The virtual buffer has 24 lines of off-screen space
|
|
|
|
xba ; Each virtual scan line is 256 bytes wide for overdraw space
|
|
|
|
clc
|
2021-10-29 02:41:01 +00:00
|
|
|
adc tmp0 ; Add the horizontal position
|
2021-10-24 03:31:38 +00:00
|
|
|
sta _Sprites+VBUFF_ADDR,x
|
|
|
|
|
2021-10-21 13:50:07 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
; Sprite data structures. We cache quite a few pieces of information about the sprite
|
|
|
|
; to make calculations faster, so this is hidden from the caller.
|
|
|
|
;
|
|
|
|
; Each sprite record contains the following properties:
|
|
|
|
;
|
|
|
|
; +0: Sprite status word (0 = unoccupied)
|
|
|
|
; +2: Tile data address
|
|
|
|
; +4: Screen offset address (used for data and masks)
|
|
|
|
|
|
|
|
; Number of "off-screen" lines above logical (0,0)
|
|
|
|
NUM_BUFF_LINES equ 24
|
|
|
|
|
|
|
|
MAX_SPRITES equ 64
|
2021-10-29 02:41:01 +00:00
|
|
|
SPRITE_REC_SIZE equ 12
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
SPRITE_STATUS_EMPTY equ 0
|
|
|
|
SPRITE_STATUS_CLEAN equ 1
|
|
|
|
SPRITE_STATUS_DIRTY equ 2
|
|
|
|
|
|
|
|
SPRITE_STATUS equ 0
|
|
|
|
TILE_DATA_OFFSET equ {MAX_SPRITES*2}
|
|
|
|
VBUFF_ADDR equ {MAX_SPRITES*4}
|
|
|
|
SPRITE_X equ {MAX_SPRITES*6}
|
|
|
|
SPRITE_Y equ {MAX_SPRITES*8}
|
2021-10-29 02:41:01 +00:00
|
|
|
OLD_VBUFF_ADDR equ {MAX_SPRITES*10}
|
2021-10-21 13:50:07 +00:00
|
|
|
|
|
|
|
_Sprites ds SPRITE_REC_SIZE*MAX_SPRITES
|