[4.5.9] Simple Fade in and out from Black screen script

SciNEStist

Well-known member
One of the things that makes a game seem to have that extra bit of polish is when you load up a level or map and it fades in to existence from blackness.

This script requires you to first implement this fix to correct the problem with the transparent background colour : FIND IT HERE

This scripts features:
  • only about 75 lines of code
  • only 1 new user variable
  • easy to add to existing games
  • fades all background colours including the transparent colour
  • fully noted script, great for learning
IF YOU WANT BOTH SPRITES AND BACKGROUNDS TO FADE IN AND OUT, USE THIS CODE INSTEAD

STEP 1: under Palettes > All Palettes, scroll to the bottom and turn Palette #63 into all black
STEP 2: in the project settings window under user variables, add "fade" with an initial value of 0
STEP 3: also in project settings, under project labels, click "Screen Flags" and change "Screen Flag 7" to "Fade in from black"
STEP 4: in your BASE_4_5\Game\Subroutines folder, open up doLoadScreen.asm and scroll to about line 50. we are looking for this:
Code:
LoadBackgroundPalettes newPal

replace it with this:

Code:
LDA ScreenFlags00
AND #%00000001
BNE +
    LoadBackgroundPalettes newPal
    JMP +donebgpal
+
    LoadBackgroundPalettes #63
    LDA #$01
    STA fade
+donebgpal

This will make it so any screens that have "Fade in from black" checked will start as solid black instead of the regular palette.

STEP 5:
NOTE: if your module doesn't use a camera script, then save this code anywhere that gets ran every frame and is saved to the main bank (1F)

In NESMaker, click Scripts>Defined Scripts>Subroutines>Handle Camera and at the top of the doUpdateCamera.asm, add this code:
Code:
;;FADE IN STUFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    LDA fade
    BNE +
        JMP +nofadein ; if fade = 0, then we are not doing anything
    +
    CMP #$10 ; STEP 4, LOAD REGULAR PAL at frame 16
    BNE +
        LoadBackgroundPalettes newPal ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +

    TXA ;these 4 lines are to preserve our x and y registers (although we dont currently do anything to x register)
    PHA
    TYA
    PHA
    SwitchBank #$16 ; go to the bank that has the data
    LDY newPal ; grab the screens pallette setting
    LDA GameBckPalLo,y
    STA temp16 ; saves what we need to temp16
    LDA GameBckPalHi,y
    STA temp16+1
    LDY #$00 ; sets y to zero so we can start our loop right
 
    LDA fade
    CMP #$04 ; 4 frames in, we do step 1
    BNE +
    LDA #$2F ; in this step, any color over value 2F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 1, mostly dark
    +
    CMP #$08 ; 8 frames in, we do step 2
    BNE +
        LDA #$1F ; in this step, any color over value 1F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 2, bit lighter
    +
    CMP #$0C ; 12 frames in, we do step 3
    BNE +
    LDA #$0F; in this step, any color over falue 0F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 3, lighter still
    +
    JMP NoFadechange ; if it doesnt land on any of those frames, we skip ahead with no change
 
fadeinloop: ;this loop goes through each palette and subtracts tempx from it. each step is less of a subtraction. lower numbers are brighter
    LDA (temp16),y ; load up the true palette
    CMP tempx
    BCC +
        CLC
        SBC tempx ; subtract from the palette
        STA bckPal,y ; save our new darkened palette
    +
    INY
    CPY #$10
    BNE fadeinloop ;if we havent done all 16, we go back and do it again
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData ; we tell the game it needs to update the screen
NoFadechange:
    ReturnBank ; get back to the normal bank
    PLA ; These four lines restore the x and y registers
    TAY
    PLA
    TAX
    INC fade ; we increase the variable count to act as a ticker
+nofadein
;;DONE FADE IN STUFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DONE!

Now, if you want a screen to fade in from black whenever it is loaded, simply check the flag in screen settings
 
Last edited:

baardbi

Well-known member
Awesome! It works great. It's worth noting though that this will only work with modules that have doUpdateCamera. So it will not work on the Adventure, ArcadePlatformer and Maze modules.
 

mileco

Active member
Thanks so much! Having the option to fade in and out is always very important. Hope you can also adapt this script for the arcade MOD.
 

mileco

