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.

DS development > Setting the Alpha bit of 16bit color to 0

#93385 - TheRain - Tue Jul 18, 2006 10:15 pm

I am trying to set the alpha bit in a 16 bit color to 0 and I cannot think of the solution to this.

I have seen a lot of examples for setting it to 1 but I want to set it to 0.

people do things like u16 col=RGB15(12,34,25)|BIT(15);

but you cannot OR a zero on there if there is a one. Any idears??

I tried making a bit pointer to it, but could figure out how to do it.

#93386 - Mighty Max - Tue Jul 18, 2006 10:20 pm

TheRain wrote:

I have seen a lot of examples for setting it to 1 but I want to set it to 0.

people do things like u16 col=RGB15(12,34,25)|BIT(15);

but you cannot OR a zero on there if there is a one. Any idears??


Just don't or and it won't be set:

u16 col = RGB15(31,31,31) ;
=> col = 0111 1111 1111 1111

u16 col = RGB15(31,31,31) | BIT(15) ;
=> col = 1111 1111 1111 1111

However, keep in mind that a pixel can only be transparent if there is something below this pixel to get drawn (another BG, sprites, whatever)
_________________
GBAMP Multiboot

#93387 - TheRain - Tue Jul 18, 2006 10:33 pm

Thanks, but let's say the alpha bit is 1 and I want to set it to 0. If you need to know why... I'm writting a subroutine that will read the RGB, match it up with an RGB that will represent alpha... then set alpha to zero if it matches that RGB... I want the function to accept 16 bit values and perform this function under the conditions that the alpha bit is set or not set both.

I just tried to do this ~(~colorRGB15|1<<15) and that didn't seem to work either... though I would think it would because this would invert the bits, then make bit 15 1 no matter what it is now, then invert it again which would make it the original number and 0 at bit 15??


like NOT 1111 1111 1111 1111 will equal 0000 0000 0000 0000
OR it with 1000 0000 0000 0000 will equal that
then NOT it again will equal 0111 1111 1111 1111

#93392 - Valmond - Tue Jul 18, 2006 10:41 pm

If you know the bithddepth (8,16,32 etc) you can just
do a

Code:
x&=$FFFF-(1<<bit); //16bits value
x&=$FF-(1<<bit); //8bits value

sort of thing, where 'bit' is the bitnumber to put out.

/Valmond

#93394 - Mighty Max - Tue Jul 18, 2006 10:58 pm

correct.

more easy readable:

u16 col=sourcecolor & ~BIT(15);

will create a copy of sourcecolor with the opacity bit cleared.
_________________
GBAMP Multiboot

#93398 - TheRain - Tue Jul 18, 2006 11:31 pm

BHahaha!

I can't believe that... that's the function I had to begin with then I got very confused because it wasn't working... when all I had to do was put it in parenthesis to make the order of operations right!

Might Max... that's the right solution. Or, the same thing as

u16 col=(sourcecolor&0x7FFF) ;

so here is my final, working routine:
Code:

static inline u16 set_Alpha(u16 buffer, u16 alphaCol)
{    
   if(buffer==(alphaCol|1<<15)||buffer==(alphaCol&0x7FFF))
      {
      return (alphaCol&0x7FFF);
      }
      else
      {
       return (buffer|BIT(15));
      }
}



So with this function, you can draw up a 16 bit bitmap in like photoshop, make the background color a color you want to represent alpha.... say RGB(31,0,31) since that's an ugly ass color anyhow

alphaCol=RGB(31,0,31);

for(int i=0;i<32*32;i++)
{
SPRITE_GFX[i]=set_Alpha(((u16*)sprite1_bin)[i],alphaCol);
}



But apparently that is not all that necessary to write it to set bit 15 to 0 since most of the converters will have already set it to 0... so it would be a matter of setting the non-alpha ones to 1.

But I wanted to figure it out both out of curiosity and to have a flexible function.

Thanks guys for working through this with me!!