org.jrabbit.base.core.loop
Class LoopController

java.lang.Object
  extended by org.jrabbit.base.core.loop.LoopController

public class LoopController
extends java.lang.Object

Provides methods of regulating processes.

Author:
Chris Molini

Field Summary
protected  int duration
          The duration of the recorded interval, in the set resolution.
protected  int fps
          How many iterations we want per second.
protected  long lastCall
          Keeps track of when the time was last recorded so we can know the interval between checks.
protected  boolean regulate
          Whether or not to sleep the thread.
protected  int resolution
          The units for the timer to use - the number of ticks in a second.
protected  boolean smooth
          Whether or not the update variable should be a constant, "predicted" value - i.e., resolution / fps.
 
Constructor Summary
LoopController()
          Creates a default controller that will call at 60 fps and report times in microseconds.
 
Method Summary
 int delta()
          Returns the desired interval of time.
 int fps()
          Gets execution rate.
 boolean regulating()
          Gets if the controller is pausing execution.
 int resolution()
          Gets the current resolution.
 void setFPS(int fps)
          Sets the desired iterations per second.
 void setRegulate(boolean regulate)
          Controls if the game sleeps.
 void setResolution(int resolution)
          Sets the resolution for recorded deltas.
 void setSmooth(boolean smooth)
          Tells the controller to use the recorded time interval for delta() or if it should calculate the desired delta (resolution / fps).
 void sleep()
          If the controller is allowed to regulate, this sleeps the game so as to sync to the desired fps.
 boolean smoothing()
          Gets if the controller is smoothing.
 void tick()
          Advances the clock to record the amount of time that has passed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

regulate

protected boolean regulate
Whether or not to sleep the thread. By disabling this, the associated process will run as fast as it can (useful for simulating over a large number of iterations). Default: true.


smooth

protected boolean smooth
Whether or not the update variable should be a constant, "predicted" value - i.e., resolution / fps. Default: true.


resolution

protected int resolution
The units for the timer to use - the number of ticks in a second. Default: 10,000 - i.e., the controller reports in microseconds.


fps

protected int fps
How many iterations we want per second. Default: 60


lastCall

protected long lastCall
Keeps track of when the time was last recorded so we can know the interval between checks.


duration

protected int duration
The duration of the recorded interval, in the set resolution.

Constructor Detail

LoopController

public LoopController()
Creates a default controller that will call at 60 fps and report times in microseconds.

Method Detail

tick

public void tick()
Advances the clock to record the amount of time that has passed.


delta

public int delta()
Returns the desired interval of time. If delta smoothing is on, it actually ignores the value calculated in advance() and returns a predicted, constant value instead.

Returns:
The interval of time to update, in the set resolution.

sleep

public void sleep()
If the controller is allowed to regulate, this sleeps the game so as to sync to the desired fps.


setFPS

public void setFPS(int fps)
Sets the desired iterations per second.

Parameters:
fps - How many times we want to execute per second.

fps

public int fps()
Gets execution rate.

Returns:
How many times we execute per second.

setResolution

public void setResolution(int resolution)
Sets the resolution for recorded deltas. Obviously, this makes the most sense if resolution is a multiple of 10 (1000 for milliseconds, 10000 for microseconds, etc). Recalculates the current delta to take advantage of the new resolution.


resolution

public int resolution()
Gets the current resolution.

Returns:
How many ticks are in a second.

setRegulate

public void setRegulate(boolean regulate)
Controls if the game sleeps.

Parameters:
regulate - Whether or not the game should sync to the required fps.

regulating

public boolean regulating()
Gets if the controller is pausing execution.

Returns:
Whether or not the game is sleeping the thread.

setSmooth

public void setSmooth(boolean smooth)
Tells the controller to use the recorded time interval for delta() or if it should calculate the desired delta (resolution / fps). This really only changes performance if the game drops below the desired frame rate. If it does, you have a tradeoff to make. By keeping the delta constant, you are able to make every update keep a steady pace. This is useful if you have, say, a physics engine that must update at a certain rate (example: Phys2D). Additionally, you can actually hard-code the rate of updateables, allowing for less calculation. However, if you allow the delta to scale, you game may perform better on a worse computer.


smoothing

public boolean smoothing()
Gets if the controller is smoothing.

Returns:
Whether or not the update values are constant or dynamic.