Active member
Awesome! It works great. It's worth noting though that this will only work with modules that have doUpdateCamera. So it will not work on the Adventure, ArcadePlatformer and Maze modules.
Reversing his logic, would this work to fade out?

;;FADE OUT STUFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA fade
BNE +
JMP +nofadeout ; Si fade = 0, no hacemos nada
+
CMP #$10 ; STEP 4, PALETA COMPLETAMENTE NEGRA en frame 16
BNE +
; Cargar paleta negra (todos los colores a 0)
LDY #$00
LDA #$00
-
STA bckPal, y
INY
CPY #$10
BNE -
LDA updateScreenData
ORA #%00000001
STA updateScreenData
LDA #$00
STA fade ; Resetear fade
JMP +nofadeout
+

TXA ; Preservar registros
PHA
TYA
PHA
SwitchBank #$16 ; Banco con datos de paleta
LDY newPal
LDA GameBckPalLo,y
STA temp16 ; Dirección base de la paleta
LDA GameBckPalHi,y
STA temp16+1
LDY #$00 ; Inicializar contador

LDA fade
CMP #$04 ; Frame 4 - Paso 1
BNE +
LDA #$0F ; Valor bajo - oscurecimiento leve
STA tempx
JMP fadeoutloop
+
CMP #$08 ; Frame 8 - Paso 2
BNE +
LDA #$1F ; Valor medio - más oscuro
STA tempx
JMP fadeoutloop
+
CMP #$0C ; Frame 12 - Paso 3
BNE +
LDA #$2F ; Valor alto - muy oscuro
STA tempx
JMP fadeoutloop
+
JMP NoFadeChangeOut ; Sin cambios en otros frames

fadeoutloop:
; Lógica INVERSA al fade in: limitar valores MÁXIMOS en lugar de mínimos
LDA (temp16),y ; Color original
CMP tempx
BCS + ; Si color >= tempx, limitarlo a tempx
LDA (temp16),y ; Cargar color original
JMP ++
+
LDA tempx ; Usar valor límite (más oscuro)
++
STA bckPal,y ; Guardar color procesado
INY
CPY #$10 ; Procesar 16 colores
BNE fadeoutloop

; Marcar para actualizar pantalla
LDA updateScreenData
ORA #%00000001
STA updateScreenData

NoFadeChangeOut:
ReturnBank ; Restaurar banco
PLA
TAY
PLA
TAX ; Restaurar registros
INC fade ; Incrementar contador
+nofadeout
;;DONE FADE OUT STUFF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 

SciNEStist

Well-known member
doesnt look quite right to me. for a fadeout the loop part would have to subtract from all colours more each stage until all are black. Stay tuned, ill see what I can do.

Also, the code that is loaded into the camerahandler can be placed in a lot of other locations too, any script that is triggered every frame that is on the main bank should work.
 

mileco

Active member
doesnt look quite right to me. for a fadeout the loop part would have to subtract from all colours more each stage until all are black. Stay tuned, ill see what I can do.

Also, the code that is loaded into the camerahandler can be placed in a lot of other locations too, any script that is triggered every frame that is on the main bank should work.
Thanks so much. It would help me a lot.
 

SciNEStist

Well-known member
VERSION 2: Fade In and Out

Same instructions, but paste this code instead in step 5.

With this new code I added a fade out feature. To trigger a fade out, you need to set the fade variable to #$11 (#17) and it will force the screen to fade to black.

Code:
;;FADE IN STUFF

    LDA fade
    BNE +
        JMP +nofadein ; if fade = 0, then we are not doing anything
    +
    CMP #$10 ; FADE IN STEP 4, LOAD REGULAR PAL at frame 16
    BNE +
        LoadBackgroundPalettes newPal ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +
    CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +

    TXA ;these 4 lines are to preserve our x and y registers (although we dont currently do anything to x register)
    PHA
    TYA
    PHA
    SwitchBank #$16 ; go to the bank that has the data
    LDY newPal ; grab the screens pallette setting
    LDA GameBckPalLo,y
    STA temp16 ; saves what we need to temp16
    LDA GameBckPalHi,y
    STA temp16+1
    LDY #$00 ; sets y to zero so we can start our loop right
 
    LDA fade
    CMP #$04 ; 4 frames in, we do step 1
    BNE +
        LDA #$2F ; in this step, any color over value 2F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 1, mostly dark
    +
    CMP #$08 ; 8 frames in, we do step 2
    BNE +
        LDA #$1F ; in this step, any color over value 1F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 2, bit lighter
    +
    CMP #$0C ; 12 frames in, we do step 3
    BNE +
        LDA #$0F; in this step, any color over value 0F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 3, lighter still
    +
 
    CMP #$14 ; fade out stage 1
    BNE +
        LDA #$0F;
        STA tempx
        JMP fadeoutloop ; STEP 3, lighter still
    +
 
    CMP #$18 ; fade out stage 2
    BNE +
        LDA #$1F; in this step, any color over falue 0F will get reduced and updated
        STA tempx
        JMP fadeoutloop ;
    +
 
    CMP #$1C ; fade out stage 3
    BNE +
        LDA #$2F; in this step, any color over value 0F will get reduced and updated
        STA tempx
        JMP fadeoutloop ;
    +
 
    JMP NoFadechange ; if it doesn't land on any of those frames, we skip ahead with no change


