glut timer function burns my CPU (still..)

Hello,
I’m new to OGL so sorry for this stupid question…
I make a simple 2d application and I need to refresh my scene periodically. so I use glutTimerFunc. The problem is that whatever the period (1000ms for example), my CPU is used at 100%.
Here is my code :

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRects(0,0,10,10);
glutSwapBuffers();
}

void timerFunction(int arg)
{
glutPostRedisplay();
glutTimerFunc(500, timerFunction, 0);
}

int main(int argc, char** argv)
{

//no idle function
glutTimerFunc(500, timerFunction, 0);
glutPostRedisplay();
glutMainLoop();

return 0;
}

Is it a bug of myself or is a normal comportment of glut?

Thanks in advance

As far as I know, it’s a byproduct of GLUT. Your drawing routine is not taking up all the CPU. In fact, it’s GLUT being idle that’s sucking it all up. You might have better luck using freeGLUT, which is a free implimentation of GLUT. It’s got a function glutMainLoopEvent() which you can use at your discretion to have GLUT process any events it may be waiting for. This way you can control how often GLUT updates itself through your own loop. Having the computer delay() in your main loop will save some CPU, at the risk of (possibly) missing some events. But I wouldn’t worry about it eating up so much CPU, as that’s what tight loops do. It’s perfectly normal because it’s veeeerry quickly checking whether it has something to do or not, and that takes up a lot of CPU. Expect this in any game development. Hope this helps!

-= soniCron =-

Ok, thank you very much sonicron. I will try freeGLUT.

Was your problem resolved using FreeGLUT?

Otherwise, you may like to try OpenGLUT.
I commonly use timers as an alternative to
the idle callback, and it works fine.

www.openglut.org

Cheers,

Nigel