c - 驯服 glutTimerFunc

标签 c opengl glut

我正在使用 GLUT 计时器功能 的场景比教程和常见问题解答中介绍的场景稍微复杂一些。简化摘要如下:

如果满足条件,屏幕必须以短间隔及时重绘。如果不满足条件,则意味着重新显示是由其他机制控制的,因此定时器功能不应通过重复已经完成的工作来阻碍。由于它不能注销,是否必须以编程方式减慢它的速度并取消其目的?以下代码片段是否是在 C 中实现它的正确方法?

#define SLOW 1000
#define FAST 100
GLboolean condition = GL_FALSE

//   {   …   } //mechanism for setting the condition

void update (int val)
{
if(val)glutPostRedisplay();
return;
}

void timerCallback(int par)
{
if(condition == GL_TRUE){
  update(1);
  glutTimerFunc(FAST, timerCB, FAST); 
  }else {
  glutTimerFunc(SLOW, timerCB, SLOW);
  }
return;
}

提前致谢!

最佳答案

您可以简单地避免在 condition 为 false 时注册回调,而不是让它运行得更慢。来自 glutTimerFunc 文档:

Unlike most other callbacks, timers only occur once.

然后,当 condition 再次变为 true 时,从将 condition 设置为 true 的代码中注册回调。小心不要重新注册一个已经安排好的回调——你应该保留另一个标志,告诉回调当前是否已注册,正如文档所说“你不能注销定时器回调”。

代码示例:

GLboolean isCallbackRegistered = GL_FALSE;

void timerCallback(int par)
{
  if(condition == GL_TRUE){
    update(1);
    glutTimerFunc(FAST, timerCB, FAST); 
  } else {
    isCallbackRegistered = GL_FALSE;
  }
}

// ... the code the changes the value of `condition`
condition = GL_TRUE;
if(isCallbackRegistered != GL_TRUE) {
  isCallbackRegistered = GL_TRUE;
  glutTimerFunc(FAST, timerCB, FAST); 
}

关于c - 驯服 glutTimerFunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35175485/

相关文章:

c - 'Segmentation fault (core dumped) '是什么原因

opengl - 隐藏 GLUT 窗口

c++ - 将 SDL 表面转换为 GL 纹理时出现问题

c++ - 如何在 OpenGL 中跨图元重用顶点

C++:跟踪耗时

ubuntu - 在 Ubuntu 14.04.5 中使用 cabal 时,如何在 Haskell 中安装分析库?

c - C 中带有随机主元的链表快速排序

c - 在 C 中迭代字符串 block 以构建新字符串的模式?

c - 关于 "object having more than one object representation"的未指定行为

c++ - OpenGL以像素为单位定义顶点位置