fadeoutloop: ; this is the loop for fading to black.
    LDA (temp16),y
    CMP tempx
    BPL +
        LDA #$0F ; anything below our threshold is black
        STA bckPal,y
    +  
    SBC tempx ; everything else is reduced
    STA bckPal,y
    INY
    CPY #$10
    BNE fadeoutloop ;if we haven't done all 16, we go back and do it again
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData ; we tell the game it needs to update the screen
    JMP NoFadechange

 
fadeinloop: ;this loop goes through each palette and subtracts tempx from it. each step is less of a subtraction. higher numbers are brighter
    LDA (temp16),y ; load up the true palette
    CMP tempx
    BCC +
        CLC
        SBC tempx ; subtract from the palette
        STA bckPal,y ; save our new darkened palette
    +  
    INY
    CPY #$10
    BNE fadeinloop ;if we haven't done all 16, we go back and do it again
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData ; we tell the game it needs to update the screen
NoFadechange:
    ReturnBank ; get back to the normal bank
    PLA ; These four lines restore the x and y registers
    TAY
    PLA
    TAX
    INC fade ; we increase the variable count to act as a ticker
+nofadein

OPTIONAL:
With an extra modifications, you could also force a screen warp after the fade is complete.

Take this section:

Code:
  CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +

and replace it with this:

Code:
    CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        INC fade
        JMP +nofadein ; jump to end
    +
    CMP #$24
    BNE +
        LDA #$00
        STA fade ; set fade to 0
        WarpToScreen warpToMap, warpToScreen, #$01
        JMP +nofadein
    +
 
Last edited:

mileco

Active member
VERSION 2: Fade In and Out

Same instructions, but paste this code instead in step 5.

With this new code I added a fade out feature. To trigger a fade out, you need to set the fade variable to #$11 (#17) and it will force the screen to fade to black.

Code:
;;FADE IN STUFF

    LDA fade
    BNE +
        JMP +nofadein ; if fade = 0, then we are not doing anything
    +
    CMP #$10 ; FADE IN STEP 4, LOAD REGULAR PAL at frame 16
    BNE +
        LoadBackgroundPalettes newPal ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +
    CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +

    TXA ;these 4 lines are to preserve our x and y registers (although we dont currently do anything to x register)
    PHA
    TYA
    PHA
    SwitchBank #$16 ; go to the bank that has the data
    LDY newPal ; grab the screens pallette setting
    LDA GameBckPalLo,y
    STA temp16 ; saves what we need to temp16
    LDA GameBckPalHi,y
    STA temp16+1
    LDY #$00 ; sets y to zero so we can start our loop right
 
    LDA fade
    CMP #$04 ; 4 frames in, we do step 1
    BNE +
        LDA #$2F ; in this step, any color over value 2F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 1, mostly dark
    +
    CMP #$08 ; 8 frames in, we do step 2
    BNE +
        LDA #$1F ; in this step, any color over value 1F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 2, bit lighter
    +
    CMP #$0C ; 12 frames in, we do step 3
    BNE +
        LDA #$0F; in this step, any color over value 0F will get reduced and updated
        STA tempx
        JMP fadeinloop ; STEP 3, lighter still
    +
 
    CMP #$14 ; fade out stage 1
    BNE +
        LDA #$0F;
        STA tempx
        JMP fadeoutloop ; STEP 3, lighter still
    +
 
    CMP #$18 ; fade out stage 2
    BNE +
        LDA #$1F; in this step, any color over falue 0F will get reduced and updated
        STA tempx
        JMP fadeoutloop ;
    +
 
    CMP #$1C ; fade out stage 3
    BNE +
        LDA #$2F; in this step, any color over value 0F will get reduced and updated
        STA tempx
        JMP fadeoutloop ;
    +
 
    JMP NoFadechange ; if it doesn't land on any of those frames, we skip ahead with no change


