MoustacheSpy
MoustacheSpy

Reputation: 763

How to use swapbuffers in Graphics.h

So atm im using swapbuffers in this code to refresh it:

    #include "graphics.h"

void drawGridOnX(int xtotal, int ytotal);
int levelcode[400][45][100];
void decodelevelAndDraw();

void main() {
    initwindow(1600, 900,"Testscreen",0,0,true,true);
    int gridposx = 0, gridposy = 0, diffx = 0, diffy = 0, xtotal=0, ytotal = 0,distanceFromMouse=50;


    while (1) {
        setbkcolor(9);
        ytotal = 0;
        diffx = mousex() - gridposx;
        while (gridposx < mousex()&&diffx>=70) {
            gridposx += 70;

        }
        while (gridposx > mousex()&&diffx<=-70 + distanceFromMouse) {
            gridposx =gridposx-70;

        }
        diffy = mousey() - gridposy;
        while (gridposy < mousey() && diffy >= 70) {
            gridposy += 70;

        }
        while (gridposy > mousey() && diffy <= -70+distanceFromMouse) {
            gridposy = gridposy - 70;

        }
        while (ytotal < 900) {
            drawGridOnX(xtotal, ytotal);
            ytotal += 70;
        }
        if (WM_LBUTTONDOWN) {
            levelcode[gridposx/70][gridposy/70][0]=1;
            printf("CLICK");
        }
        decodelevelAndDraw();
        readimagefile("question.bmp", gridposx,gridposy, 70+gridposx, 70+gridposy);
        printf("gridposx:%d\tgridposy:%d\ttitlenumberx:%d\ttitlenumbery%d",gridposx,gridposy,gridposx/70,gridposy/70);
        swapbuffers();
        cleardevice();
    }
}


void drawGridOnX(int xtotal, int ytotal) {
    while (xtotal < 1600) {
        rectangle(xtotal, ytotal, 70 + xtotal, 70+ytotal);
        xtotal += 70;

    }


}


void decodelevelAndDraw() {
    int x = 0, y = 0;
    while (y != 12) {
        while (x != 22) {
            if (levelcode[x][y][1] == 1) {
                readimagefile("question.bmp", x*70, y*70, 70 + x*70, 70 + y*70);

            }
            x++;
        }
        y++;
    }

}

So im using it at the end of the drawing procedure followed by a clear device. That kinda works exept when playing animations or drawing something in the backround ( that gets deleted ). I dont understand how i should use it. I know its used for double buffering and its supposed to swap out the pregenerated next frame with the old one but everywhere i looked its explained poorly. How can i use swapbuffers effectively so that i can play animations and draw stuff in the backround ( like my programm is supposed to do when the player left clicks ) without it dissapearing?

Im using the librarys from this site http://winbgim.codecutter.org/

Upvotes: 0

Views: 1051

Answers (1)

danieltm64
danieltm64

Reputation: 481

I'm not sure what libraries you're using, but from the names of the functions you are calling, it looks like you're clearing the color buffer right after swapping the back buffer with the front buffer. I don't think that will work. Also, I'll further assume that the setbkcolor function sets the color that will be used to clear the color buffer. The way I'm used to doing real-time graphics is like this:

repeat {
    // check for and handle user input
    // set background color
    // clear color buffer
    // update scene information
    // render the new scene
    // swap the buffers
}

However, in your code, you seem to clear the color buffer right after swapping, your loop should look like this (the changes have been marked with comments):

while (1) {
    setbkcolor(9);
    cleardevice(); // line of code added
    ytotal = 0;
    diffx = mousex() - gridposx;
    while (gridposx < mousex()&&diffx>=70) {
        gridposx += 70;

    }
    while (gridposx > mousex()&&diffx<=-70 + distanceFromMouse) {
        gridposx =gridposx-70;

    }
    diffy = mousey() - gridposy;
    while (gridposy < mousey() && diffy >= 70) {
        gridposy += 70;

    }
    while (gridposy > mousey() && diffy <= -70+distanceFromMouse) {
        gridposy = gridposy - 70;

    }
    while (ytotal < 900) {
        drawGridOnX(xtotal, ytotal);
        ytotal += 70;
    }
    if (WM_LBUTTONDOWN) {
        levelcode[gridposx/70][gridposy/70][0]=1;
        printf("CLICK");
    }
    decodelevelAndDraw();
    readimagefile("question.bmp", gridposx,gridposy, 70+gridposx, 70+gridposy);
    printf("gridposx:%d\tgridposy:%d\ttitlenumberx:%d\ttitlenumbery%d",gridposx,gridposy,gridposx/70,gridposy/70);
    swapbuffers();
    // cleardevice(); // line of code removed
}

Edit: I forgot to explain the changes. The way you were doing it, every time a frame was rendered and shown to the user, it would be immediately erased by the cleardevice call. By moving the cleardevice call to the beginning of the loop, you clear the old frame before rendering and showing the new one. I must confess though that this is mostly guesswork on my part because I don't know which libraries you're using.

Upvotes: 0

Related Questions