我正在创建一个迷宫程序,它随机生成一个路径。我正在使用空闲函数来计算路径的下一个方向和形状,但由于某些原因,空闲函数未被glutIdleFunc调用。我使用visual studio的调试器检查了这一点,并逐步完成了每行代码。当调试器进入“glutIdleFunc(空闲)”时,它只是跳过它而不是进入函数。glutIdleFunc不会调用空闲函数
先前的构建已经被调用,但其逻辑不正确,所以我不得不完全重写空闲函数。我确实摆脱了一些我不再需要的全局变量,但我认为他们不应该影响是否调用闲置。
这是调用glutIdleFunc的主要方法。
//<<<<<<<<<<<<<<<<<<<<<<<<main>>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char **argv){
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize(640,480); // set the window size
glutInitWindowPosition(100, 150); // set the window position on the screen
glutCreateWindow("Maze"); // open the screen window(with its exciting title)
glutDisplayFunc(myDisplay); // register the redraw function
myInit();
glutIdleFunc(idle); // IDLE FUNCTION IS CALLED HERE
glutMainLoop(); // go into a perpetual loop
}
这里是空闲功能
void idle(){
int c; // holds column value for square
int r; // holds row value for square
if((done == false) && just_visited.empty()){
// initialize random seed
srand (time(NULL));
// randomly select first maze square indices
c = rand() % num_col + 1;
r = rand() % num_row + 1;
}
else if(done == false){
// set c and r to index values for last
// accessed block
c = just_visited.top().col;
r = just_visited.top().row;
vector<square> possible_paths;
bool open_path = false; // will set to true if there is an untouched adjacent square to move to
// will not exit loop until an open path is found or the maze has been completed
while(open_path != true || done != true){
// if statements check each adjacent square to see if they are possible_paths
// if they are then they get put into possible paths vector for access later
if(map[r][c].north == true && map[r+1][c].east == true && map[r+1][c].north == true && map[r+1][c-1].east == true){
possible_paths.push_back(map[r+1][c]);
open_path = true;
}
if(map[r][c].east == true && map[r][c+1].east == true && map[r][c+1].north == true && map[r-1][c+1].north == true){
possible_paths.push_back(map[r][c+1]);
open_path = true;
}
if(map[r-1][c].north == true && map[r-1][c].east == true && map[r-2][c].north == true && map[r-1][c-1].east == true){
possible_paths.push_back(map[r-1][c]);
open_path = true;
}
if(map[r][c-1].north == true && map[r][c-1].east == true && map[r][c-2].east == true && map[r-1][c].north == true){
possible_paths.push_back(map[r][c-1]);
open_path = true;
}
if(!open_path){ // if no direction around current square is open, backtrack in maze
just_visited.pop(); // pop last off the stack
if(just_visited.empty()){ // if stack is empty then the maze is done
done = true;
}
// set and c and r to new square values
c = just_visited.top().col;
r = just_visited.top().row;
}
} // end of while loop
if(!done && open_path){
//choose a direction to go
int dir = rand() % possible_paths.size();
if(possible_paths[dir].col > c){
map[c][r].east = false;
just_visited.push(map[c+1][r]);
}
else if(possible_paths[dir].col < c){
map[c-1][r].east = false;
just_visited.push(map[c-1][r]);
}
else if(possible_paths[dir].row > r){
map[c][r].north = false;
just_visited.push(map[c][r+1]);
}
else if(possible_paths[dir].row < r){
map[c][r-1].north = false;
just_visited.push(map[c][r-1]);
}
} // end of if statement
glutPostRedisplay();
} //end of if statement
} // end of idle
任何人都可以帮我吗?我错过了什么错误,闲置没有被调用?
(另外我无法将代码粘贴到这里从视觉工作室,格式化被超级搞砸了。我在使用Chrome浏览器)
这里是我完整的视觉工作室项目文件夹的链接
http://dl.dropbox.com/u/15786901/Maze.rar
glutIdleFunc不调用空闲函数,它只是注册回调函数。应在glutMainLoop执行过程中调用回调。 –
这更有意义。我在错误的地方寻找问题。当我第一次初始化迷宫时,发现我没有更新闲置功能中的堆栈,所以它似乎跳过了我的闲置功能。 –