How can I use two buffer in one screen ? Is it possible ? (Allegro.h)

138 Views Asked by At

I'm working on Microsoft Visual Studio 2010 , trying to code a game with using Allegro.h library .

The point is that

  1. I'm using a buffer for drawing to buildings with draw_sprite(screen,buf1,0,0);
  2. After that I want to move a bitmap ball image in that screen
  3. when I move .bmp image , I'm using buf1 (the same buffer) again but I should clean buf1 for create a moving image
  4. When I do this naturally I miss all things in buffer , but I want buildings background during my game .
  5. How can I use two buffer in one screen ?

This is my code :

void  BinaCizdir1(BITMAP *buf1){
    int r;
    int renk;
    int say=0;
    BITMAP *turuncu_beton=load_bitmap("turuncu.bmp",NULL);
    BITMAP *sari_beton=load_bitmap("sari.bmp",NULL);
    BITMAP *pembe_beton=load_bitmap("pink.bmp",NULL);
    BITMAP *oyunu1_resmi=load_bitmap("topbu.bmp",NULL);

    //Birinci oyuncunun x koordinatını belirledik.Random olarak!
    int oyuncu1_y_koor=0;

    int oyuncu1_x_koor1= (1+rand()%4);
    if (oyuncu1_x_koor1==1){
        oyuncu1_x_koor1=30;
    }
    if (oyuncu1_x_koor1==2){
        oyuncu1_x_koor1=150;
    }if (oyuncu1_x_koor1==3){
        oyuncu1_x_koor1=270;
    }if (oyuncu1_x_koor1==4){
        oyuncu1_x_koor1=390;
    }


    if(kontrol==0){
        renk=(rand()%3);r= (1+rand()%6)*30;
        for(int x=0;x<1080;x=x+30){
            for(int y=600;y>450-r;y=y-30){
                if(oyuncu1_x_koor1==x){
                    oyuncu1_y_koor=390-r;
                    draw_sprite(buf1,oyunu1_resmi,oyuncu1_x_koor1,oyuncu1_y_koor);
                }
                if(renk==0){
                    draw_sprite(buf1,turuncu_beton,x,y);
                }
                if(renk==1){
                    draw_sprite(buf1,sari_beton,x,y);
                }
                if(renk==2){
                    draw_sprite(buf1,pembe_beton,x,y);
                }
            }
            if( say%3==2 && x!=1020){
                renk=(rand()%3);
                r= (1+rand()%6)*30;
                x=x+30;
            }
            say++;
            /*
           velocityY = velocityY +acc*dt; // updating the y component of the velocity  
           x = x + (velocityX*dt); // updating the x position  
           y = y + (velocityY*dt) + 0.5*acc*(dt*dt);// updating the y position. 
           rest(5);       
           draw_sprite(buf1,oyunu1_resmi,x,y);
          */
        }
         kontrol=1;
    }
    draw_sprite(screen,buf1,0,0);
}

This is my moving bmp image codes :

  velocityY = velocityY +acc*dt; // updating the y component of the velocity  
  x = x + (velocityX*dt); // updating the x position  
  y = y + (velocityY*dt) + 0.5*acc*(dt*dt);// updating the y position. 
  rest(5);       
  draw_sprite(buf1,oyunu1_resmi,x,y);
1

There are 1 best solutions below

0
On

Sorry, I don't quite understand the statement; it sounds like you are asking about double-buffering.

You will draw your buildings onto an offscreen frame buffer. Then you will draw your ball onto the offscreen frame buffer. Then you will wait for the screen's vertical refresh. (This step is probably done automatically these days by Allegro) Then you will swap the buffers so that the offscreen frame buffer becomes the onscreen. (Repeat)

Cf. https://wiki.allegro.cc/index.php?title=Double_buffering