# OpenGL Having trouble using glutIdleFunc()

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've used openGL before but don't really have much experience with animation. I'm working on a simply Pacman game in C++ with openGL. I've got my display callback function which is quite simple
void displayScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
drawMap();
pacman->draw();
glPopMatrix();
glFlush();
}

calls drawMap which draws the map tiles and draw() which draws pacman at his new position (the position is updated when the user presses the arrow keys). Here's my main function:
int main(int argc, char **argv) {  // called when application starts
glutInit(&argc, argv);            // initialize GLUT
glutInitDisplayMode(GLUT_RGBA);   // set RGBA display mode
glutInitWindowPosition(325, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Pacman");
init();
pacman = new Pacman();
glutDisplayFunc(displayScene);     // set our rendering function
glutSpecialFunc(arrow_keys);
glutMainLoop();               // process events and wait until exit
glutIdleFunc(update);
return 0;
}

I put the IdleFunc where it is because I read a small tutorial online which said "one thing you should not do is setup the idle callback before calling glutMainLoop". update() is a function which _only_ calls glutPostRedisplay(). From what I understand, glutPostRedisplay() just forces a call to the display function. However, when I press the arrow keys to update pacman`s position, the position is updated but pacman is still drawn in the same position. The draw() function is only being invoked once. Why? EDIT: I did exactly what I read not to do, I interchanged the last two lines of my main function i.e. glutMainLoop() and glutIdleFunc(update) just to put glutIdleFunc before glutMainLoop like I read I shouldn't and now my program works as expected... Did I misunderstand what is meant by "one thing you should not do is setup the idle callback before calling glutMainLoop"? Am I misusing glutIdleFunc? [Edited by - Omelas0469 on December 2, 2008 10:25:18 PM]

##### Share on other sites
Small tutorials one finds online are often written by stupid people, and should in any case be taken with a grain of salt. glutMainLoop() never returns, so calling functions after that one is not particularly gainful.

EDIT: Ah, I see that's in the documentation. That's probably not written by stupid people. [wink] You should read the rest of that paragraph, though, where it talks about the visibility function. Personally I think the extra step is a little silly... I've never experienced a significant delay before the window shows up.

##### Share on other sites
Arghhhh missed something if it was present, different sites seem to have their own version of the man pages sometimes! I didn't see the part that precedes it in the original: "_If you do use the idle callback for animation,_ one thing you should not do is setup the idle callback before calling glutMainLoop."

DOH! I am using it for animation so I guess I can put it anywhere...

Thanks for making me look it up somewhere else.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013759
×