[committed] Issue #65 pass game instance to DebugGamestate

http://code.google.com/p/jmonkeyengine/issues/detail?id=65



DebugGameState used System.exit() when pressing ESC which results in a EXCEPTION_ACCESS_VIOLATION.



Added constructors to DebugGameState to pass a AbstaractGame instance for a clean shutdown.

Also modified the Tests using DebugGamestate.



Index: src/com/jmex/model/util/ModelLoader.java
===================================================================
--- src/com/jmex/model/util/ModelLoader.java   (revision 4483)
+++ src/com/jmex/model/util/ModelLoader.java   (working copy)
@@ -107,7 +107,7 @@
                     // Set it in preferences so we remember next time
                     preferences.put("StartDirectory", file.getAbsolutePath());
                    
-                    StandardGame game = new StandardGame("Model Loader");
+                    final StandardGame game = new StandardGame("Model Loader");
                     try {
                         game.getSettings().clear();
                     } catch(Exception exc) {
@@ -123,7 +123,7 @@
                         }
                     });
                    
-                    DebugGameState debug = new DebugGameState();
+                    DebugGameState debug = new DebugGameState(game);
                     GameStateManager.getInstance().attachChild(debug);
                     debug.setActive(true);
                    
Index: src/com/jmex/game/state/DebugGameState.java
===================================================================
--- src/com/jmex/game/state/DebugGameState.java   (revision 4483)
+++ src/com/jmex/game/state/DebugGameState.java   (working copy)
@@ -33,6 +33,7 @@
 
 import java.util.logging.Logger;
 
+import com.jme.app.AbstractGame;
 import com.jme.image.Texture;
 import com.jme.input.FirstPersonHandler;
 import com.jme.input.InputHandler;
@@ -72,7 +73,8 @@
     protected boolean showDepth = false;
     protected boolean showNormals = false;
     protected boolean statisticsCreated = false;
-
+    protected AbstractGame game = null;
+    
     public DebugGameState() {
        this(true);
     }
@@ -81,7 +83,19 @@
         super("F4 - toggle stats");
         init(handleInput);
     }
+    
+    public DebugGameState(AbstractGame game, boolean handleInput) {
+        super("F4 - toggle stats");
+        init(handleInput);
+        this.game = game;
+    }    
 
+    public DebugGameState(AbstractGame game) {
+       super("F4 - toggle stats");
+       init(true);
+        this.game = game;
+    }
+    
     private void init(boolean handleInput) {
         rootNode = new Node("RootNode");
 
@@ -263,7 +277,11 @@
    
            if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit",
                    false)) {
-               System.exit(0);
+              if (game != null) {
+                 game.finish();
+              } else {
+                 System.exit(0);
+              }
            }
         }
     }
Index: src/jmetest/awt/swingui/dnd/TestDnd.java
===================================================================
--- src/jmetest/awt/swingui/dnd/TestDnd.java   (revision 4483)
+++ src/jmetest/awt/swingui/dnd/TestDnd.java   (working copy)
@@ -57,7 +57,7 @@
         }
       game.start();
 
-      TestDnd.MyGameState ingameState = test.new MyGameState();
+      TestDnd.MyGameState ingameState = test.new MyGameState(game);
       GameStateManager.getInstance().attachChild(ingameState);
 
       // always updateRenderState or get funky effects
@@ -82,8 +82,8 @@
       private float angle = 0;
       private Vector3f axis = new Vector3f(1, 1, 0.5f);
 