fadeoutloop: ; this is the loop for fading to black.
    LDA (temp16),y
    CMP tempx
    BPL +
        LDA #$0F ; anything below our threshold is black
        STA bckPal,y
    +   
    SBC tempx ; everything else is reduced
    STA bckPal,y
    INY
    CPY #$10
    BNE fadeinloop ;if we haven't done all 16, we go back and do it again
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData ; we tell the game it needs to update the screen
    JMP NoFadechange

 
fadeinloop: ;this loop goes through each palette and subtracts tempx from it. each step is less of a subtraction. higher numbers are brighter
    LDA (temp16),y ; load up the true palette
    CMP tempx
    BCC +
        CLC
        SBC tempx ; subtract from the palette
        STA bckPal,y ; save our new darkened palette
    +   
    INY
    CPY #$10
    BNE fadeinloop ;if we haven't done all 16, we go back and do it again
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData ; we tell the game it needs to update the screen
NoFadechange:
    ReturnBank ; get back to the normal bank
    PLA ; These four lines restore the x and y registers
    TAY
    PLA
    TAX
    INC fade ; we increase the variable count to act as a ticker
+nofadein

OPTIONAL:
With an extra modifications, you could also force a screen warp after the fade is complete.

Take this section:

Code:
  CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        LDA #$00
        STA fade ; set fade to 0
        JMP +nofadein ; jump to end
    +

and replace it with this:

Code:
    CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        INC fade
        JMP +nofadein ; jump to end
    +
    CMP #$24
    BNE +
        LDA #$00
        STA fade ; set fade to 0
        WarpToScreen warpToMap, warpToScreen, #$01
        JMP +nofadein
    +
THANK YOU!! You mean the user variable should be set to 11? Or it is in the code?
 

SciNEStist

Well-known member
any time you want the screen to fade out, set the fade variable to #$11 and it will happen. something like this:
Code:
LDA #$11
STA fade

and the screen will go black. then to fade back in to colors, you would do this:

Code:
LDA #$01
STA fade
 

mileco

Active member
any time you want the screen to fade out, set the fade variable to #$11 and it will happen. something like this:
Code:
LDA #$11
STA fade

and the screen will go black. then to fade back in to colors, you would do this:

Code:
LDA #$01
STA fade
I see, thanks so much. It works perfect in a vanilla NESMaker. In a NESMaker with doLoadScreen16 fix to free up space, it doesn't. Or (very likely) I'm putting the code in the wrong place. Do you know where it should be?
 

SciNEStist

Well-known member
yes, if you do the extra modification you could create a warp tile that instead of a WarpToScreen line in the tile code, you just set the fade variable. If you were to do that, you would also want to check if the fade variable is 0 first.
 

SciNEStist

Well-known member
UPDATE AGAIN: SPRITES AND BACKGROUND FADING IN AND OUT

This script requires you to first implement this fix to correct the problem with the transparent background colour : FIND IT HERE

This scripts features:
  • only 1 new user variable
  • easy to add to existing games
  • fades all background colours and sprites
STEP 1: under Palettes > All Palettes, scroll to the bottom and turn Palette #63 into all black. do the same for monster palettes
STEP 2: in the project settings window under user variables, add "fade" with an initial value of 0
STEP 3: also in project settings, under project labels, click "Screen Flags" and change "Screen Flag 7" to "Fade in from black"
STEP 4: in your BASE_4_5\Game\Subroutines folder, open up doLoadScreen.asm and scroll to about line 50. we are looking for this:

Code:
LoadBackgroundPalettes newPal
    JSR doWaitFrame
        ;; We wait this frame because the LoadBackgroundPalettes routine
        ;; sets bckpal variables and activates the update palette.
        ;; waiting this frame holds up on the next routine until the
        ;; background palettes are loaded into the PPU.
    ;LoadObjectPalettes #$00
 
    LoadObjectSubPalettes spriteSubPal1, #$00
    LoadObjectSubPalettes spriteSubPal2, #$04
    LoadObjectSubPalettes spriteSubPal3, #$08
    LoadObjectSubPalettes spriteSubPal4, #$0C


