Performing tasks periodically

To perform a task periodically, you can use the TClock::DelayUntil function inside a loop.

The following example assumes that:

The example draws a frame of the animation every 15th of a second, and can play the animation both forward and backward. There is no guarantee that the first and last frames are drawn.

NOTE This example uses DelayUntil to avoid time slippage. If you use DelayFor(frameRate), the time between the delays and the time that DelayFor takes are not taken into account, causing time to slip.

      TTime   duration = aMovie.GetDuration();
      static const TSecond kBaseFrameRate = (1.0/15.0);
      TSeconds    frameRate = kBaseFrameRate;
      TSeconds    nextTime;
      TSeconds    currentTime;
      
      animationClock.SetTime(TTime::kZero);
      nextTime = TTime::kZero;
      
      while (duration > nextTime && nextTime >= TTime::kZero) {
      
          aMovie.Draw(nextTime);
      
          nextTime += frameRate;
      
          animationClock.DelayUntil(nextTime);
          animationClock.Now(currentTime);
          if (currentTime < nextTime){
              frameRate = -kBaseFrameRate;
          } else {
              frameRate = kBaseFrameRate;
          }
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker