From 9323828a9f6118313d431f13a1f309da9c79ba09 Mon Sep 17 00:00:00 2001 From: "Kenneth B. Russell" Date: Sun, 2 Mar 2008 20:21:15 +0000 Subject: Changes to enable Jake2 to run well as an applet inside the next-generation Java Plug-In. Added Globals.appletMode, Globals.applet and Globals.sizeChangeListener to be able to easily pass around the knowledge that the system is running in applet mode, and the applet itself, which becomes the parent container for the output. Most changes were in Jsr231Driver to support putting the Display into a preexisting parent container rather than a new Frame each time. Changed JOGLKBD to allow manual initialization of the parent container rather than obtaining it from a CreateNotify or ConfigureNotify event since these will never be generated in the applet case. Removed various calls to System.exit(), although strictly speaking this is no longer necessary because it is expected that the separate_jvm parameter will be used in conjunction with the new Java Plug-In to create a fresh JVM instance for each run of Jake2. Video mode switching in applet mode is working; the applet resizes (via JavaScript) to accommodate the newly selected resolution. Full screen mode when running as an applet is not implemented at this point, as the intent was to show this inside the browser, though support could be added very straightforwardly. --- src/jake2/render/opengl/Jsr231Driver.java | 219 +++++++++++++++++++----------- 1 file changed, 137 insertions(+), 82 deletions(-) (limited to 'src/jake2/render') diff --git a/src/jake2/render/opengl/Jsr231Driver.java b/src/jake2/render/opengl/Jsr231Driver.java index 39c3384..b6fa6eb 100644 --- a/src/jake2/render/opengl/Jsr231Driver.java +++ b/src/jake2/render/opengl/Jsr231Driver.java @@ -26,6 +26,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package jake2.render.opengl; import jake2.Defines; +import jake2.Globals; +import jake2.SizeChangeListener; import jake2.client.VID; import jake2.qcommon.Cbuf; import jake2.qcommon.xcommand_t; @@ -54,6 +56,10 @@ public abstract class Jsr231Driver extends Jsr231GL implements GLDriver { private volatile Display display; private volatile Frame window; + // This is either the above Window reference or the global + // applet if we're running in applet mode + private volatile Container container; + // window position on the screen int window_xpos, window_ypos; @@ -135,6 +141,10 @@ public abstract class Jsr231Driver extends Jsr231GL implements GLDriver { VID.Printf(Defines.PRINT_ALL, "...setting mode " + mode + ":"); + if (Globals.appletMode && container == null) { + container = (Container) Globals.applet; + } + /* * full screen handling */ @@ -143,53 +153,68 @@ public abstract class Jsr231Driver extends Jsr231GL implements GLDriver { device = env.getDefaultScreenDevice(); } - if (oldDisplayMode == null) { - oldDisplayMode = device.getDisplayMode(); - } - - if (!VID.GetModeInfo(newDim, mode)) { - VID.Printf(Defines.PRINT_ALL, " invalid mode\n"); - return Base.rserr_invalid_mode; - } - - VID.Printf(Defines.PRINT_ALL, " " + newDim.width + " " + newDim.height + '\n'); - - // destroy the existing window - if (window != null) shutdown(); - - window = new Frame("Jake2 (jsr231)"); - ImageIcon icon = new ImageIcon(getClass().getResource("/icon-small.png")); - window.setIconImage(icon.getImage()); - window.setLayout(new GridBagLayout()); + if (oldDisplayMode == null) { + oldDisplayMode = device.getDisplayMode(); + } + + if (!VID.GetModeInfo(newDim, mode)) { + VID.Printf(Defines.PRINT_ALL, " invalid mode\n"); + return Base.rserr_invalid_mode; + } + + VID.Printf(Defines.PRINT_ALL, " " + newDim.width + " " + newDim.height + '\n'); + + if (!Globals.appletMode) { + // destroy the existing window + if (window != null) shutdown(); + + window = new Frame("Jake2 (jsr231)"); + container = window; + ImageIcon icon = new ImageIcon(getClass().getResource("/icon-small.png")); + window.setIconImage(icon.getImage()); + window.setLayout(new GridBagLayout()); + // register event listener + window.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit"); + } + }); + } - Display canvas = new Display(new GLCapabilities()); - // we want keypressed events for TAB key - canvas.setFocusTraversalKeysEnabled(false); - - // the OpenGL canvas grows and shrinks with the window - GridBagConstraints gbc = new GridBagConstraints(); - gbc.fill = GridBagConstraints.BOTH; - gbc.weightx = gbc.weighty = 1; - window.add(canvas, gbc); + if (Globals.appletMode) { + // Destroy the previous display if there is one + shutdown(); + + // We don't support full-screen mode + fullscreen = false; + + // We need to feed the container to the JOGL + // keyboard class manually because we'll never get + // a component shown event for it + JOGLKBD.Init(container); + } + + Display canvas = new Display(new GLCapabilities()); + // we want keypressed events for TAB key + canvas.setFocusTraversalKeysEnabled(false); + canvas.setSize(newDim.width, newDim.height); + + // the OpenGL canvas grows and shrinks with the window + final GridBagConstraints gbc = new GridBagConstraints(); + gbc.fill = GridBagConstraints.BOTH; + gbc.weightx = gbc.weighty = 1; - canvas.setSize(newDim.width, newDim.height); - - // register event listener - window.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { - Cbuf.ExecuteText(Defines.EXEC_APPEND, "quit"); - } - }); - - // D I F F E R E N T J A K E 2 E V E N T P R O C E S S I N G - window.addComponentListener(JOGLKBD.listener); - canvas.addKeyListener(JOGLKBD.listener); - canvas.addMouseListener(JOGLKBD.listener); - canvas.addMouseMotionListener(JOGLKBD.listener); - canvas.addMouseWheelListener(JOGLKBD.listener); + // D I F F E R E N T J A K E 2 E V E N T P R O C E S S I N G + container.addComponentListener(JOGLKBD.listener); + canvas.addKeyListener(JOGLKBD.listener); + canvas.addMouseListener(JOGLKBD.listener); + canvas.addMouseMotionListener(JOGLKBD.listener); + canvas.addMouseWheelListener(JOGLKBD.listener); if (fullscreen) { + container.add(canvas, gbc); + DisplayMode displayMode = findDisplayMode(newDim); newDim.width = displayMode.getWidth(); @@ -209,26 +234,48 @@ public abstract class Jsr231Driver extends Jsr231GL implements GLDriver { VID.Printf(Defines.PRINT_ALL, "...setting fullscreen " + getModeString(displayMode) + '\n'); } else { - final Frame f2 = window; - try { - EventQueue.invokeAndWait(new Runnable() { - public void run() { - //f2.setLocation(window_xpos, window_ypos); - f2.pack(); - f2.setResizable(true); - f2.setVisible(true); - } - }); - } catch (Exception e) { - e.printStackTrace(); - } + if (!Globals.appletMode) { + container.add(canvas, gbc); + final Frame f2 = window; + try { + EventQueue.invokeAndWait(new Runnable() { + public void run() { + //f2.setLocation(window_xpos, window_ypos); + f2.pack(); + f2.setResizable(false); + f2.setVisible(true); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + final Display fd = canvas; + try { + EventQueue.invokeAndWait(new Runnable() { + public void run() { + container.add(fd, BorderLayout.CENTER); + // Notify the size listener about the change + SizeChangeListener listener = Globals.sizeChangeListener; + if (listener != null) { + listener.sizeChanged(newDim.width, newDim.height); + } + fd.setSize(newDim.width, newDim.height); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } } - while (!canvas.isDisplayable() || !window.isDisplayable()) { + if (!Globals.appletMode) { + while (!canvas.isDisplayable() || !window.isDisplayable()) { try { - Thread.sleep(100); + Thread.sleep(100); } catch (InterruptedException e) {} - } + } + } canvas.requestFocus(); this.display = canvas; @@ -240,39 +287,47 @@ public abstract class Jsr231Driver extends Jsr231GL implements GLDriver { } public void shutdown() { - try { + if (!Globals.appletMode) { + try { EventQueue.invokeAndWait(new Runnable() { public void run() { - if (oldDisplayMode != null - && device.getFullScreenWindow() != null) { - try { - if (device.isFullScreenSupported()) { - if (!device.getDisplayMode().equals( - oldDisplayMode)) - device.setDisplayMode(oldDisplayMode); - - } - device.setFullScreenWindow(null); - } catch (Exception e) { - e.printStackTrace(); - } - } + if (oldDisplayMode != null + && device.getFullScreenWindow() != null) { + try { + if (device.isFullScreenSupported()) { + if (!device.getDisplayMode().equals(oldDisplayMode)) + device.setDisplayMode(oldDisplayMode); + + } + device.setFullScreenWindow(null); + } catch (Exception e) { + e.printStackTrace(); + } + } } - }); - } catch (Exception e) { + }); + } catch (Exception e) { e.printStackTrace(); - } - if (window != null) { + } + + if (window != null) { if (display != null) display.destroy(); window.dispose(); while (window.isDisplayable()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } } - } + } + } else { + if (display != null) { + display.destroy(); + // Remove the old display if there is one + container.remove(display); + } + } display = null; } -- cgit v1.2.3