New context management code (incomplete)

This commit is contained in:
Caspian Rychlik-Prince 2004-02-23 23:42:58 +00:00
parent 5a8267a1e2
commit 6a58bc26e6
14 changed files with 501 additions and 120 deletions

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2002 Lightweight Java Game Library Project
* 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 'Light Weight Java Game Library' 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 org.lwjgl;
import javax.swing.JOptionPane;
/**
* $Id$
*
* An AWT adapter for using AWT to take care of things on platforms where we
* know AWT is present.
* <p><em>Note</em> To compile LWJGL applications with Excelsior JET that use JetPerfect
* and that have no dependencies on AWT, do not include this class in your
* JET project.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
final class AWTAdapter implements Adapter {
/**
* C'tor
*/
AWTAdapter() {
}
/**
* Spawn a "modal" dialog in the centre of the screen with a message in it
* and an OK button. This method blocks until the dialog is dismissed.
* @param title
* @param message
*/
public void alert(String title, String message) {
JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2002 Lightweight Java Game Library Project
* 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 'Light Weight Java Game Library' 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 org.lwjgl;
/**
* $Id$
*
* Interface for adapting to window environments.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
public interface Adapter {
/**
* Spawn a "modal" dialog in the centre of the screen with a message in it
* and an OK button. This method blocks until the dialog is dismissed.
* @param title
* @param message
*/
public void alert(String title, String message);
}

View file

