Allegro 5 C/C++: Color problem

Asked

Viewed 123 times

0

I am using Allegro 5, and read that to create colors just use al_map_rgb or al_color_html, then I made a map to easily access various colors:

#include "allegro5\allegro.h"
#include "allegro5\allegro_color.h"

int main()
{
    al_init();

    map<string, ALLEGRO_COLOR> color;
    color["black"] = al_color_html("#000000");
    color["gray"] = al_color_html("#808080");
    color["silver"] = al_color_html("#c0c0c0");
    color["white"] = al_color_html("#ffffff");
    color["maroon"] = al_color_html("#800000");
    color["red"] = al_color_html("#ff0000");
    color["olive"] = al_color_html("#808000");
    color["yellow"] = al_color_html("#ffff00");
    color["green"] = al_color_html("#008000");
    color["lime"] = al_color_html("#00ff00");
    color["teal"] = al_color_html("#008080");
    color["aqua"] = al_color_html("#00ffff");
    color["naavi"] = al_color_html("#0000ff");
    color["blue"] = al_color_html("#000080");
    color["purple"] = al_color_html("#800080");
    color["fuchsia"] = al_color_html("#ff00ff");

    return 0;
}

But the program crashes when I test. Then, by doing tests, I found that if I use only 6 colors, my program runs perfectly:

#include "allegro5\allegro.h"
#include "allegro5\allegro_color.h"

int main()
{
    al_init();

    map<string, ALLEGRO_COLOR> color;
    color["black"] = al_color_html("#000000");
    color["gray"] = al_color_html("#808080");
    color["silver"] = al_color_html("#c0c0c0");
    color["white"] = al_color_html("#ffffff");
    color["maroon"] = al_color_html("#800000");
    color["red"] = al_color_html("#ff0000");

    return 0;
}

But if I add one more color any it comes back crashing:

#include "allegro5\allegro.h"
#include "allegro5\allegro_color.h"

int main()
{
    al_init();

    map<string, ALLEGRO_COLOR> color;
    color["black"] = al_color_html("#000000");
    color["gray"] = al_color_html("#808080");
    color["silver"] = al_color_html("#c0c0c0");
    color["white"] = al_color_html("#ffffff");
    color["maroon"] = al_color_html("#800000");
    color["red"] = al_color_html("#ff0000");
    color["blue"] = al_color_html("#000080");

    return 0;
}

Similarly, if I use one for generating colors, without saving in any variable, after the seventh repetition the program crasha.

It works:

for (int i = 0; i < 6; i++)
{
    al_color_html("#000080");
}

That’s not:

for (int i = 0; i < 7; i++)
{
    al_color_html("#000080");
}

But if I do:

for (int i = 1; true; i++)
{
    cout << i << " ";
    al_color_html("#000080");
}

Got a way out 1 2 3 4 5 ... 57 58 59, and if I do that:

for (int i = 1; true; i++)
{
    al_color_html("#000080");
    cout << i << " ";
}

Got a way out 1 2 3 4 5 ... 15 16 17

Note: the error also occurs with the al_map_rgb, but I can execute the command more often

  • Also occurs if you use std::map::insert() instead of the std::map::operator[]()? This looks like trouble with the allocator, but maybe it’s the problem of operator[].

  • Anyway, if you just want to access the colors quickly and conveniently, you’d better use a enum as key and, as you should not add colors dynamically, use a array static with as many positions as there are members in enum.

  • @Wtrmute, the error is not in the map, if I use a for and only generate colors, after the seventh the crash program

  • It breaks and tells you nothing? You can put the for within a try ... catch (std::exception & ex) { cout << ex.what() << endl; } catch (std::string & str) { cout << "Peguei string " << str << endl; }?

  • @Wtrmute, it doesn’t work, I don’t get error message or anything like that, the program just crashes. I updated the post

1 answer

0

The answer is very simple: update the Allegro and the Mingw.

  • Please remember to accept your own answer as soon as possible... :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.