replace it with this:

Code:
LDA ScreenFlags00
AND #%00000001
BNE +
    LoadBackgroundPalettes newPal
    JSR doWaitFrame
    LoadObjectSubPalettes spriteSubPal1, #$00
    LoadObjectSubPalettes spriteSubPal2, #$04
    LoadObjectSubPalettes spriteSubPal3, #$08
    LoadObjectSubPalettes spriteSubPal4, #$0C
    JMP +donepal
+
    LoadBackgroundPalettes #63
    LDA #$01
    STA fade
    JSR doWaitFrame
    LoadObjectSubPalettes #63, #$00
    LoadObjectSubPalettes #63, #$04
    LoadObjectSubPalettes #63, #$08
    LoadObjectSubPalettes #63, #$0C
+donepal

This will make it so any screens that have "Fade in from black" checked will start as solid black instead of the regular palette.

STEP 5: In NESMaker, click Scripts>Defined Scripts>Subroutines>Handle Camera and at the top of the doUpdateCamera.asm, add this code:
NOTE: if your module doesn't use a camera script, then save this code anywhere that gets ran every frame and is saved to the main bank (1F)

Code:
;;FADE IN/OUT STUFF

    LDA fade
    BNE +
        JMP +nofadein ; if fadein = 0, then we are not doing anything
    +
    CMP #$0F; SPRITE FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadObjectSubPalettes spriteSubPal1, #00
        LoadObjectSubPalettes spriteSubPal2, #04
        LoadObjectSubPalettes spriteSubPal3, #08
        LoadObjectSubPalettes spriteSubPal4, #12
        LDA updateScreenData
        ORA #%00000010 ;; sprite palette
        STA updateScreenData
        INC fade
        JMP +nofadein ; jump to end
    +
    CMP #$10 ; FADE IN STEP 4, LOAD REGULAR PAL at frame 16
    BNE +
        LoadBackgroundPalettes newPal ; load screen palette
        LDA updateScreenData
        ORA #%00000001 ;; sprite palette
        STA updateScreenData
        LDA #$00
        STA fade ; set fadein to 0
        JMP +nofadein ; jump to end
    +
    CMP #$1F; SPRITE FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadObjectSubPalettes #63, #00
        LoadObjectSubPalettes #63, #04
        LoadObjectSubPalettes #63, #08
        LoadObjectSubPalettes #63, #12
 
        LDA updateScreenData
        ORA #%00000010 ;; sprite palette
        STA updateScreenData
        INC fade
        JMP +nofadein ; jump to end
    +
    CMP #$20; FADE OUT STEP 4 TOTAL BLACK
    BNE +
        LoadBackgroundPalettes #63 ; load screen palette
        LDA updateScreenData
        ORA #%00000001 ;; sprite palette
        STA updateScreenData
        INC fade
        JMP +nofadein ; jump to end
    +
    CMP #$24
    BNE +
        LDA #$00
        STA fade ; set fadein to 0
        ;WarpToScreen warpToMap, warpToScreen, #$01 ; UNCOMMENT HERE IF YOU WANT TO WARP AFTER FADE!!!!!!!!!!!!!!!!!!!!!!!
        JMP +nofadein
    +

    TXA
    PHA
    TYA
    PHA
    SwitchBank #$16
    LDY newPal
    LDA GameBckPalLo,y
    STA temp16
    LDA GameBckPalHi,y
    STA temp16+1
    LDY #$00
 
    LDA fade
    CMP #$03
    BNE +
        LDA #$30
        STA tempx
        JMP fadeinsprites
    +
    CMP #$04 ; 4 frames in, we do step 1
    BNE +
        LDA #$30
        STA tempx
        JMP fadeinloop ; STEP 1, mostly dark
    +
    CMP #$07
    BNE +
        LDA #$20
        STA tempx
        JMP fadeinsprites
    +
    CMP #$08 ; 8 frames in, we do step 2
    BNE +
    LDA #$20
        STA tempx
        JMP fadeinloop ; STEP 2, bit lighter
    +
    CMP #$0B
    BNE +
        LDA #$0F
        STA tempx
        JMP fadeinsprites
    +
    CMP #$0C ; 12 frames in, we do step 3
    BNE +
        LDA #$0F
        STA tempx
        JMP fadeinloop ; STEP 3, lighter still
    +
 
    CMP #$13 ; fade out sprite
    BNE +
        LDA #$10;
        STA tempx
        JMP fadeoutsprites ; STEP 3, lighter still
    +
 
    CMP #$14 ; fade out stage 1
    BNE +
    LDA #$10;
        STA tempx
        JMP fadeoutloop ; STEP 3, lighter still
    +
 
    CMP #$17 ; fade out sprite
    BNE +
        LDA #$20;
        STA tempx
        JMP fadeoutsprites ; STEP 3, lighter still
    +
 
    CMP #$18 ; fade out stage 2
    BNE +
    LDA #$20
        STA tempx
        JMP fadeoutloop ;
    +
 
    CMP #$1B ; fade out sprite
    BNE +
        LDA #$30;
        STA tempx
        JMP fadeoutsprites ; STEP 3, lighter still
    +
 
    CMP #$1C ; fade out stage 3
    BNE +
    LDA #$30
        STA tempx
        JMP fadeoutloop ;
    +
 
    JMP NoFadechange

