From: Kenneth B. R. <kbr...@us...> - 2008-03-02 20:21:50
|
Update of /cvsroot/jake2/jake2/src/jake2 In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv16186/src/jake2 Modified Files: Globals.java Added Files: CompatibilityApplet.java Jake2Applet.java SizeChangeListener.java Log Message: 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. --- NEW FILE: CompatibilityApplet.java --- /* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package jake2; import java.applet.*; import java.awt.Canvas; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.net.*; import java.text.*; import java.util.*; /** * An applet to smooth the adoption of newer applet content which runs * only with the new Java Plug-In: in particular, applets launched via * JNLP. It displays some informative text; clicking on the text will * open a new browser window pointing to the download page for the new * Java Plug-In. This applet runs even on very early versions of the * JRE and can be referenced from the applet tag's code / archive * attributes while the jnlp_href can point to the new-style applet's * code. * <P> * In 6u10 build 13, it is necessary to copy this applet's source code * and rename it to be exactly the same class name as is in the * main-class attribute in the JNLP file, preferably referenced via * the archive attribute in a jar named something like * "BackwardCompatibility.jar". In 6u10 build 14, the applet tag may * refer to this class via its code attribute, and the JNLP file may * point to an entirely different class name, allowing one copy of the * compatibility applet to be used for many different JNLP-launched * applets. * <P> * Parameters supported: * <P> * <CODE>compat_fontsize</CODE> - the font size used to draw the text.<BR> * <CODE>compat_fgcolor</CODE> - the color used to draw the text.<BR> * <CODE>compat_bgcolor</CODE> - the color used to draw the text. * * @author Kenneth Russell */ public class CompatibilityApplet extends java.applet.Applet { private Font font; private static final String inputText = "Click here to get the new Java Plug-In"; private static final String url = "https://jdk6.dev.java.net/6uNea.html"; public void init() { int fontSize = 36; try { fontSize = Integer.parseInt(getParameter("compat_fontsize")); } catch (Exception e) { } Color fgColor = Color.black; Color bgColor = Color.white; try { fgColor = Color.decode(getParameter("compat_fgcolor")); } catch (Exception e) { } try { bgColor = Color.decode(getParameter("compat_bgcolor")); } catch (Exception e) { } font = new Font("SansSerif", Font.PLAIN, fontSize); setForeground(fgColor); setBackground(bgColor); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { try { getAppletContext().showDocument(new URL(url), "_blank"); } catch (Exception ex) { } } }); } static class LineInfo { String text; float width; float height; LineInfo(String text, float width, float height) { this.text = text; this.width = width; this.height = height; } } public void paint(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; List/*<LineInfo>*/ lines = new ArrayList(); FontMetrics fm = g.getFontMetrics(); FontRenderContext frc = g.getFontRenderContext(); Map attrs = new HashMap(); attrs.put(TextAttribute.FONT, font); float totalHeight = 0; int curPos = 0; AttributedString str = new AttributedString(inputText, attrs); LineBreakMeasurer measurer = new LineBreakMeasurer(str.getIterator(), frc); while (measurer.getPosition() < inputText.length()) { // Give us a few pixels inset from the edges int nextPos = measurer.nextOffset(getWidth() - 10); String line = inputText.substring(curPos, nextPos); GlyphVector gv = font.createGlyphVector(frc, line); Rectangle2D bounds = gv.getVisualBounds(); float height = (float) bounds.getHeight() + 5; lines.add(new LineInfo(line, (float) bounds.getWidth(), height)); totalHeight += height; curPos = nextPos; measurer.setPosition(curPos); } // Draw the strings centered vertically and horizontally in this component g.setFont(font); float curY = (getHeight() - totalHeight) / 2; for (Iterator iter = lines.iterator(); iter.hasNext(); ) { LineInfo line = (LineInfo) iter.next(); curY += line.height; float x = (getWidth() - line.width) / 2; g.drawString(line.text, (int) x, (int) curY); } } } --- NEW FILE: SizeChangeListener.java --- /* * SizeChangeListener.java * Copyright (C) 2008 * * $Id: SizeChangeListener.java,v 1.1 2008/03/02 20:21:13 kbrussel Exp $ */ package jake2; /** Provides a callback when the user changes the video mode of the game. */ public interface SizeChangeListener { public void sizeChanged(int width, int height); } Index: Globals.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/Globals.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Globals.java 7 Feb 2005 17:49:26 -0000 1.5 --- Globals.java 2 Mar 2008 20:21:12 -0000 1.6 *************** *** 386,388 **** --- 386,398 ---- public static Random rnd = new Random(); + + //============================================================================= + + // Information used when we're running as an applet + // Whether we're running as an applet + public static boolean appletMode; + // The applet, represented as an Object to avoid an AWT dependency here + public static Object applet; + // A listener to receive video mode changes + public static SizeChangeListener sizeChangeListener; } --- NEW FILE: Jake2Applet.java --- /* * Jake2Applet.java * Copyright (C) 2008 * * $Id: Jake2Applet.java,v 1.1 2008/03/02 20:21:12 kbrussel Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package jake2; import jake2.game.Cmd; import jake2.qcommon.*; import jake2.sys.Timer; import java.awt.BorderLayout; import java.util.Locale; import javax.swing.JApplet; import netscape.javascript.*; /** * Jake2 is the main class of Quake2 for Java. */ public class Jake2Applet extends JApplet { private JSObject self; private volatile boolean shouldShutDown; private boolean shutDown; private Object shutDownLock = new Object(); public void start() { setLayout(new BorderLayout()); new GameThread().start(); } public void stop() { synchronized(shutDownLock) { shouldShutDown = true; while (!shutDown) { try { shutDownLock.wait(); } catch (InterruptedException e) { } } } Cmd.ExecuteString("quit"); } class GameThread extends Thread { public GameThread() { super("Jake2 Game Thread"); } public void run() { // TODO: check if dedicated is set in config file Globals.dedicated= Cvar.Get("dedicated", "0", Qcommon.CVAR_NOSET); // Set things up for applet execution Qcommon.appletMode = true; Qcommon.applet = Jake2Applet.this; Qcommon.sizeChangeListener = new SizeChangeListener() { public void sizeChanged(int width, int height) { try { if (self == null) { JSObject win = JSObject.getWindow(Jake2Applet.this); self = (JSObject) win.eval("document.getElementById(\"" + getParameter("id") + "\")"); } self.setMember("width", new Integer(width)); self.setMember("height", new Integer(height)); } catch (Exception e) { e.printStackTrace(); } } }; // open the q2dialog, if we are not in dedicated mode. if (Globals.dedicated.value != 1.0f) { Jake2.Q2Dialog = new Q2DataDialog(); Locale.setDefault(Locale.US); Jake2.Q2Dialog.setVisible(true); } Qcommon.Init(new String[] { "Jake2" }); Globals.nostdout = Cvar.Get("nostdout", "0", 0); try { while (!shouldShutDown) { int oldtime = Timer.Milliseconds(); int newtime; int time; while (true) { // find time spending rendering last frame newtime = Timer.Milliseconds(); time = newtime - oldtime; if (time > 0) Qcommon.Frame(time); oldtime = newtime; } } } finally { synchronized(shutDownLock) { shutDown = true; shutDownLock.notifyAll(); } } } } } |