Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Promit

Union tricks?

This topic is 5879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A while back, I thought it might be handy to represent a ARGB Color as a BYTE[4], a DWORD, and A/R/G/B. I built his union:
  
typedef union _Color
{
    struct { float a, r, g, b; }argb;
    DWORD dwColor;
    BYTE ColorArray[4];
}Color;
  
It did come in pretty handy, but there''s one little nagging thing. Accessing the components means I have to go thru the struct, i.e. SomeColor.argb.a = //something Is there anyway to remove the struct from it or somehow make it invisible? I don''t want a, r, g, and b to occupy the same memspace; they should be a struct. I want to do something like omeColor.g which is a little shorter and cleaner. Is there anyway to do this?

Share this post


Link to post
Share on other sites
Advertisement
1. While DWORD and BYTE[4] map to each other nicely, a struct of four floats does not match the others. Are you sure that''s what you want?

2. Nested structs and unions and unions can be anonymous in which case their members appear in the enclosing namespace. i.e. just leave the "argb" off your your struct and you can do "SomeColor.g".

3. Another way to do it is to use a macro. i.e. "#define color_g argb.g" and then "SomeColor.color_g". Sockets code does this sort of thing a lot.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Promit
Is there anyway to remove the struct from it or somehow make it invisible? I don''t want a, r, g, and b to occupy the same memspace; they should be a struct. I want to do something like omeColor.g which is a little shorter and cleaner. Is there anyway to do this?

Yes, there is! (at least in C++)

  
union Color {
struct {
float a, r, g, b;
}; // use an unnamed struct!

DWORD dwColor;
BYTE ColorArray[4];
};


Example usage:

  
void func()
{
Color color;
color.a = 1.0f; // ok!

color.dwColor = 0x00F00F00; // also ok!

}


Do keep in mind what Anon Mike said - Changing ColorArray[4] won''t change "b" - it will change the last byte of "a"...(because each of the floats is 4 bytes, and the sum total of ColorArray is 4 bytes...)

Cheers...

Share this post


Link to post
Share on other sites
quote:
Fruny
The ability to use the members of an unnamed nested union is standard.
It is not so for unnamed nested structs.
The Standard mandates that you would not be able to access the struct.



That's kinda lame... we should submit it as a defect for correction in C++0x.


[edited by - Magmai Kai Holmlor on June 28, 2002 8:54:09 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Fruny
The ability to use the members of an unnamed nested union is standard.
It is not so for unnamed nested structs.
The Standard mandates that you would not be able to access the struct.

Hypocrites.

(it does work in everyone''s favorite not-so-standard C++ compiler, though. (MSVC) )


Share this post


Link to post
Share on other sites
quote:
Original post by Promit
struct { float a, r, g, b; }argb;


Yeah, shouldn''t this be

struct { unsigned char b, g, r, a; }argb;

? Remember, LSB is stored first.

Share this post


Link to post
Share on other sites
quote:
Original post by Anon Mike
1. While DWORD and BYTE[4] map to each other nicely, a struct of four floats does not match the others. Are you sure that''s what you want?



Oops. I wrote the above union/struct off the top of my head and I wasn''t really thinking about it. You''re right, they should be unsigned char, not float.

Lastly, this structure is built for Direct3D. The correct color storage order is ARGB. RGBA would be if I was working in OpenGL, and I don''t know when you might use BGRA.

Hell, as long as MSVC supports it. I never intend to use anything other than MSVC under Win32 anyway. If I was using OpenGL I might care...

____________________________________________________________
Direct3D vs. OpenGL

Share this post


Link to post
Share on other sites
Note to get the correct r,g,b,a components you should have them in the following order

struct SOMECOLOUR
{
union
{
struct{u32 ARGB;}; // directx ARGB
struct{u8 b,g,r,a;}; // correct order for intel endian
};
};

[as indirectX mentioned ;]

[edited by - mark duffill on July 1, 2002 10:34:06 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!