views:

202

answers:

1

I have a standard glut implementation. The display function redraws each object, but I need a constant update on certain values of each object. As it is, the only way I can think to do this is to spawn a thread to handle the updating. However, I can't use glutPostRedisplay() from a different thread to get glut to refresh the window. What is a good way to have a loop to update values alongside the glut loop?

Also, what's the best way to sleep for fractions of seconds (instead of sleep() for whole seconds).

+2  A: 

If you need some sort of regular updating, you probably want to set a glutIdleFunc. This is a function that will get called in a loop whenever there are no events coming in to be processed. If you instead want to call something at regular intervals (as opposed to as fast as possible), you might want to try glutTimerFunc which allows you to schedule something to be run by the GLUT loop some number of milliseconds in the future.

As for your second question, if you need to sleep for fractions of seconds, you might want to try usleep for microsecond resolution sleep periods, or nanosleep to specify sleep periods in nanoseconds (though you're not actually going to get nanosecond resolution). I don't know what platform you're on or if these are available on Windows, but they should be available on any POSIX compatible system (Linux, BSD, Mac OS X). But perhaps for your purposes glutTimerFunc will work better.

edit to add: It looks like on Windows you would need to use Sleep (note the capital S), which takes a time in milliseconds.

Brian Campbell
That sounds perfect. You rule
James