PDA

View Full Version : problem with Sleep( ) in glut



onlyhuman23
08-10-2002, 01:25 AM
I am have made a 2D app in glut, which updates after a change in the functionality of the program. The problem is that if I want to run the program in a loop instead of initiating it manually the functions execute so quickly that it doesnt show. In order to slow it down I use Sleep( ) function after every glutPostRedisplay() funtions but the problem is that the screen freezes when I call Sleep( ). Another thing is that I manually update the program through pop-up menu. Has the pop-up menu got anything to do with this problem?
Please help.

Morglum
08-10-2002, 07:38 AM
The problem is that you should never use a Sleep-like function to slow down a program. You have to find other ways. Possibilities include :
1) make a "timer callback" : that is, ask to GLUT to call a function of your choice every x milliseconds.
2) before doing an action that has to be done "not too fast", measure the elapsed time since the last time you did it. Then, adapt this action to this time.

If it's for graphical animation, 2) is definitely the right way. the glut code could look like :

float position_of_object;

void display ()
{ ...
float elapsed_time = measure_elapsed_time ();
position_of_object += a_constant * elapsed_time;
draw_object (position_of_object);
glutPostRedisplay ();
}

void main (int argc, char *argv[])
{ ...
glutDisplayFunc (display);
}