Class SparkleApp

java.lang.Object
net.fokinatorr.sparkle.SparkleApp

public abstract class SparkleApp extends Object
Base class for all Sparkle apps.

Subclasses must:

  • Have a public no-argument constructor. Note: it is recommended to define no constructor at all (letting the compiler generate a nullary one) and move initialization to init(), since at the moment of object construction, all getters return null.
  • Override start(GraphicsManager)

Example:

public class MyGame extends SparkleApp {
    private static final Assets STAGE = ...;
    private static final Assets PLAYER = ...;

    public static void main(String[] args) {
        SparkleApp.launch(SparkleInit.DEFAULT, args);
    }

    private static Stage primaryStage;

    protected void start(GraphicsManager gm) {
        RenderingContext renderCtx = gm.createWindowContext("My Game", new StageCreator() {
            @Override
            public Assets getStageAssets() {
                return STAGE;
            }

            @Override
            public void customize(Stage stage) {
                stage.setSize(480, 360); // Scratch's stage size

                Sprite player = new Sprite(PLAYER);
                player.setX(-100);
                player.setRotationMode(RotationMode.LEFT_RIGHT);
                player.setCostume("blue");
                stage.registerSprite("player", player);

                // ...
                Global.registerEventListener(new MyGameLogic());
            }
        });
        primaryStage = renderCtx.getStage();
        renderCtx.getSurface().show();
    }
}
  • Constructor Details

    • SparkleApp

      public SparkleApp()
  • Method Details

    • init

      protected void init() throws Exception
      Called once during startup on the same thread that called one of the launch methods, before creating audio/graphics loops. Override to configure beans, load assets, register event listeners, etc. (Use getContext() to access the app context.)
      Throws:
      Exception - on error
    • start

      protected abstract void start(GraphicsManager graphics) throws Exception
      Called after init() on the graphics thread. Use to create and initialize rendering contexts (e.g. registering Sprites or other renderable objects) and register Stage-dependent event listeners.

      Note: if no rendering contexts are created in this method, the app will immediately terminate.

      This method is never called for headless apps!

      Parameters:
      graphics - the graphics manager
      Throws:
      Exception - on error
    • stop

      protected void stop() throws Exception
      Called during shutdown on the terminator thread, when all rendering contexts close or on explicit shutdown request. Override to clean up resources.
      Throws:
      Exception - on error
    • launch

      public static <A extends SparkleApp> A launch(Class<A> appClass, SparkleInit init, String... args)
      Launches a Sparkle app.
      Parameters:
      appClass - the app class to launch
      init - the init configuration
      args - optional command-line arguments
      Returns:
      the launched app instance
    • launchHeadless

      public static <A extends SparkleApp> A launchHeadless(Class<A> appClass, SparkleInit init, String... args)
      Launches a headless Sparkle app (no audio and graphics).
      Parameters:
      appClass - the app class to launch
      init - the init configuration
      args - optional command-line arguments
      Returns:
      the launched app instance
    • launch

      public static SparkleApp launch(SparkleInit init, String... args)
      Launches a Sparkle app, using the directly calling class as the app class.
      Parameters:
      init - the init configuration
      args - optional command-line arguments
      Returns:
      the launched app instance
      Throws:
      ClassCastException - if caller class is not a SparkleApp
    • launchHeadless

      public static SparkleApp launchHeadless(SparkleInit init, String... args)
      Launches a headless Sparkle app, using the directly calling class as the app class.
      Parameters:
      init - the init configuration
      args - optional command-line arguments
      Returns:
      the launched app instance
      Throws:
      ClassCastException - if caller class is not a SparkleApp
    • terminate

      public final void terminate() throws InterruptedException
      Attempts to gracefully terminate execution of this app.
      Throws:
      InterruptedException - if interrupted while waiting
      IllegalStateException - if this app hasn't been started yet
      See Also: