Using D3DCOLOR

Started by
5 comments, last by xegoth 19 years, 6 months ago
Alright I've got a particle engine that uses point sprites and I want to make my sprites fade to black as they die out. My sprites store their color as type D3DCOLOR. The only problem is, I'm not totally clear on how to decrement color values with D3DCOLOR. I was hoping I could do something like

D3DCOLOR myColor;

myColor.r -= .01;

Unfortunately the definition of D3DCOLOR is something like: typedef DWORD D3DCOLOR which isn't any help to me. I know how to set D3DCOLOR to something new, I do it this way:

myColor = D3DCOLOR_COLORVALUE( 1.0, .05, 0.1, 1.0f );

But what if I don't want to reset the entire color, I just want to fade it to black? There must be an easy way to do this?
Advertisement
[As with everything, I'm getting old, so my memory isn't as reliable as it should be.]

Think of it this way:

a DWORD is 4 bytes, Alpha, Red, Green, Blue and is usually in the form:
0xffffffff[255,255,255,255  opaque white]

Each 2 characters represents 1 of the bytes. The first ff is alpha=255. So to lower the red value like you explain there do something like
 0xffffffff-0x00010000----------- 0xfffeffffor 255,254,255,255


just don't go below 0 or above 255, or else the 'carry' of addition/subtraction will cause one of the other colors to be changed. And -make sure- that there's 8 characters in the hex stuff that you assign/add/subtract. If there's not it will pad the number with zeroes causing bugs that are difficult to track down.
Aaaah.. Good answer but I'd like to avoid using hex if I can. Is there a way to do it without manually changing the hex? Since I don't know hex it's not something I'd like to mess with if I can avoid it.
You could simply call myColor=... with different parameters, but that would require you to keep the rgb values as well as the DWORD...

There's probably a D3D function to split the DWORD back into bytes, which you could then edit and pass back...

I don't know, I actually keep the RGB values for things that change alot rather than the DWORD.
You could get away with just subtracting D3DCOLOR_ARGB(0, 1, 1, 1), I believe.
MyColor -= D3DCOLOR_ARGB(0, 1, 1, 1);
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Or you could define your own color type as such:
union MY_COLOR{  D3DCOLOR Color;  struct  {    BYTE Alpha;    BYTE Red;    BYTE Green;    BYTE Blue;  };};

Then you can access things either as an entire D3DCOLOR type, by using the Color member, or according to each component, Alpha, Red, Green, or Blue.

Note: I can't remember how it'll work out; you might need to change the order of the components, and you might also need to check out memory packing options (often changed through the #pragma directive), to make sure that the 4 bytes are all right next to each other in a 32-bit chunk. But you likely won't need to bother. Just a warning in case, since I can't remember.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by Agony
You could get away with just subtracting D3DCOLOR_ARGB(0, 1, 1, 1), I believe.
MyColor -= D3DCOLOR_ARGB(0, 1, 1, 1);


If that works then its problem solved. I'll give it a try :)

This topic is closed to new replies.

Advertisement