This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

The simplest way to Fade In and Fade Out the screen

The simplest way to Fade In and Fade Out the screen
by on (#235993)
Hello,
I'm looking to fade in and fade out the entire screen when a scene change occur, I want to gradually put the screen from color to black and vicerversa. I read something about subtract $10 every interval of time to the background tile and change the HUE. But I can't find any example or code to understand how to do that in a simple way.

Any code example? Thanks a lot!
Re: The simplest way to Fade In and Fade Out the screen
by on (#235994)
Well yeah if you have a color X and you subtract $10 from X, that color is going to get darker while maintaining its hue.

Image

See how colors with the same hue have the same highest digit?

Here's some example code:

Code:
copy_palette: ; Put this in your NMI handler
  lda #$3F
  sta PPUADDR
  ldy #$00
  sty PPUADDR
loop:
  lda (palete_ptr), y
  sec
  sbc fade_amount
  bcs :+
  lda #$0F
:
  sta PPUDATA
  iny
  cpy #32
  bcc loop
  rts

fade_out: ; Call when you want to fade
.repeat 5, i
  lda #$10*i
  sta fade_amount
  jsr wait_for_nmi
.endrepeat
  rts

fade_in: ; Call when you want to fade
.repeat 5, i
  lda #$40 - $10*i
  sta fade_amount
  jsr wait_for_nmi
.endrepeat
  rts

Re: The simplest way to Fade In and Fade Out the screen
by on (#236002)
Ok thanks, I will try as soon as I can! :)
Re: The simplest way to Fade In and Fade Out the screen
by on (#236047)
here's from megaman 3 (copy-pasted from my romhack megaman odyssey)
just JSR to palfade_restore ...and/or palfade_dark, that's all :)

Code:
   .byte "PALETTE FADE"
;Fade in/out palette
;600-61F current BG/Spr Pal
;620-63F backup/previous BG/Spr Pal
;colortrig non-zero, will tell NMI to update pal on next frame

PalFade_Speed = $04

palfade_restore:
   lda #$30
   ldx #$F0
   bne PalFade_Proc

palfade_darken:
   lda #$10
   tax
;-------------------------------------------
PalFade_Proc
   stx $0D
.Store_PalIndex
   sta $0F
   ldy #$1F
.loop_AddPal_Value
   lda $620,y
   sec
   sbc $0F
   bpl .Store_PalValue
   lda #$0F   ;black
.Store_PalValue
   sta $600,y
   dey
   bpl .loop_AddPal_Value
   sty ColorTrig
   ldx #PalFade_Speed
   jsr waitvblank_x

;next session, 4 total
   lda $0F
   clc
   adc $0D
   bmi .PalFade_RTS
   cmp #$50
   bne .Store_PalIndex
.PalFade_RTS
   rts
Re: The simplest way to Fade In and Fade Out the screen
by on (#236053)
I don't see any immediate handling of this in the routines, but it's important to remember that colour $0D should not be used (see 2nd to last paragraph).
Re: The simplest way to Fade In and Fade Out the screen
by on (#236057)
Here's my answer from a very similar thread a few weeks back. There might be other useful things in that thread if you read the rest of it though, it's answering the same question.
https://forums.nesdev.com/viewtopic.php?p=224707#p224707