I am using the GLUT timer function in a scenario slightly more complex than covered in tutorials and FAQs. Simplified abstract is the following:
If a condition is met, screen has to redraw timely and in short intervals. If a condition isn't met, it means the re-display is controlled by other mechanisms, therefore the timer function shouldn't stay in the way by repeating the work already being done otherwise. As it cannot be deregistered, does it have to be programmatically slowed down and put out of purpose? Is the following code snippet a proper way of implementing it in 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;
}
Thanks in advance!
You can simply avoid registering the callback when condition
is false, instead of making it run more slowly. From glutTimerFunc
documentation:
Unlike most other callbacks, timers only occur once.
Then, when the condition
becomes true again, register the callback, from the code that sets condition
to true. Be careful not to re-register an already scheduled callback - you should keep another flag that tells whether the callback is currently registered or not, as the docs says that "you cannot deregister a timer callback".
Code example:
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);
}