0

所以 atm 我在这段代码中使用 swapbuffers 来刷新它:

    #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++;
    }

}

所以我在绘图过程结束时使用它,然后是一个清晰的设备。在播放动画或在背景中绘制某些东西(被删除)时,这有点工作。我不明白我应该如何使用它。我知道它用于双缓冲,它应该用旧的交换掉预先生成的下一帧,但我在任何地方都看到它的解释很糟糕。我如何有效地使用交换缓冲区,以便我可以在背景中播放动画和绘制东西(就像我的程序在玩家左键单击时应该做的那样)而不会消失?

我正在使用该站点http://winbgim.codecutter.org/中的库

4

1 回答 1

0

我不确定您使用的是什么库,但从您正在调用的函数的名称来看,您似乎在将后缓冲区与前缓冲区交换后立即清除颜色缓冲区。我认为这行不通。此外,我将进一步假设该setbkcolor函数设置将用于清除颜色缓冲区的颜色。我习惯做实时图形的方式是这样的:

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

但是,在您的代码中,您似乎在交换后立即清除了颜色缓冲区,您的循环应如下所示(更改已标有注释):

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
}

编辑:我忘了解释这些变化。按照您的操作方式,每次渲染一帧并向用户显示时,cleardevice调用都会立即将其删除。通过将cleardevice调用移动到循环的开头,您可以在渲染和显示新帧之前清除旧帧。我必须承认,这主要是我的猜测,因为我不知道您正在使用哪些库。

于 2016-03-29T14:37:10.137 回答