Reputation: 235
I am trying to make a man jump on the y axis, so I use a loop of 2 seconds, the first second it should move down and bend its knees, and the second second it should move up and then finish in the starting position. Right now am just starting to make the man go down and bend it's knees in the first second, I haven't programmed the rest of the animation. The problem is that glutPostRedisplay is not refreshing the screen inside the loop as needed so it looks like an animation, it just re displays the screen after the loop has completed. How do I get glutPostRedisplay to refresh the screen inside my loop so it looks like an animation?
case ' ':
miliseconds = 0;
MoveManY = 0;
start = clock();
i=0;
while (miliseconds<2000)
{
finish = clock();
miliseconds = (finish - start);
Sleep(1);
if (miliseconds<1000)
{
MoveManY=MoveManY-0.02;
}
glutPostRedisplay();
}
break;
Upvotes: 2
Views: 1500
Reputation: 3369
glutPostRedisplay
does not directly trigger redrawing the screen. It only indicates to the system that the screen needs redrawing as soon as possible. Calling it multiple times in a loop, like you do, actually has the same effect as calling it once. See here for more details.
I cannot tell where the code you posted is called from. However, what you want to do is use glutIdleFunc
to designate a method to be called when there's nothing else to do and, in it, do what you need for a single frame of your animation and call glutPostRedisplay
once. This will require to move your timer somewhere else.
I found this tutorial to be really helpful with GLUT animations.
Upvotes: 4