static const Color not fully working with Allegro5

109 views Asked by At

I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in pure Lua environment). I've been trying to solve this issue I came across, but did not succeed. Basically, I created a class named Color and added static consts as shortcuts for various colors and it is used for the text writing using allegro (I am creating my own game engine for internal use and am creating one API in C++ for all the libraries the engine uses). What happens is that when I define a color using the static const, the text does not appear, while if I use the constructor, everything works as expected. The printf() function in main_menu returns proper results in both cases, so the local variables are being set in either cases. So the problem is really with the allegro part of the "equation".

Also, if any of this is malformed, like if there are any bad practices or anything like, I would appreciate tips on how to improve it.

Thank you in advance.


color.hpp

#pragma once
#include "allegro5/color.h"
#include "onidrive/vector2.hpp"

namespace oni {
  enum Align: int;
  class Font;

  class Color {
    public:
      Color(unsigned char r = 0xFF, unsigned char g = 0xFF, unsigned char b = 0xFF, unsigned char a = 0xFF);
      ~Color();
      unsigned char r;
      unsigned char g;
      unsigned char b;
      unsigned char a;

      static const Color white;
      static const Color black;
      static const Color red;
      static const Color green;
      static const Color blue;
      static const Color yellow;
      static const Color magenta;
      static const Color cyan;

      friend void draw_text(Font *font, Color *color, Vector2<float> position, Align align, std::string text);

    private:
      ALLEGRO_COLOR color;

  };
}

color.cpp

#include "onidrive/color.hpp"
#include "allegro5/allegro.h"

oni::Color::Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) : r(r), g(g), b(b), a(a) {
  this->color = al_map_rgba(r, g, b, a);
}

oni::Color::~Color() {

}

const oni::Color oni::Color::white(  0xFF, 0xFF, 0xFF, 0xFF);
const oni::Color oni::Color::black(  0x00, 0x00, 0x00);
const oni::Color oni::Color::red(    0xFF, 0x00, 0x00);
const oni::Color oni::Color::green(  0x00, 0xFF, 0x00);
const oni::Color oni::Color::blue(   0x00, 0x00, 0xFF);
const oni::Color oni::Color::yellow( 0xFF, 0xFF, 0x00);
const oni::Color oni::Color::magenta(0xFF, 0x00, 0xFF);
const oni::Color oni::Color::cyan(   0x00, 0xFF, 0xFF);

main_menu.cpp

...
void MainMenu::draw_ui() {

  //when this is used, compiling, text is invisible
  oni::Color color = oni::Color::red;

  //when this is used, compiling, text is visible, correct color, works as expected
  oni::Color color = oni::Color(0xFF, 0x00, 0x00, 0xFF);

  printf("color(%X, %X, %X, %X);\n", color.r, color.g, color.b, color.a);

  oni::draw_text(font, &color, Vector2<float>(32, 32), oni::ALIGN_LEFT, "Hello World");

}
...

function draw_text

void oni::draw_text(Font *font, Color *color, Vector2<float> position, oni::Align align, std::string text) {
  al_draw_text(font->font, color->color, position.x, position.y, (int)align, text.c_str());
}
1

There are 1 answers

0
BugSquasher On

Your static const Color objects are created in the global namespace. This means any code in their constructor runs before al_init is called in main. There are only a few allegro functions that can be called before al_init, and al_map_rgb is not one of them.

That is why it works when you create a new Color object after al_init but not when you use your static Color objects.