glutTimerFunc问题

发布于 2024-12-01 21:50:10 字数 1067 浏览 4 评论 0原文

我使用 Glut 制作一个简单的动画。在主函数中,调用了 glutTimerFunc(TIMERMSECS, animate, 0)。这两段代码生成相同的图形。

const int TIMERMSECS = 20;
float animation_time = 0;
const float  animation_step = .5;

方法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();
}

方法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);
}

它们之间唯一的区别是glutTimerFunc的位置。对于方法 1,它看起来像是一个永远不会到达 animate() 函数末尾的递归。但为什么这仍然有效?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-12-08 21:50:10

在任何情况下,glutTimerFunc 都不会立即调用计时器函数。即使时间为 0,它也始终等待消息处理循环,即使如此,它也只会在所有其他消息处理完成后才会调用所请求的函数。这样,“重绘窗口”和“调整窗口大小”等重要消息仍然会得到处理。

一般来说,您不应该依赖计时器函数特别准确。

glutTimerFunc will not immediately call the timer function under any circumstances. Even if the time is 0. It always waits for the message processing loop, and even then it will only call the requested function when all other message processing has completed. That way, important messages like "repaint window" and "resize window" still get processed.

In general, you should not rely on the timer function being particularly accurate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文