Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_get_backbuffer does not work

Credits go to Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
al_get_backbuffer does not work
rkuci24
Member #15,694
August 2014

Hi! I am new in Allegro5 world :D
So... I am using arch linux, codeblocks and allegro5 and when i want to use al_get_backbuffer it does nothing ... but if I use it as argument of al_draw_scaled_bitmap it draws white square... so ? :D Any ideas ? :D

bckbuffer = al_get_backbuffer(display);
al_draw_scaled_bitmap(bckbuffer,100, 100, 50, 50, 700, 50, 100, 100, 0);

#SelectExpand
1#include <allegro5.h> 2#include <allegro_image.h> 3 4ALLEGRO_DISPLAY *display = NULL; 5ALLEGRO_BITMAP *image1, *bckbuffer; 6 7void init() 8{ 9 al_init(); 10 al_init_image_addon(); 11 12 display = al_create_display(900,600); 13 image1 = al_load_bitmap("1.png"); 14 al_draw_bitmap(image1,0,0,0); 15 bckbuffer = al_get_backbuffer(display); 16 al_draw_scaled_bitmap(bckbuffer,100, 100, 50, 50, 700, 50, 100, 100, 0); 17 al_flip_display(); 18} 19 20void exito() 21{ 22 al_destroy_display(display); 23} 24 25int main() 26{ 27 init(); 28 al_rest(5); 29 exito(); 30 return 0; 31}

This is waht it does
{"name":"608808","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a6deb644c8210a5f696ffbe1808448c8.png","w":1600,"h":900,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a6deb644c8210a5f696ffbe1808448c8"}608808

Thanks for help ;D ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

I am not sure drawing from the backbuffer to the backbuffer is a valid thing?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

rkuci24
Member #15,694
August 2014

OK i did this and it works :D

#SelectExpand
1#include <allegro5.h> 2#include <allegro_image.h> 3 4ALLEGRO_DISPLAY *display = NULL; 5ALLEGRO_BITMAP *image1, *bckbuffer, *cb; /**change**/ 6 7void init() 8{ 9 al_init(); 10 al_init_image_addon(); 11 12 display = al_create_display(900,600); 13 image1 = al_load_bitmap("1.png"); 14 15 al_draw_bitmap(image1,0,0,0); 16 17 bckbuffer = al_get_backbuffer(display); 18 cb = al_clone_bitmap(bckbuffer); /**change**/ 19 al_draw_scaled_bitmap(cb,100, 100, 50, 50, 700, 50, 100, 100, 0); /**change**/ 20 al_flip_display(); 21} 22 23void exito() 24{ 25 al_destroy_display(display); 26} 27 28int main() 29{ 30 init(); 31 al_rest(5); 32 exito(); 33 return 0; 34}

Ty for help ;D :) ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

Is there a reason you want to draw from the backbuffer? Was this just a test to play around? Normally there isn't a good reason to read back from the backbuffer.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

rkuci24
Member #15,694
August 2014

I did this because i want easy way to capture something displayed on display and display it elsewhere :D

