Fixed stack-based clip/unclip

This commit is contained in:
blondie7575 2018-03-05 12:32:05 -08:00
parent 3292511ba8
commit 24269638fb
6 changed files with 73 additions and 53 deletions

View File

@ -39,12 +39,12 @@ lastCompiledTerrainY = $75 ; The highest Y value that the compiled renderer must
; Terrain constants
TERRAINWIDTH = 640 ; In pixels
MAXTERRAINHEIGHT = 100 ; In pixels
COMPILEDTERRAINROW = TERRAINWIDTH/4+3 ; In words, +3 to make room for clipping jump at end of row
COMPILEDTERRAINROW = TERRAINWIDTH/4+4 ; In words, +4 to make room for clipping jump at end of row
VISIBLETERRAINWIDTH = TERRAINWIDTH/4 ; In words- width minus jump return padding
VISIBLETERRAINWINDOW = 80 ; In words
MAXSPANSPERROW = 15
SPANROWBYTES = MAXSPANSPERROW*2 + 2 ; In bytes
CLIPPEDTERRAINSTACK = $3fff ; Location of stack where clipped terrain data lives
; Terrain data, stored as height values 2 pixels wide (bytes)
terrainDataFar = $02f500

View File

@ -99,12 +99,9 @@ gameplayLoopRender:
;
; Render the terrain if needed
; lda terrainDirty
; beq gameplayLoopProjectiles
lda terrainDirty
beq gameplayLoopProjectiles
BORDER_COLOR #$3
jsr unclipTerrain
jsl unrenderTerrainSpans
jsr clipTerrain
jsl renderTerrainSpans
jsr renderTerrain

Binary file not shown.

View File

@ -30,7 +30,7 @@
.macro DBR bankNum
BITS8
lda bankNum
lda #bankNum
pha
plb
BITS16
@ -109,7 +109,7 @@
.macro FASTGRAPHICS ;34 cycles, 12 bytes
sei ;2
phd ;4
sep #%00100000 ; 3 16-bit A only, to preserve X/Y
sep #%00100000 ; 3 8-bit A only, to preserve X/Y
.a8
lda STACKCTL ;5
@ -125,7 +125,7 @@
.macro SLOWGRAPHICS ;28 cycles, 12 bytes
sep #%00100000 ; 3 16-bit A only, to preserve X/Y
sep #%00100000 ; 3 8-bit A only, to preserve X/Y
.a8
lda STACKREGISTER ;4
@ -177,6 +177,16 @@ nobrk:
pla
.endmacro
.macro BREAK_NOSTACK
lda breakpoint
beq nobrk
lda #1
sta $e1c029
brk
nobrk:
.endmacro
.macro HARDBRK
pha
lda #1

View File

@ -20,7 +20,7 @@ renderTerrain:
tcs ; 2
sec
lda #compiledTerrainEnd-VISIBLETERRAINWINDOW-3
lda #compiledTerrainEnd-VISIBLETERRAINWINDOW-4
sbc mapScrollPos
sta PARAML0
@ -32,6 +32,7 @@ renderTerrainLoop:
jmp (PARAML0)
renderRowComplete:
lda PARAML0
sec
sbc #COMPILEDTERRAINROW
@ -127,44 +128,43 @@ craterTerrainDone:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; clipTerrain
;
; Saves the compiled terrain data that we overwrite into
; a buffer at $E04000. We do this by shadowing the stack
; to that area and pushing.
;
clipTerrain:
SAVE_AXY
; Shadow stack into $E04000 to save clipped data fast
tsc
sta STACKPTR
lda #CLIPPEDTERRAINSTACK
tcs
sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-4
sbc mapScrollPos
tay
ldx #0
clipTerrainLoop:
clc ; Compute buffer to use for saved data
txa
asl
asl
adc #clippedTerrainData;-4
sta PARAML0
lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting
inc PARAML0
inc PARAML0
and #$ff00
ora #$004c ; jmp in low byte
pha
lda #$4cea ; NOP followed by JMP
sta compiledTerrain,y
iny
iny
iny
lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting
pha
lda #renderRowComplete
sta compiledTerrain,y
tya
sec
sbc #COMPILEDTERRAINROW+1
sbc #COMPILEDTERRAINROW+2
tay
inx
@ -172,6 +172,12 @@ clipTerrainLoop:
bcc clipTerrainLoop
beq clipTerrainLoop
; Put stack back where it belongs
tsc
sta clippedTerrainStackPtr
lda STACKPTR
tcs
RESTORE_AXY
rts
@ -179,42 +185,52 @@ clipTerrainLoop:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; unclipTerrain
;
; Restores the compiled terrain data that we wrote
; to a buffer at $E04000. We do this by mapping the stack
; to $4000, then using stack-relative addressing to pull the data.
; We can't pop the data because it's all stored in reverse.
;
; On first move-left unclip every second row is unclipped incorrectly
unclipTerrain:
SAVE_AXY
phd
lda #(CLIPPEDTERRAINSTACK & $ff00)
pha
pld ; Point direct page at our clip data
sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-4
sbc mapScrollPos
tay
ldx #0
lda clippedTerrainStackPtr
and #$00ff
tax
inx
unclipTerrainLoop:
clc ; Compute buffer that saved data is in
txa
asl
asl
adc #clippedTerrainData;-4
sta PARAML0
lda (PARAML0)
lda 2,x
sta compiledTerrain,y
inc PARAML0
inc PARAML0
iny
iny
lda (PARAML0)
lda 0,x
sta compiledTerrain,y
tya
sec
sbc #COMPILEDTERRAINROW+1
sbc #COMPILEDTERRAINROW+2
tay
inx
cpx lastCompiledTerrainY
bcc unclipTerrainLoop
beq unclipTerrainLoop
inx
inx
inx
cpx #$100 ; When x hits the top of the stack, we're done
bne unclipTerrainLoop
pld
RESTORE_AXY
rts
@ -471,13 +487,10 @@ generateTerrainLoop:
compiledTerrain:
.repeat COMPILEDTERRAINROW * MAXTERRAINHEIGHT
.byte 0
.byte $00
.endrepeat
compiledTerrainEnd:
clippedTerrainData:
.repeat MAXTERRAINHEIGHT
.byte 0,0,0,0 ; xx,jmp,addr
.endrepeat
clippedTerrainStackPtr:
.word 0

View File

@ -10,7 +10,7 @@
.include "equates.s"
.include "macros.s"
UNRENDERCACHESTACK = $7ff
UNRENDERCACHESTACK = $7ff ; Stack downwards from bottom of VRAM. Mirrored from $01 into $E1!
OP16