I want to keep an object moving forward without the user having to do anything. I understand that this is a job for glutTimerFunc
and I believe I'm implementing it properly as shown below. For some reason, if I leave the screen untouched nothing updates; but if I move the mouse (or create any kind of 'event') the scene then updates. If I wait a few seconds and then retrigger an update myself, the object moves the amount that it should have in those few seconds (e.g. if it moves one unit per second, after waiting 3 seconds and retriggering the object jumps 3 units). Therefore the timer is working but I just don't know why the screen isn't redisplayed constantly.
void myTimer(int v) {
if (forward == 1) {
//increment movement variable
}
if (backward == 1) {
//increment movement variable
}
glutPostRedisplay;
glutTimerFunc(100 / n, myTimer, v);
}
Below is the snippet for my up and down (forward and backward) keys:
if (key == GLUT_KEY_UP)
{
forward = 1;
backward = 0;
}
if (key == GLUT_KEY_DOWN)
{
forward = 0;
backward = 1;
}
I also have the callback glutTimerFunc(1000, myTimer, n);
in my main before glutMainLoop()
.
Why isn't my timer causing the screen to re-render without my triggering it?
glutPostRedisplay;
^ missing something?
That just evaluates the address of glutPostRedisplay
which doesn't really do anything.
You need the empty parens to actually call glutPostRedisplay()
:
glutPostRedisplay();