2011-09-29 108 views
1

我正在创建一个迷宫程序,它随机生成一个路径。我正在使用空闲函数来计算路径的下一个方向和形状,但由于某些原因,空闲函数未被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

+0

glutIdleFunc不调用空闲函数,它只是注册回调函数。应在glutMainLoop执行过程中调用回调。 –

+0

这更有意义。我在错误的地方寻找问题。当我第一次初始化迷宫时,发现我没有更新闲置功能中的堆栈,所以它似乎跳过了我的闲置功能。 –

回答

1

当调试器进入“glutIdleFunc(空闲)”时,它只是跳过它而不是进入函数。

glutIdleFunc()只是用GLUT注册函数指针。 glutMainLoop()内部的逻辑负责实际调用它。

+0

谢谢,我误解了它的工作机制。谢谢你为我澄清事情。 –

1

glutIdleFunc不叫空闲功能,它仅分配()函数中glutMainLoop被调用时,您的过程中有没有其他事情可做。

2

你的误解就在这里:

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

glutIdleFunction不叫空闲。它仅将空闲的注册为回调,只要GLUT处理完所有待处理事件即被调用; glutPostRedisplay发布了一个重新显示事件,所以如果你在某处调用了glutPostRedisplay - 就像在显示函数的末尾 - 那么空闲处理程序将不会被调用。因此,在注册空闲回调的程序中,glutPostRedisplay只能从GLUT事件处理程序调用,除了显示处理程序。