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.

Coding > question about 15 bit brg

#18981 - Darmstadium - Fri Apr 09, 2004 3:39 am

I want to make a picture in mode 3 fade to black. So you have to decrement each color right? So can you get the seperate values for b r and g from a 16 bit color value like this?
Code:

green = COLOR & ~0x7FE0;
red = (COLOR & ~0x7C1F) >> 5;
blue = (COLOR & ~0x3FF) >> 10;


and just do something like
Code:

blue--;
red--;
green--;

to make the colors darker?

I'd really appreciate your comments and help

#18982 - niltsair - Fri Apr 09, 2004 4:03 am

It's simpler to just use the dedicated BG Effect registers for this :

REG_BLDMOD = 0x0F | 3<<6;
REG_COLY = 8;


This will darken your BG by half.
(REG_BLDMOD =) : BG Effect Register.
(0x0F) : Select All layers to be affected.
(| 3<<6;) : Select darken effect ( 2<<6; for lighten)
(REG_COLY = 8;) : Set intensity of the effect (can be 0-16).

Check into a Gba.h header for the #defines instead of actually using numeric values (0x0F, 3<<6, ...), it'll be much clearer.

If you still want to do it with the pixel value, this would work given you shift back your color values. Why are you using bits complement? Just get the mask directly.

green = COLOR & 0x001F;
red = (COLOR & 0x03E0) >> 5;
blue = (COLOR & 0x7C00) >> 10;

if(green !=0) green--;
if(red!=0) red--;
if(blue!=0) blue--;

DARKENCOLOR = (blue<<10) | (red<<5) | green
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#18985 - poslundc - Fri Apr 09, 2004 5:10 am

Mathematical fades work best in the paletted modes, since you only need to do calculations for the 256 palette entries. It would be extremely inefficient to attempt to do so for the entire screen (as you would have to in Mode 3).

You're much better off using the effects register in this case, as niltsair suggests.

Dan.

#18989 - batblaster - Fri Apr 09, 2004 1:50 pm

Hi ,

is my mistake or all of you are making an error ??? the color format are BGR

Blue , Green , Red

and not BRG

Blue , Red , Green

...
_________________
Batblaster / 7 Raven Studios Co. Ltd
------------------------------------------

#18990 - niltsair - Fri Apr 09, 2004 2:06 pm

I did not pay attention to the color order. It is indeed BGR.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#18998 - ScottLininger - Fri Apr 09, 2004 6:19 pm

EDIT: whoops, you're talking mode 3, not 4... still, this may be useful.

Here's function I use for fading in and fading out via palette manipulation. It's fast enough for most things, though I'm sure it could be optimized.

Code:

#define REDVALUE(c)      (c & 31)
#define GREENVALUE(c)   ((c>>5) & 31)
#define BLUEVALUE(c)   (c>>10)
#define WHITE      0x7FFF
#define BLACK      0x0000
#define RED      0x395A

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 );
         
      }
   }

}



You'd call it with something like:

Code:
FadeIntoColor(BLACK,MainPalette,15);


By changing the "steps" param, you can make very long or very short fades.