{"name":"608812","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/f\/3f8e3702e08139cf1c51b46687895e79.png","w":1600,"h":900,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/f\/3f8e3702e08139cf1c51b46687895e79"}608812

Thomas Fjellstrom
Member #476
June 2000
avatar

Ah, I see. You can just re-draw stuff in other locations. Allegro 5 is hardware accelerated, and its often better to just re-draw rather than read back from the display as that can be very slow.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SiegeLord
Member #7,827
October 2006
avatar

Using the backbuffer as the source is supported (with some restrictions), but it isn't the reason why this bug happens.

The real reason is that Allegro doesn't support drawing any bitmap to itself, so what you could do is draw the section you want into a separate bitmap, and then draw that bitmap somewhere. This will be more efficient than cloning the backbuffer each frame.

I suggest using the debug build of Allegro while developing, as some of these things are only enforced then.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

rkuci24
Member #15,694
August 2014

I haven't found any other functional solution... :-[

I used this technique for something more complex...

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro5.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_ttf.h> 7#include <allegro5/allegro_audio.h> 8#include <allegro5/allegro_acodec.h> 9 10 11ALLEGRO_DISPLAY *display = NULL; 12ALLEGRO_EVENT_QUEUE *event_queue = NULL; 13ALLEGRO_TIMER *timer = NULL; 14ALLEGRO_BITMAP *image1, *image2, *image3, *image4, *buffer, *clonedbuffer; 15ALLEGRO_FONT *font = NULL, *a2 = NULL; 16ALLEGRO_SAMPLE *sample=NULL; 17 18 19int height = 600, width =900; 20bool redraw = true, space = false, play = false; 21const float FPS = 60; 22double transparenci = 0, degree = 0, transparenci2 = 0, scale = 0, lsttime = 0; 23signed int x = 0, y = 0; 24double sstart = 0, inc = 16.5; 25 26 27int init() 28{ 29 al_init(); 30 al_init_font_addon(); 31 al_init_image_addon(); 32 al_init_primitives_addon(); 33 al_init_ttf_addon(); 34 al_install_keyboard(); 35 al_install_audio(); 36 al_init_acodec_addon(); 37 al_reserve_samples(1); 38 39 display = al_create_display(width,height ); 40 timer = al_create_timer(1.0/FPS); 41 event_queue = al_create_event_queue(); 42 al_register_event_source(event_queue, al_get_display_event_source(display)); 43 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 44 al_register_event_source(event_queue, al_get_keyboard_event_source()); 45 al_start_timer(timer); 46 47 48 image1 = al_load_bitmap("resources/pl.png"); /**pozadie**/ 49 image2 = al_load_bitmap("resources/FfpE2.png"); /**facepalm**/ 50 al_convert_mask_to_alpha(image2, al_get_pixel(image2,10,10)); //Remove color 51 image3 = al_load_bitmap("resources/ex.png"); /**explozia**/ 52 al_convert_mask_to_alpha(image3, al_get_pixel(image3,0,0)); //Remove color 53 image4 = al_load_bitmap("resources/fm.png"); /**wtf**/ 54 al_convert_mask_to_alpha(image4, al_get_pixel(image4,0,0)); //Remove color 55 56 font = al_load_ttf_font("resources/1.ttf", 35, 0); 57 a2 = al_load_ttf_font("resources/1.ttf", 120, 0); 58 sample = al_load_sample("resources/s.wav"); 59 60 al_draw_bitmap(image1,0,0,0); 61 al_flip_display(); 62 return 0; 63} 64 65int game_loop() 66{ 67 while(1) 68 { 69 ALLEGRO_EVENT ev; 70 al_wait_for_event(event_queue, &ev); 71 72 if(ev.type == ALLEGRO_EVENT_TIMER) 73 { 74 redraw = true; 75 } 76 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 77 { 78 break; 79 } 80 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 81 { 82 switch(ev.keyboard.keycode) 83 { 84 case ALLEGRO_KEY_SPACE: 85 space = (space == false ? true : false); 86 lsttime = al_get_time(); 87 break; 88 case ALLEGRO_KEY_ESCAPE: 89 return 0; 90 break; 91 } 92 } 93 94 if(redraw && al_is_event_queue_empty(event_queue)) 95 { 96 redraw = false; 97 al_draw_bitmap(image1,0,0,0); 98 99 if(space == true) 100 { al_draw_tinted_bitmap(image3, al_map_rgba_f(transparenci*0.1, transparenci*0.1, transparenci*0.1, transparenci*0.1), 430, 210, 0); 101 transparenci +=0.1; 102 103 if(al_get_time() - lsttime >= 1.5) 104 { 105 al_draw_tinted_scaled_bitmap(image4,al_map_rgba_f(transparenci2*0.1, transparenci2*0.1, transparenci2*0.1, transparenci2*0.1), 0, 0, al_get_bitmap_width(image4), al_get_bitmap_height(image4), 0, 0, al_get_bitmap_width(image4) * scale, al_get_bitmap_height(image4) * scale, 0); 106 transparenci2 +=0.1; 107 scale = (scale >= 2.3 ? scale : scale + 0.005); 108 109 al_draw_rotated_bitmap(image2, al_get_bitmap_width(image2) / 2, al_get_bitmap_height(image2) / 2, (width / 2) + x, (height / 2) +y, degree, 0 ); 110 degree += 0.0174532925; 111 x = (y >= 100 ? x -1 : x + 1); 112 y = (x >= 100 ? y +1 : y - 1); 113 114 al_draw_textf(a2, al_map_rgb(255, 0, 0), 200, 250, 0, ""); //say something funny :D 115 /**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**/ 116 al_destroy_bitmap(clonedbuffer); //anti-memory leaks 117 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); //GPU acceleration 118 buffer = al_get_backbuffer(display); 119 clonedbuffer = al_clone_bitmap(buffer); 120 al_draw_scaled_bitmap(clonedbuffer, 0, 0, width, height, 800, 0, 100, 100, 0); 121 /**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**/ 122 123 al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); 124 } 125 } 126 else if(space == false) 127 { 128 al_draw_textf(font, al_map_rgb(255, 255, 255), 290, 50, 0, "Press spacebar :D"); 129 transparenci = 0; 130 degree = 0; 131 x = 0; 132 y = 0; 133 transparenci2 = 0; 134 scale = 0; 135 al_stop_samples(); 136 } 137 138 al_flip_display(); 139 } 140 141 } 142} 143 144int END() 145{ 146 al_destroy_timer(timer); 147 al_destroy_display(display); 148 al_destroy_event_queue(event_queue); 149 al_destroy_sample(sample); 150} 151 152 153int main() 154{ 155 init(); 156 game_loop(); 157 END(); 158 159 return 0; 160}

And it works fine :D no lags or tearing :D
(It is a program for fun ;D)

Go to: