uk.co.javagear
Class Throttle

java.lang.Object
  extended by uk.co.javagear.Throttle

public final class Throttle
extends java.lang.Object

Provides the speed throttling functionality based on System.currentTimeMillis() and Thread.sleep(). Heavily based on code by Arnon Cardoso.

Author:
Erik Duijs, Arnon Goncalves Cardoso

Field Summary
(package private) static boolean autoFS
          Auto frame skip.
(package private) static float avgFPS
          Average FPS.
(package private) static int DEFAULT_TARGET_FPS
          Throttle, frameskip constants.
(package private) static int DEFAULT_THROTTLE_STEP
          Throttle, frameskip constants.
(package private) static long fps
          Frames per second.
(package private) static int frameDuration
          Amount of ms.
(package private) static int frameNumber
          Framenumber relative to last recalc.
(package private) static int FRAMES_UNTIL_THROTTLE_RECALC
          Throttle, frameskip constants.
(package private) static int fskip
          Frame skip.
(package private) static float MAX_FPS_DEVIATION
          Throttle, frameskip constants.
(package private) static int MAX_FRAME_SKIP
          Throttle, frameskip constants.
(package private) static int MAX_THROTTLE_STEP
          Throttle, frameskip constants.
(package private) static int maxFPS
          Maximum FPS.
(package private) static int MIN_THROTTLE_STEP
          Throttle, frameskip constants.
(package private) static int minFPS
          Minimum FPS.
(package private) static long minimumSleep
          Minimum sleeptime to have effect on the JVM.
(package private) static long recalcCount
          Count recalc for measuring avg fps.
(package private) static long sleep
          Amount of sleep time in ms.
(package private) static long sumFPS
          Sum frames per second for measuring avg FPS.
(package private) static long t
          Time in ms.
(package private) static int targetFPS
          The FPS that needs to be throttled to.
(package private) static long tempT
          Time in ms.
(package private) static java.lang.Thread thread
          The thread to throttle.
(package private) static boolean throttle
          Throttle is enabled by default.
(package private) static int throttleStep
          How fast the throttle changes sleep time.
(package private) static boolean TRY_ALT_SKIP_CALC
          Throttle, frameskip constants.
 
Constructor Summary
private Throttle()
          This class only provides static methods and variables.
 
Method Summary
static void enable(boolean enable)
          Enable throttle.
static void enableAutoFrameSkip(boolean enable)
          Enable/disable automatic frame skip.
static float getAverageFPS()
          Get the average FPS.
static int getFPS()
          Get current FPS.
static int getFrameSkip()
          Get current amount of frames to be skipped.
static int getPercentage()
           
static long getSleep()
          Get current sleep time in ms.
static int getTargetFPS()
          Get the target FPS.
static void init(int fps, java.lang.Thread thread)
          Initialize the throttle.
static boolean isAutoFrameSkip()
          Returns true if automatic frame skip is enabled, false otherwise.
static boolean isEnabled()
          Returns true if throttling is enabled and false otherwise.
private static void recalcTiming()
          Called after FRAMES_UNTIL_THROTTLE_RECALC is reached.
static void setFrameSkip(int skip)
          Set the amount of frames to skip.
static boolean skipFrame()
          Returns true if a frame needs to be skipped, false otherwise.
static void throttle()
          Call this method each frame.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_THROTTLE_STEP

static final int MAX_THROTTLE_STEP
Throttle, frameskip constants.

See Also:
Constant Field Values

MIN_THROTTLE_STEP

static final int MIN_THROTTLE_STEP
Throttle, frameskip constants.

See Also:
Constant Field Values

DEFAULT_THROTTLE_STEP

static final int DEFAULT_THROTTLE_STEP
Throttle, frameskip constants.

See Also:
Constant Field Values

DEFAULT_TARGET_FPS

static final int DEFAULT_TARGET_FPS
Throttle, frameskip constants.

