Drawing to bitmap

consider following code

1int main(void) 2{ 3 al_init(); 4 al_init_image_addon(); 5 6 ALLEGRO_DISPLAY* display = al_create_display(800, 600); 7 8 al_clear_to_color(al_map_rgb(0, 0, 0)); 9 10 al_flip_display(); 11 12 ALLEGRO_BITMAP* bitmap = al_create_bitmap(160, 80); 13 14 al_set_target_bitmap(bitmap); 15 16 al_clear_to_color(al_map_rgb(0, 100, 0)); 17 18 19 al_save_bitmap("green.png", bitmap); //green.png is green 20 al_flip_display(); //display is now green 21 cin.get(); 22 23 al_set_target_backbuffer(display); 24 25 al_clear_to_color(al_map_rgb(100, 0, 0)); 26 27 al_save_bitmap("red.png", bitmap); //red.png is red 28 al_flip_display(); //display is now red 29 cin.get(); 30 31 return 0; 32}

I read documentation and I thought that display should never be green, because I am drawing green to bitmap and bitmap "red.png" should be green. But it is not.
If it is behaving correctly, please tell me how to draw only to bitmap and only to display.
Allegro version is 5.1.8 and OS is Debian.
Also please tell me how is this program behaving on your computer.

Arthur Kalliokoski

It didn't save the bitmaps for me at all until I put a al_init_image_addon() right after al_init(). Oh, yeah, there is no "init_allegro()" unless you didn't post a function you wrote yourself, but you must have or it wouldn't have compiled. I used "al_init()".

Anyway, it now shows a blank screen, which turns to red, and both bitmap files are green.


Thanks for reply. Yes init_allegro() is function I wrote myself. It only calls al_init() and al_init_image_addon(). I will change code in the first post because this function can be confusing.
So it behave differently on your computer. What Allegro version are you using?
Can it be caused be operating system? Because I have unstable version of it...
Also I have tried some Allegro tests and some of them failed. For example "test_backbuffer.ini" in allegro/tests has 4 failed tests.
Maybe some moderator can move this to Allegro Development section, because it looks like bug.

Arthur Kalliokoski

I'm using Allegro 5.1.7 with Linux, Nvidia GeForce GTX 650.


Did you try the tests? Did some of them failed?

Arthur Kalliokoski

What tests? The Allegro examples? They all worked, except the ones for hardware I don't have (such as dual monitors). There was also some resizing thing that didn't work right a couple of years ago, I don't think it's fixed yet.

I don't think I tried them all because it's so tedious, there were a couple of GLSL examples I just ran a few days ago and I'm pretty sure I'd never seen them before (they worked correctly too).

I always get rid of the noveau (?) open source driver and install the Nvidia proprietary driver because the noveau driver sucks so hard, for example, the ex_gldepth example doesn't work right with it. OTOH, about a year ago I reported a problem and spent about three days trying to fix it when I saw a new Nvidia driver had come out and it worked correctly then. Shit happens.


No. When you download allegro, there is directory "tests". In this directory there is source file "test_driver.c" and few other files for example "test_bitmaps.ini", "test_backbuffer.ini".

Arthur Kalliokoski

I didn't try those, I just fiddled with it for two minutes and tried about five of them (including test_bitmap.ini) and they all flashed a window too fast to really see what's happening and the console is full of OK messages.


Would you please try test_backbuffer.ini? I have this output from it:

OK test bbsrc [sw]
FAIL test bbsrc [hw] - RMS error is 35.2781
OK test bbsrc translate [sw]
FAIL test bbsrc translate [hw] - RMS error is 33.9109
OK test bbsrc outside [sw]
FAIL test bbsrc outside [hw] - RMS error is 27.0248
OK test bbsrc tint [sw]
FAIL test bbsrc tint [hw] - RMS error is 27.2208

total tests: 8
passed tests: 4
failed tests: 4

But I dont know, what does it mean... Maybe it is connected to the original error I had.

Arthur Kalliokoski
pepsi@fractal:/home/prog/allegro-5.1.7/build/tests 05:45 AM $ test_driver ../../tests/test_backbuffer.ini 
OK   test bbsrc [sw]
OK   test bbsrc [hw]
OK   test bbsrc translate [sw]
OK   test bbsrc translate [hw]
OK   test bbsrc outside [sw]
OK   test bbsrc outside [hw]
OK   test bbsrc tint [sw]
OK   test bbsrc tint [hw]

total tests:  8
passed tests: 8
failed tests: 0


Kyborg those tests seems related to hardware problems, of some sort.

I'm not able to be more helpful thou :(

Arthur Kalliokoski

Kyborg, what video card and driver do you have?

If it's Nvidia I'm using NVIDIA-Linux-x86_64-331.20.run installer for 64 bit linux.


My video card is AMD Radeon HD 7570M. I dont know which driver I am using.

Edgar Reynaldo

This part of the code is fine :

The next part flips the display without drawing anything to it (which means it could display anything) (you never set the drawing target back to the backbuffer) :

 ALLEGRO_BITMAP* bitmap = al_create_bitmap(160, 80);
 al_clear_to_color(al_map_rgb(0, 100, 0));
 al_save_bitmap("green.png", bitmap); //green.png is green
 al_flip_display(); //display could hold anything

green.png should now exist and be green.

The last part of the code you showed us should save red.png as a green bitmap as well, because the green bitmap is the bitmap you specified to save :

 al_clear_to_color(al_map_rgb(100, 0, 0));
 al_save_bitmap("red.png", bitmap); //red.png is green b/c bitmap is green
 al_flip_display(); //display is now red
 cin.get(); return 0; }

The screen should be red now.

Thread #614461. Printed from Allegro.cc