Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Making color variables

This thread is locked; no one can reply to it. rss feed Print
Making color variables
imgregduh
Member #15,241
July 2013

I know to make the variable itself is something like this

ALLEGRO_COLOR red = al_map_rgb(255,0,0);

but I notice if I put that line at the top the color will not appear to something in drawing in the game loop. Also, when i move that line right above the gameloop it works.

I am wondering if I can make the above line a global variable because im using it all over the place

I did try making a separate header and putting that above line in it and trying to call it in the main function but it didnt appear. Also, putting it outside above the main function didnt work either.

torhu
Member #2,727
September 2002
avatar

This the general idea:

/* define it outside of any function */
static ALLEGRO_COLOR red;

int main() {
    /* initialize before first use (and after calling al_init()) */
    red = al_map_rgb(255,0,0);
}

You might want to give it a name that isn't tied to the color though, like "line_color" or "rocket_color". But it depends on how you use it.

If you want it to be available to more than just a single file, remove "static" from the definition and put extern ALLEGRO_COLOR red; in some header file that you then include where you want to use the symbol.

LennyLen
Member #5,313
December 2004
avatar

You can't call Allegro functions before initializing the library, which happens after main() is run, so any lines like that cannot be made global. You can make the variable global and have a function that runs after you initialize Allegro that contains color assignments.

SiegeLord
Member #7,827
October 2006
avatar

Allegro functions must run after you call al_init, so that precludes initializing them globally. You could make the variable global and initialize it after al_init though.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Mark Oates
Member #1,146
March 2001
avatar

There's also al_color_name("red") using the x11 color names.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

imgregduh
Member #15,241
July 2013

Alright, thanks guys for the info

Mark Oates
Member #1,146
March 2001
avatar

LennyLen said:

You can't call Allegro functions before initializing the library, which happens after main() is run, so any lines like that cannot be made global.

I use the color functions to define symbols globally, they work prior to initialization.

I have:

#SelectExpand
1namespace color 2{ 3 4const ALLEGRO_COLOR null_color = al_map_rgba_f(0, 0, 0, 1.0); 5const ALLEGRO_COLOR transparent = al_map_rgba_f(0, 0, 0, 0); 6const ALLEGRO_COLOR aliceblue = al_color_name("aliceblue"); 7const ALLEGRO_COLOR antiquewhite = al_color_name("antiquewhite"); 8const ALLEGRO_COLOR aqua = al_color_name("aqua"); 9const ALLEGRO_COLOR aquamarine = al_color_name("aquamarine"); 10const ALLEGRO_COLOR azure = al_color_name("azure"); 11const ALLEGRO_COLOR beige = al_color_name("beige"); 12const ALLEGRO_COLOR bisque = al_color_name("bisque"); 13const ALLEGRO_COLOR black = al_color_name("black"); 14//.. 15 16}

with a bunch of other color stuff.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

LennyLen
Member #5,313
December 2004
avatar

I use the color functions to define symbols globally, they work prior to initialization.

I haven't looked at how those functions are implemented, but I'd say that you're simply lucky that they do work for you prior to initializing Allegro. Such behavior isn't guaranteed to work, and clearly doesn't for imgregduh.

SiegeLord
Member #7,827
October 2006
avatar

Those do happen not to require initialization, but it's undocumented and shouldn't be relied on. I could very easily change al_color_name to require Allegro to be initialized without breaking any valid programs by simply using a slightly different code path.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

imgregduh
Member #15,241
July 2013

LennyLen said:

Mark Oates said:
I use the color functions to define symbols globally, they work prior to initialization.
I haven't looked at how those functions are implemented, but I'd say that you're simply lucky that they do work for you prior to initializing Allegro. Such behavior isn't guaranteed to work, and clearly doesn't for imgregduh.

Actually, the way that Mark did it, it works. It seems that namespace is doing the trick

SiegeLord
Member #7,827
October 2006
avatar

imgregduh said:

Actually, the way that Mark did it, it works. It seems that namespace is doing the trick

No, it is not. As I said, it just so happens that the functions Mark Oates use are currently implemented not to require Allegro to be initialized (a fact that should not be relied upon). al_map_rgb requires Allegro to be initialized, and there's no way around it.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

imgregduh
Member #15,241
July 2013

SiegeLord said:

No, it is not. As I said, it just so happens that the functions Mark Oates use are current implemented not to require Allegro to be initialize (a fact that should not be relied upon). al_map_rgb requires Allegro to be initialized, and there's no way around it.

So, what is recommended? Do I just redeclare the variable for white with this al_map_rgb(0,0,0) function in each file i need it in ?

Mark Oates
Member #1,146
March 2001
avatar

SiegeLord said:

.... just so happens that the functions Mark Oates use are currently implemented not to require Allegro to be initialized...

mwahahaa mWAAHAHAHAHAH!! >:(:D>:(:D

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

torhu
Member #2,727
September 2002
avatar

imgregduh said:

So, what is recommended? Do I just redeclare the variable for white with this al_map_rgb(0,0,0) function in each file i need it in ?

Either that or reread my post :P

imgregduh
Member #15,241
July 2013

torhu said:

Either that or reread my post :P

k got it

Go to: