[4.9.x] Fade In Special Screens

pierski

New member
This is definitely not an ideal solution, but it does work for the few fade ins that I needed for my 8x8 screens.

First thing, set up your screen bytes: https://www.nesmakers.com/index.php?threads/making-userscreenbyte0-7-work-plus-bonus.5842/

Next, add the following code to doSpritePreDraw.asm. This is the macro function I am using the define all the colors of the palette.

Code:
MACRO ChangePaletteColors arg0, arg2, arg3, arg4
    ;; arg0 =palette number
    ;; arg2 =color 2
    ;; arg3 =color 3
    ;; arg4 =color 4
    LDA arg0
    ASL
    ASL
    TAX
    LDA arg2
    STA bckPal+1,x
    LDA arg3
    STA bckPal+2,x                         
    LDA arg4
    STA bckPal+3,x
    ENDM

Then, add the following code to doSpritePostDraw.asm. This is the function itself. I tried to comment the code. If you don't use all the background palettes, then just remove ChangePaletteColors from those steps.

Code:
LDA userScreenByte4
BEQ noCompanyLogo

    LDA companyLoadTimer            ;; LOAD TIMER BEFORE STARTING FADE IN ;;
    CMP #$30                        ;; TIMER LENGTH ;;
    BCS companyPaletterTimer
    INC companyLoadTimer 
    JMP noCompanyLogo   

companyPaletterTimer:
    LDA companyTimer                ;; LOAD PALETTE TIMER ;;
    BNE companyPaletteCountdown     ;; IF NOT 0 JUMP DOWN ;;
    JSR companyPaletteChange               
    LDA #$04                        ;; FRAMES BETWEEN FADES ;;
    STA companyTimer

companyPaletteCountdown:
    DEC companyTimer                ;; DECREASE TIMER ;;
    JMP noCompanyLogo 

companyPaletteChange: 
    LDA companyStepNum
    CMP #$05
    BCC companyStep0
noCompanyLogo:
    RTS
fadeCompanyLogo:
    LDA #%00000111
    STA updateScreenData
    RTS

companyStep0:   
    INC companyStepNum                          ;; INCREMENT STEP ;;
    LDA companyStepNum
    CMP #$02
    BCS companyStep1
    ;; FIRST STEP IN PROCESS ;;           
    ChangePaletteColors #$00, #$01, #$01, #$01  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$01, #$01, #$01  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$01, #$01, #$01  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$01, #$01, #$01  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep1: 
    LDA companyStepNum
    CMP #$03
    BCS companyStep2
    ;; SECOND STEP IN PROCESS ;;   
    ChangePaletteColors #$00, #$11, #$11, #$11  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$11, #$11, #$11  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$11, #$11, #$11  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$11, #$11, #$11  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep2:
    LDA companyStepNum
    CMP #$04
    BCS companyStep3 
    ;; THIRD STEP IN PROCESS ;; 
    ChangePaletteColors #$00, #$21, #$21, #$21  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$21, #$21, #$21  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$21, #$21, #$21  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$21, #$21, #$21  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep3:
    ;; FINAL STEP IN PROCESS ;; 
    ChangePaletteColors #$00, #$31, #$31, #$31  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$31, #$31, #$31  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$31, #$31, #$31  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$31, #$31, #$31  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

On the screen you want to use this, set userScreenByte4 = 1 and create a color palette where all the colors are the same as the default background color (i.e. black) so that it will be the default background when the page loads. Whatever colors you define in each ChangePaletteColors, will decide what the fade looks like for that frame.

I know this code can be improved by referencing tables, instead of passing the values directly in the macro. I also would love to have a more programmatic solution, like suggested by @Jonny here: https://www.nesmakers.com/index.php?threads/4-5-9-fade-in-fade-out.7140/#post-38396

This just does the job I needed it to do in [4.9.x], so I decided to share.
 

Jonny

Well-known member
@pierski nice work. I was wondering.. is there a reason for putting the macro in doSpritePreDraw.asm instead of saving it with the other macros?
 

pierski

New member
@pierski nice work. I was wondering.. is there a reason for putting the macro in doSpritePreDraw.asm instead of saving it with the other macros?
No. Definitely makes more sense to be with the other macros. I just hard the doSprite(Pre|Post)Draw.asm files open together, so it was easier for me to keep track of it.
 

Jonny

Well-known member
No. Definitely makes more sense to be with the other macros. I just hard the doSprite(Pre|Post)Draw.asm files open together, so it was easier for me to keep track of it.
Cool. It all works really well, love it. I copied your macro and made a sprPal one too so with the same routine sprite pals can be faded in.

I still want to use tables to fade in different palettes which will probably mean not using the macros at all which will be a shame but will see. This is a really great starting point for that though. I was thinking of using the same UserScreenByte4 to select certain fade table depending on what the end colors needed to be. Hopefully I can get something which works as an in-game fade in which doesn't take up too much process time or break other things.

I'm starting to consider what seqence of color changes makes a good fade effect too. When I tried out your code I basically started with the end colors and reduced each by 10 until they were at 0F. Most were already at 0F for Step0 or even Step1 so I'm going to try keeping some not-0F for a bit longer, see how that looks.

