Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Allegro 5] Bitmap does not appear, unless clear_to_color is called

Credits go to pkrcel for helping out!
This thread is locked; no one can reply to it. rss feed Print
[Allegro 5] Bitmap does not appear, unless clear_to_color is called
Skyrunner
Member #14,886
January 2013

SoilColorDict soilcolorDict;
  bitmap = al_create_bitmap(soilGrid->getWidth(), soilGrid->getHeight());
  al_lock_bitmap(bitmap, al_get_bitmap_format(bitmap), ALLEGRO_LOCK_READWRITE);
  for (int y = 0; y < soilGrid->getHeight(); y++)
    for (int x = 0; x < soilGrid->getWidth(); x++)
      {
      al_put_pixel(x, y, soilcolorDict.getColor(soilGrid->ref(x, y).getTopsoilType()));
      }
    al_unlock_bitmap(bitmap);

Here, an object that stores ALLEGRO_COLORs in a std::map is created (soilcolorDict). The bitmap, which is a global variable, is assigned an ALLEGRO_BITMAP and is filled by fetching colors from soilcolorDict. A delve with debugger confirms that ALLEGRO_COLOR objects are being retrieved, each filled with color information.

The constructor for soilcolorDict looks like

#SelectExpand
1SoilColorDict() /// MUST BE CREATED __AFTER__ al_primatives IS INSTALLED o_O 2 { 3 colors.insert(pair<int, ALLEGRO_COLOR>(stCLAY, al_map_rgb(240,190,153))); 4 colors.insert(pair<int, ALLEGRO_COLOR>(stSANDYCLAY, al_map_rgb(220, 217, 198))); 5 colors.insert(pair<int, ALLEGRO_COLOR>(stSILTYCLAY, al_map_rgb(200, 173, 152))); 6 colors.insert(pair<int, ALLEGRO_COLOR>(stCLAYLOAM, al_map_rgb(192, 161, 140))); 7 colors.insert(pair<int, ALLEGRO_COLOR>(stSILTYCLAYLOAM, al_map_rgb(187, 174, 168))); 8 colors.insert(pair<int, ALLEGRO_COLOR>(stSANDYCLAYLOAM, al_map_rgb(211,193,157))); 9 colors.insert(pair<int, ALLEGRO_COLOR>(stLOAM, al_map_rgb(202, 195, 169))); 10 colors.insert(pair<int, ALLEGRO_COLOR>(stSILTLOAM, al_map_rgb(227, 226, 214))); 11 colors.insert(pair<int, ALLEGRO_COLOR>(stSANDYLOAM, al_map_rgb(209, 204, 162))); 12 colors.insert(pair<int, ALLEGRO_COLOR>(stLOAMYSAND, al_map_rgb(194, 187, 159))); 13 colors.insert(pair<int, ALLEGRO_COLOR>(stSAND, al_map_rgb(239, 236, 205))); 14 colors.insert(pair<int, ALLEGRO_COLOR>(stSILT, al_map_rgb(166, 168, 167))); 15 }

A bunch of key-color pairs are inserted into the map, after al_init() and the other functions are called.

Next, in the rendering part of the function,

is executed. The backbuffer, which is cleared to a magenta just so I can see the contrast, gets blitted to by a bitmap.

Finally,

Problem: the screen looks like this.

{"name":"iGRJBWr.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/7\/d73e3ad75f78d73dc875fa99314b540d.png","w":656,"h":518,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/7\/d73e3ad75f78d73dc875fa99314b540d"}iGRJBWr.png

When I add

before setting the target to the backbuffer, the screen looks like

{"name":"HVchHqc.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/4\/c47f3f1a7bdcdbe64e56ef2a9cefdc2e.png","w":656,"h":518,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/4\/c47f3f1a7bdcdbe64e56ef2a9cefdc2e"}HVchHqc.png

It leads me to guess that I'm doing something wrong when drawing to the bitmap, but I can't tell why. Been at this a couple of hours and I can't really make progress :<

The exact way the functions are implemented is that there is a framework that provides the functions

init() // Installing keyboards, setting FPS, installing addons...
  userDefinedInit() // Anything else you want to do before starting the game loop.
while loop
  catchEvents()
    update()
    render()
destroy()

Everything except the render code is in init(). Help would be greatly appreciated! D:

pkrcel
Member #14,001
February 2012

Seems to me that you're missing al_put_pixel needs the target bitmap to be set to bitmap....try al_set_target_bitmap(bitmap) before you nested for loops.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Skyrunner
Member #14,886
January 2013

... That seems like quite the stupid mistake. In fact, I'be done exactly the same loop but WITH the set_target done in another project >_> Not at PC, but I' pretty sure that'll fix it. Will return if it doesn't, within 25 hours ;3

Edgar Reynaldo
Member #8,592
May 2007
avatar

I'm still looking at your code, but I did notice 2 things at least - you don't need to use insert on a std::map - using the map indexing method is cleaner. Same code behind the scenes, but it looks better to use colors[stClay] = al_map_rgb(64,127,0);. The second pkrcel already pointed out - you have to set the target bitmap before drawing to it, or you will be drawing to whatever was set to the target bitmap previously.

Go to: