Karnivaurus
Karnivaurus

Reputation: 24121

What does glutTimerFunc() actually do?

If I run the following loop as part of my OpenGL program:

void Loop(int state)
{
    glutPostRedisplay();
    glutTimerFunc(1, Loop, 0);
}

Then, my program runs fine, and I am able to move the camera around the scene by dragging my mouse.

However, if I use this loop:

void Loop(int state)
{
    glutPostRedisplay();
    Loop();
}

I am not able to move around the scene with my mouse. The loop is being called, but it is just not processing any mouse or keyboard inputs.

I don't understand why this is. The only difference is that in the first example, a timer function is used to call Loop(), whereas in the second example, Loop() is called explicitly, without any delay.

So what is it that glutTimerFunc() is actually doing, besides calling Loop()?

Upvotes: 4

Views: 11742

Answers (1)

May Oakes
May Oakes

Reputation: 4619

In the first example, glutTimerFunc ensures Loop is called after 1 millisecond, however it returns and does not wait, meaning Loop in this case returns almost immediately.

In the second example, Loop is calling itself which results in infinite recursion and locks up your program's main thread, which would explain why nothing works in that case. Loop, then, never returns. If you left your program running long enough, your system would run out of memory due to the infinite recursion.

Upvotes: 4

Related Questions