gpt4 book ai didi

c++ - glutIdleFunc 不调用空闲函数

转载 作者:行者123 更新时间:2023-11-30 00:39:59 25 4
gpt4 key购买 nike

我正在创建一个迷宫程序,它会随机生成一条路径。我正在使用一个空闲函数来计算路径的下一个方向和形状,但由于某种原因,空闲函数没有被 glutIdleFunc 调用。我用 visual studio 的调试器对此进行了检查,并让它逐步执行每一行代码。当调试器到达“glutIdleFunc(idle)”时,它只是跳过它而不是进入函数。

之前的一个build调用了idle getting,但是里面的逻辑不对,只好完全重写了idle函数。我确实摆脱了我的新逻辑不再需要的一些全局变量,但我认为它们不应该影响是否调用 idle。

这是调用 glutIdleFunc 的 main。

//<<<<<<<<<<<<<<<<<<<<<<<< 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

谁能帮帮我? idle 没有被调用,我错过了什么错误?

(我也无法从 visual studio 将代码粘贴到这里,格式变得非常困惑。我的浏览器使用 chrome)

这是我完整的 Visual Studio 项目文件夹的链接

http://dl.dropbox.com/u/15786901/Maze.rar

最佳答案

你的误解在于:

glutIdleFunc(idle);  // IDLE FUNCTION IS CALLED HERE

glutIdleFunction 不会调用 idle。它只是将 idle 注册为回调,每当 GLUT 处理所有未决事件时调用; glutPostRedisplay 发布一个重新显示事件,因此如果您在某处调用了 glutPostRedisplay——比如在显示函数的末尾——那么空闲处理程序将不会被调用。因此,在注册空闲回调的程序中,glutPostRedisplay 只能从 GLUT 事件处理程序调用,显示处理程序除外。

关于c++ - glutIdleFunc 不调用空闲函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7601700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com