[committed] lwjgl 2 applets using Display.setParent()

i would like to add Wizem/SomethingNew’s applets to jme.



see:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=10569.msg84803#msg84803



To test it easier i zipped up all the classes:



jmex/awt/applet/

 BaseApplet.java

 BaseSimpleApplet.java

 SimpleApplet.java

 SimplePassApplet.java

 StandardApplet.java



jmetest/awt/applet/

 TestStandardApplet.java

 TestSimpleApplet.java



–> lwjgl2.0_applets.zip <–




edit: removed link, since its committed now



The only additional change is the initForApplet() Method in LWJGLDisplaySystem.


Index: src/com/jme/system/lwjgl/LWJGLDisplaySystem.java
===================================================================
--- src/com/jme/system/lwjgl/LWJGLDisplaySystem.java   (revision 4337)
+++ src/com/jme/system/lwjgl/LWJGLDisplaySystem.java   (working copy)
@@ -670,5 +670,18 @@
             Display.setLocation(locX, locY);
         }
     }
-
+    
+    /**
+     * Initializes the displaysystem for use in Applets.
+     * @param w width of the applet
+     * @param h height of the applet
+     */
+    public void initForApplet(int w, int h) {
+        renderer = new LWJGLRenderer(w, h);
+       switchContext(this);
+        renderer.setHeadless(true);
+        currentContext.setupRecords(renderer);
+        DisplaySystem.updateStates(renderer);
+        created = true;
+    }
 }

Sounds good to me!

I fixed a few things in PassApplet, but i still can't get the ShadowedRenderPass working.

It seems that the stencilbit's are never set, i tried to set it in BaseSimpleApplet#initsystem() but it didn't seem to have any effect.

found the problem:

In BaseApplet and StandardApplet, the LWJGL Display is created with a default format (Display.create()) which has no stencil support.

Display.create(new PixelFormat(0, 16, 4)); works.



See http://lwjgl.org/forum/index.php/topic,1340.msg8821.html#msg8821



edit:

I moved the protected variables stencitbits depthbits etc from BaseSimpleApplet to BaseApplet.

To enable StencilBits on can set stencilBits = 4; in the derived applets constructor.

Did we ever get the mouse working in the simpleApplet?

SomethingNew said:

Did we ever get the mouse working in the simpleApplet?

Yes, I think it was working right away. During initialization with the applet viewer, the mouse disappears for a bit, but then reappears (I explicitly set the cursor to visible at some point in the SimpleApplet hierarchy) and works just fine for me.

yep, i think everything works now the same as in simplegame

Good job guys.

