Fade Out (and maybe warp) 4.5.9

baardbi

Well-known member
I know there are other tutorials here on the forum that cover this, but I couldn't get them to work so I made my own. This will fade everything to black (both background and sprites). You can put this code wherever you need to make the screen fade to black. I made a monster AI with warp. So when the screen has faded to black you go to warp.

Here's the code for the fade out effect:

LDX #0
fadeOutLoopOuter:
TXA
PHA

LDY #0
fadeOutLoop:
TYA
ASL
ASL
TAX

;;;; Background palettes ;;;;;;
LDA bckPal+1,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA bckPal+1,x

LDA bckPal+2,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA bckPal+2,x

LDA bckPal+3,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA bckPal+3,x

;;;; Sprite palettes ;;;;;;
LDA sprPal+1,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA sprPal+1,x

LDA sprPal+2,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA sprPal+2,x

LDA sprPal+3,x
CMP #$10
BCS +decreaseColor
LDA #$0F ;Black
JMP +storeNewColor
+decreaseColor:
SEC
SBC #$10
+storeNewColor:
STA sprPal+3,x

LDA updateScreenData
ORA #%00000011 ;; Update both background palettes and sprite palettes
;; %00000001 = Background palettes
;; %00000010 = Sprite palettes
STA updateScreenData

LDX #0
fadeOutWaitLoop:
JSR doWaitFrame
INX
CPX #4 ;Wait for 4 frames before next palette decrease
BNE fadeOutWaitLoop

INY
CPY #4 ;All sub palettes
BEQ nextPaletteDecrease
JMP fadeOutLoop
nextPaletteDecrease:

PLA
TAX

INX
CPX #4 ;All color rows
BEQ doneFadeOut
JMP fadeOutLoopOuter

doneFadeOut:

If you want to do what I did (AI - fade out, then warp), just make a new asm file. Copy the code above and paste it in the new file. Then add this at the bottom of the code:

WarpToScreen warpToMap, warpToScreen, #$01

Then you assign it to a free monster AI_Behavior, and maybe change the Action Type in Project Labels to something like FadeWarp.
 
Top Bottom