I use Glut
to make a simple animation. In the main function, glutTimerFunc(TIMERMSECS, animate, 0)
is called. The two pieces of codes generate the same graphic.
const int TIMERMSECS = 20;
float animation_time = 0;
const float animation_step = .5;
Method 1:
void animate(int t){
float time_elapsed = TIMERMSECS/1000.0;
float current_step = animation_step* time_elapsed;
glutTimerFunc(TIMERMSECS, animate, 0);
if(current_step < animation_step*2)
animation_time += current_step;
glutPostRedisplay();
}
Method 2:
void animate(int t){
float time_elapsed = TIMERMSECS/1000.0;
float current_step = animation_step* time_elapsed;
if(current_step < animation_step*2)
animation_time += current_step;
glutPostRedisplay();
glutTimerFunc(TIMERMSECS, animate, 0);
}
The only difference between them is the position of glutTimerFunc
. For Method 1
, it looks like a recursive that will never reach the end of animate()
function. But why does that still work?