com.brackeen.javagamebook.util
Class TimeSmoothie

java.lang.Object
  extended by com.brackeen.javagamebook.util.TimeSmoothie

public class TimeSmoothie
extends java.lang.Object

Smoothes out the jumps in time do to poor timer accuracy. This is a simple algorithm that is slightly inaccurate (the smoothed time may be slightly ahead of real time) but gives better-looking results.


Field Summary
protected static long AVERAGE_PERIOD
          Take the average of the last few samples during the last 100ms
protected  int firstIndex
           
protected static long FRAME_RATE_RECALC_PERIOD
          How often to recalc the frame rate
protected  float frameRate
           
protected static long MAX_ELAPSED_TIME
          Don't allow the elapsed time between frames to be more than 100 ms
protected static int NUM_SAMPLES
           
protected static int NUM_SAMPLES_BITS
           
protected static int NUM_SAMPLES_MASK
           
protected  int numFrames
           
protected  int numSamples
           
protected  long[] samples
           
protected  long startTime
           
 
Constructor Summary
TimeSmoothie()
           
 
Method Summary
 void addSample(long elapsedTime)
          Adds a time sample.
 long getAverage()
          Gets the average of the recorded time samples.
 float getFrameRate()
          Gets the frame rate (number of calls to getTime() or addSample() in real time).
 long getTime(long elapsedTime)
          Adds the specified time sample and returns the average of all the recorded time samples.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FRAME_RATE_RECALC_PERIOD

protected static final long FRAME_RATE_RECALC_PERIOD
How often to recalc the frame rate

See Also:
Constant Field Values

MAX_ELAPSED_TIME

protected static final long MAX_ELAPSED_TIME
Don't allow the elapsed time between frames to be more than 100 ms

See Also:
Constant Field Values

AVERAGE_PERIOD

protected static final long AVERAGE_PERIOD
Take the average of the last few samples during the last 100ms

See Also:
Constant Field Values

NUM_SAMPLES_BITS

protected static final int NUM_SAMPLES_BITS
See Also:
Constant Field Values

NUM_SAMPLES

protected static final int NUM_SAMPLES
See Also:
Constant Field Values

NUM_SAMPLES_MASK

protected static final int NUM_SAMPLES_MASK
See Also:
Constant Field Values

samples

protected long[] samples

numSamples

protected int numSamples

firstIndex

protected int firstIndex

numFrames

protected int numFrames

startTime

protected long startTime

frameRate

protected float frameRate
Constructor Detail

TimeSmoothie

public TimeSmoothie()
Method Detail

getTime

public long getTime(long elapsedTime)
Adds the specified time sample and returns the average of all the recorded time samples.


addSample

public void addSample(long elapsedTime)
Adds a time sample.


getAverage

public long getAverage()
Gets the average of the recorded time samples.


getFrameRate

public float getFrameRate()
Gets the frame rate (number of calls to getTime() or addSample() in real time). The frame rate is recalculated every 500ms.