驯服 glutTimerFunc
taming the glutTimerFunc
我正在使用 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
再次变为真时,从将 condition
设置为真的代码中注册回调。小心不要重新注册一个已经安排好的回调——你应该保留另一个标志,告诉回调当前是否已注册,正如文档所说 "you cannot deregister a timer callback".
代码示例:
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);
}
我正在使用 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
再次变为真时,从将 condition
设置为真的代码中注册回调。小心不要重新注册一个已经安排好的回调——你应该保留另一个标志,告诉回调当前是否已注册,正如文档所说 "you cannot deregister a timer callback".
代码示例:
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);
}