in Education by
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 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(32, 32), oni::ALIGN_LEFT, "Hello World"); } ... function draw_text void oni::draw_text(Font *font, Color *color, Vector2 position, oni::Align align, std::string text) { al_draw_text(font->font, color->color, position.x, position.y, (int)align, text.c_str()); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    What are generic methods? (a) Generic methods are the methods defined in a generic class (b) Generic methods ... Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept any type of object? (a) ... chapter Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept a number? (a) K (b) N ... in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these is an correct way of defining generic method? (a) name(T1, T2, , Tn) { /* */ } ... topic in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Take the following code snippet: #include std::vector good; //illegal, because std::allocator is ill-formed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I was supposed to do this exercise: Write a program in C to print a frequency chart on the screen ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I am following simple examples to build up my understanding on private static members. However, I am getting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
...