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.
See Also:
process(), delay()
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.lang.Thread

    java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
  • Field Summary

    Fields inherited from class java.lang.Thread

    MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
  • Constructor Summary

    Constructors 
    ModifierConstructorDescription
    protected UpdateLoop​(java.lang.String name, int tickRate) 
  • Method Summary

    Modifier and TypeMethodDescription
    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 attached IUpdatable instances of this loop.
    protected java.util.Set<IUpdateable>getUpdatables() 
    protected voidprocess()
    Performs the actual workload of a tick.
    voidrun()
    The loop implementation, executing the process() 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 the update() 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

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface de.gurkenlabs.litiengine.ILaunchable

    start
  • Constructor Details

    • UpdateLoop

      protected UpdateLoop​(java.lang.String name, int tickRate)
  • Method Details

    • run

      public void run()
      The loop implementation, executing the process() 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 interface java.lang.Runnable
      Overrides:
      run in class java.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 interface ILaunchable
    • close

      public void close()
      Specified by:
      close in interface java.lang.AutoCloseable
    • attach

      public void attach​(IUpdateable updatable)
      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.
      Specified by:
      attach in interface ILoop
      Parameters:
      updatable - The instance that will be registered for the update event.
    • detach

      public void detach​(IUpdateable updatable)
      Description copied from interface: ILoop
      Detaches the specified instance from the game loop.
      Specified by:
      detach in interface ILoop
      Parameters:
      updatable - The instance that will be unregistered for the update event.
    • getUpdatableCount

      public int getUpdatableCount()
      Description copied from interface: ILoop
      Gets the amount of attached IUpdatable instances of this loop.
      Specified by:
      getUpdatableCount in interface ILoop
      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 interface ILoop
      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 interface ILoop
      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 interface ILoop
      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 interface ILoop
      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 interface ILoop
      Parameters:
      tickRate - The tickrate of the loop.
    • getUpdatables

      protected java.util.Set<IUpdateable> 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 the update() procedure on all registered instances.
      See Also:
      IUpdateable.update()
    • delay

      protected double delay() throws java.lang.InterruptedException
      This 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.
      Specified by:
      getLock in interface ILoop
      Returns:
      A Lock for this loop.