This is committed, there is still issues with StandardApplet tho (F4 crashes, DebugGamestate doesn't work).

i'll take a look at it sooner or later if no one else beats me to it.

fixed a few things:

  • created a new com.jmex.awt.applet.AppletResizeListener to eliminate the duplicate code in the 3 Applet implementations
  • BaseApplet and StandardApplet were calling display.update() instead display.getRenderer().displayBackBuffer();

      this resulted in the renderqueue not being rendered, which is why F4 was not working for example.
  • LWJGLDisplaySystem.initForApplet() was not updating DisplaySystems width and height, which resulted in a endlessloop when creating StatisticsGamestate. (because width and height were 0)
  • also initForApplet() was creating a headless display, but headless needs to be false or Display.update(); is not called in Renderer.displayBackBuffer()



    edit: those changes are commited


Index: src/com/jme/system/lwjgl/LWJGLDisplaySystem.java
===================================================================
--- src/com/jme/system/lwjgl/LWJGLDisplaySystem.java   (revision 4389)
+++ src/com/jme/system/lwjgl/LWJGLDisplaySystem.java   (working copy)
@@ -677,9 +677,11 @@
      * @param h height of the applet
      */
     public void initForApplet(int w, int h) {
+       width = w;
+       height = h;
         renderer = new LWJGLRenderer(w, h);
        switchContext(this);
-        renderer.setHeadless(true);
+        renderer.setHeadless(false);
         currentContext.setupRecords(renderer);
         DisplaySystem.updateStates(renderer);
         created = true;
Index: src/com/jmex/awt/applet/AppletResizeListener.java
===================================================================
--- src/com/jmex/awt/applet/AppletResizeListener.java   (revision 0)
+++ src/com/jmex/awt/applet/AppletResizeListener.java   (revision 0)
@@ -0,0 +1,58 @@
+package com.jmex.awt.applet;
+
+import java.applet.Applet;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
+import java.util.concurrent.Callable;
+
+import com.jme.system.DisplaySystem;
+import com.jme.util.GameTaskQueue;
+import com.jme.util.GameTaskQueueManager;
+
+/**
+ * ComponentListener for use with the newer lwjgl2 Applets.<br>
+ * Listens for resize events from an Applet and reinitializes the Renderer.
+ */
+public class AppletResizeListener implements ComponentListener {
+   private Applet applet;
+   
+   public AppletResizeListener(Applet applet) {
+      this.applet = applet;
+   }
+   
+   public void componentHidden(ComponentEvent ce) {
+
+   }
+
+   public void componentMoved(ComponentEvent ce) {
+
+   }
+
+   /**
+    * Reinitializes the renderer based on the applets new size.<br>
+    * Sets the new width and height in the displaysystem.
+    */
+   public void componentResized(final ComponentEvent ce) {
+      Callable<?> exe = new Callable<Object>() {
+         int w = applet.getWidth();
+         int h = applet.getHeight();
+         
+          public Object call() {
+             DisplaySystem display = DisplaySystem.getDisplaySystem();
+             display.getRenderer().reinit(w, h);
+             display.getRenderer().getCamera().setFrustumPerspective(45.0f,
+                   (float) applet.getWidth() / (float)applet.getHeight(), 1, 1000);
+             display.setWidth(w);
+             display.setHeight(h);
+             return null;
+          }
+      };
+      GameTaskQueueManager.getManager()
+         .getQueue(GameTaskQueue.RENDER).enqueue(exe);
+   }
+
+   public void componentShown(ComponentEvent ce) {
+
+   }
+
+}
Index: src/com/jmex/awt/applet/BaseApplet.java
===================================================================
--- src/com/jmex/awt/applet/BaseApplet.java   (revision 4389)
+++ src/com/jmex/awt/applet/BaseApplet.java   (working copy)
@@ -165,7 +165,7 @@
                // render, do not use interpolation parameter
                render(-1.0f);
                // Swap buffers, process messages, handle input
-               Display.update();
+               display.getRenderer().displayBackBuffer();
                Thread.yield();
             }
          }
Index: src/com/jmex/awt/applet/SimpleApplet.java
===================================================================
--- src/com/jmex/awt/applet/SimpleApplet.java   (revision 4389)
+++ src/com/jmex/awt/applet/SimpleApplet.java   (working copy)
@@ -1,17 +1,11 @@
 package com.jmex.awt.applet;
 
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
-import java.util.concurrent.Callable;
-
 import com.jme.app.AbstractGame;
 import com.jme.app.BaseSimpleGame;
 import com.jme.app.SimpleGame;
 import com.jme.input.FirstPersonHandler;
 import com.jme.input.MouseInput;
 import com.jme.renderer.Renderer;
-import com.jme.util.GameTaskQueue;
-import com.jme.util.GameTaskQueueManager;
 
 /**
  * LWJGL2 Applet imlpementation similar to {@link SimpleGame}
@@ -79,23 +73,7 @@
     */
    protected void initSystem() {
       super.initSystem();
-      this.addComponentListener(new ComponentAdapter() {
-         public void componentResized(ComponentEvent ce) {
-            Callable<?> exe = new Callable<Object>() {
-               public Object call() {
-                  display.getRenderer().reinit(
-                        SimpleApplet.this.getWidth(),
-                        SimpleApplet.this.getHeight());
-                  cam.setFrustumPerspective(45.0f, (float) displayParent
-                        .getWidth()
-                        / (float) displayParent.getHeight(), 1, 1000);
-                  return null;
-               }
-            };
-            GameTaskQueueManager.getManager()
-                  .getQueue(GameTaskQueue.RENDER).enqueue(exe);
-         }
-      });
+      this.addComponentListener(new AppletResizeListener(this));
    }
 
 }
