Class SparkleApp
java.lang.Object
net.fokinatorr.sparkle.SparkleApp
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 returnnull. - 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();
}
}
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidinit()Called once during startup on the same thread that called one of thelaunchmethods, before creating audio/graphics loops.static <A extends SparkleApp>
Alaunch(Class<A> appClass, SparkleInit init, String... args) Launches a Sparkle app.static SparkleApplaunch(SparkleInit init, String... args) Launches a Sparkle app, using the directly calling class as the app class.static <A extends SparkleApp>
AlaunchHeadless(Class<A> appClass, SparkleInit init, String... args) Launches a headless Sparkle app (no audio and graphics).static SparkleApplaunchHeadless(SparkleInit init, String... args) Launches a headless Sparkle app, using the directly calling class as the app class.protected abstract voidstart(GraphicsManager graphics) Called afterinit()on the graphics thread.protected voidstop()Called during shutdown on the terminator thread, when all rendering contexts close or on explicit shutdown request.final voidAttempts to gracefully terminate execution of this app.
-
Constructor Details
-
SparkleApp
public SparkleApp()
-
-
Method Details
-
init
Called once during startup on the same thread that called one of thelaunchmethods, before creating audio/graphics loops. Override to configure beans, load assets, register event listeners, etc. (UsegetContext()to access the app context.)- Throws:
Exception- on error
-
start
Called afterinit()on the graphics thread. Use to create and initialize rendering contexts (e.g. registeringSprites or other renderable objects) and registerStage-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
-
launch
Launches a Sparkle app.- Parameters:
appClass- the app class to launchinit- the init configurationargs- 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 launchinit- the init configurationargs- optional command-line arguments- Returns:
- the launched app instance
-
launch
Launches a Sparkle app, using the directly calling class as the app class.- Parameters:
init- the init configurationargs- optional command-line arguments- Returns:
- the launched app instance
- Throws:
ClassCastException- if caller class is not aSparkleApp
-
launchHeadless
Launches a headless Sparkle app, using the directly calling class as the app class.- Parameters:
init- the init configurationargs- optional command-line arguments- Returns:
- the launched app instance
- Throws:
ClassCastException- if caller class is not aSparkleApp
-
terminate
Attempts to gracefully terminate execution of this app.- Throws:
InterruptedException- if interrupted while waitingIllegalStateException- if this app hasn't been started yet- See Also:
-