[SDL] SDL_color assignment

Lilith Calbridge lilith at dcccd.edu
Sat Dec 15 17:09:06 PST 2007



>>> On 12/15/2007 at 6:31 PM, James Barrett <xucaen at gmail.com> wrote:
> Hi, I've been searching google for a way to assign an SDL_Color struct
> from a single numeric value. I might be misremembering, but I thought
> there was a way to do this. I thought I remembered seeing somewhere an
> example using preprocessor defines to store colors (probably as hex
> values) and then using those values to asign to an SDL_Color.
> 
> Obviously, when I try it like this, I get a compile error:
> 
> #define WHITE {255,255,255}
> #define BLUE  {0,0,255}
> 
>     SDL_Color c = WHITE;//OK
>     c = BLUE;//Compile error
> 
> 
> The only other way I can think of is to store SDL_Color objects for each
> color I want to use.
> 
> static const SDL_Color WHITE = {255,255,255};
> static const SDL_Color BLUE = {0,0,255};
> 
>     SDL_Color c = WHITE;//OK
>     c = BLUE;//Now this is OK

Without looking up what SDL_Color is, I'm assuming it's a structure.  When you declare an object of the structure type you can assign it as you have it.  The #define expands like

SDL_Color c = {255, 255, 255};

but when you just assign a variable outside the declaration of the variable it doesn't work like that.  It expands to 

c = {0, 0, 255};

which isn't valid.

If you're using C++ you might be able to get what you want by overloading the assignment operator as a copy constuctor with some redefinition of your #defines.

#define WHITE SDL_Color {255,255,255}

Just guessing.

-- 
Lilith





More information about the SDL mailing list