Hi,
I'm trying to get AppStates working in JME3 and finally got to a point where it's kinda working.
I was hoping you could look over it and check if i use it like it's meant to be or if it's just total crap
Here is the Code:
AppTest.java
import com.jme3.app.Application;
import com.jme3.renderer.ViewPort;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.Timer;
public class AppTest extends Application {
private GameState te = null;
private MenuState ms = null;
public AppTest() {
}
@Override
public void start(){
// set some default settings in-case
// settings dialog is not shown
if (settings == null)
setSettings(new AppSettings(true));
// show settings dialog
if (!JmeSystem.showSettingsDialog(settings))
return;
super.start();
}
@Override
public void initialize() {
// initialize the standard environment first
super.initialize();
// Create the States
ms = new MenuState(this);
te = new GameState(this);
// Attach MenuState
getStateManager().attach(ms);
}
@Override
public void update() {
super.update();
float tpf = timer.getTimePerFrame() * speed;
// update states
stateManager.update(tpf);
// render states
stateManager.render(renderManager);
renderManager.render(tpf);
}
public void loadMenu() {
getStateManager().detach(te);
getStateManager().attach(ms);
}
public void loadGame() {
getStateManager().detach(ms);
getStateManager().attach(te);
}
public ViewPort getViewPort() {
return viewPort;
}
public ViewPort getGUIViewPort() {
return guiViewPort;
}
public Timer getTimer() {
return timer;
}
public static void main(String... args) {
new AppTest().start();
}
}
GameState.java
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.FlyByCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
public class GameState extends AbstractAppState {
protected Node rootNode = new Node("Root Node");
protected Node guiNode = new Node("Gui Node");
protected BitmapText fpsText;
protected BitmapText menuText;
protected BitmapFont guiFont;
protected FlyByCamera flyCam;
private AppTest game = null;
private AppActionListener actionListener = new AppActionListener();
public GameState(AppTest game) {
this.game = game;
}
private class AppActionListener implements ActionListener {
public void onAction(String name, boolean value, float tpf) {
if (!value)
return;
if (name.equals("SIMPLEAPP_Exit")){
game.stop();
}else if (name.equals("SIMPLEAPP_Memory")){
game.loadMenu();
}
}
}
public void loadText(){
guiFont = game.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
fpsText = new BitmapText(guiFont, false);
fpsText.setSize(guiFont.getCharSet().getRenderedSize());
fpsText.setLocalTranslation(0, fpsText.getLineHeight(), 0);
fpsText.setText("Frames per second");
guiNode.attachChild(fpsText);
menuText = new BitmapText(guiFont, false);
menuText.setSize(guiFont.getCharSet().getRenderedSize());
menuText.setLocalTranslation(0, (game.getContext().getSettings().getHeight()/2f)-(menuText.getLineHeight()/2f), 0);
menuText.setText("Press [M] to go back to the Menu");
guiNode.attachChild(menuText);
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// enable depth test and back-face culling for performance
app.getRenderer().applyRenderState(RenderState.DEFAULT);
guiNode.setQueueBucket(Bucket.Gui);
guiNode.setCullHint(CullHint.Never);
loadText();
if (game.getInputManager() != null){
flyCam = new FlyByCamera(game.getCamera());
flyCam.setMoveSpeed(1f);
flyCam.registerWithInput(game.getInputManager());
game.getInputManager().addMapping("SIMPLEAPP_Exit", new KeyTrigger(KeyInput.KEY_ESCAPE));
game.getInputManager().addMapping("SIMPLEAPP_Memory", new KeyTrigger(KeyInput.KEY_M));
}
// Add a simple Box
Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f);
Geometry cube = new Geometry("My Textured Box", boxshape1);
Material mat_stl = new Material(game.getAssetManager(), "Common/MatDefs/Misc/SimpleTextured.j3md");
Texture tex_ml = game.getAssetManager().loadTexture("Interface/Logo/Monkey.jpg");
mat_stl.setTexture("m_ColorMap", tex_ml);
cube.setMaterial(mat_stl);
rootNode.attachChild(cube);
}
@Override
public void update(float tpf) {
super.update(tpf);
int fps = (int) game.getTimer().getFrameRate();
fpsText.setText("Frames per second: "+fps);
// simple update and root node
rootNode.updateLogicalState(tpf);
guiNode.updateLogicalState(tpf);
rootNode.updateGeometricState();
guiNode.updateGeometricState();
}
public void stateAttached(AppStateManager stateManager) {
game.getInputManager().addListener(actionListener, "SIMPLEAPP_Exit",
"SIMPLEAPP_CameraPos", "SIMPLEAPP_Memory");
if(flyCam != null) flyCam.setEnabled(true);
game.getViewPort().attachScene(rootNode);
game.getGUIViewPort().attachScene(guiNode);
}
public void stateDetached(AppStateManager stateManager) {
game.getInputManager().removeListener(actionListener);
if(flyCam != null) flyCam.setEnabled(false);
game.getViewPort().detachScene(rootNode);
game.getGUIViewPort().detachScene(guiNode);
}
public void render(RenderManager rm) {
}
}
MenuState.java
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.RenderState;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
public class MenuState extends AbstractAppState {
protected Node rootNode = new Node("Root Node");
protected Node guiNode = new Node("Gui Node");
protected BitmapText menuText;
protected BitmapFont menuFont;
private AppTest game = null;
private AppActionListener actionListener = new AppActionListener();
public MenuState(AppTest game) {
this.game = game;
}
private class AppActionListener implements ActionListener {
public void onAction(String name, boolean value, float tpf) {
if (!value)
return;
// Load other state
game.loadGame();
}
}
public void loadFPSText(){
menuFont = game.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
menuText = new BitmapText(menuFont, false);
menuText.setSize(menuFont.getCharSet().getRenderedSize());
menuText.setLocalTranslation(0, (game.getContext().getSettings().getHeight()/2f)-(menuText.getLineHeight()/2f), 0);
menuText.setText("Frames per second");
guiNode.attachChild(menuText);
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// enable depth test and back-face culling for performance
app.getRenderer().applyRenderState(RenderState.DEFAULT);
guiNode.setQueueBucket(Bucket.Gui);
guiNode.setCullHint(CullHint.Never);
loadFPSText();
// Init input
if (game.getInputManager() != null){
game.getInputManager().addMapping("SIMPLEAPP_Exit1", new KeyTrigger(KeyInput.KEY_ESCAPE));
}
}
@Override
public void update(float tpf) {
super.update(tpf);
menuText.setText("Press [Escape] to go to the Game-State");
// simple update and root node
rootNode.updateLogicalState(tpf);
guiNode.updateLogicalState(tpf);
rootNode.updateGeometricState();
guiNode.updateGeometricState();
}
public void stateAttached(AppStateManager stateManager) {
game.getInputManager().addListener(new AppActionListener(), "SIMPLEAPP_Exit1");
game.getViewPort().attachScene(rootNode);
game.getGUIViewPort().attachScene(guiNode);
}
public void stateDetached(AppStateManager stateManager) {
game.getInputManager().removeListener(actionListener);
game.getViewPort().detachScene(rootNode);
game.getGUIViewPort().detachScene(guiNode);
}
public void render(RenderManager rm) {
}
}
Did i use it right or what did I do wrong? :)