fadeoutsprites:
        LDY spriteSubPal1
        LDX #$00
dothespritepalfadeagain:
        LDA ObjectPaletteDataLo,y
        STA temp16
        LDA ObjectPaletteDataHi,y
        STA temp16+1
        LDY #$00
        loop_fadeoutsprites:
            LDA (temp16),y
            CMP tempx
            BPL +
                LDA #$0F
                STA sprPal,x
            +
            SBC tempx
            STA sprPal,x
            INY
            INX
            CPY #$04
            BNE loop_fadeoutsprites
        CPX #$04
        BNE +
            LDY spriteSubPal2
            JMP dothespritepalfadeagain
        +
        CPX #$08
        BNE +
            LDY spriteSubPal3
            JMP dothespritepalfadeagain
        +
        CPX #$0C
        BNE +
            LDY spriteSubPal4
            JMP dothespritepalfadeagain
        +
        LDA updateScreenData
        ORA #%00000010 ;; sprite palette
        STA updateScreenData
        JMP NoFadechange
 
 
fadeinsprites:
        LDY spriteSubPal1
        LDX #$00
dothespritepalfadeinagain:
        LDA ObjectPaletteDataLo,y
        STA temp16
        LDA ObjectPaletteDataHi,y
        STA temp16+1
        LDY #$00
        loop_fadeinsprites:
            LDA (temp16),y
            CMP tempx
            BCC +
                CLC
                SBC tempx ; subtract from the palette
                STA sprPal,x
            +
            INY
            INX
            CPY #$04
            BNE loop_fadeinsprites
        CPX #$04
        BNE +
            LDY spriteSubPal2
            JMP dothespritepalfadeinagain
        +
        CPX #$08
        BNE +
            LDY spriteSubPal3
            JMP dothespritepalfadeinagain
        +
        CPX #$0C
        BNE +
            LDY spriteSubPal4
            JMP dothespritepalfadeinagain
        +
        LDA updateScreenData
        ORA #%00000010 ;; sprite palette
        STA updateScreenData
        JMP NoFadechange

fadeoutloop:
    LDA (temp16),y
    CMP tempx
    BPL +
        LDA #$0F
        STA bckPal,y
    +
    SBC tempx
    STA bckPal,y
    INY
    CPY #$10
    BNE fadeoutloop
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData
    JMP NoFadechange
 
fadeinloop:
    LDA (temp16),y
    CMP tempx
    BCC +
        CLC
        SBC tempx
        STA bckPal,y
    +
    INY
    CPY #$10
    BNE fadeinloop
    ;if we are done with the loop, we proceed
    LDA updateScreenData
    ORA #%00000001 ;; palette
    STA updateScreenData
NoFadechange:
    ReturnBank
    PLA
    TAY
    PLA
    TAX
    INC fade
+nofadein

DONE!

Now, if you want a screen to fade in from black whenever it is loaded, simply check the flag in screen settings.
Or you can Set the variable "fade" to #$11 (#17) to trigger a fade out and set it to #$01 (#1) to trigger a fade in

IF you want the screen to warp automatically after the fade out, uncomment the line "WarpToScreen warpToMap, warpToScreen, #$01"

ENJOY!
 
Last edited:
Top Bottom