No newline at end of file
Index: src/com/jmex/awt/applet/SimplePassApplet.java
===================================================================
--- src/com/jmex/awt/applet/SimplePassApplet.java   (revision 4389)
+++ src/com/jmex/awt/applet/SimplePassApplet.java   (working copy)
@@ -1,17 +1,11 @@
 package com.jmex.awt.applet;
 
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
-import java.util.concurrent.Callable;
-
 import com.jme.app.AbstractGame;
 import com.jme.app.BaseSimpleGame;
 import com.jme.app.SimplePassGame;
 import com.jme.input.MouseInput;
 import com.jme.renderer.Renderer;
 import com.jme.renderer.pass.BasicPassManager;
-import com.jme.util.GameTaskQueue;
-import com.jme.util.GameTaskQueueManager;
 
 /**
  * LWJGL2 Applet imlpementation similar to {@link SimplePassGame}<br>
@@ -78,22 +72,6 @@
 
    protected void initSystem() {
       super.initSystem();
-      this.addComponentListener(new ComponentAdapter() {
-         public void componentResized(ComponentEvent ce) {
-            Callable<?> exe = new Callable<Object>() {
-               public Object call() {
-                  display.getRenderer().reinit(
-                        SimplePassApplet.this.getWidth(),
-                        SimplePassApplet.this.getHeight());
-                  cam.setFrustumPerspective(45.0f, (float) displayParent
-                        .getWidth()
-                        / (float) displayParent.getHeight(), 1, 1000);
-                  return null;
-               }
-            };
-            GameTaskQueueManager.getManager()
-                  .getQueue(GameTaskQueue.RENDER).enqueue(exe);
-         }
-      });
+      this.addComponentListener(new AppletResizeListener(this));
    }
 }
No newline at end of file
Index: src/com/jmex/awt/applet/StandardApplet.java
===================================================================
--- src/com/jmex/awt/applet/StandardApplet.java   (revision 4389)
+++ src/com/jmex/awt/applet/StandardApplet.java   (working copy)
@@ -5,8 +5,6 @@
 import java.awt.Canvas;
 import java.awt.Dimension;
 import java.awt.Toolkit;
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -41,8 +39,6 @@
 
 /**
  * LWJGL2 Applet imlpementation similar to {@link StandardGame}
- *
- * TODO: not yet finished, crashes when pressing F4
  */
 public abstract class StandardApplet extends Applet {
     private static final long serialVersionUID = 6894421316159346138L;
@@ -199,7 +195,8 @@
                height);
             camera.setFrustumPerspective(45.0f, (float) width
                / (float) height, 1, 1000);
-         
+         DisplaySystem.getDisplaySystem().setWidth(width);
+         DisplaySystem.getDisplaySystem().setHeight(height);
       } catch (LWJGLException e) {
          e.printStackTrace();
       }
@@ -358,7 +355,7 @@
              // render game state
              render(tpf);
              // Swap buffers, process messages, handle input
-             Display.update();
+             display.getRenderer().displayBackBuffer();
              
                // Fixed framerate End
                if (preferredTicksPerFrame >= 0) {
@@ -463,23 +460,7 @@
            display = DisplaySystem.getDisplaySystem();
         }
         
-       this.addComponentListener(new ComponentAdapter() {
-           public void componentResized(ComponentEvent ce) {
-          Callable<?> exe = new Callable<Object>() {
-              public Object call() {
-             display.getRenderer().reinit(
-                StandardApplet.this.getWidth(),
-                StandardApplet.this.getHeight());
-             camera.setFrustumPerspective(45.0f,
-                   (float) StandardApplet.this.getWidth()
-                      / (float)StandardApplet.this.getHeight(), 1, 1000);
-             return null;
-              }
-          };
-          GameTaskQueueManager.getManager()
-             .getQueue(GameTaskQueue.RENDER).enqueue(exe);
-           }
-       });
+       this.addComponentListener(new AppletResizeListener(this));
     }
     
     protected void initSound() {
Index: src/jmetest/awt/applet/TestSimpleApplet.java
===================================================================
--- src/jmetest/awt/applet/TestSimpleApplet.java   (revision 4389)
+++ src/jmetest/awt/applet/TestSimpleApplet.java   (working copy)
@@ -18,6 +18,7 @@
     */
    @Override
    public void init() {
+      System.setProperty("jme.stats", "set");
       setSize(640, 480);
       super.init();
    }
@@ -31,4 +32,8 @@
       b.updateModelBound();
        rootNode.attachChild(b);
     }
