[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: Time problem


On Saturday 14 June 2003 11:10, you wrote:
> I know the problem I will expose is classical, but Im a newbie in these
> things. Im coding a computer graphics work wich have a OpenGL doll made of
> spirals. This doll must do many movements. My problem is making this doll
> to be animated independent of machine clock. I mean, the animation must
> play at same speed in any machine.

There are two constraints involved here:
1. CPU speed
This is a hard constraint, and a very obvious one: If the CPU is too slow to 
calculate (and deliver) enough frames per second to meet your favorite speed, 
it won't work.
(For OpenGL, the same applies to the graphics board.)

2. Scheduling
I'm not sure about the other OS you mentioned, but at least Linux (the vanilla 
thingie from kernel.org) cannot guarantee any real-time performance, not even 
So you'll either live with the fact that in case of disk activity or sudden 
process fubar, your animation will become terribly slow, or you take a look 
at realtime capable operating systems (and I've seen plenty of them doing 
fancy graphics already).

Theory aside, since you don't know the duration of the frame calculations in 
advance, you cannot use a simple sleep()-like algorithm, but rather one which 
takes the current time _before_ the frame calculation starts, and sleeps 
until, say, 40 milliseconds from then on (which results in 25 fps).

void sleep_to(stamp){do{x = currenttime();}while(x < stamp);}
cur = currenttime();

(Yes, you could sleep() for 40 minus the time used up by calc_frame(), but 
this wouldn't allow you to do other things in the meantime, at least in a 
single thread.)

The exact code depends on the functions which are available on all the OSes 
you want to consider; also keep in mind that timing with milliseconds can be 
inaccurate (there was a thread on this list about it already).