gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Graphics > Simple trouble...

#25906 - pio_troland@wp.pl - Tue Aug 31, 2004 1:45 pm

How to change a backgroud?
BG1 --> PRESS START --> FLUENT EFFECT OF CHANGE TO --> BG2

#25913 - tepples - Tue Aug 31, 2004 4:40 pm

What kind of effect do you want?

First try this: scroll out one background, load VRAM, and scroll in another.

If each screen uses only one rot/scale background or only two text backgrounds, you can use effects such as a crossfade (by using the blend mode registers) or a wipe (by using the window registers).
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#25918 - ScottLininger - Tue Aug 31, 2004 5:21 pm

Probably the most old fashioned method is to fade your palette to be all one color (usually black), turn on BG2, then fade your palette back.

Here are a couple of simple functions. It's not the fastest way to do this, but the code should be clear enough.

Code:

// Your palette memory location
u16* paletteMem = (u16*)0x5000000;   

// some predefined colors
#define WHITE         0x7FFF
#define BLACK         0x0000
#define RED            0x395A

// some macros to pull out specific color bits
#define RGB(r,g,b)      ((r)+((g)<<5)+((b)<<10))
#define REDVALUE(c)      (c & 31)
#define GREENVALUE(c)   ((c>>5) & 31)
#define BLUEVALUE(c)   (c>>10)

void FadeIntoColor(u16 targetColor,u16* bmpPalette,int fadeSteps) {

   int readX, step, red,green,blue;
   u16 sourceColor;

   for (step=0;step<=fadeSteps ;step++ ){
      WaitForVblank;
      for(readX = 0; readX < 256; readX++) {
         
         sourceColor = bmpPalette[readX];
         red = ((REDVALUE(sourceColor) * (fadeSteps-step)) + (REDVALUE(targetColor) * step)) /fadeSteps;
         green = ((GREENVALUE(sourceColor) * (fadeSteps-step)) + (GREENVALUE(targetColor) * step)) /fadeSteps;
         blue = ((BLUEVALUE(sourceColor) * (fadeSteps-step)) + (BLUEVALUE(targetColor) * step)) /fadeSteps;

         paletteMem[readX] = RGB( red , green , blue );
      }
   }

}

void FadeFromColor(u16 targetColor,u16* bmpPalette,int fadeSteps) {

   int readX, step, red,green,blue;
   u16 sourceColor;
   

   for (step=0;step<=fadeSteps ;step++ ){
      WaitForVblank();   
      for(readX = 0; readX < 256; readX++) {
         
         sourceColor = bmpPalette[readX];
         red = ((REDVALUE(targetColor) * (fadeSteps-step)) + (REDVALUE(sourceColor) * step)) /fadeSteps;
         green = ((GREENVALUE(targetColor) * (fadeSteps-step)) + (GREENVALUE(sourceColor) * step)) /fadeSteps;
         blue = ((BLUEVALUE(targetColor) * (fadeSteps-step)) + (BLUEVALUE(sourceColor) * step)) /fadeSteps;

         paletteMem[readX] = RGB( red , green , blue );
         
      }
   }
}


// Change palette to black with 15 steps of fading
FadeIntoColor(BLACK,myPalettePointer,15);

// here's where you'd switch around your backgrounds

// Change palette back with 15 steps of fading
FadeFromColor(BLACK,myPalettePointer,15);




-Scott