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
This commit is contained in:
Michael Bayne
2005-06-16 00:04:09 +00:00
parent 6ce0059b29
commit 1f9d28aa59
3 changed files with 227 additions and 10 deletions
+24 -10
View File
@@ -41,6 +41,7 @@ import com.jme.system.PropertiesIO;
import com.jme.system.lwjgl.LWJGLPropertiesDialog; import com.jme.system.lwjgl.LWJGLPropertiesDialog;
import com.jme.bui.event.InputDispatcher; import com.jme.bui.event.InputDispatcher;
import com.jme.bui.event.PolledInputDispatcher;
import com.jme.input.InputHandler; import com.jme.input.InputHandler;
import com.jme.input.InputSystem; import com.jme.input.InputSystem;
import com.jme.input.Mouse; import com.jme.input.Mouse;
@@ -156,7 +157,8 @@ public class JmeApp
// enter the main rendering and event processing loop // enter the main rendering and event processing loop
while (!_finished && !_display.isClosing()) { while (!_finished && !_display.isClosing()) {
try { try {
processFrame(); long frameStart = processFrame();
processEvents(frameStart);
_failures = 0; _failures = 0;
} catch (Throwable t) { } catch (Throwable t) {
@@ -225,12 +227,16 @@ public class JmeApp
protected void initDisplay () protected void initDisplay ()
throws JmeException throws JmeException
{ {
// create the main display system // create the main display system (JmeCanvasApp creates the
_display = DisplaySystem.getDisplaySystem(_properties.getRenderer()); // display and the window earlier and in a different way, so we
_display.createWindow( // need to avoid doing that here if it's already been done)
_properties.getWidth(), _properties.getHeight(), if (_display == null) {
_properties.getDepth(), _properties.getFreq(), _display = DisplaySystem.getDisplaySystem(_properties.getRenderer());
_properties.getFullscreen()); _display.createWindow(
_properties.getWidth(), _properties.getHeight(),
_properties.getDepth(), _properties.getFreq(),
_properties.getFullscreen());
}
_display.setVSyncEnabled(true); _display.setVSyncEnabled(true);
// create a camera // create a camera
@@ -264,6 +270,7 @@ public class JmeApp
*/ */
protected void initInput () protected void initInput ()
{ {
InputSystem.createInputSystem(_properties.getRenderer());
_input = createInputHandler(_camera, _properties.getRenderer()); _input = createInputHandler(_camera, _properties.getRenderer());
_input.setMouse(createMouse()); _input.setMouse(createMouse());
} }
@@ -332,8 +339,7 @@ public class JmeApp
_iface = new Node("Interface"); _iface = new Node("Interface");
_root.attachChild(_iface); _root.attachChild(_iface);
InputSystem.createInputSystem(_properties.getRenderer()); _dispatcher = new PolledInputDispatcher(_timer, _input, _iface);
_dispatcher = new InputDispatcher(_timer, _input, _iface);
// we don't hide the cursor // we don't hide the cursor
InputSystem.getMouseInput().setCursorVisible(true); InputSystem.getMouseInput().setCursorVisible(true);
} }
@@ -350,7 +356,7 @@ public class JmeApp
/** /**
* Processes a single frame. * Processes a single frame.
*/ */
protected final void processFrame () protected final long processFrame ()
{ {
// update our simulation and render a frame // update our simulation and render a frame
long frameStart = _timer.getTime(); long frameStart = _timer.getTime();
@@ -358,7 +364,15 @@ public class JmeApp
render(frameStart); render(frameStart);
_display.getRenderer().displayBackBuffer(); _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 // now process events or sleep until the next frame (assume zero
// frame duration to start to ensure that we always process at // frame duration to start to ensure that we always process at
// least one event per frame) // least one event per frame)
@@ -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;
}
@@ -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();
}
}