开发者

glutIdleFunc not calling idle function

I'm creating a maze program, that randomly generates a path. I'm using an idle function to calculate the next direction and shape of the path, but for some reason the idle function is not being called by glutIdleFunc. I checked this with visual studio's debugger and having it step through each line of code. When the debugger gets to the "glutIdleFunc(idle)", it just skips over it rather than going into the function.

A previous build had idle getting called, but the logic in it was not right, so I had to completely rewrite the idle function. I did get rid of some global variables that I no longer needed with my new logic, but I don't think they should have affected whether or not idle gets called.

This is the main that calls 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

}

Here is the idle function

 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().ro开发者_如何学运维w;

     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

Can anyone help me out? What am I missing doing wrong that idle isn't getting called?

(Also I'm having trouble pasting code into here from visual studios, the formatting gets super messed up. I'm using chrome for my browser)

Here is a link to my full visual studios project folder

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


Your misconception lies here:

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

glutIdleFunction does not call idle. It merely registers idle as being a callback, called whenever GLUT did process all pending events; glutPostRedisplay posts a redisplay event, so if you called glutPostRedisplay somewhere – like at the end of the display function – then the idle handler will not be called. Thus in a program registering a idle callback, glutPostRedisplay should only be called from GLUT event handlers, except the display handler.


When the debugger gets to the "glutIdleFunc(idle)", it just skips over it rather than going into the function.

glutIdleFunc() just registers the function pointer with GLUT. Logic inside of glutMainLoop() takes care of actually calling it.


glutIdleFunc does not call the Idle function, it only assigns a function to be called in glutMainLoop() whenever your process has nothing else to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