Fade IN and OUT

baardbi

Well-known member
I recently published a tutorial on how to fade out screens, but here's one that both fades in and out. It fades in and out both the background and the sprites.

PS! This will take up 25 bytes of RAM.

Before you begin make sure you do the following:

- Set up user screen bytes:

- Create a Zero Page variable called backgroundPaletteBackup and make sure it is set up with 12 bytes
- Create a Zero Page variable called spritePaletteBackup and make sure it is set up with 12 bytes
- Create a User Variable called screenFadeIn
- Download the attached file called fading.zip. Unzip it and place the file fading.asm in ...\GameEngineData\Routines\BASE_4_5\Game\Subroutines

1. Go to Project Settings - Script Settings. At the top (under System) you will find a script define called Load Subroutines. Select it and click Edit. Put this following line at the top of the file:

.include ROOT\Game\Subroutines\fading.asm
Save the file and close it.

2. Go to Project Settings - Project Labels and find Screen Flags. Change Screen Flag 7 to Fade In.

3. Put the following code in the extraScreenLoad file. It's in Project Settings - Script Settings. You can find it under Game. Select Extra Screen Load and click Edit.
LDA ScreenFlags00
AND #%00000001
BEQ dontFadeIn
LDA #1
STA screenFadeIn
dontFadeIn:
Put it right above doneWithExtraCheck: Save the file and close it.

4. Put the following code at the end of the doSpritePostDraw file. It's in Project Settings - Script Settings. You can find it under Game. Select Sprite Post-draw and click Edit.
;;; Handle fade in
LDA screenFadeIn
BEQ +dontFadeIn
JSR fadeIn
LDA #0
STA screenFadeIn
+dontFadeIn:
Put it at the bottom of the file. Save the file and close it.

5. Set all colors in background palette 63 to black. Do the same for monster palette 63.

Now you're done. On the screens that you want to fade in you just select Fade In under Screen Info.
fadeIn.png

You can use fade in and fade out wherever you want in your code by writing:

JSR fadeIn

or

JSR fadeOut

For example you can make your warp tiles fade the screen to black before a warp by changing your warp tile to this:
CPX player1_object
BNE notPlayerForWarpTile

JSR fadeOut

WarpToScreen warpToMap, warpToScreen, #$01
;; arg0 = warp to map. 0= map1. 1= map2.
;; arg1 = screen to warp to.
;; arg2 = screen transition type - most likely use 1 here.
;; 1 = warp, where it observes the warp in position for the player.
notPlayerForWarpTile:
Or you can change the input script startGameWithNewContinuePoints to fade out the screen when you press the start button on the start screen:
;;; This script can be used as a start game input script.
;;; It will take you to the first screen and set up new continue points based on
;;; that screen's info.

JSR fadeOut

LDA screenUpdateByte
ORA #%00000100
STA screenUpdateByte

LDA warpToMap

STA warpMap

LDA warpToScreen
STA currentNametable

LDX player1_object
STA Object_screen,x

LDA #$01
STA screenTransitionType ;; is of warp type


LDA gameHandler
ORA #%10000000
STA gameHandler ;; this will set the next game loop to update the screen.
RTS

The only thing I have noticed is that when fading in to a new screen the monsters and game objects are not faded in (only the player). They will pop in after the fade is complete.
 

Attachments

  • fading.zip
    997 bytes · Views: 42
Last edited:

kevin81

Well-known member
Awesome tutorial! Thanks for this script (y)

Re:
The only thing I have noticed is that when fading in to a new screen the monsters and game objects are not faded in (only the player). They will pop in after the fade is complete.

I think this is due to how NESmaker updates palettes in the NMI. Looking at BASE_4_5\System\NMI.asm lines 130-137:

Code:
    LDA updateScreenData
    AND #%00000001
    BNE doPaletteUpdates
    JMP +

 doPaletteUpdates:
     .include SCR_LOAD_PALETTES
     JMP skipScreenUpdates

If bit-0 of updateScreenData is set, the background palette gets updated and then all other updates (e.g. sprite palettes) are omitted.

I think that if you comment out line 137 (JMP skipScreenUpdates), it should then also check for sprite palette changes and update those accordingly within the same frame. I think the vblank period should be long enough to update 32 palette values instead of 16 per frame, but that's something I'd have to test later when I'm near a NESmaker-installed device :)
 

baardbi

Well-known member
Awesome tutorial! Thanks for this script (y)

Re:


I think this is due to how NESmaker updates palettes in the NMI. Looking at BASE_4_5\System\NMI.asm lines 130-137:

Code:
    LDA updateScreenData
    AND #%00000001
    BNE doPaletteUpdates
    JMP +

 doPaletteUpdates:
     .include SCR_LOAD_PALETTES
     JMP skipScreenUpdates

If bit-0 of updateScreenData is set, the background palette gets updated and then all other updates (e.g. sprite palettes) are omitted.

I think that if you comment out line 137 (JMP skipScreenUpdates), it should then also check for sprite palette changes and update those accordingly within the same frame. I think the vblank period should be long enough to update 32 palette values instead of 16 per frame, but that's something I'd have to test later when I'm near a NESmaker-installed device :)
That's interesting. I tried commenting out line 137 but it didn't work. I have a theory that it depends on where I put the fade-in code. Depending on where I put it, sometimes no objects would be faded in and other times the player would fade in, but never any other objects. I did a lot of experiments on where to place the fade in code when I worked on this. The hard thing is to find the exact moment when all objects are loaded and when the game actually starts.
 

vanderblade

Active member
That's interesting. I tried commenting out line 137 but it didn't work. I have a theory that it depends on where I put the fade-in code. Depending on where I put it, sometimes no objects would be faded in and other times the player would fade in, but never any other objects. I did a lot of experiments on where to place the fade in code when I worked on this. The hard thing is to find the exact moment when all objects are loaded and when the game actually starts.
The FadeOut works great (replaced my older solution for this), but where would you suggest putting the JSR fadeIn? I set the 63 palettes to all black, but I can't get the fadeIn routine to work for me.
 

dale_coop

Moderator
Staff member
The FadeOut works great (replaced my older solution for this), but where would you suggest putting the JSR fadeIn? I set the 63 palettes to all black, but I can't get the fadeIn routine to work for me.
have you tried at the end of Post Load Screen or the Handle Screen Loads scripts?
 

baardbi

Well-known member
The FadeOut works great (replaced my older solution for this), but where would you suggest putting the JSR fadeIn? I set the 63 palettes to all black, but I can't get the fadeIn routine to work for me.
Hmm. The fade in part is handled in step 3 and 4 of this tutorial. Are you trying to fade in during gameplay or something like that?
 
Last edited:

vanderblade

Active member
Hmm. The fade in part is handled in step 3 and 4 of this tutorial. Are you trying to fade in during gameplay or something like that?
It's weird. But probably something to do with the fact I'm using a highly customized engine at this point. At least the fadeout works great. :)
 

baardbi

Well-known member
It's weird. But probably something to do with the fact I'm using a highly customized engine at this point. At least the fadeout works great. :)
I would really like to help you, but it's a little hard to troubleshoot without looking at any code. Maybe you can doublecheck step 3 and 4, and make sure you're not using screen flag 7 for something else. One thing I noticed when working on this is that if the screen you're fading in to has very bright colors the fade in is less noticeable.
 
Top Bottom