Jump to content
  • Advertisement
Sign in to follow this  
Omelas0469

OpenGL Having trouble using glutIdleFunc()

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

If you intended to correct an error in the post then please contact us.

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);
	glLoadIdentity();
	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();
	loadGLTextures();
	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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!