-      public MyGameState() {
-         super();
+      public MyGameState(StandardGame game) {
+         super(game);
          // remove the MouseLookHnadler because the mouse is used for the hud
          ((FirstPersonHandler)input).getMouseLookHandler().setEnabled(false);
          this.box = new Box("my box", new Vector3f(0, 0, 0), 2, 2, 2);
Index: src/jmetest/game/state/TestJMEDesktopState.java
===================================================================
--- src/jmetest/game/state/TestJMEDesktopState.java   (revision 4483)
+++ src/jmetest/game/state/TestJMEDesktopState.java   (working copy)
@@ -50,7 +50,7 @@
  */
 public class TestJMEDesktopState extends JMEDesktopState {
    public static void main(String[] args) throws Exception {
-      StandardGame game = new StandardGame("Testing JMEDesktopState");
+      final StandardGame game = new StandardGame("Testing JMEDesktopState");
       game.start();
       
       GameTaskQueueManager.getManager().update(new Callable<Void>() {
@@ -59,7 +59,7 @@
             // Create a DebugGameState - has all the built-in features that SimpleGame provides
             // NOTE: for a distributable game implementation you'll want to use something like
             // BasicGameState instead and provide control features yourself.
-            DebugGameState state = new DebugGameState();
+            DebugGameState state = new DebugGameState(game);
             Box box = new Box("my box", new Vector3f(0, 0, 0), 2, 2, 2);
             box.setModelBound(new BoundingSphere());
             box.updateModelBound();
Index: src/jmetest/game/state/TestDebugGameState.java
===================================================================
--- src/jmetest/game/state/TestDebugGameState.java   (revision 4483)
+++ src/jmetest/game/state/TestDebugGameState.java   (working copy)
@@ -47,13 +47,13 @@
  */
 public class TestDebugGameState {
     public static void main(String[] args) throws Exception {
-        StandardGame game = new StandardGame("TestGame");   // Create our game
+        final StandardGame game = new StandardGame("TestGame");   // Create our game
         game.start();   // Start the game thread
        
         GameTaskQueueManager.getManager().update(new Callable<Void>(){
 
          public Void call() throws Exception {
-            DebugGameState gameState = new DebugGameState();   // Create our game state
+            DebugGameState gameState = new DebugGameState(game);   // Create our game state
             GameStateManager.getInstance().attachChild(gameState);   // Attach it to the GameStateManager
             gameState.setActive(true);   // Activate it
 
Index: src/jmetest/game/state/TestLoadingGameState.java
===================================================================
--- src/jmetest/game/state/TestLoadingGameState.java   (revision 4483)
+++ src/jmetest/game/state/TestLoadingGameState.java   (working copy)
@@ -44,7 +44,7 @@
  */
 public class TestLoadingGameState {
    public static void main(String[] args) throws Exception {
-      StandardGame game = new StandardGame("Test LoadingGameState");
+      final StandardGame game = new StandardGame("Test LoadingGameState");
       game.getSettings().clear();
       game.start();
 
@@ -57,7 +57,7 @@
             loading.setActive(true);
 
             // Enable DebugGameState
-            DebugGameState debug = new DebugGameState();
+            DebugGameState debug = new DebugGameState(game);
             GameStateManager.getInstance().attachChild(debug);
             debug.setActive(true);
 
Index: src/jmetest/game/TestStandardGame.java
===================================================================
--- src/jmetest/game/TestStandardGame.java   (revision 4483)
+++ src/jmetest/game/TestStandardGame.java   (working copy)
@@ -56,7 +56,7 @@
        System.setProperty("jme.stats", "set");
        
       // Instantiate StandardGame
-      StandardGame game = new StandardGame("A Simple Test");
+      final StandardGame game = new StandardGame("A Simple Test");
       // Show settings screen
       if (GameSettingsPanel.prompt(game.getSettings())) {
          // Start StandardGame, it will block until it has initialized successfully, then return
@@ -68,7 +68,7 @@
                // Create a DebugGameState - has all the built-in features that SimpleGame provides
                // NOTE: for a distributable game implementation you'll want to use something like
                // BasicGameState instead and provide control features yourself.
-               DebugGameState state = new DebugGameState();
+               DebugGameState state = new DebugGameState(game);
                // Put our box in it
                Box box = new Box("my box", new Vector3f(0, 0, 0), 2, 2, 2);
                 box.setModelBound(new BoundingSphere());
Index: src/jmetest/effects/TestParticleSystem.java
===================================================================
--- src/jmetest/effects/TestParticleSystem.java   (revision 4483)
+++ src/jmetest/effects/TestParticleSystem.java   (working copy)
@@ -108,7 +108,7 @@
 
     pMesh = ParticleFactory.buildParticles("particles", 300);
     pMesh.setEmissionDirection(new Vector3f(0,1,0));
-    pMesh.setInitialVelocity(.006f);
+    pMesh.setInitialVelocity(.06f);
     pMesh.setStartSize(2.5f);
     pMesh.setEndSize(.5f);
     pMesh.setMinimumLifeTime(1200f);
Index: src/jmetest/text/Test3DFlatText.java
===================================================================
--- src/jmetest/text/Test3DFlatText.java   (revision 4483)
+++ src/jmetest/text/Test3DFlatText.java   (working copy)
@@ -49,13 +49,13 @@
  */
 public class Test3DFlatText {
    public static void main(String[] args) throws Exception {
-      StandardGame game = new StandardGame("Test 3D Flat Text");
+      final StandardGame game = new StandardGame("Test 3D Flat Text");
       game.start();
       
       GameTaskQueueManager.getManager().update(new Callable<Void>() {
 
          public Void call() throws Exception {            
-            final DebugGameState debug = new DebugGameState();
+            final DebugGameState debug = new DebugGameState(game);
             GameStateManager.getInstance().attachChild(debug);
             debug.setActive(true);
 
@@ -65,6 +65,6 @@
             debug.getRootNode().attachChild(text);
             return null;
          }
-      });
+      }).get();
    }
 }
Index: src/jmetest/input/controls/TestControls.java
===================================================================
--- src/jmetest/input/controls/TestControls.java   (revision 4483)
+++ src/jmetest/input/controls/TestControls.java   (working copy)
@@ -59,7 +59,7 @@
       game.start();
       
       // Create our GameState
-      DebugGameState state = new DebugGameState(false);
+      DebugGameState state = new DebugGameState(game, false);
       GameStateManager.getInstance().attachChild(state);
       state.setActive(true);
       

I agree with you, your fix seems reliable.

I need another opinion, should i commit this?



Basically when pressing escape in DebugGameState it calls StandardGame.quit() instead of System.exit().

Simply System.exiit()'ing can cause problems: http://www.jmonkeyengine.com/forum/index.php?topic=13638.0

Yes do it. 



…and have a nice weekend

committed … after almost a year :wink: