Class SceneContext

java.lang.Object
nl.colorize.multimedialib.scene.SceneContext
All Implemented Interfaces:
Updatable

public final class SceneContext extends Object implements Updatable
Provides access to the contents of the currently active scene, including the stage, the scene's graphics, and the underlying renderer. The renderer passes the SceneContext to the currently active scene during each frame update, which allows the scene to perform frame update logic. The renderer then renders the frame based on the current contents of the stage.

The SceneContext also allows sub-scenes to be attached to the current scene. These sub-scenes can contain their own logic, but cannot outlive their parent scene. When the active scene is changed, both the scene itself and its sub-scenes will be terminated and the stage will be cleared in preparation for the next scene.

  • Constructor Details

    • SceneContext

      public SceneContext(Renderer renderer, MediaLoader media, InputDevice input, Network network)
      Creates a new SceneContext from the specified renderer. This constructor should not be called from application code. The renderer will create the SceneContext from the animation loop thread.
  • Method Details

    • replaceTimer

      protected void replaceTimer(nl.colorize.util.Stopwatch timer)
      Replaces the Stopwatch that is used as a timer for the animation loop and frame updates.
    • syncFrame

      public int syncFrame()
      Synchronizes between "native" frames and application frame updates. Should be called by the renderer during every "native" frame. Depending on the elapsed time and target framerate, this method will then manage application frame updates by calling update(float) accordingly.

      Although this class provides the renderer with the delta time since the last frame update, this may not always reflect the actual elapsed time. it is not realistic to expect applications to be able to function correctly for every possible deltaTime value, so this method will attempt to produce frame updates that try to find a balance between the targeted framerate and the actual elapsed time.

      Note that rendering the frame is not managed by this method. The renderer should make sure that every "native" frame is rendered, even if that frame did not lead to an application frame update.

      Calling this method will also register the corresponding performance statistics with the FrameStats instance provided in the constructor.

      Returns:
      The number of application frame updates that were performed during the frame synchronization process. A value of zero indicates no frame updates were performed, meaning that it is not necessary for the renderer to render the frame.
    • update

      public void update(float deltaTime)
      Performs an application frame update. The renderer should not call this method. Instead, it should call syncFrame(), which will decouple "native" frame updates in the renderer from application frame updates. Attempting to render the application at the renderer's native refresh rate can introduce slowdown if application and/or graphics complexity makes it impossible to actually achieve the native framerate.
      Specified by:
      update in interface Updatable
    • changeScene

      public void changeScene(Scene requestedScene)
      Requests to change the active scene after the current frame update has been completed. If another scene had already been requested, calling this method again will overrule that request.
    • attach

      public void attach(Scene subScene)
      Attaches a sub-scene to the currently active scene. The sub-scene will remain active until it is detached or the parent scene ends.
    • attach

      public void attach(Updatable subScene)
      Attaches an Updatable to act as a sub-scene for the currently active scene. The sub-scene will remain active until it is detached or the parent scene ends.
    • attach

      public void attach(Runnable subScene)
      Wraps a Runnable so that is acts as a sub-scene for the currently active scene. The sub-scene will remain active until it is detached or the parent scene ends.
    • attachGlobal

      public void attachGlobal(Scene globalSubScene)
      Attaches a scene that is *not* tied to the currently active scene, and will remain active for the rest of the application. Multiple global scenes can be attached.
    • getRendererName

      public String getRendererName()
      Returns the display name for the underlying renderer. The display name will not include the word "renderer".
    • getDebugInformation

      public List<String> getDebugInformation()
      Returns debug and support information that can be displayed when running a MultimediaLib application in debug mode. The returned list is intended to be displayed in a Text, which can be styled to match the application appearance.
    • terminate

      public void terminate()
      Terminates the application. This method will forward to the underlying Renderer.terminate(). It is exposed by this class to allow it to be used during frame updates.
    • getGraphicsMode

      public GraphicsMode getGraphicsMode()
    • getDisplayMode

      public DisplayMode getDisplayMode()
    • getCanvas

      public Canvas getCanvas()
    • getStage

      public Stage getStage()
    • getMediaLoader

      public MediaLoader getMediaLoader()
    • getInput

      public InputDevice getInput()
    • getNetwork

      public Network getNetwork()
    • getFrameStats

      public FrameStats getFrameStats()