简体   繁体   中英

How to show the draw of subbitmap in screen in Allegro?

I using two bitmaps, image (subbitmap) and grid (main bitmap)

ALLEGRO_BITMAP* grid = NULL;
ALLEGRO_BITMAP* image = NULL;
ALLEGRO_DISPLAY* display = NULL;

//Initialization
display = al_create_display(SCREEN_W, SCREEN_H + INFO_H);
grid = al_create_bitmap(SCREEN_W, SCREEN_H + INFO_H); 

al_set_target_bitmap(grid);
al_set_target_bitmap(al_get_backbuffer(display));

al_draw_bitmap(grid, 0, 0, 0);

//Creating a model
image = loadBitmapAtSize(...);
al_create_sub_bitmap(grid, 0, 0, columns, rows);
al_draw_bitmap(image, 0, 0, 0);

Until here it is all going well, but If I draw directly to image(subbitmapt) then I not found how to send the changes to display.

al_set_target_bitmap(image);

//rows and cols are the height and width of subbitmap
for (int y = 0; y < rows; ++y) {
    for (int x = 0; x < columns; ++x) {
        if(x == y || x-1 == y || x+1  == y || x == y-1 || x == y+1){
            //I test the condition and the program is entering to the if
            al_draw_pixel(x, y, al_map_rgb(255, 255, 255));
        }
    }
}

al_set_target_bitmap(grid);

al_flip_display();

Any idea how can I update the main bitmap after edit the subbitmap?

From Allegro documentation:

A sub-bitmap is a bitmap that shares drawing memory with a pre-existing (parent) bitmap, but possibly with a different size and clipping settings.

https://www.allegro.cc/manual/5/al_create_sub_bitmap

al_create_sub_bitmap returns a pointer to ALLEGRO_BITMAP that you need to assign to a variable to use.

To create and assign your subbitmap, you can use:

ALLEGRO_BITMAP* subbitmap = al_create_sub_bitmap(grid, 0, 0, columns, rows);
al_set_target_bitmap(subbitmap);

You have to draw your 'grid' bitmap onto the display, and then flip it.

al_set_target_backbuffer(display);
al_draw_bitmap(grid , 0 , 0 , 0);
al_flip_display();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM