From 1f9d28aa59a6dd74c9d60c803e54adcee9e91de3 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 16 Jun 2005 00:04:09 +0000 Subject: [PATCH] A bunch of fiddling to support running JME in an AWT canvas. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3601 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/jme/JmeApp.java | 34 +++-- src/java/com/threerings/jme/JmeCanvasApp.java | 141 ++++++++++++++++++ .../com/threerings/jme/JmeCanvasTest.java | 62 ++++++++ 3 files changed, 227 insertions(+), 10 deletions(-) create mode 100644 src/java/com/threerings/jme/JmeCanvasApp.java create mode 100644 tests/src/java/com/threerings/jme/JmeCanvasTest.java diff --git a/src/java/com/threerings/jme/JmeApp.java b/src/java/com/threerings/jme/JmeApp.java index bc4d2fd19..2ef548813 100644 --- a/src/java/com/threerings/jme/JmeApp.java +++ b/src/java/com/threerings/jme/JmeApp.java @@ -41,6 +41,7 @@ import com.jme.system.PropertiesIO; import com.jme.system.lwjgl.LWJGLPropertiesDialog; import com.jme.bui.event.InputDispatcher; +import com.jme.bui.event.PolledInputDispatcher; import com.jme.input.InputHandler; import com.jme.input.InputSystem; import com.jme.input.Mouse; @@ -156,7 +157,8 @@ public class JmeApp // enter the main rendering and event processing loop while (!_finished && !_display.isClosing()) { try { - processFrame(); + long frameStart = processFrame(); + processEvents(frameStart); _failures = 0; } catch (Throwable t) { @@ -225,12 +227,16 @@ public class JmeApp protected void initDisplay () throws JmeException { - // create the main display system - _display = DisplaySystem.getDisplaySystem(_properties.getRenderer()); - _display.createWindow( - _properties.getWidth(), _properties.getHeight(), - _properties.getDepth(), _properties.getFreq(), - _properties.getFullscreen()); + // create the main display system (JmeCanvasApp creates the + // display and the window earlier and in a different way, so we + // need to avoid doing that here if it's already been done) + if (_display == null) { + _display = DisplaySystem.getDisplaySystem(_properties.getRenderer()); + _display.createWindow( + _properties.getWidth(), _properties.getHeight(), + _properties.getDepth(), _properties.getFreq(), + _properties.getFullscreen()); + } _display.setVSyncEnabled(true); // create a camera @@ -264,6 +270,7 @@ public class JmeApp */ protected void initInput () { + InputSystem.createInputSystem(_properties.getRenderer()); _input = createInputHandler(_camera, _properties.getRenderer()); _input.setMouse(createMouse()); } @@ -332,8 +339,7 @@ public class JmeApp _iface = new Node("Interface"); _root.attachChild(_iface); - InputSystem.createInputSystem(_properties.getRenderer()); - _dispatcher = new InputDispatcher(_timer, _input, _iface); + _dispatcher = new PolledInputDispatcher(_timer, _input, _iface); // we don't hide the cursor InputSystem.getMouseInput().setCursorVisible(true); } @@ -350,7 +356,7 @@ public class JmeApp /** * Processes a single frame. */ - protected final void processFrame () + protected final long processFrame () { // update our simulation and render a frame long frameStart = _timer.getTime(); @@ -358,7 +364,15 @@ public class JmeApp render(frameStart); _display.getRenderer().displayBackBuffer(); + return frameStart; + } + /** + * Processes as many events as possible until it is time to display + * the next frame. + */ + protected void processEvents (long frameStart) + { // now process events or sleep until the next frame (assume zero // frame duration to start to ensure that we always process at // least one event per frame) diff --git a/src/java/com/threerings/jme/JmeCanvasApp.java b/src/java/com/threerings/jme/JmeCanvasApp.java new file mode 100644 index 000000000..f019b641a --- /dev/null +++ b/src/java/com/threerings/jme/JmeCanvasApp.java @@ -0,0 +1,141 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme; + +import java.awt.Canvas; +import java.awt.EventQueue; + +import com.jme.renderer.Renderer; +import com.jme.renderer.lwjgl.LWJGLRenderer; +import com.jme.scene.Node; +import com.jme.system.DisplaySystem; +import com.jme.util.awt.JMECanvas; +import com.jme.util.awt.JMECanvasImplementor; + +import com.jme.bui.event.CanvasInputDispatcher; + +/** + * Extends the basic {@link JmeApp} with the necessary wiring to use the + * GL/AWT bridge to display our GL interface in an AWT component. + */ +public class JmeCanvasApp extends JmeApp +{ + public JmeCanvasApp (int width, int height) + { + _display = DisplaySystem.getDisplaySystem("LWJGL"); + _canvas = _display.createCanvas(width, height); + ((JMECanvas)_canvas).setImplementor(_winimp); + _canvas.setBounds(0, 0, width, height); + } + + /** + * Returns the AWT canvas that contains our GL display. + */ + public Canvas getCanvas () + { + return _canvas; + } + + public void run () + { + Thread t = new Thread() { + public void run () { + while (!_finished) { + // queue up another repaint + _canvas.repaint(); + try { + Thread.sleep(10); + } catch (Exception e) { + } + } + } + }; + t.setDaemon(true); + t.start(); + } + + // documentation inherited from interface RunQueue + public void postRunnable (Runnable r) + { + EventQueue.invokeLater(r); + } + + // documentation inherited from interface RunQueue + public boolean isDispatchThread () + { + return EventQueue.isDispatchThread(); + } + + protected void initInput () + { + // can't do GL input with AWT + } + + /** + * Initializes our user interface bits. + */ + protected void initInterface () + { + _iface = new Node("Interface"); + _root.attachChild(_iface); + + _dispatcher = new CanvasInputDispatcher(_iface, _canvas); + } + + /** This is used if we embed our GL display in an AWT component. */ + protected JMECanvasImplementor _winimp = new JMECanvasImplementor() { + public void doSetup () { + super.doSetup(); + + LWJGLRenderer renderer = + new LWJGLRenderer(_canvas.getWidth(), _canvas.getHeight()); + renderer.setHeadless(true); + _display.setRenderer(renderer); + DisplaySystem.updateStates(renderer); + + if (!init()) { + Log.warning("JmeCanvasApp init failed."); + } + } + + public void doUpdate () { + } + + public void doRender () { + // here we do our normal frame processing + try { + processFrame(); + // we don't process events as the AWT queue handles them + _failures = 0; + } catch (Throwable t) { + Log.logStackTrace(t); + // stick a fork in things if we fail too many + // times in a row + if (++_failures > MAX_SUCCESSIVE_FAILURES) { + JmeCanvasApp.this.stop(); + } + } + } + }; + + protected Canvas _canvas; +} diff --git a/tests/src/java/com/threerings/jme/JmeCanvasTest.java b/tests/src/java/com/threerings/jme/JmeCanvasTest.java new file mode 100644 index 000000000..089778ddc --- /dev/null +++ b/tests/src/java/com/threerings/jme/JmeCanvasTest.java @@ -0,0 +1,62 @@ +// +// $Id$ + +package com.threerings.jme; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JFrame; + +import com.jme.bounding.BoundingBox; +import com.jme.math.Vector3f; +import com.jme.scene.shape.Box; + +/** + * Tests the JME/AWT integration bits. + */ +public class JmeCanvasTest extends JmeCanvasApp +{ + public static void main (String[] args) + { + final JmeCanvasTest app = new JmeCanvasTest(); + JFrame frame = new JFrame("JmeCanvasTest"); + frame.getContentPane().add(app.getCanvas(), BorderLayout.CENTER); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setVisible(true); + + JButton button = new JButton("Create box"); + frame.getContentPane().add(button, BorderLayout.SOUTH); + button.addActionListener(new ActionListener() { + public void actionPerformed (ActionEvent event) { + app.addBox(); + } + }); + app.run(); + } + + public void addBox () + { + Vector3f max = new Vector3f(5, 5, 5); + Vector3f min = new Vector3f(-5, -5, -5); + + Box box = new Box("Box", min, max); + box.setModelBound(new BoundingBox()); + box.updateModelBound(); + box.setLocalTranslation(new Vector3f(0, 0, -10)); + _geom.attachChild(box); + _geom.updateRenderState(); + } + + protected JmeCanvasTest () + { + super(800, 600); + } + + protected void initRoot () + { + super.initRoot(); + } +}