|
A question about DWORDs and WORDs...
I have a question to do with using DWORDs for color values in DirectX 8 (but its more of a general question, so thats why I''m posting it here).
When DirectX uses a DWORD for a color value, its something like 0x00ffffff where the first 2 zeros are alpha values, the next 2 fs are the red color, the next 2 fs are the green color, and the last 2 are the blue color. Now lets say I want to do some blending by using the difuse color with alpha blending enabled. How would I specifically change just the alpha value by using something like an int? I don''t want to have to wrtie out 30 different alpha values and have a giant switch statement to sort it out. Could I specify and integer that would be below 225 and then bitshift it by 6, like so?
Would something like that work?
Yes, but you would have to shift it 24 steps, not 6. That's because when shifting you specify how many bits to shift, and each two digits in a hexadecimal number represents a byte (8 bits), so you have: (3 bytes)*(8 bits each) = (24 bits).
Edited by - Dactylos on August 17, 2001 1:29:48 PM
Edited by - Dactylos on August 17, 2001 1:29:48 PM
Ok, thanks. I guess I was thinking bytes, not bits. And lets suppose I did that with the alphavalue being 220. I wouldn''t have to convert that to hex/oct or something, would I?
No conversion necessary. If you want alpha of 220, just do this
// set color
myColor = 0x00rrggbb;
// set alpha
myColor |= 220<<24;
// set color
myColor = 0x00rrggbb;
// set alpha
myColor |= 220<<24;
Moe, It''d be like this:
DWORD color = 0x00ffffff;
color |= (220 << 24);
That''d make the alpha value 220.
DWORD color = 0x00ffffff;
color |= (220 << 24);
That''d make the alpha value 220.
My Gamedev Journal: 2D Game Making, the Easy Way
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
To set the alpha value, just use
color |= 220 << 24;
You must OR the new value, not add it.
Basically, as you know, a color is in the format
AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
So if you for instance have the color red:
0000 0000 1111 1111 0000 0000 0000 0000 (0x00FF0000)
And you have your alpha value 220:
0000 0000 0000 0000 0000 0000 1101 1100 (0x000000DC)
By shifting left 24 you get:
1101 1100 0000 0000 0000 0000 0000 0000 (0xDC)
Shifting left puts the alpha value in the right spot basically.
Then you add it into your color using OR. Using the color red above:
0000 0000 1111 1111 0000 0000 0000 0000 (Color)
1101 1100 0000 0000 0000 0000 0000 0000 (Alpha)
----------------------------------------
=1101 1100 1111 1111 0000 0000 0000 0000
OR evaluates to 1 if either or both operands are 1.
Edited by - Midnight Coder on August 17, 2001 6:33:31 PM
color |= 220 << 24;
You must OR the new value, not add it.
Basically, as you know, a color is in the format
AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
So if you for instance have the color red:
0000 0000 1111 1111 0000 0000 0000 0000 (0x00FF0000)
And you have your alpha value 220:
0000 0000 0000 0000 0000 0000 1101 1100 (0x000000DC)
By shifting left 24 you get:
1101 1100 0000 0000 0000 0000 0000 0000 (0xDC)
Shifting left puts the alpha value in the right spot basically.
Then you add it into your color using OR. Using the color red above:
0000 0000 1111 1111 0000 0000 0000 0000 (Color)
1101 1100 0000 0000 0000 0000 0000 0000 (Alpha)
----------------------------------------
=1101 1100 1111 1111 0000 0000 0000 0000
OR evaluates to 1 if either or both operands are 1.
Edited by - Midnight Coder on August 17, 2001 6:33:31 PM
I think the formula
color |= 220 << 24;
will only work, if the 8 alpha-bits are all 0 where they should be in the new value!
For example, if you wish to use this formula to set alpha-value 220 into a color that allready has a alpha-value of - lets say - 35 (0010 0011), it would OR the two together and you''d get
1101 1100
0010 0011
---------
1111 1111
which gives you a new alpha-value of 255, not 220!
So it might be better to mask the old alpha-value off before you use the formula.
color = (color & 0x00FFFFFF) | (alpha << 24)
This should do the trick! Of course, only if you really wish to set the new alpha-value. If you wish to only modify your old value, you should stick to your old formula and do some checks before to see if the new value is not out of range (0 - 255).
color |= 220 << 24;
will only work, if the 8 alpha-bits are all 0 where they should be in the new value!
For example, if you wish to use this formula to set alpha-value 220 into a color that allready has a alpha-value of - lets say - 35 (0010 0011), it would OR the two together and you''d get
1101 1100
0010 0011
---------
1111 1111
which gives you a new alpha-value of 255, not 220!
So it might be better to mask the old alpha-value off before you use the formula.
color = (color & 0x00FFFFFF) | (alpha << 24)
This should do the trick! Of course, only if you really wish to set the new alpha-value. If you wish to only modify your old value, you should stick to your old formula and do some checks before to see if the new value is not out of range (0 - 255).
"If I knew where I was I wouldn't be here, believe me!"
It would pretty much be the same procedure for changing one of the RGB values, but bitshift by 16 and 8, right?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement