On 2/26/07, Luc Le Blanc <[EMAIL PROTECTED]> wrote:
I'm surprised the RGBColorType was not declared as a union of UInt32 and other elements. But to copy or compare RGBColorTypes quickly without having to resort to MemMove or Compare, is there a CPU-effective smart cast I can use (( UInt32 ) is refused by the compiler)? Is this cost-free (CPU-wise):
you have a point, it should have been: typedef struct RGBColorType { union { struct { uint8 r; uint8 g uint8 b; } elements; uint32 rgb; } data; } RGBColorType ; but to your question. yes.
RGBColorType a, b; *( UInt32 * ) &a = 0xffffffffL; *( UInt32 * ) &b = 0x80800101L;
*((UInt32 *)&a) = 0xffffffffL; may help the compiler understand better - why not use a macro to make your code easier to understand at the same time :) #define RGBColorTypeSet(a, val) (*((UInt32 *)(a)) = (val) #define RGBColorTypeCompare(a, b) ( (*((UInt32 *)(a)) == *((UInt32 *)(b))) ) then do: RGBColorTypeSet(&a, 0xffffffffL); RGBColorTypeSet(&b, 0x80800101L); if (RGBColorTypeCompare(&a, &b)) { .. same } else { .. different } C/C++ can be pretty nasty about how you code your pointer casts; so, just put extra brackets in there to help. it all comes down to memory; as long as the RGBColorType components are after each other; it'll work. you can do a lot of cool tricks like color averaging etc with this too. -- // Aaron Ardiri -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/