See Also:
Constant Field Values

FRAMES_UNTIL_THROTTLE_RECALC

static final int FRAMES_UNTIL_THROTTLE_RECALC
Throttle, frameskip constants.

See Also:
Constant Field Values

MAX_FRAME_SKIP

static final int MAX_FRAME_SKIP
Throttle, frameskip constants.

See Also:
Constant Field Values

MAX_FPS_DEVIATION

static final float MAX_FPS_DEVIATION
Throttle, frameskip constants.

See Also:
Constant Field Values

TRY_ALT_SKIP_CALC

static final boolean TRY_ALT_SKIP_CALC
Throttle, frameskip constants.

See Also:
Constant Field Values

throttle

static boolean throttle
Throttle is enabled by default.


autoFS

static boolean autoFS
Auto frame skip.


fps

static long fps
Frames per second.


sumFPS

static long sumFPS
Sum frames per second for measuring avg FPS.


avgFPS

static float avgFPS
Average FPS.


sleep

static long sleep
Amount of sleep time in ms.


targetFPS

static int targetFPS
The FPS that needs to be throttled to.


throttleStep

static int throttleStep
How fast the throttle changes sleep time.


minFPS

static int minFPS
Minimum FPS.


maxFPS

static int maxFPS
Maximum FPS.


minimumSleep

static long minimumSleep
Minimum sleeptime to have effect on the JVM.


fskip

static int fskip
Frame skip.


frameDuration

static int frameDuration
Amount of ms. of one frame.


t

static long t
Time in ms.


tempT

static long tempT
Time in ms.


frameNumber

static int frameNumber
Framenumber relative to last recalc.


recalcCount

static long recalcCount
Count recalc for measuring avg fps.


thread

static java.lang.Thread thread
The thread to throttle.

Constructor Detail

Throttle

private Throttle()
This class only provides static methods and variables.

Method Detail

init

public static void init(int fps,
                        java.lang.Thread thread)
Initialize the throttle.

Parameters:
fps - the target number of frames persecond.
thread - the thread to throttle.

throttle

public static void throttle()
Call this method each frame. Here the actual throttling takes place.


getSleep

public static long getSleep()
Get current sleep time in ms.

Returns:
current sleep time in ms.

skipFrame

public static boolean skipFrame()
Returns true if a frame needs to be skipped, false otherwise.

Returns:
true if a frame needs to be skipped, false otherwise.

enableAutoFrameSkip

public static void enableAutoFrameSkip(boolean enable)
Enable/disable automatic frame skip.

Parameters:
enable - true to enable automatic frame skip, false to disable.

isAutoFrameSkip

public static boolean isAutoFrameSkip()
Returns true if automatic frame skip is enabled, false otherwise.

Returns:
true if automatic frame skip is enabled, false otherwise.

enable

public static void enable(boolean enable)
Enable throttle.

Parameters:
enable - true to enable throttle, false to disable.

isEnabled

public static boolean isEnabled()
Returns true if throttling is enabled and false otherwise.

Returns:
true if throttling is enabled and false otherwise.

setFrameSkip

public static void setFrameSkip(int skip)
Set the amount of frames to skip.

Parameters:
skip - the number of frames to skip.

getFrameSkip

public static int getFrameSkip()
Get current amount of frames to be skipped.

Returns:
the current number of frames to skip.

getFPS

public static int getFPS()
Get current FPS.

Returns:
the current FPS.

getTargetFPS

public static int getTargetFPS()
Get the target FPS.

Returns:
the target FPS.

getAverageFPS

public static float getAverageFPS()
Get the average FPS.

Returns:
the average FPS.

getPercentage

public static int getPercentage()

recalcTiming

private static void recalcTiming()
Called after FRAMES_UNTIL_THROTTLE_RECALC is reached. Here the sleep time and auto frame skip is re-evaluated.



Copyright © 2007 JavaGear. All Rights Reserved.