我目前正在尝试使用OpenGL在C ++中编写Pacman。我目前正在尝试创建一个显示列表来绘制迷宫。我的想法是创建一个方形瓷砖,然后只要matrix
指示某个位置的墙段,就显示所述瓷砖。我的代码是:
const unsigned int FREE = 0;
const unsigned int WALL = 1;
const unsigned int GHOST_WALL = 2;
const unsigned int matrix[22][19] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
GLuint tile_handle = 0;
GLuint maze_handle = 0;
/*
* Initialize maze by precompiling its components
*/
void maze_init() {
// request a single display list handle for the tile
tile_handle = glGenLists(1);
glNewList(tile_handle, GL_COMPILE);
glColor3f(0, 0, 0.7f); // Blue
glBegin(GL_QUADS);
glVertex2i(0,0);
glVertex2i(1,0);
glVertex2i(1,1);
glVertex2i(0,1);
glEnd();
glEndList();
// request a single display list handle for the entire maze
maze_handle = glGenLists(1);
glNewList(maze_handle, GL_COMPILE);
glColor3f(0, 0, 0.7f); // Blue
for(int row = 0; row < 22; row--) {
for(int col = 0; col < 19; col++) {
if(matrix[21-row][col] == WALL) {
glPushMatrix();
glScaled(30, 30, 1);
glTranslated(col, row, 0);
glCallList(tile_handle);
glPopMatrix();
}
}
}
glEndList();
}
然后我在我的主要初始化方法中使用maze_init()
并在我的主显示功能中使用调用glCallList(maze_handle);
。编译得很好,但是当我尝试运行它时,它会给我Bus error: 10
错误。
现在我尝试了同样没有glNewList(maze_handle, GL_COMPILE);
定义中的循环并且运行正常。因此我的问题是:为什么调用列表中的循环(或条件语句)会产生总线错误?
我在glNewList(maze_handle, GL_COMPILE);
内尝试了一个简单的循环,它调用了DISTINCT显示列表并且运行正常。那么问题就是我重复调用同一个显示列表?出于某种原因,我似乎无法在这里找到问题......