Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Struct with a #define in it

This thread is locked; no one can reply to it. rss feed Print
Struct with a #define in it
AceBlkwell
Member #13,038
July 2011
avatar

I've created an include file of defines to simplify colors IE.

#define WHITE al_map_rgb(255,255,255)

I have created an array of structs and want to store block color. I created the struct as such .

struct BLOCKS {
float fTX;
float fTY;
float fBX;
float fBY;
std::string sColor;
};

BLOCK Blocks[8]

The problem is, if I store the color in sColor as "WHITE" then the quotation marks negate the define data and the string is just seen as "a string".
If I leave the quotation marks off, I get a compiler error due to the mismatch.

error: could not convert 'al_map_rgb(255, 255, 255)' from 'ALLEGRO_COLOR' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
#define WHITE al_map_rgb(255,255,255)

My question is how to store the define label so it is actually storing the data and not the label? Thanks.

DanielH
Member #934
January 2001
avatar

Doesn't work that way.

Define doesn't store data or labels. It is command to the compiler to process before compiling. In the code, every WHITE is replaced with your al_map_rgb.

However, strings are ignored because they are char arrays.

char color[6] = "WHITE";

to the compiler it sees this

char color[6] = {57, 48, 49, 54, 45};

and not actual text

AceBlkwell
Member #13,038
July 2011
avatar

Thanks for the clarification, but you didn't answer my question. ;D.

Unless you are saying you can't mix the two.

DanielH
Member #934
January 2001
avatar

You can't. At least not using defines.

What about using a std::map with your color list?

std::map<std::string, ALLEGRO_COLOR> color_list;

color_list["WHITE"] = al_map_rgb(255, 255, 255);

AceBlkwell
Member #13,038
July 2011
avatar

That looks doable. Out of curiosity, could I...

al_draw_filled_rectangle(fTX, fTY, fBX, fBY, "WHITE");

or

al_draw_filled_rectangle(fTX, fTY, fBX, fBY, color_list["WHITE"]);

Also I'm a little confused (obviously), If my colors are in an array themselves, how do I store them as a value in another array. Is std::map a storage type? IE

Blocks[5].sColor = color_list["WHITE"];

The reason for the issue overall is I'm wanting to initialize an array and then put the rectangle command in for loop. So the amount of rectangles could vary with out needing to add and subtract a bunch of line items. If I went from 8 to 16 rectangles I could just update a LIMIT define and not have to add 8 additional lines to a previous 8 individual lines

DanielH
Member #934
January 2001
avatar

Access it like colorlist["color name"]

Your sColor is a string so it would be: Blocks[5].sColor = "WHITE";

and when you need it: color_list[Blocks[5].sColor]

AceBlkwell
Member #13,038
July 2011
avatar

Makes sense. Thanks Daniel

Just thought of something. Is ALLEGRO_COLOR a storage type? Why can't I...

struct BLOCKS {
float fTX;
float fTY;
float fBX;
float fBY;
ALLEGRO_COLOR AColor; //or ALLEGRO_COLOR *AColor
};

DanielH
Member #934
January 2001
avatar

ALLEGRO_COLOR is a simple struct with 3 floats (red, green and blue)

You could do what I do and store it as hex

int32_t white = 0xffffff;
int32_t red = 0xff0000;

There are functions in the color add on to convert.

Or write your own

ALLEGRO_COLOR color = fromHex(int32_t hex)
{
     return al_map_rgb((hex & 0xff0000) >> 16, (hex 0xff00) >> 8, hex & 0xff));
}

AceBlkwell
Member #13,038
July 2011
avatar

I appreciate your time Daniel. Using your suggestions, I'll figure something out. Thanks again.

Polybios
Member #12,293
October 2010

You could also simply define constants initialized using al_map_rgb_f, e.g.:

namespace Colors {
const ALLEGRO_COLOR WHITE = al_map_rgb_f(1.0, 1.0, 1.0);
}

I think for al_map_rgb (without "_f") to work, al_init needs to be called in advance. So not usable for static initialization. Or has this been changed?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Polybios. Using your suggestion I created an .H file using the const ALLEGRO_COLOR. I then updated my struct to include ALLEGRO_COLOR sCards. Lastly I used sCards = COLOR::WHITE and it works.

I'm going to try Daniel's map suggestion as well.

In either case I appreciate everyone's recommendations.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: