Skip to content

Commit 904def3

Browse files
committed
added embeddedShutdown() that allows restarting an FXGL instance without the need to exit JavaFX, added sample, #1075
1 parent 40e2afd commit 904def3

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package sandbox;
8+
9+
import com.almasb.fxgl.app.GameApplication;
10+
import com.almasb.fxgl.dsl.FXGL;
11+
import javafx.application.Application;
12+
import javafx.scene.Parent;
13+
import javafx.scene.Scene;
14+
import javafx.scene.control.Button;
15+
import javafx.scene.layout.HBox;
16+
import javafx.scene.layout.Pane;
17+
import javafx.scene.layout.VBox;
18+
import javafx.stage.Stage;
19+
import sandbox.test3d.Model3DSample;
20+
21+
import java.util.List;
22+
import java.util.function.Supplier;
23+
24+
/**
25+
* @author Almas Baimagambetov (almaslvl@gmail.com)
26+
*/
27+
public class RestartableSample extends Application {
28+
@Override
29+
public void start(Stage stage) throws Exception {
30+
stage.setScene(new Scene(createContent()));
31+
stage.show();
32+
}
33+
34+
private Parent createContent() {
35+
List<Supplier<GameApplication>> games = List.of(
36+
SpriteSheetAnimationApp::new,
37+
ScrollingBackgroundSample::new,
38+
PlatformerSample::new,
39+
TiledMapSample::new,
40+
Model3DSample::new
41+
);
42+
43+
var root = new VBox();
44+
root.setPrefSize(1280, 720);
45+
46+
var hbox = new HBox(5);
47+
48+
for (int i = 0; i < games.size(); i++) {
49+
var btn = new Button("game " + i);
50+
var gameConstructor = games.get(i);
51+
52+
btn.setOnAction(e -> {
53+
GameApplication.embeddedShutdown();
54+
55+
var pane = GameApplication.embeddedLaunch(gameConstructor.get());
56+
57+
root.getChildren().set(1, pane);
58+
});
59+
60+
hbox.getChildren().add(btn);
61+
}
62+
63+
root.getChildren().addAll(hbox, new Pane());
64+
return root;
65+
}
66+
67+
@Override
68+
public void stop() throws Exception {
69+
FXGL.getGameController().exit();
70+
}
71+
72+
public static void main(String[] args) {
73+
launch(args);
74+
}
75+
}

fxgl/src/main/java/com/almasb/fxgl/app/GameApplication.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.almasb.fxgl.core.reflect.ReflectionUtils;
99
import com.almasb.fxgl.core.util.Platform;
1010
import com.almasb.fxgl.dev.profiling.ProfilerService;
11+
import com.almasb.fxgl.dsl.FXGL;
1112
import com.almasb.fxgl.generated.BuildProperties;
1213
import com.almasb.fxgl.logging.*;
1314

@@ -142,6 +143,19 @@ private void initLogger(ReadOnlyGameSettings settings) {
142143
log.debug("Logging settings\n" + settings);
143144
}
144145

146+
/**
147+
* Shuts down currently running embedded FXGL instance.
148+
* No-op if no FXGL instance is launched in embedded mode.
149+
* After this call, another {@link #embeddedLaunch(GameApplication)} can be started.
150+
* Note that after FXGL is no longer needed (no launch calls will be made),
151+
* FXGL.getGameController().exit() should be called.
152+
*/
153+
public static void embeddedShutdown() {
154+
FXGL.extract$fxgl();
155+
156+
Logger.removeAllOutputs();
157+
}
158+
145159
/**
146160
* Initialize app settings.
147161
*/

fxgl/src/main/kotlin/com/almasb/fxgl/dsl/FXGL.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class FXGL private constructor() { companion object {
9292
app = gameApp
9393
}
9494

95+
@JvmStatic
96+
internal fun extract() {
97+
if (this::engine.isInitialized)
98+
engine.stopLoopAndExitServices()
99+
}
100+
95101
private val controller = object : GameController {
96102
override fun gotoIntro() {
97103
getWindowService().gotoIntro()

0 commit comments

Comments
 (0)