【问题标题】:How to avoid to redraw static background in Allegro 5?如何避免在 Allegro 5 中重绘静态背景?
【发布时间】:2020-04-19 04:47:57
【问题描述】:

当我初始化 Allegro 时,我绘制了一个 2D 表格,它的单元格将在程序期间更改为白色/黑色

//Global variables
int** board;

//Draw 2D board
for (int i = 0; i < SCREEN_H ; i++) {
    for (int j = 0; j < SCREEN_W; j++) {
        al_draw_rectangle((j) * CELL_SIZE, (i) * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE, BLACK, 1.0);
    }
}

al_flip_display();

在 while 循环之前,它初始化 2D 动态指针 board,随机值介于 0 和 1 之间。

在while循环中我改变了单元格的颜色

//changes the values of board based on the rules of the Conway's Game of Life
generate();

for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
        //Redrawing the table
        al_draw_rectangle((j) * CELL_SIZE, (i) * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE, BLACK, 1.0);

        if(board[i][j] == 1){
            al_draw_filled_rectangle((j) * CELL_SIZE, (i) * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE, BLACK);
        }else{
            al_draw_filled_rectangle((j) * CELL_SIZE, (i) * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE, WHITE);
        }
    }
}

但是在 for 循环中我需要重新绘制表格。

如何避免 Allegro 在我绘制单个单元格时重绘静态背景?

【问题讨论】:

    标签: c++ allegro allegro5


    【解决方案1】:

    我不相信有一个简单的方法可以做到这一点,无论如何,在你的单元格之前重绘背景会更简单,每一帧。

    如果您的背景没有太大变化或根本没有变化,您可以将其存储在纹理中并进行绘制。这被称为Painter's algorithm,一开始做起来也很简单。

    如果你真的不想重绘背景,你可以做的是当你改变隐藏背景的单元格的值时,你应该存储那块背景,当那个单元格“死”时,绘制再次回来。以前的游戏用了很多,今天可能仍然用得很多,我不知道。

    如果您的大部分框架都发生了很大变化,那么重新绘制所有内容似乎只是最简单的事情。 (我的直觉是它会是最有效的,但我可能是错的)。

    There's a a question over on gamedev network that goes deeper into what you're asking..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多