Sign in to follow this  
xegoth

Using D3DCOLOR

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[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
-----------
0xfffeffff

or
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this