Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Using ALLEGRO_COLOR in Classes

This thread is locked; no one can reply to it. rss feed Print
Using ALLEGRO_COLOR in Classes
Eric Johnson
Member #14,841
January 2013
avatar

Hi. I want to create a colour variable in one of my classes, but it's throwing errors when I attempt to assign it a value later on. Example code:

#SelectExpand
1class A { 2 3 public: 4 5 A(void) { 6 7 intensity_ = 0.4; 8 9 bitmap_ = al_load_bitmap("some_bitmap.png"); 10 11 alpha_= al_map_rgba(0, 0, 0, intensity_); // Throws error 12 } 13 ~A(void) {} 14 15 void Draw(void) { 16 17 al_draw_tinted_bitmap(bitmap_, alpha_, 0, 0, 0); // Doesn't work because alpha_ wasn't established 18 } 19 20 private: 21 22 float intensity_; 23 24 ALLEGRO_COLOR alpha_; // Colour variable is created 25 ALLEGRO_BITMAP *bitmap_; 26};

As you can see, I create an ALLEGRO_COLOR variable and assign its value in A's constructor, but this fails. My compiler (G++) says that I'm attempting to assign a value to a constant variable after its creation.

One way I got "around" this was to simply define the variable outside of the class itself, which worked, but I want it specific to the class itself. Do you see what I mean?

How would I go about properly using an ALLEGRO_COLOR in a class setting? Thank you for your time.

ph03nix
Member #15,028
April 2013
avatar

You have to call al_init() first (before this A object is initialized)

Eric Johnson
Member #14,841
January 2013
avatar

ph03nix said:

You have to call al_init() first (before this A object is initialized)

Thanks for the reply, ph03nix. The above was an example what one of my classes is like (not true source). In my true code (too long I thought to deal with posting) already has Allegro initiated.

EDIT
Ha, stupid me. I was assigning a NULL value to it in my true constructor, which threw it off. It's all good now. :P

torhu
Member #2,727
September 2002
avatar

Don't post "example code" when your problem is a concrete one. There's a reason most of us will roll our eyes when you do that :P

ph03nix
Member #15,028
April 2013
avatar

I assumed he hadn't called al_init because he said al_map_rgba was throwing an error. If he set the color to NULL afterward it wouldn't affect al_map_rgba. That's a bit weird.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

torhu said:

Don't post "example code" when your problem is a concrete one. There's a reason most of us will roll our eyes when you do that

Yes, the code you posted is not the error. It's the way you're using an object of class A. Are you using a const object anywhere and trying to call non-const methods? They have to be marked constant (the methods) in order to access and change const members. OR you can cheat and use Obj& o = const_cast<Obj&>(const_obj_reference);. But don't do that, you don't really need const safe methods, just don't use const objects.

Or else I have no idea because you haven't shown us the exact code line and method that is being flagged by the compiler. Show us that and maybe we can help.

Eric Johnson
Member #14,841
January 2013
avatar

#SelectExpand
1// Generic constructor which establishes a few base values. 2October::October(void) { 3 4 number_of_acres_ = 4; 5 this_acre_ = 0; 6 7 light_alpha_ = NULL; // My ALLEGRO_COLOR; threw an error; has since been removed 8 light_bitmap_ = NULL; 9 light_intensity_ = 0.0; 10 11 // By default, none of the buttons are being pressed 12 for (int i = 0; i < 4; ++i) button_[i] = false; 13} 14 15// Later on in my code... 16// Attempts to achieve a day/night effect with a tinted bitmap 17void October::ManageLightCycle(void) { 18 19 static bool transitioning_to_night = true; 20 21 if (transitioning_to_night) { 22 23 ++light_intensity_; 24 25 if (light_intensity_ > kMidnight) { 26 27 // Night cycle is over with; revert back to day time 28 transitioning_to_night = false; 29 } 30 } 31 else { 32 33 --light_intensity_; 34 35 if (light_intensity_ < kMorning) { 36 37 // Day cycle is over with; head towards the night 38 transitioning_to_night = true; 39 } 40 } 41 42 light_alpha_ = al_map_rgba(0, 0, 0, light_intensity_); 43}

I apologise for posting an example instead of the real thing; above is the real thing. In my constructor, when I set light_alpha_ to NULL, it caused a bunch of errors. I removed it and it ran just fine thereafter.

So, things are all sorted out; I no longer require assistance on the matter. :P

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: