Skip to main content
GameDev.net gamedev.net
🔒 Locked

FreeGLUT glutMainLoopEvent() won't return

Started by MountainGod Sep 22, 2010 at 4:58 AM 3 replies 8.2k views
Original Post
MountainGod
MountainGod
Can someone explain how to use glutMainLoopEvent() to me? It's surely one of the most useful additions FreeGLUT provides, but I can't seem to use it properly. Am I missing something?

Specifically, why doesn't the following code work:
#include <GL/freeglut.h>//display function - draws a triangle rotating about the originvoid cback_render(){	//keeps track of rotations	static float rotations = 0;	//OpenGL stuff for triangle	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glRotatef(rotations, 0, 0, 1);	glBegin(GL_TRIANGLES);		glVertex3f(0,0,0);		glVertex3f(1,0,0);		glVertex3f(0,1,0);	glEnd();	//display on screen	glutSwapBuffers();	//rotate triangle a little bit, wrapping around at 360°	if (++rotations > 360) rotations -= 360;}int main(int argc, char **argv){	//initialisations	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);	glutInitWindowPosition(100, 100);	glutInitWindowSize(512, 512);	//create window and register display callback	glutCreateWindow("freegluttest");	glutDisplayFunc (cback_render);	//loop forever	while(1) glutMainLoopEvent();	return 0;}


It only does one iteration rather than looping fully like I'd expect. Clicking on the window (to generate an event) causes a single further iteration to occur for every click (i.e. the triangle moves round a little bit).

Specifying glutIdleFunc(cback_render) doesn't make a difference.
Specifying glutPostRedisplay() in the display callback DOES work, but it thrashes the CPU whereas standard glutMainLoop() + glutIdleFunc() looping doesn't. I've modified the above code with macros to demonstrate the difference.

It's the difference in CPU use that makes me think my solution isn't the best. I'd appreciate anyone's suggestions.
Ekas
Ekas
Well the docs say that:

"Exit the event processing. This happens if you either called glutMainLoopEvent() instead of glutMainLoop() (glutMainLoopEvent() resolves all pending events, then returns to you), or if certain specific events occur, such as glutLeaveMainLoop()."

It returns out after taking care of all pending events. After that the idle callback will be fired if hooked. When you touch the window you generate new events therefor triggering glutMainLoopEvent to fire your rendercallback.

Also i don't think you're supposed to while(1). That's polling (pulling), not the Event driven (pushing) way of doing things.
MountainGod
MountainGod
So I have to continually manufacture and provide events in order to have FreeGLUT loop freely like I want it to?

I must admit the polling rather than pushing design comes more naturally, but this solution sounds pretty hacky.
Ekas
Ekas
No. You don't have to create events to get continous update calls. A simple way of getting continous update calls is to fill out the OnIdle eventhandler with a call to your own update function/method.

Look at the "one.c" example supplied with the FreeGlut-2.6.0 package. There should be a function called "SampleIdle" that does what you want (you have to fill it out yourself).


MountainGod
MountainGod
As I mentioned before, using an idle callback ( via glutIdleFunc() ) doesn't solve my issue with glutMainLoopEvent() .

As in, the program still doesn't loop continuously like it should do with:
//...glutDisplayFunc(renderfunction);glutIdleFunc   (renderfunction);while(1){    glutMainLoopEvent();    printf("Loop completed"); //for testing purposes}


The console fills up with test output, meaning glutMainLoopEvent() is returning. But on every iteration after the first one, it doesn't do anything, not even with an idle callback defined.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.