SDL_color assignment

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

jim

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.>>> On 12/15/2007 at 6:31 PM, James Barrett wrote:


Lilith

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.

In C++, you can do it thus:

SDL_Color make_color(Uint8 r, Uint8 g, Uint8 b)
{
SDL_Color c= { r, g, b };
return c;
}

#define RED make_color(0xff, 0, 0)
#define GREEN make_color(0, 0xff, 0) // etc…----

Or thus if you want to instantiate all the colors before using them:
(global statics are deprecated)

namespace
{
SDL_Color red, green, blue;
struct color_initializer
{
color_initializer(SDL_Color &red, SDL_Color &green, SDL_Color &blue)
{
red.r= 0xff; red.g= 0; red.b= 0;
green.r= 0xff; green.g= 0; green.b= 0;
blue.r= 0xff; blue.g= 0; blue.b= 0;
}
} initialize_colors(red, green, blue);
}

I forget if this was a GNU C extension or a recent change to the
standard, but in either GNU C or GNU C++ it should work if you do:

#define WHITE (SDL_Color){255,255,255}
#define BLUE (SDL_Color){0,0,255}

-:sigma.SB

Thanks everyone for the replies. I ended up creating static const
SDL_Color objects, one for each color I want to use.On Dec 21, 2007 3:25 PM, Solra Bizna wrote:

I forget if this was a GNU C extension or a recent change to the
standard, but in either GNU C or GNU C++ it should work if you do:

#define WHITE (SDL_Color){255,255,255}
#define BLUE (SDL_Color){0,0,255}

-:sigma.SB