+   @Override
+   protected void simpleUpdate() {
+      super.simpleUpdate();
+   }
 }
No newline at end of file
Index: src/jmetest/awt/applet/TestStandardApplet.java
===================================================================
--- src/jmetest/awt/applet/TestStandardApplet.java   (revision 4389)
+++ src/jmetest/awt/applet/TestStandardApplet.java   (working copy)
@@ -6,9 +6,12 @@
 import com.jme.bounding.BoundingSphere;
 import com.jme.input.KeyBindingManager;
 import com.jme.input.KeyInput;
+import com.jme.light.DirectionalLight;
 import com.jme.math.Vector3f;
 import com.jme.renderer.ColorRGBA;
 import com.jme.scene.shape.Box;
+import com.jme.scene.state.LightState;
+import com.jme.system.DisplaySystem;
 import com.jme.util.GameTaskQueueManager;
 import com.jmex.awt.applet.StandardApplet;
 import com.jmex.game.state.BasicGameState;
@@ -24,11 +27,11 @@
 public class TestStandardApplet extends StandardApplet {
 
    private static final long serialVersionUID = 1L;
-
+   
    public void init() {
+      setSize(800, 600);
       System.setProperty("jme.stats", "set");
       setBackgroundColor(ColorRGBA.blue.clone());
-      setSize(640, 480);
       super.init();
       
       try {
@@ -69,19 +72,34 @@
       KeyBindingManager.getKeyBindingManager().add("switch", KeyInput.KEY_SPACE);
       this.applet = applet;
       
+      addLight();
+      
       Box box = new Box("my box", new Vector3f(0, 0, 0), 2, 2, 2);
       box.setModelBound(new BoundingSphere());
       box.updateModelBound();
+      rootNode.attachChild(box);
+      rootNode.updateGeometricState(0, true);
       // We had to add the following line because the render thread is already running
       // Anytime we add content we need to updateRenderState or we get funky effects
-      rootNode.attachChild(box);
-      
       rootNode.updateRenderState();
    }
-   
+
+   /**
+    * create a LightState and attach a DirectionalLight to it.
+    */
+   private void addLight() {
+      LightState ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
+      DirectionalLight dr = new DirectionalLight();
+      dr.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
+      dr.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
+      dr.setEnabled(true);
+      dr.setDirection(new Vector3f(0.5f, -1, -0.5f));
+      ls.attach(dr);
+      rootNode.setRenderState(ls);
+   }
+
    @Override
    public void update(float tpf) {
-      
       if (KeyBindingManager.getKeyBindingManager().isValidCommand("switch", false)) {
          applet.toggleFullscreen();
          KeyInput.get().clear();

in case your still having problems with Display.setParent() applets crashing have a look at the gears applet example in lwjgl svn found at http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/test/applet/GearsApplet.java?view=log notice how lwjgl's Display is linked to the Canvas, should help you avoid the crashes.

faust said:

in case your still having problems with Display.setParent() applets crashing have a look at the gears applet example in lwjgl svn found at http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/test/applet/GearsApplet.java?view=log notice how lwjgl's Display is linked to the Canvas, should help you avoid the crashes.


That's what these new applets are based off of, just so you know....  :D
SomethingNew said:

That's what these new applets are based off of, just so you know....  :D


ah good, do note the recent changes :).

Hi Core-Dump



Just an FYI. I get an "invalid archive" when I try to unzip "lwjgl2.0_applets.zip". Perhaps it is just me.

that was just a temporary zip anyway, I'll remove the link.



Get the newest source from jme2.

com.jmex.awt.applet.SimpleApplet  etc.