Package de.gurkenlabs.litiengine
Class UpdateLoop
java.lang.Object
java.lang.Thread
de.gurkenlabs.litiengine.UpdateLoop
- All Implemented Interfaces:
ILaunchable,ILoop,java.lang.AutoCloseable,java.lang.Runnable
- Direct Known Subclasses:
GameLoop
public class UpdateLoop
extends java.lang.Thread
implements java.lang.AutoCloseable, ILoopThe
UpdateLoop is a basic loop implementation that performs operations at the specified tickRate by continuously processing the registered logic and delaying the loop until the requested rate is met.Nested Class Summary
Field Summary
Constructor Summary
Constructors Modifier Constructor Description protectedUpdateLoop(java.lang.String name, int tickRate)Method Summary
Modifier and Type Method Description voidattach(IUpdateable updatable)Attaches the update method of the specified IUpdatable instance to be called every tick.voidclose()protected doubledelay()This method determines how long the current tick should be delayed to match the expected delta time for the specified tick rate.voiddetach(IUpdateable updatable)Detaches the specified instance from the game loop.longgetDeltaTime()Gets the total time in milliseconds that passed since the last tick.protected longgetExpectedDelta()java.util.concurrent.locks.LockgetLock()Returns a lock that can be used for actions that must be performed either within or independently of the loop.doublegetProcessTime()Gets the actual process time in milliseconds that was required during the last tick.intgetTickRate()Gets the rate at which this loop performs its updates.longgetTicks()Gets the total amount of ticks performed by this loop since it was started.intgetUpdatableCount()Gets the amount of attachedIUpdatableinstances of this loop.protected java.util.Set<IUpdateable>getUpdatables()protected voidprocess()Performs the actual workload of a tick.voidrun()The loop implementation, executing theprocess()method which does the actual work.voidsetTickRate(int tickRate)Sets the tickrate at which the loop performs its updates.voidterminate()Terminates the operation of this instance.protected voidupdate()Calls theupdate()procedure on all registered instances.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
Constructor Details
UpdateLoop
protected UpdateLoop(java.lang.String name, int tickRate)
Method Details
run
public void run()The loop implementation, executing theprocess()method which does the actual work. It also tracks the processing time and the total number of performed ticks while making sure that the expected tick rate is met by delaying the loop accordingly.- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread- See Also:
process(),delay(),getDeltaTime(),getProcessTime()
terminate
public void terminate()Description copied from interface:ILaunchableTerminates the operation of this instance.- Specified by:
terminatein interfaceILaunchable
close
public void close()- Specified by:
closein interfacejava.lang.AutoCloseable
attach
Description copied from interface:ILoopAttaches the update method of the specified IUpdatable instance to be called every tick. The tick rate can be configured in the client configuration and is independent from rendering.detach
Description copied from interface:ILoopDetaches the specified instance from the game loop.getUpdatableCount
public int getUpdatableCount()Description copied from interface:ILoopGets the amount of attachedIUpdatableinstances of this loop.- Specified by:
getUpdatableCountin interfaceILoop- Returns:
- The amount instances attached to this loop.
getTicks
public long getTicks()Description copied from interface:ILoopGets the total amount of ticks performed by this loop since it was started.- Specified by:
getTicksin interfaceILoop- Returns:
- The total amount of elapsed ticks.
- See Also:
ILaunchable.start()
getTickRate
public int getTickRate()Description copied from interface:ILoopGets the rate at which this loop performs its updates.- Specified by:
getTickRatein interfaceILoop- Returns:
- The update rate in ticks per second.
getDeltaTime
public long getDeltaTime()Description copied from interface:ILoopGets the total time in milliseconds that passed since the last tick.
i.e. process time + delay (to enforce the tick rate of this loop)- Specified by:
getDeltaTimein interfaceILoop- Returns:
- The delta time in ms.
- See Also:
ILoop.getProcessTime()
getProcessTime
public double getProcessTime()Description copied from interface:ILoopGets the actual process time in milliseconds that was required during the last tick.
i.e. delta time - delay- Specified by:
getProcessTimein interfaceILoop- Returns:
- The actual process time of the last tick in ms.
- See Also:
ILoop.getDeltaTime()
setTickRate
public void setTickRate(int tickRate)Description copied from interface:ILoopSets the tickrate at which the loop performs its updates.- Specified by:
setTickRatein interfaceILoop- Parameters:
tickRate- The tickrate of the loop.
getUpdatables
process
protected void process()Performs the actual workload of a tick. This base implementation just calls the update method on all registered instances. For derived loop implementations this is more sophisticated.getExpectedDelta
protected long getExpectedDelta()update
protected void update()Calls theupdate()procedure on all registered instances.- See Also:
IUpdateable.update()
delay
protected double delay() throws java.lang.InterruptedExceptionThis method determines how long the current tick should be delayed to match the expected delta time for the specified tick rate. It then delays the execution of this loop by pausing the thread for the necessary delay.- Returns:
- The delay for which this tick was paused after the actual processing.
- Throws:
java.lang.InterruptedException- If the thread was interrupted while sleeping
getLock
public java.util.concurrent.locks.Lock getLock()Description copied from interface:ILoopReturns a lock that can be used for actions that must be performed either within or independently of the loop.
