I use Glut
制作一个简单的动画。在主函数中,glutTimerFunc(TIMERMSECS, animate, 0)
叫做。这两段代码生成相同的图形。
const int TIMERMSECS = 20;
float animation_time = 0;
const float animation_step = .5;
方法一:
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();
}
方法二:
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);
}
他们之间唯一的区别是位置glutTimerFunc
. For Method 1
,它看起来像一个永远不会到达末尾的递归animate()
功能。但为什么这仍然有效?
glutTimerFunc
将不会立即地在任何情况下都调用定时器函数。即使时间为0。它总是等待消息处理循环,即使如此,它也只会在所有其他消息处理完成后才调用请求的函数。这样,“重绘窗口”和“调整窗口大小”等重要消息仍然会得到处理。
一般来说,您不应该依赖计时器函数特别准确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)