net.kolls.railworld
Class GameLoop

java.lang.Object
  extended by net.kolls.railworld.GameLoop

public abstract class GameLoop
extends java.lang.Object

Represents a time stepping update loop. The loop performs given behavior as often as possible whilst still being consistent. To that end, the system maintains a count of the time required to perform the operation and narrows its wait to be close to that time. The wait time is automatically adjusted as the program progresses; thus, it is essential that the behavior pays attention to the CLOCK_WAIT value to determine "how much" of behavior or movement it should do at each step. Note that if the user provides a clock wait through options, this will override the adjusting algorithm and be fixed.

Author:
Steve Kollmansberger

Field Summary
static int CLOCK_WAIT
          Time between each run of the behavior, in milliseconds.
 boolean paused
          Set to true to suspend behavior but retain painting.
 
Constructor Summary
GameLoop(RailCanvas rc)
          Constructs a gameloop.
 
Method Summary
static double feetPerStepSpeed(double vel)
          Given a velocity in MPH, how many feet should we move per clock step? This is a convenience method.
 int fps()
          Calculate the current target frames per second
 void gameLoop()
          Begin the loop.
protected abstract  void prePaint()
          Behavior to perform before painting occurs.
protected abstract  void run()
          Behavior to perform after painting occurs.
 void runInLoop(java.lang.Runnable r)
          Run some commands inside the game loop.
 void stop()
          Stop the game loop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

paused

public boolean paused
Set to true to suspend behavior but retain painting. Note that the loop continues to run and update the display, so display events (scrolling, etc) are still possible.


CLOCK_WAIT

public static int CLOCK_WAIT
Time between each run of the behavior, in milliseconds. Applications should sample this every time the behavior is called, and they may reasonably assume that in this amount of time the behavior will be called again.

Constructor Detail

GameLoop

public GameLoop(RailCanvas rc)
Constructs a gameloop.

Parameters:
rc - A RailCanvas this loop should paint.
Method Detail

feetPerStepSpeed

public static double feetPerStepSpeed(double vel)
Given a velocity in MPH, how many feet should we move per clock step? This is a convenience method.

Parameters:
vel - The velocity in MPH, not to exceed Train.MAX_SPEED_MPH.
Returns:
A double indicating how many feet to move this clock step.

fps

public int fps()
Calculate the current target frames per second

Returns:
The current target FPS

gameLoop

public void gameLoop()
Begin the loop. This method should be called in a separate thread, as it will run until the canvas is finished, either by user command or by error. At that point, the method will return and the canvas and its helpers may be safely disposed.


stop

public void stop()
Stop the game loop. This method returns immediately, but the gameloop method will complete its current iteration before returning.


runInLoop

public void runInLoop(java.lang.Runnable r)
Run some commands inside the game loop. Given command will be run at the beginning of the next game loop. This ensures appropriate synchronization with train activity.

Parameters:
r - The commands to run.

prePaint

protected abstract void prePaint()
Behavior to perform before painting occurs.


run

protected abstract void run()
Behavior to perform after painting occurs.