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, ILoop
The
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 protected
UpdateLoop(java.lang.String name, int tickRate)
Method Summary
Modifier and Type Method Description void
attach(IUpdateable updatable)
Attaches the update method of the specified IUpdatable instance to be called every tick.void
close()
protected double
delay()
This method determines how long the current tick should be delayed to match the expected delta time for the specified tick rate.void
detach(IUpdateable updatable)
Detaches the specified instance from the game loop.long
getDeltaTime()
Gets the total time in milliseconds that passed since the last tick.protected long
getExpectedDelta()
java.util.concurrent.locks.Lock
getLock()
Returns a lock that can be used for actions that must be performed either within or independently of the loop.double
getProcessTime()
Gets the actual process time in milliseconds that was required during the last tick.int
getTickRate()
Gets the rate at which this loop performs its updates.long
getTicks()
Gets the total amount of ticks performed by this loop since it was started.int
getUpdatableCount()
Gets the amount of attachedIUpdatable
instances of this loop.protected java.util.Set<IUpdateable>
getUpdatables()
protected void
process()
Performs the actual workload of a tick.void
run()
The loop implementation, executing theprocess()
method which does the actual work.void
setTickRate(int tickRate)
Sets the tickrate at which the loop performs its updates.void
terminate()
Terminates the operation of this instance.protected void
update()
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:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
- See Also:
process()
,delay()
,getDeltaTime()
,getProcessTime()
terminate
public void terminate()Description copied from interface:ILaunchable
Terminates the operation of this instance.- Specified by:
terminate
in interfaceILaunchable
close
public void close()- Specified by:
close
in interfacejava.lang.AutoCloseable
attach
Description copied from interface:ILoop
Attaches 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:ILoop
Detaches the specified instance from the game loop.getUpdatableCount
public int getUpdatableCount()Description copied from interface:ILoop
Gets the amount of attachedIUpdatable
instances of this loop.- Specified by:
getUpdatableCount
in interfaceILoop
- Returns:
- The amount instances attached to this loop.
getTicks
public long getTicks()Description copied from interface:ILoop
Gets the total amount of ticks performed by this loop since it was started.- Specified by:
getTicks
in interfaceILoop
- Returns:
- The total amount of elapsed ticks.
- See Also:
ILaunchable.start()
getTickRate
public int getTickRate()Description copied from interface:ILoop
Gets the rate at which this loop performs its updates.- Specified by:
getTickRate
in interfaceILoop
- Returns:
- The update rate in ticks per second.
getDeltaTime
public long getDeltaTime()Description copied from interface:ILoop
Gets 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:
getDeltaTime
in interfaceILoop
- Returns:
- The delta time in ms.
- See Also:
ILoop.getProcessTime()
getProcessTime
public double getProcessTime()Description copied from interface:ILoop
Gets the actual process time in milliseconds that was required during the last tick.
i.e. delta time - delay- Specified by:
getProcessTime
in 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:ILoop
Sets the tickrate at which the loop performs its updates.- Specified by:
setTickRate
in 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:ILoop
Returns a lock that can be used for actions that must be performed either within or independently of the loop.