public class TestLoadingGameState {
public static void main(String[] args) throws Exception {
StandardGame game = new StandardGame("Test LoadingGameState");
//game.getSettings().clear();
game.start();
// Create LoadingGameState and enable
LoadingGameState loading = new LoadingGameState();
GameStateManager.getInstance().attachChild(loading);
loading.setActive(true);
// Enable DebugGameState
DebugGameState debug = new DebugGameState();
GameStateManager.getInstance().attachChild(debug);
debug.setActive(true);
// Start our slow loading test
String status = "Started Loading";
for (int i = 0; i <= 100; i++) {
if (i == 100) {
status = "I'm Finished!";
} else if (i > 80) {
status = "Almost There!";
} else if (i > 70) {
status = "Loading Something Extremely Useful";
} else if (i > 50) {
status = "More Than Half-Way There!";
} else if (i > 20) {
status = "Loading Something That You Probably Won't Care About";
}
Thread.sleep(100);
loading.setProgress(i / 100.0f, status);
}
}
}
In the TestLoadingGameState ,when it set up,it will display a loading state by percents.But when I use the
LoadingGameState class in the SimpleGame,it doesn't work.Can anyone tell me how can I display a loading state when my SimpleGame loading?
Thanks!
Well if you want to load something in the background and at the same time render a progress bar, you will probably need a separate thread.
And thats what StandardGame is for
:lol:
what you have to do to make it work in simple game is:
- create the GameStateManager
- let the GamestateManager render and update the GameStates
- and finally create a new thread for example in simpleInitGame and load your stuff and display a LoadingGamestate.
Constructing the LoadingGamestate maybe should happen in the main thread, because its accessing the renderer, but it seem to work that way too.
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jmex.game.state.GameStateManager;
import com.jmex.game.state.load.LoadingGameState;
public class SimpleLoadingGamestate extends SimpleGame {
@Override
protected void simpleInitGame() {
// create a new Thread which displays a LoadingGamestate while something is being computed
Thread t = new Thread(new Runnable() {
public void run() {
// create a LoadingGamestate and attach it
LoadingGameState loading = new LoadingGameState(10);
GameStateManager.getInstance().attachChild(loading);
loading.setActive(true);
try {
for (int i = 0; i <= 100; i++) {
loading.setProgress(i / 100f, "Progress:");
// TODO load resources or compute something useful here instead of sleeping
Thread.sleep(100);
}
// all work is done, sleep a bit to let the LoadingGamestate fade out, then detach
Thread.sleep(3000);
GameStateManager.getInstance().detachChild(loading);
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
// display a Box to see that the Game is already running
final Box b = new Box("spinning Box", new Vector3f(), 1, 1, 1);
b.setModelBound(new BoundingBox());
rootNode.attachChild(b);
}
@Override
protected void simpleRender() {
GameStateManager.getInstance().render(timer.getTimePerFrame());
}
@Override
protected void simpleUpdate() {
GameStateManager.getInstance().update(timer.getTimePerFrame());
}
public static void main(String[] args) {
GameStateManager.create();
new SimpleLoadingGamestate().start();
}
}
Core-Dump said:
:lol:
what you have to do to make it work in simple game is:
- create the GameStateManager
- let the GamestateManager render and update the GameStates
- and finally create a new thread for example in simpleInitGame and load your stuff and display a LoadingGamestate.
Constructing the LoadingGamestate maybe should happen in the main thread, because its accessing the renderer, but it seem to work that way too.
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jmex.game.state.GameStateManager;
import com.jmex.game.state.load.LoadingGameState;
public class SimpleLoadingGamestate extends SimpleGame {
@Override
protected void simpleInitGame() {
// create a new Thread which displays a LoadingGamestate while something is being computed
Thread t = new Thread(new Runnable() {
public void run() {
// create a LoadingGamestate and attach it
LoadingGameState loading = new LoadingGameState(10);
GameStateManager.getInstance().attachChild(loading);
loading.setActive(true);
try {
for (int i = 0; i <= 100; i++) {
loading.setProgress(i / 100f, "Progress:");
// TODO load resources or compute something useful here instead of sleeping
Thread.sleep(100);
}
// all work is done, sleep a bit to let the LoadingGamestate fade out, then detach
Thread.sleep(3000);
GameStateManager.getInstance().detachChild(loading);
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
// display a Box to see that the Game is already running
final Box b = new Box("spinning Box", new Vector3f(), 1, 1, 1);
b.setModelBound(new BoundingBox());
rootNode.attachChild(b);
}
@Override
protected void simpleRender() {
GameStateManager.getInstance().render(timer.getTimePerFrame());
}
@Override
protected void simpleUpdate() {
GameStateManager.getInstance().update(timer.getTimePerFrame());
}
public static void main(String[] args) {
GameStateManager.create();
new SimpleLoadingGamestate().start();
}
}
oh,I see.Thank you