@ -188,12 +188,13 @@ public final class Sys {
* Attempt to display a modal alert to the user. This method should be used
* when a game fails to initialize properly or crashes out losing its display
* in the process. It is provided because AWT may not be available on the target
* platform.
*
* platform, although on Mac and Linux and other platforms supporting AWT we
* delegate the task to AWT instead of doing it ourselves.
* <p>
* The alert should display the title and the message and then the current
* thread should block until the user dismisses the alert - typically with an
* OK button click.
*
* <p>
* It may be that the user's system has no windowing system installed for some
* reason, in which case this method may do nothing at all, or attempt to provide
* some console output.
@ -201,31 +202,41 @@ public final class Sys {
* @param title The title of the alert. We suggest using the title of your game.
* @param message The message text for the alert.
*/
public static native void alert(String title, String message);
/*
* Cas: this is just a debugging aid. The native code is also commented out.
*
public static native int getDirectBufferAddress(Buffer buf);
*/
public static void alert(String title, String message) {
String osName = System.getProperty("os.name");
if (osName.startsWith("Windoxws")) {
nAlert(title, message);
} else {
try {
Adapter adapter = (Adapter) Class.forName("org.lwjgl.AWTAdapter").newInstance(); // This avoids a Jet error message
adapter.alert(title, message);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
private static native void nAlert(String title, String message);
/**
* Open the system web browser and point it at the specified URL. It is recommended
* that this not be called whilst your game is running, but on application exit in
* a shutdown hook, as the screen resolution will not be reset when the browser is
* brought into view.
*
* <p>
* There is no guarantee that this will work, nor that we can detect if it has
* failed - hence we don't return success code or throw an Exception. This is just a
* best attempt at opening the URL given - don't rely on it to work!
* <p>
* @param url The URL
* @return false if we are CERTAIN the call has failed
*/
public static void openURL(String url) {
public static boolean openURL(String url) {
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS") || osName.startsWith("Windows")) {
// Mac and Windows both do this nicely from native code.
nOpenURL(url);
return;
return true;
}
// Linux may as well resort to pure Java hackery, as there's no Linux native way of doing it
// right anyway.
@ -235,12 +246,15 @@ public final class Sys {
for (int i = 0; i < browsers.length; i ++) {
try {
Runtime.getRuntime().exec(new String[] { browsers[i], url });
break;
return true;
} catch (IOException e) {
// Ignore
e.printStackTrace(System.err);
}
}
// Seems to have failed
return false;
}

View file

@ -36,7 +36,7 @@ import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -60,6 +60,9 @@ public final class GLContext {
/** A map of WeakReferences to contexts to LWJGL pointers-to-extension-structs */
private static final Map contextMap = new HashMap();
/** A map of WeakReferences to contents to Sets of extension names */
private static final Map extensionsMap = new HashMap();
/*
* Available extensions
*/
@ -132,9 +135,6 @@ public final class GLContext {
public static boolean GL_ATI_vertex_streams;
public static boolean GL_ATI_separate_stencil;
public static boolean GL_ATIX_point_sprites;
public static boolean GL_ATIX_texture_env_route;
public static boolean GL_NV_blend_square;
public static boolean GL_NV_copy_depth_to_color;
public static boolean GL_NV_depth_clamp;
@ -177,25 +177,6 @@ public final class GLContext {
System.loadLibrary(Sys.getLibraryName());
}
private static void setExtensionFields(Set exts, HashMap field_map) {
Sys.log("Available extensions:");
Iterator it = exts.iterator();
while ( it.hasNext() ) {
String ext = (String)it.next();
Sys.log(ext);
Field f = (Field)field_map.get(ext);
if ( f != null ) {
try {
f.setBoolean(GLContext.class, true);
} catch (IllegalAccessException e) {
e.printStackTrace(System.err);
}
}
}
}
/**
* Determine which extensions are available. Use this to initialize capability fields. Can only be
* called _after_ a GLWindow or Pbuffer has been created (or a context from some other GL library).
@ -204,23 +185,19 @@ public final class GLContext {
*
* @param exts A Set of OpenGL extension string names
*/
public static void determineAvailableExtensions(Set exts) {
private static void determineAvailableExtensions(Set exts) {
// Grab all the public static booleans out of this class
Field[] fields = GLContext.class.getDeclaredFields();
HashMap map = new HashMap(fields.length);
for ( int i = 0; i < fields.length; i++ ) {
if ( Modifier.isStatic(fields[i].getModifiers()) && fields[i].getType() == boolean.class ) {
map.put(fields[i].getName(), fields[i]);
// reset fields
try {
fields[i].setBoolean(GLContext.class, false);
fields[i].setBoolean(GLContext.class, exts.contains(fields[i].getName()));
} catch (IllegalAccessException e) {
e.printStackTrace(System.err);
}
}
}
setExtensionFields(exts, map);
}
/**
@ -248,27 +225,30 @@ public final class GLContext {
// Look in the context map to see if we've encountered this context before
Integer encountered = (Integer) contextMap.get(currentContext);
Set exts;
if (encountered != null) {
exts = (Set) extensionsMap.get(currentContext);
reinit(encountered.intValue());
} else {
contextMap.put(currentContext, new Integer(init()));
exts = new HashSet();
contextMap.put(currentContext, new Integer(init(exts)));
}
// Now determine the available extensions
determineAvailableExtensions(exts);
}
/**
* Native method to initialize a context from scratch or load its function pointers from a
* cache.
* @param exts An empty Set of Strings that will be filled with the names of enabled extensions
* @return a LWJGL context-index-pointer
*/
private static native int init();
private static native int init(Set exts);
/**
* Native method to re-initialize a context.
* @param context Hash code of the context object
*/
private static native int reinit(int context);
private static native void reinit(int context);
}

View file

@ -308,16 +308,13 @@ public final class Window {
int alpha,
int depth,
int stencil,
int samples,
HashSet extensions)
int samples)
throws Exception;
private static void createWindow(int bpp, int alpha, int depth, int stencil, int samples) throws Exception {
HashSet extensions = new HashSet();
nCreate(title, x, y, width, height, fullscreen, bpp, alpha, depth, stencil, samples, extensions);
nCreate(title, x, y, width, height, fullscreen, bpp, alpha, depth, stencil, samples);
context = new Window();
makeCurrent();
GLContext.determineAvailableExtensions(extensions);
}
/**

View file

@ -54,10 +54,10 @@ public class SysTest {
* Runs the tests
*/
public void executeTest() {
testDebug();
testAlert();
testDebug();
testTimer();
testPriority();
testAlert();
testUrl();
}