Thanks again :)
 

3DPLABox

Member
First things first: This is a really nice effect and exactly what I was looking for to get fade ins on select screens. I got it working with little to no problem, so thanks!

Second: I'm attempting to modify the code to change which colors load in based on what the userScreenByte4 value is, and I have it mostly working, but not entirely. Currently my first (userScreenByte4 = 1) and second (userScreenByte4 = 2) screens are the only ones I want to fade in. When the game starts, whichever screen the player starts on will fade in, but when warping to the second screen, it stays black and will not fade in. I have a feeling it has something to do with 'fadeInStepNum' not resetting to its initial value between screens? But every attempt I've made at decreasing that number results in a Branch Out of Range error. I could also be completely wrong as 6502 is still very new to me and I may be reading it all wrong.

Here's my (probably very cumbersome) attempt at the code modification. If there's a simpler/more efficient way to accomplish my goal, I'd love to hear it.

Code:
LDA userScreenByte4
BEQ noFadeInLogo                    ;; If userScreenByte4 is equal to 0, jump to noFadeInLogo

CMP #$01                            ;; If userScreenByte4 is equal to 1, fade in 3DPLABox Logo
BNE +checkTwo
    LDA fadeInLoadTimer             ;; LOAD TIMER BEFORE STARTING FADE IN ;;
    CMP #$30                        ;; TIMER LENGTH ;;
    BCS fadeInPaletteTimer
    INC fadeInLoadTimer
    JMP noFadeInLogo   
    
+checkTwo
CMP #$02                            ;; If userScreenByte4 is equal to 2, fade in Title Screen
BNE +checkThree
    LDA fadeInLoadTimer             ;; LOAD TIMER BEFORE STARTING FADE IN ;;
    CMP #$30                        ;; TIMER LENGTH ;;
    BCS fadeInPalette1
    INC fadeInLoadTimer
    JMP noFadeInLogo
    
+checkThree
CMP #$03                            ;;If userScreenByte4 is equal to 3, fade in ______________
JMP RESET



fadeInPaletteTimer:
    LDA fadeInTimer                 ;; LOAD PALETTE TIMER ;;
    BNE fadeInPaletteCountdown      ;; IF NOT 0 JUMP DOWN ;;
    JSR fadeInPaletteChange               
    LDA #$04                        ;; FRAMES BETWEEN FADES ;;
    STA fadeInTimer
    
fadeInPalette1:
    LDA fadeInTimer                 ;; LOAD PALETTE TIMER ;;
    BNE fadeInPaletteCountdown      ;; IF NOT 0 JUMP DOWN ;;
    JSR fadeInPaletteChange1               
    LDA #$04                        ;; FRAMES BETWEEN FADES ;;
    STA fadeInTimer



fadeInPaletteCountdown:
    DEC fadeInTimer                 ;; DECREASE TIMER ;;
    JMP noFadeInLogo



fadeInPaletteChange:
    LDA fadeInStepNum
    CMP #$05
    BCC fadeInStep0

fadeInPaletteChange1:
    LDA fadeInStepNum
    CMP #$05
    BCC fadeInStep4
    

    
noFadeInLogo:
    RTS

fadeInLogo:
    LDA #%00000111
    STA updateScreenData
    RTS



fadeInStep0:   
    INC fadeInStepNum                           ;; INCREMENT STEP ;;
    LDA fadeInStepNum
    CMP #$02
    BCS fadeInStep1
    ;; FIRST STEP IN PROCESS ;;           
    ChangePaletteColors #$0f, #$0f, #$0f, #$0f  ;; BACKGROUND PALETTE 1 ;;
    JMP fadeInLogo

fadeInStep1:
    LDA fadeInStepNum
    CMP #$03
    BCS fadeInStep2
    ;; SECOND STEP IN PROCESS ;;   
    ChangePaletteColors #$00, #$00, #$01, #$02  ;; BACKGROUND PALETTE 1 ;;
    JMP fadeInLogo

fadeInStep2:
    LDA fadeInStepNum
    CMP #$04
    BCS fadeInStep3
    ;; THIRD STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$10, #$11, #$01  ;; BACKGROUND PALETTE 1 ;;
    JMP fadeInLogo

fadeInStep3:
    ;; FINAL STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$30, #$21, #$11  ;; BACKGROUND PALETTE 1 ;;
    JMP fadeInLogo


fadeInStep4:   
    INC fadeInStepNum                           ;; INCREMENT STEP ;;
    LDA fadeInStepNum
    CMP #$02
    BCS fadeInStep5
    ;; FIRST STEP IN PROCESS ;;           
    ChangePaletteColors #$00, #$0f, #$0f, #$0f  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$0f, #$0f, #$0f  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$0f, #$0f, #$0f  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$0f, #$0f, #$0f  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeInLogo

fadeInStep5:
    LDA fadeInStepNum
    CMP #$03
    BCS fadeInStep6
    ;; SECOND STEP IN PROCESS ;;   
    ChangePaletteColors #$00, #$00, #$01, #$02  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$11, #$11, #$11  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$11, #$11, #$11  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$11, #$11, #$11  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeInLogo

