I'm trying to write a timing loop in my empeg app that maintains more or less a constant frame rate. You can imagine that such a loop would be very handy for games etc. Here's my current attempt:


struct timeval frameTime, startTime, currentTime, deltaTime, padTime;
struct timespec padTimeSpec;

frameTime.tv_sec = 0;
frameTime.tv_usec = 1000000 / FPS; /* desired frames per second */

while (looping) {
gettimeofday(&startTime, NULL);

/* update the application here */

/* calculate sleep time to required maintain desired FPS */
gettimeofday(&currentTime, NULL);
timersub(&currentTime, &startTime, &deltaTime);
timersub(&frameTime, &deltaTime, &padTime);
if (padTime.tv_usec > 0) {
/* have a sleep */
TIMEVAL_TO_TIMESPEC(&padTime, &padTimeSpec);
nanosleep(&padTimeSpec, NULL);
}
}



At present, when running my app with the player software going under hijack, and setting a target frame rate of 30 fps,
it takes about 50 seconds to do 1000 updates, whereas it *should* take around 33 seconds.

Am I approaching this the right way or am I completely off track? I can't say I'm very experienced at unix programming so if somebody has any suggestions I'd be very grateful.

Cheers,

Richard
--