我正在使用 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/