fadeInStep6:
    LDA fadeInStepNum
    CMP #$04
    BCS fadeInStep7
    ;; THIRD STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$10, #$11, #$01  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$21, #$21, #$21  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$21, #$21, #$21  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$21, #$21, #$21  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeInLogo

fadeInStep7:
    ;; FINAL STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$30, #$31, #$21  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$38, #$31, #$11  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$33, #$23, #$13  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$26, #$16, #$06  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeInLogo
 

jakenastysnake

New member
This is definitely not an ideal solution, but it does work for the few fade ins that I needed for my 8x8 screens.

First thing, set up your screen bytes: https://www.nesmakers.com/index.php?threads/making-userscreenbyte0-7-work-plus-bonus.5842/

Next, add the following code to doSpritePreDraw.asm. This is the macro function I am using the define all the colors of the palette.

Code:
MACRO ChangePaletteColors arg0, arg2, arg3, arg4
    ;; arg0 =palette number
    ;; arg2 =color 2
    ;; arg3 =color 3
    ;; arg4 =color 4
    LDA arg0
    ASL
    ASL
    TAX
    LDA arg2
    STA bckPal+1,x
    LDA arg3
    STA bckPal+2,x                        
    LDA arg4
    STA bckPal+3,x
    ENDM

Then, add the following code to doSpritePostDraw.asm. This is the function itself. I tried to comment the code. If you don't use all the background palettes, then just remove ChangePaletteColors from those steps.

Code:
LDA userScreenByte4
BEQ noCompanyLogo

    LDA companyLoadTimer            ;; LOAD TIMER BEFORE STARTING FADE IN ;;
    CMP #$30                        ;; TIMER LENGTH ;;
    BCS companyPaletterTimer
    INC companyLoadTimer
    JMP noCompanyLogo  

companyPaletterTimer:
    LDA companyTimer                ;; LOAD PALETTE TIMER ;;
    BNE companyPaletteCountdown     ;; IF NOT 0 JUMP DOWN ;;
    JSR companyPaletteChange              
    LDA #$04                        ;; FRAMES BETWEEN FADES ;;
    STA companyTimer

companyPaletteCountdown:
    DEC companyTimer                ;; DECREASE TIMER ;;
    JMP noCompanyLogo

companyPaletteChange:
    LDA companyStepNum
    CMP #$05
    BCC companyStep0
noCompanyLogo:
    RTS
fadeCompanyLogo:
    LDA #%00000111
    STA updateScreenData
    RTS

companyStep0:  
    INC companyStepNum                          ;; INCREMENT STEP ;;
    LDA companyStepNum
    CMP #$02
    BCS companyStep1
    ;; FIRST STEP IN PROCESS ;;          
    ChangePaletteColors #$00, #$01, #$01, #$01  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$01, #$01, #$01  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$01, #$01, #$01  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$01, #$01, #$01  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep1:
    LDA companyStepNum
    CMP #$03
    BCS companyStep2
    ;; SECOND STEP IN PROCESS ;;  
    ChangePaletteColors #$00, #$11, #$11, #$11  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$11, #$11, #$11  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$11, #$11, #$11  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$11, #$11, #$11  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep2:
    LDA companyStepNum
    CMP #$04
    BCS companyStep3
    ;; THIRD STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$21, #$21, #$21  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$21, #$21, #$21  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$21, #$21, #$21  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$21, #$21, #$21  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

companyStep3:
    ;; FINAL STEP IN PROCESS ;;
    ChangePaletteColors #$00, #$31, #$31, #$31  ;; BACKGROUND PALETTE 1 ;;
    ChangePaletteColors #$01, #$31, #$31, #$31  ;; BACKGROUND PALETTE 2 ;;
    ChangePaletteColors #$02, #$31, #$31, #$31  ;; BACKGROUND PALETTE 3 ;;
    ChangePaletteColors #$03, #$31, #$31, #$31  ;; BACKGROUND PALETTE 4 ;;
    JMP fadeCompanyLogo

On the screen you want to use this, set userScreenByte4 = 1 and create a color palette where all the colors are the same as the default background color (i.e. black) so that it will be the default background when the page loads. Whatever colors you define in each ChangePaletteColors, will decide what the fade looks like for that frame.

I know this code can be improved by referencing tables, instead of passing the values directly in the macro. I also would love to have a more programmatic solution, like suggested by @Jonny here: https://www.nesmakers.com/index.php?threads/4-5-9-fade-in-fade-out.7140/#post-38396

This just does the job I needed it to do in [4.9.x], so I decided to share.

I'm getting my feet wet with NESMaker, and I found your post here. Whenever I test my game, it won't compile. I check the log.txt and each time it shows "Illegal Instruction" on the first ChangePaletteColors. Not sure if this is because I'm using the Adventure module, or if I need to put doSpritePreDraw.asm somewhere else. Any ideas?

Thank you for sharing this by the way. Awesome job!
 
Top Bottom