Linux support for AWTGLCanvas. Big refactor of context specific code into Context.java that is now shared between Display, Pbuffer and AWTGLCanvas. (Win32 and Mac OS X is now broken while I implement the same changes on those platforms)

This commit is contained in:
Elias Naur 2005-02-20 11:24:22 +00:00
parent 8feec32e60
commit fb5d89599b
47 changed files with 2598 additions and 653 deletions

View file

@ -272,10 +272,10 @@ public class Game {
* Sets the display mode for fullscreen mode
*/
private boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(width, height, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(width, height, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + width,
"height=" + height,

View file

@ -64,6 +64,8 @@ public class Cursor {
/** Index into list of cursors */
private int index = 0;
private boolean destroyed;
/**
* Constructs a new Cursor, with the given parameters. Mouse must have been created before you can create
@ -219,15 +221,23 @@ public class Cursor {
* Gets the native handle associated with the cursor object.
*/
Object getHandle() {
checkValid();
return cursors[index].cursorHandle;
}
private void checkValid() {
if (destroyed)
throw new IllegalStateException("The cursor is destroyed");
}
/**
* Destroy the native cursor. If the cursor is current,
* the current native cursor is set to null (the default
* OS cursor)
*/
public void destroy() {
if (destroyed)
return;
if (Mouse.getNativeCursor() == this) {
try {
Mouse.setNativeCursor(null);
@ -238,12 +248,14 @@ public class Cursor {
for(int i=0; i<cursors.length; i++) {
Display.getImplementation().destroyCursor(cursors[i].cursorHandle);
}
destroyed = true;
}
/**
* Sets the timout property to the time it should be changed
*/
protected void setTimeout() {
checkValid();
cursors[index].timeout = System.currentTimeMillis() + cursors[index].delay;
}
@ -252,6 +264,7 @@ public class Cursor {
* @return true if the this cursor has timed out, false if not
*/
protected boolean hasTimedOut() {
checkValid();
return cursors.length > 1 && cursors[index].timeout < System.currentTimeMillis();
}
@ -259,6 +272,7 @@ public class Cursor {
* Changes to the next cursor
*/
protected void nextCursor() {
checkValid();
index = ++index % cursors.length;
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsConfiguration;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
interface AWTCanvasImplementation {
/**
* Return an opaque handle to the canvas peer information required to create a context from it.
*/
public PeerInfo createPeerInfo(AWTGLCanvas canvas) throws LWJGLException;
/**
* Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat.
*
* @return A GraphicsConfiguration matching the given GraphicsConfiguration and PixelFormat.
* @throws LWJGLException if no suitable configuration could be found.
*/
public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException;
}

View file

@ -33,6 +33,8 @@ package org.lwjgl.opengl;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
@ -45,37 +47,88 @@ import org.lwjgl.Sys;
* @version $Revision$
* @author $Author$
*/
public class AWTGLCanvas extends Canvas {
public class AWTGLCanvas extends Canvas implements Drawable {
private final static AWTCanvasImplementation implementation;
static {
System.loadLibrary("jawt");
Sys.initialize();
String class_name;
String OS_NAME = System.getProperty("os.name");
if (OS_NAME.startsWith("Linux")) {
class_name = "org.lwjgl.opengl.LinuxCanvasImplementation";
} else if (OS_NAME.startsWith("Windows")) {
class_name = "org.lwjgl.opengl.DefaultCanvasImplementation";
} else if (OS_NAME.startsWith("Mac")) {
class_name = "org.lwjgl.opengl.DefaultCanvasImplementation";
} else
throw new IllegalStateException("The platform " + OS_NAME + " is not supported");
try {
Class impl_class = Class.forName(class_name);
implementation = (AWTCanvasImplementation)impl_class.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
/** The requested pixel format */
private PixelFormat pixelFormat;
private final PeerInfo peer_info;
/** The drawable to share context with */
private final Drawable drawable;
/** Context handle */
private long context;
private Context context;
/**
* This method should only be called internally.
*/
public Context getContext() {
return context;
}
/**
* Constructor using the default PixelFormat.
*/
public AWTGLCanvas() {
public AWTGLCanvas() throws LWJGLException {
this(new PixelFormat());
}
/**
* Create an AWTGLCanvas with the requested PixelFormat. Construction is always
* successful, however, when the time comes to actually realise the component on the
* screen
* Create an AWTGLCanvas with the requested PixelFormat on the default GraphicsDevice.
*
* @param pixelFormat The desired pixel format. May not be null
* @param device the device to create the canvas on.
*/
public AWTGLCanvas(PixelFormat pixelFormat) {
if (pixelFormat == null) {
throw new IllegalArgumentException("Pixel format may not be null");
}
this.pixelFormat = pixelFormat;
public AWTGLCanvas(PixelFormat pixel_format) throws LWJGLException {
this(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(), pixel_format);
}
/**
* Create an AWTGLCanvas with the requested PixelFormat on the default GraphicsDevice.
*
* @param pixelFormat The desired pixel format. May not be null
* @param device the device to create the canvas on.
*/
public AWTGLCanvas(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException {
this(device, pixel_format, null);
}
/**
* Create an AWTGLCanvas with the requested PixelFormat on the specified GraphicsDevice.
*
* @param device the device to create the canvas on.
* @param pixelFormat The desired pixel format. May not be null
* @param shared_drawable The Drawable to share context with
*/
public AWTGLCanvas(GraphicsDevice device, PixelFormat pixel_format, Drawable drawable) throws LWJGLException {
super(implementation.findConfiguration(device, pixel_format));
this.peer_info = implementation.createPeerInfo(this);
this.drawable = drawable;
}
/* (non-Javadoc)
@ -94,64 +147,74 @@ public class AWTGLCanvas extends Canvas {
* @see java.awt.Component#removeNotify()
*/
public void removeNotify() {
super.removeNotify();
try {
destroyContext();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
super.removeNotify();
}
/**
* Enable vsync
*/
public synchronized void setVSyncEnabled(boolean enabled) throws LWJGLException {
if (context == null)
throw new IllegalStateException("Canvas not yet displayable");
context.setVSync(enabled);
}
/**
* Swap the canvas' buffer
*/
public synchronized void swapBuffers() throws LWJGLException {
if (context == null)
throw new IllegalStateException("Canvas not yet displayable");
context.swapBuffers();
}
public synchronized void releaseContext() throws LWJGLException {
if (context == null)
throw new IllegalStateException("Canvas not yet displayable");
if (context.isCurrent())
Context.releaseCurrentContext();
}
/**
* Make the canvas' context current. It is highly recommended that the context
* is only made current inside the AWT thread (for example in an overridden paint()).
*/
public synchronized void makeCurrent() throws LWJGLException {
if (context == null)
throw new IllegalStateException("Canvas not yet displayable");
context.makeCurrent();
}
/**
* Create the OpenGL context. This occurs when the component becomes displayable
* @throws LWJGLException
*/
private synchronized void createContext() throws LWJGLException {
nCreateContext();
if (context == null)
context = new Context(peer_info, drawable != null ? drawable.getContext() : null);
}
private native void nCreateContext() throws LWJGLException;
/**
* Destroy the OpenGL context. This occurs when the component is no longer displayable.
* Destroy the OpenGL context. This happens when the component becomes undisplayable
*/
private synchronized void destroyContext() throws LWJGLException {
nDestroyContext();
context.forceDestroy();
context = null;
}
private native void nDestroyContext() throws LWJGLException;
/* (non-Javadoc)
* @see java.awt.Canvas#paint(java.awt.Graphics)
*/
public synchronized final void paint(Graphics g) {
try {
nPaint();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private native void nPaint() throws Exception;
/**
* Paint callback from native code
* Empty paint to avoid clearing
*/
private final void cPaint() {
try {
GLContext.useContext(this);
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
doPaint();
public void paint(Graphics g) {
}
/**
* Do painting. Override this method to call GL commands.
*/
protected void doPaint() {
}
/* (non-Javadoc)
* @see java.awt.Canvas#update(java.awt.Graphics)
* override update to avoid clearing
*/
public void update(Graphics g) {
paint(g);

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import java.awt.Canvas;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class AWTSurfaceLock {
private final static int LOCK_HANDLE_SIZE = 64;
private final ByteBuffer lock_buffer = BufferUtils.createByteBuffer(LOCK_HANDLE_SIZE);
public ByteBuffer lockAndGetHandle(Canvas canvas) throws LWJGLException {
lockAndInitHandle(lock_buffer, canvas);
return lock_buffer;
}
private static native void lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException;
protected void unlock() throws LWJGLException {
nUnlock(lock_buffer);
}
private static native void nUnlock(ByteBuffer lock_buffer) throws LWJGLException;
}

View file

@ -0,0 +1,230 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
/**
* $Id$
* <p/>
* Context encapsulates an OpenGL context.
* <p/>
*
* This class is thread-safe.
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class Context {
/**
* The platform specific implementation of context methods
*/
private final static ContextImplementation implementation;
/** The current Context */
private final static ThreadLocal current_context_local = new ThreadLocal();
/**
* Handle to the native GL rendering context
*/
private final ByteBuffer handle;
private final PeerInfo peer_info;
/** Whether the context has been destroyed */
private boolean destroyed;
private boolean destroy_requested;
/** The thread that has this context current, or null. */
private Thread thread;
static {
Sys.initialize();
String class_name;
String OS_NAME = System.getProperty("os.name");
if (OS_NAME.startsWith("Linux")) {
class_name = "org.lwjgl.opengl.LinuxContextImplementation";
} else if (OS_NAME.startsWith("Windows")) {
class_name = "org.lwjgl.opengl.Win32ContextImplementation";
} else if (OS_NAME.startsWith("Mac")) {
class_name = "org.lwjgl.opengl.MacOSXContextImplementation";
} else
throw new IllegalStateException("The platform " + OS_NAME + " is not supported");
try {
Class impl_class = Class.forName(class_name);
implementation = (ContextImplementation)impl_class.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
PeerInfo getPeerInfo() {
return peer_info;
}
static Context getCurrentContext() {
return (Context)current_context_local.get();
}
/**
* Create a context with the specified peer info and shared context
*/
public Context(PeerInfo peer_info, Context shared_context) throws LWJGLException {
Context context_lock = shared_context != null ? shared_context : this;
// If shared_context is not null, synchronize on it to make sure it is not deleted
// while this context is created. Otherwise, simply synchronize on ourself to avoid NPE
synchronized (context_lock) {
if (shared_context != null && shared_context.destroyed)
throw new IllegalArgumentException("Shared context is destroyed");
GLContext.loadOpenGLLibrary();
try {
this.peer_info = peer_info;
this.handle = implementation.create(peer_info, shared_context != null ? shared_context.handle : null);
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
throw e;
}
}
}
/**
* Release the current context (if any). After this call, no context is current.
*/
public static void releaseCurrentContext() throws LWJGLException {
Context current_context = getCurrentContext();
if (current_context != null) {
implementation.releaseCurrentContext();
GLContext.useContext(null);
current_context_local.set(null);
synchronized (current_context) {
current_context.thread = null;
current_context.checkDestroy();
}
}
}
/**
* Swap the buffers on the current context. Only valid for double-buffered contexts
*/
public static void swapBuffers() throws LWJGLException {
implementation.swapBuffers();
}
private boolean canAccess() {
return thread == null || Thread.currentThread() == thread;
}
private void checkAccess() {
if (!canAccess())
throw new IllegalStateException("From thread " + Thread.currentThread() + ": " +thread + " already has the context current");
}
/**
* Make the context current
*/
public synchronized void makeCurrent() throws LWJGLException {
checkAccess();
if (destroyed)
throw new IllegalStateException("Context is destroyed");
thread = Thread.currentThread();
current_context_local.set(this);
implementation.makeCurrent(peer_info, handle);
GLContext.useContext(this);
}
/**
* Query whether the context is current
*/
public synchronized boolean isCurrent() throws LWJGLException {
if (destroyed)
throw new IllegalStateException("Context is destroyed");
return implementation.isCurrent(handle);
}
private void checkDestroy() {
if (!destroyed && destroy_requested) {
try {
implementation.destroy(peer_info, handle);
destroyed = true;
thread = null;
GLContext.unloadOpenGLLibrary();
} catch (LWJGLException e) {
Sys.log("Exception occurred while destroying context: " + e);
}
}
}
/**
* Enable or disable vertical monitor synchronization. This call is a best-attempt at changing
* the monitor vertical refresh synchronization of the context, and is not guaranteed to be successful.
* @param sync true to synchronize; false to ignore synchronization
*/
public synchronized void setVSync(boolean enable) {
implementation.setVSync(enable);
}
/**
* Destroy the context. This method behaves the same as destroy() with the extra
* requirement that the context must be either current to the current thread or not
* current at all.
*/
public synchronized void forceDestroy() throws LWJGLException {
checkAccess();
destroy();
}
/**
* Request destruction of the Context. If the context is current, no context will be current after this call.
* The context is destroyed when no thread has it current.
*/
public synchronized void destroy() throws LWJGLException {
if (destroyed)
return;
destroy_requested = true;
boolean was_current = isCurrent();
int error = GL11.GL_NO_ERROR;
if (was_current) {
error = GL11.glGetError();
releaseCurrentContext();
}
checkDestroy();
if (was_current && error != GL11.GL_NO_ERROR)
throw new OpenGLException(error);
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
/**
* $Id$
* <p/>
* Context implementation interface.
* <p/>
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
interface ContextImplementation {
/**
* Create a context.
*/
public ByteBuffer create(PeerInfo peer_info, ByteBuffer shared_context_handle) throws LWJGLException;
/**
* Swap the buffers of the current context. Only valid for double-buffered contexts.
*/
public void swapBuffers() throws LWJGLException;
/**
* Release the current context (if any). After this call, no context is current.
*/
public void releaseCurrentContext() throws LWJGLException;
/**
* Query whether the context is current
*/
public void makeCurrent(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException;
/**
* Query whether the context is current
*/
public boolean isCurrent(ByteBuffer handle) throws LWJGLException;
public void setVSync(boolean enable);
/**
* Destroys the Context.
*/
public void destroy(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException;
}

View file

@ -86,20 +86,41 @@ public final class Display {
private static boolean vsync;
/** A unique context object, so we can track different contexts between creates() and destroys() */
private static Display context;
private static Context context;
private static boolean window_created = false;
static {
Sys.initialize();
display_impl = createDisplayImplementation();
current_mode = initial_mode = display_impl.init();
Sys.log("Initial mode: " + initial_mode);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
reset();
}
});
try {
current_mode = initial_mode = display_impl.init();
Sys.log("Initial mode: " + initial_mode);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
reset();
}
});
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
/**
* Fetch the Drawable from the Display.
*
* @return the Drawable corresponding to the Display context, or null it display is
* not created.
*/
public static Drawable getDrawable() {
if (context != null) {
return new Drawable() {
public Context getContext() {
return context;
}
};
} else
return null;
}
private static DisplayImplementation createDisplayImplementation() {
@ -141,7 +162,7 @@ public final class Display {
*
* @return an array of all display modes the system reckons it can handle.
*/
public static DisplayMode[] getAvailableDisplayModes() {
public static DisplayMode[] getAvailableDisplayModes() throws LWJGLException {
DisplayMode[] unfilteredModes = display_impl.getAvailableDisplayModes();
if (unfilteredModes == null) {
@ -192,7 +213,7 @@ public final class Display {
switchDisplayMode();
createWindow();
} catch (LWJGLException e) {
display_impl.destroyContext();
destroyContext();
display_impl.resetDisplayMode();
throw e;
}
@ -385,7 +406,7 @@ public final class Display {
}
createWindow();
} catch (LWJGLException e) {
display_impl.destroyContext();
destroyContext();
display_impl.resetDisplayMode();
throw e;
}
@ -483,7 +504,11 @@ public final class Display {
// We paint only when the window is visible or dirty
if (isVisible() || isDirty()) {
Util.checkGLError();
display_impl.swapBuffers();
try {
context.swapBuffers();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
processMessages();
@ -505,8 +530,7 @@ public final class Display {
public static void makeCurrent() throws LWJGLException {
if (!isCreated())
throw new IllegalStateException("No window created to make current");
display_impl.makeCurrent();
GLContext.useContext(context);
context.makeCurrent();
}
/**
@ -537,25 +561,35 @@ public final class Display {
* @throws LWJGLException
*/
public static void create(PixelFormat pixel_format) throws LWJGLException {
create(pixel_format, null);
}
/**
* Create the OpenGL context with the given minimum parameters. If isFullscreen() is true or if windowed
* context are not supported on the platform, the display mode will be switched to the mode returned by
* getDisplayMode(), and a fullscreen context will be created. If isFullscreen() is false, a windowed context
* will be created with the dimensions given in the mode returned by getDisplayMode(). If a context can't be
* created with the given parameters, a LWJGLException will be thrown.
*
* <p>The window created will be set up in orthographic 2D projection, with 1:1 pixel ratio with GL coordinates.
*
* @param pixel_format Describes the minimum specifications the context must fulfill.
* @param shared_drawable The Drawable to share context with.
* @throws LWJGLException
*/
public static void create(PixelFormat pixel_format, Drawable shared_drawable) throws LWJGLException {
if (isCreated())
throw new IllegalStateException("Only one LWJGL context may be instantiated at any one time.");
if (fullscreen)
switchDisplayMode();
try {
GLContext.loadOpenGLLibrary();
PeerInfo peer_info = display_impl.createPeerInfo(pixel_format);
context = new Context(peer_info, shared_drawable != null ? shared_drawable.getContext() : null);
try {
display_impl.createContext(pixel_format);
try {
context = new Display();
createWindow();
initContext();
} catch (LWJGLException e) {
display_impl.destroyContext();
context = null;
throw e;
}
createWindow();
initContext();
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
destroyContext();
throw e;
}
} catch (LWJGLException e) {
@ -617,18 +651,22 @@ public final class Display {
}
destroyWindow();
display_impl.destroyContext();
GLContext.unloadOpenGLLibrary();
context = null;
x = y = -1;
try {
GLContext.useContext(null);
} catch (LWJGLException e) {
Sys.log("Failed to reset GLContext due to: " + e);
}
destroyContext();
x = y = -1;
reset();
}
private static void destroyContext() {
try {
context.forceDestroy();
} catch (LWJGLException e) {
throw new RuntimeException(e);
} finally {
context = null;
display_impl.destroyPeerInfo();
}
}
/*
* Reset display mode if fullscreen. This method is also called from the shutdown hook added
* in the static constructor
@ -641,7 +679,7 @@ public final class Display {
/**
* @return the unique Display context (or null, if the Display has not been created)
*/
public static Object getContext() {
public static Context getContext() {
return context;
}
@ -660,7 +698,7 @@ public final class Display {
public static void setVSyncEnabled(boolean sync) {
vsync = sync;
if (isCreated())
display_impl.setVSyncEnabled(vsync);
context.setVSync(vsync);
}
/**

View file

@ -87,7 +87,7 @@ public interface DisplayImplementation {
/**
* Initialize and return the current display mode.
*/
DisplayMode init();
DisplayMode init() throws LWJGLException;
/**
* Implementation of setTitle(). This will read the window's title member
@ -103,22 +103,12 @@ public interface DisplayImplementation {
boolean isDirty();
/**
* Swap double buffers.
*/
void swapBuffers();
/**
* Make the window the current rendering context for GL calls.
*/
void makeCurrent() throws LWJGLException;
/**
* Create the native OpenGL context.
* Create the native PeerInfo.
* @throws LWJGLException
*/
void createContext(PixelFormat pixel_format) throws LWJGLException;
PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException;
void destroyContext();
void destroyPeerInfo();
/**
* Updates the windows internal state. This must be called at least once per video frame
@ -126,14 +116,12 @@ public interface DisplayImplementation {
*/
void update();
void setVSyncEnabled(boolean sync);
void reshape(int x, int y, int width, int height);
/**
* Method for getting displaymodes
*/
DisplayMode[] getAvailableDisplayModes();
DisplayMode[] getAvailableDisplayModes() throws LWJGLException;
/*
* Mouse methods
@ -147,7 +135,7 @@ public interface DisplayImplementation {
/**
* Method to create the mouse.
*/
void createMouse();
void createMouse() throws LWJGLException;
/**
* Method the destroy the mouse
@ -224,28 +212,23 @@ public interface DisplayImplementation {
/**
* Method to test for buffer integrity
*/
public boolean isBufferLost(ByteBuffer handle);
/**
* Method to make a pbuffer current.
*/
public void makePbufferCurrent(ByteBuffer handle) throws LWJGLException;
public boolean isBufferLost(PeerInfo handle);
/**
* Method to create a Pbuffer
*/
public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException;
IntBuffer pBufferAttribs) throws LWJGLException;
/**
* Destroy pbuffer
*/
public void destroyPbuffer(ByteBuffer handle);
public void destroyPbuffer(PeerInfo handle);
public void setPbufferAttrib(ByteBuffer handle, int attrib, int value);
public void setPbufferAttrib(PeerInfo handle, int attrib, int value);
public void bindTexImageToPbuffer(ByteBuffer handle, int buffer);
public void bindTexImageToPbuffer(PeerInfo handle, int buffer);
public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);
public void releaseTexImageFromPbuffer(PeerInfo handle, int buffer);
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
/**
* The Drawable interface describes an OpenGL drawable with an associated
* Context.
*
* @author elias_naur
*/
public interface Drawable {
Context getContext();
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class LinuxAWTGLCanvasPeerInfo extends LinuxPeerInfo {
private final static int LOCK_HANDLE_SIZE = 64;
private final ByteBuffer lock_buffer = BufferUtils.createByteBuffer(LOCK_HANDLE_SIZE);
private final AWTGLCanvas canvas;
private final AWTSurfaceLock awt_surface = new AWTSurfaceLock();
public LinuxAWTGLCanvasPeerInfo(AWTGLCanvas canvas) {
this.canvas = canvas;
}
protected void doLockAndInitHandle() throws LWJGLException {
int screen = LinuxCanvasImplementation.getScreenFromDevice(canvas.getGraphicsConfiguration().getDevice());
nInitHandle(screen, awt_surface.lockAndGetHandle(canvas), getHandle());
}
private static native void nInitHandle(int screen, ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException;
protected void doUnlock() throws LWJGLException {
awt_surface.unlock();
}
}

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.BufferUtils;
import java.awt.GraphicsDevice;
import java.awt.GraphicsConfiguration;
/*
* Import the needed SUN specific classes. They
* are needed since there is no official way to access
* the screen and visual from GraphicsDevice and
* GraphicsConfiguration respectively.
*/
import sun.awt.X11GraphicsDevice;
import sun.awt.X11GraphicsConfig;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class LinuxCanvasImplementation implements AWTCanvasImplementation {
static int getScreenFromDevice(GraphicsDevice device) {
X11GraphicsDevice x11_device = (X11GraphicsDevice)device;
return x11_device.getScreen();
}
private static int getVisualIDFromConfiguration(GraphicsConfiguration configuration) {
X11GraphicsConfig x11_config = (X11GraphicsConfig)configuration;
return x11_config.getVisual();
}
public PeerInfo createPeerInfo(AWTGLCanvas canvas) throws LWJGLException {
return new LinuxAWTGLCanvasPeerInfo(canvas);
}
/**
* Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat.
*
* @return The GraphicsConfiguration corresponding to a visual that matches the pixel format.
*/
public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException {
X11GraphicsDevice x11_device = (X11GraphicsDevice)device;
int screen = getScreenFromDevice(device);
int visual_id_matching_format = findVisualIDFromFormat(screen, pixel_format);
GraphicsConfiguration[] configurations = device.getConfigurations();
for (int i = 0; i < configurations.length; i++) {
int visual_id = getVisualIDFromConfiguration(configurations[i]);
if (visual_id == visual_id_matching_format)
return configurations[i];
}
throw new LWJGLException("Could not find the matching GraphicsConfiguration to visual id");
}
private static int findVisualIDFromFormat(int screen, PixelFormat pixel_format) throws LWJGLException {
try {
LinuxDisplay.lockAWT();
try {
GLContext.loadOpenGLLibrary();
try {
LinuxDisplay.incDisplay();
return nFindVisualIDFromFormat(screen, pixel_format);
} finally {
LinuxDisplay.decDisplay();
}
} finally {
GLContext.unloadOpenGLLibrary();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native int nFindVisualIDFromFormat(int screen, PixelFormat pixel_format) throws LWJGLException;
}

View file

@ -0,0 +1,153 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.BufferUtils;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class LinuxContextImplementation implements ContextImplementation {
private final static int HANDLE_SIZE = 64;
private static PeerInfo getCurrentPeerInfo() {
return Context.getCurrentContext().getPeerInfo();
}
public ByteBuffer create(PeerInfo peer_info, ByteBuffer shared_context_handle) throws LWJGLException {
ByteBuffer handle = BufferUtils.createByteBuffer(HANDLE_SIZE);
LinuxDisplay.lockAWT();
try {
ByteBuffer peer_handle = peer_info.lockAndGetHandle();
try {
nCreate(peer_handle, handle, shared_context_handle);
return handle;
} finally {
peer_info.unlock();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native void nCreate(ByteBuffer peer_handle, ByteBuffer context_handle, ByteBuffer shared_context_handle) throws LWJGLException;
public void swapBuffers() throws LWJGLException {
PeerInfo current_peer_info = getCurrentPeerInfo();
if (current_peer_info == null)
throw new IllegalStateException("No context is current");
LinuxDisplay.lockAWT();
try {
ByteBuffer peer_handle = current_peer_info.lockAndGetHandle();
try {
nSwapBuffers(peer_handle);
} finally {
current_peer_info.unlock();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native void nSwapBuffers(ByteBuffer peer_info_handle) throws LWJGLException;
public void releaseCurrentContext() throws LWJGLException {
PeerInfo current_peer_info = getCurrentPeerInfo();
if (current_peer_info == null)
return; // No context is current
LinuxDisplay.lockAWT();
try {
ByteBuffer peer_handle = current_peer_info.lockAndGetHandle();
try {
nReleaseCurrentContext(peer_handle);
} finally {
current_peer_info.unlock();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native void nReleaseCurrentContext(ByteBuffer peer_info_handle) throws LWJGLException;
public void makeCurrent(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException {
LinuxDisplay.lockAWT();
try {
ByteBuffer peer_handle = peer_info.lockAndGetHandle();
try {
nMakeCurrent(peer_handle, handle);
} finally {
peer_info.unlock();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native void nMakeCurrent(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException;
public boolean isCurrent(ByteBuffer handle) throws LWJGLException {
LinuxDisplay.lockAWT();
try {
boolean result = nIsCurrent(handle);
return result;
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native boolean nIsCurrent(ByteBuffer context_handle) throws LWJGLException;
public void setVSync(boolean enabled) {
LinuxDisplay.lockAWT();
nSetVSync(enabled);
LinuxDisplay.unlockAWT();
}
private static native void nSetVSync(boolean enabled);
public void destroy(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException {
LinuxDisplay.lockAWT();
try {
ByteBuffer peer_handle = peer_info.lockAndGetHandle();
try {
nDestroy(peer_handle, handle);
} finally {
peer_info.unlock();
}
} finally {
LinuxDisplay.unlockAWT();
}
}
private static native void nDestroy(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException;
}

View file

@ -42,6 +42,7 @@ import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.Sys;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
@ -51,41 +52,62 @@ final class LinuxDisplay implements DisplayImplementation {
private static final int PBUFFER_HANDLE_SIZE = 24;
private static final int NUM_BUTTONS = 3;
private static PeerInfo peer_info;
/* Since Xlib is not guaranteed to be thread safe, we need a way to synchronize LWJGL
* Xlib calls with AWT Xlib calls. Fortunately, JAWT implements LockAWT and UnlockAWT(), to
* Xlib calls with AWT Xlib calls. Fortunately, JAWT implements LockAWT and UnlockAWT() to
* do just that.
*/
private native void lockAWT();
private native void unlockAWT();
static native void lockAWT();
static native void unlockAWT();
/**
* increment and decrement display usage.
*/
static native void incDisplay() throws LWJGLException;
static native void decDisplay();
public void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException {
lockAWT();
nCreateWindow(mode, fullscreen, x, y);
unlockAWT();
try {
ByteBuffer handle = peer_info.lockAndGetHandle();
try {
nCreateWindow(handle, mode, fullscreen, x, y);
} finally {
peer_info.unlock();
}
} finally {
unlockAWT();
}
}
public native void nCreateWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
private static native void nCreateWindow(ByteBuffer peer_info_handle, DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException;
public void destroyWindow() {
lockAWT();
nDestroyWindow();
unlockAWT();
}
public native void nDestroyWindow();
private static native void nDestroyWindow();
public void switchDisplayMode(DisplayMode mode) throws LWJGLException {
lockAWT();
nSwitchDisplayMode(mode);
unlockAWT();
try {
nSwitchDisplayMode(mode);
} finally {
unlockAWT();
}
}
public native void nSwitchDisplayMode(DisplayMode mode) throws LWJGLException;
private static native void nSwitchDisplayMode(DisplayMode mode) throws LWJGLException;
public void resetDisplayMode() {
lockAWT();
nResetDisplayMode();
unlockAWT();
try {
nResetDisplayMode();
} finally {
unlockAWT();
}
}
public native void nResetDisplayMode();
private static native void nResetDisplayMode();
public int getGammaRampLength() {
lockAWT();
@ -93,126 +115,130 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return length;
}
public native int nGetGammaRampLength();
private static native int nGetGammaRampLength();
public void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException {
lockAWT();
nSetGammaRamp(gammaRamp);
unlockAWT();
try {
nSetGammaRamp(gammaRamp);
} finally {
unlockAWT();
}
}
public native void nSetGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
private static native void nSetGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
public String getAdapter() {
return null;
}
public String getVersion() {
return null;
}
public DisplayMode init() {
public DisplayMode init() throws LWJGLException {
lockAWT();
DisplayMode mode = nInit();
unlockAWT();
return mode;
try {
DisplayMode mode = nInit();
return mode;
} finally {
unlockAWT();
}
}
public native DisplayMode nInit();
private static native DisplayMode nInit() throws LWJGLException;
public void setTitle(String title) {
lockAWT();
nSetTitle(title);
unlockAWT();
}
public native void nSetTitle(String title);
private static native void nSetTitle(String title);
public boolean isCloseRequested() {
lockAWT();
boolean result = nIsCloseRequested();
unlockAWT();
return result;
}
public native boolean nIsCloseRequested();
private static native boolean nIsCloseRequested();
public boolean isVisible() {
lockAWT();
boolean result = nIsVisible();
unlockAWT();
return result;
}
public native boolean nIsVisible();
private static native boolean nIsVisible();
public boolean isActive() {
lockAWT();
boolean result = nIsActive();
unlockAWT();
return result;
}
public native boolean nIsActive();
private static native boolean nIsActive();
public boolean isDirty() {
lockAWT();
boolean result = nIsDirty();
unlockAWT();
return result;
}
public native boolean nIsDirty();
public void swapBuffers() {
lockAWT();
nSwapBuffers();
unlockAWT();
}
public native void nSwapBuffers();
private static native boolean nIsDirty();
public void makeCurrent() throws LWJGLException {
public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException {
lockAWT();
nMakeCurrent();
unlockAWT();
try {
incDisplay();
try {
GLContext.loadOpenGLLibrary();
try {
peer_info = new LinuxDisplayPeerInfo(pixel_format);
return peer_info;
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
throw e;
}
} catch (LWJGLException e) {
decDisplay();
throw e;
}
} finally {
unlockAWT();
}
}
public native void nMakeCurrent() throws LWJGLException;
public void createContext(PixelFormat pixel_format) throws LWJGLException {
lockAWT();
nCreateContext(pixel_format);
unlockAWT();
}
public native void nCreateContext(PixelFormat pixel_format) throws LWJGLException;
public void destroyContext() {
public void destroyPeerInfo() {
lockAWT();
nDestroyContext();
peer_info = null;
GLContext.unloadOpenGLLibrary();
decDisplay();
unlockAWT();
}
public native void nDestroyContext();
public void update() {
lockAWT();
nUpdate();
unlockAWT();
}
public native void nUpdate();
public void setVSyncEnabled(boolean sync) {
lockAWT();
nSetVSyncEnabled(sync);
unlockAWT();
}
public native void nSetVSyncEnabled(boolean sync);
private static native void nUpdate();
public void reshape(int x, int y, int width, int height) {
lockAWT();
nReshape(x, y, width, height);
unlockAWT();
}
public native void nReshape(int x, int y, int width, int height);
private static native void nReshape(int x, int y, int width, int height);
public DisplayMode[] getAvailableDisplayModes() {
public DisplayMode[] getAvailableDisplayModes() throws LWJGLException {
lockAWT();
DisplayMode[] modes = nGetAvailableDisplayModes();
unlockAWT();
return modes;
try {
DisplayMode[] modes = nGetAvailableDisplayModes();
return modes;
} finally {
unlockAWT();
}
}
public native DisplayMode[] nGetAvailableDisplayModes();
private static native DisplayMode[] nGetAvailableDisplayModes() throws LWJGLException;
/* Mouse */
public boolean hasWheel() {
@ -228,20 +254,20 @@ final class LinuxDisplay implements DisplayImplementation {
nCreateMouse();
unlockAWT();
}
public native void nCreateMouse();
private static native void nCreateMouse();
public void destroyMouse() {
lockAWT();
nDestroyMouse();
unlockAWT();
}
public native void nDestroyMouse();
private static native void nDestroyMouse();
public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons) {
lockAWT();
nPollMouse(coord_buffer, buttons);
unlockAWT();
}
public native void nPollMouse(IntBuffer coord_buffer, ByteBuffer buttons);
private static native void nPollMouse(IntBuffer coord_buffer, ByteBuffer buttons);
public int readMouse(IntBuffer buffer, int buffer_position) {
lockAWT();
@ -249,29 +275,36 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return count;
}
public native int nReadMouse(IntBuffer buffer, int buffer_position);
private static native int nReadMouse(IntBuffer buffer, int buffer_position);
public void grabMouse(boolean grab) {
lockAWT();
nGrabMouse(grab);
unlockAWT();
}
public native void nGrabMouse(boolean grab);
private static native void nGrabMouse(boolean grab);
public int getNativeCursorCapabilities() {
lockAWT();
int caps = nGetNativeCursorCapabilities();
unlockAWT();
return caps;
try {
incDisplay();
int caps = nGetNativeCursorCapabilities();
decDisplay();
return caps;
} catch (LWJGLException e) {
throw new RuntimeException(e);
} finally {
unlockAWT();
}
}
public native int nGetNativeCursorCapabilities();
private static native int nGetNativeCursorCapabilities() throws LWJGLException;
public void setNativeCursor(Object handle) throws LWJGLException {
lockAWT();
nSetNativeCursor(handle);
unlockAWT();
}
public native void nSetNativeCursor(Object handle) throws LWJGLException;
private static native void nSetNativeCursor(Object handle) throws LWJGLException;
public int getMinCursorSize() {
lockAWT();
@ -279,7 +312,7 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return min_size;
}
public native int nGetMinCursorSize();
private static native int nGetMinCursorSize();
public int getMaxCursorSize() {
lockAWT();
@ -287,29 +320,32 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return max_size;
}
public native int nGetMaxCursorSize();
private static native int nGetMaxCursorSize();
/* Keyboard */
public void createKeyboard() throws LWJGLException {
lockAWT();
nCreateKeyboard();
unlockAWT();
try {
nCreateKeyboard();
} finally {
unlockAWT();
}
}
public native void nCreateKeyboard() throws LWJGLException;
private static native void nCreateKeyboard() throws LWJGLException;
public void destroyKeyboard() {
lockAWT();
nDestroyKeyboard();
unlockAWT();
}
public native void nDestroyKeyboard();
private static native void nDestroyKeyboard();
public void pollKeyboard(ByteBuffer keyDownBuffer) {
lockAWT();
nPollKeyboard(keyDownBuffer);
unlockAWT();
}
public native void nPollKeyboard(ByteBuffer keyDownBuffer);
private static native void nPollKeyboard(ByteBuffer keyDownBuffer);
public int readKeyboard(IntBuffer buffer, int buffer_position) {
lockAWT();
@ -317,28 +353,38 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return count;
}
public native int nReadKeyboard(IntBuffer buffer, int buffer_position);
private static native int nReadKeyboard(IntBuffer buffer, int buffer_position);
public int isStateKeySet(int key) {
return Keyboard.STATE_UNKNOWN;
}
public native void nCreateCursor(ByteBuffer handle, int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException;
private static native void nCreateCursor(ByteBuffer handle, int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException;
public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
lockAWT();
ByteBuffer handle = BufferUtils.createByteBuffer(CURSOR_HANDLE_SIZE);
nCreateCursor(handle, width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1);
unlockAWT();
return handle;
try {
incDisplay();
try {
ByteBuffer handle = BufferUtils.createByteBuffer(CURSOR_HANDLE_SIZE);
nCreateCursor(handle, width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1);
return handle;
} catch (LWJGLException e) {
decDisplay();
throw e;
}
} finally {
unlockAWT();
}
}
public void destroyCursor(Object cursorHandle) {
lockAWT();
nDestroyCursor(cursorHandle);
decDisplay();
unlockAWT();
}
public native void nDestroyCursor(Object cursorHandle);
private static native void nDestroyCursor(Object cursorHandle);
public int getPbufferCapabilities() {
lockAWT();
@ -346,50 +392,87 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
return caps;
}
public native int nGetPbufferCapabilities();
private static native int nGetPbufferCapabilities();
public boolean isBufferLost(ByteBuffer handle) {
public boolean isBufferLost(PeerInfo handle) {
return false;
}
public void makePbufferCurrent(ByteBuffer handle) throws LWJGLException {
/* public void makePbufferCurrent(ByteBuffer handle) throws LWJGLException {
lockAWT();
nMakePbufferCurrent(handle);
unlockAWT();
try {
nMakePbufferCurrent(handle);
} finally {
unlockAWT();
}
}
public native void nMakePbufferCurrent(ByteBuffer handle) throws LWJGLException;
public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
private static native void nMakePbufferCurrent(ByteBuffer handle) throws LWJGLException;
*/
/* public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException {
lockAWT();
ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE);
nCreatePbuffer(handle, width, height, pixel_format, pixelFormatCaps, pBufferAttribs, shared_pbuffer_handle);
unlockAWT();
return handle;
try {
ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE);
incDisplay();
try {
nCreatePbuffer(handle, width, height, pixel_format, pixelFormatCaps, pBufferAttribs, shared_pbuffer_handle);
return handle;
} catch (LWJGLException e) {
decDisplay();
throw e;
}
} finally {
unlockAWT();
}
}
private native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format,
private static native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException;
public void destroyPbuffer(ByteBuffer handle) {
*/
public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs) throws LWJGLException {
lockAWT();
nDestroyPbuffer(handle);
try {
incDisplay();
try {
GLContext.loadOpenGLLibrary();
try {
PeerInfo peer_info = new LinuxPbufferPeerInfo(width, height, pixel_format);
return peer_info;
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
throw e;
}
} catch (LWJGLException e) {
decDisplay();
throw e;
}
} finally {
unlockAWT();
}
}
public void destroyPbuffer(PeerInfo handle) {
lockAWT();
((LinuxPbufferPeerInfo)handle).destroy();
decDisplay();
GLContext.unloadOpenGLLibrary();
unlockAWT();
}
public native void nDestroyPbuffer(ByteBuffer handle);
public void setPbufferAttrib(ByteBuffer handle, int attrib, int value) {
public void setPbufferAttrib(PeerInfo handle, int attrib, int value) {
throw new UnsupportedOperationException();
}
public void bindTexImageToPbuffer(ByteBuffer handle, int buffer) {
public void bindTexImageToPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer) {
public void releaseTexImageFromPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class LinuxDisplayPeerInfo extends LinuxPeerInfo {
public LinuxDisplayPeerInfo(PixelFormat pixel_format) throws LWJGLException {
LinuxDisplay.lockAWT();
initDefaultPeerInfo(getHandle(), pixel_format);
LinuxDisplay.unlockAWT();
}
private static native void initDefaultPeerInfo(ByteBuffer peer_info_handle, PixelFormat pixel_format) throws LWJGLException;
protected void doLockAndInitHandle() throws LWJGLException {
LinuxDisplay.lockAWT();
initDrawable(getHandle());
LinuxDisplay.unlockAWT();
}
private static native void initDrawable(ByteBuffer peer_info_handle);
protected void doUnlock() throws LWJGLException {
// NO-OP
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
abstract class LinuxPeerInfo extends PeerInfo {
private static final int PEER_HANDLE_SIZE = 64;
public LinuxPeerInfo() {
super(BufferUtils.createByteBuffer(PEER_HANDLE_SIZE));
}
}

View file

@ -157,11 +157,11 @@ final class MacOSXDisplay implements DisplayImplementation {
return new DisplayMode(awt_mode.getWidth(), awt_mode.getHeight(), bit_depth, refresh_rate);
}
public DisplayMode init() {
public DisplayMode init() throws LWJGLException {
return createLWJGLDisplayMode(MacOSXFrame.getDevice().getDisplayMode());
}
public DisplayMode[] getAvailableDisplayModes() {
public DisplayMode[] getAvailableDisplayModes() throws LWJGLException {
java.awt.DisplayMode[] awt_modes = MacOSXFrame.getDevice().getDisplayModes();
List modes = new ArrayList();
for (int i = 0; i < awt_modes.length; i++)
@ -199,13 +199,17 @@ final class MacOSXDisplay implements DisplayImplementation {
public native void setView(MacOSXGLCanvas canvas);
public native void swapBuffers();
// public native void swapBuffers();
public native void makeCurrent() throws LWJGLException;
// public native void makeCurrent() throws LWJGLException;
public native void createContext(PixelFormat pixel_format) throws LWJGLException;
public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException {
throw new RuntimeException("Not supported yet");
}
// public native void createContext(PixelFormat pixel_format) throws LWJGLException;
public native void destroyContext();
public native void destroyPeerInfo();
// public native void destroyContext();
public void update() {
if (frame.syncShouldUpdateContext()) {
@ -251,7 +255,7 @@ final class MacOSXDisplay implements DisplayImplementation {
return MouseEventQueue.NUM_BUTTONS;
}
public void createMouse() {
public void createMouse() throws LWJGLException {
MacOSXGLCanvas canvas = frame.getCanvas();
this.mouse_queue = new MouseEventQueue(canvas.getWidth(), canvas.getHeight());
canvas.addMouseListener(mouse_queue);
@ -409,13 +413,19 @@ final class MacOSXDisplay implements DisplayImplementation {
}
}
public boolean isBufferLost(ByteBuffer handle) {
public boolean isBufferLost(PeerInfo handle) {
return false;
}
public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException;
// public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException;
public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs) throws LWJGLException {
throw new RuntimeException("Not yet supported");
}
/* public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException {
ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE);
@ -426,18 +436,19 @@ final class MacOSXDisplay implements DisplayImplementation {
private native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException;
*/
// public native void destroyPbuffer(ByteBuffer handle);
public native void destroyPbuffer(PeerInfo handle);
public native void destroyPbuffer(ByteBuffer handle);
public void setPbufferAttrib(ByteBuffer handle, int attrib, int value) {
public void setPbufferAttrib(PeerInfo handle, int attrib, int value) {
throw new UnsupportedOperationException();
}
public void bindTexImageToPbuffer(ByteBuffer handle, int buffer) {
public void bindTexImageToPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer) {
public void releaseTexImageFromPbuffer(PeerInfo handle, int buffer) {
throw new UnsupportedOperationException();
}
}

View file

@ -41,14 +41,13 @@ import org.lwjgl.Sys;
* <p/>
* Pbuffer encapsulates an OpenGL pbuffer.
* <p/>
* Each instance of GL is only valid in the thread that creates it. In addition, only one instance of an OpenGL window or
* Pbuffer may be the current GL context in any one thread. To make a GL instance the current context, use makeCurrent().
*
* This class is thread-safe.
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
public final class Pbuffer {
public final class Pbuffer implements Drawable {
/**
* Indicates that Pbuffers can be created.
*/
@ -137,7 +136,7 @@ public final class Pbuffer {
/**
* Handle to the native GL rendering context
*/
private final ByteBuffer handle;
private final PeerInfo peer_info;
/**
* Width
@ -149,6 +148,10 @@ public final class Pbuffer {
*/
private final int height;
private final Context context;
private boolean destroyed;
static {
Sys.initialize();
}
@ -169,29 +172,40 @@ public final class Pbuffer {
* @param height Pbuffer height
* @param pixel_format Minimum Pbuffer context properties
* @param renderTexture
* @param shared_context If non-null the Pbuffer will share display lists and textures with it. Otherwise, the Pbuffer will share
* @param shared_drawable If non-null the Pbuffer will share display lists and textures with it. Otherwise, the Pbuffer will share
* with the Display context (if created).
*/
public Pbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, Pbuffer shared_context) throws LWJGLException {
public Pbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, Drawable shared_drawable) throws LWJGLException {
this.width = width;
this.height = height;
this.handle = createPbuffer(width, height, pixel_format, renderTexture, shared_context != null ? shared_context.handle : null);
this.peer_info = createPbuffer(width, height, pixel_format, renderTexture);
Context shared_context = null;
if (shared_drawable != null) {
shared_context = shared_drawable.getContext();
} else {
Drawable display_drawable = Display.getDrawable();
if (display_drawable != null)
shared_context = display_drawable.getContext();
}
this.context = new Context(peer_info, shared_context);
}
private static ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, ByteBuffer shared_context_handle) throws LWJGLException {
GLContext.loadOpenGLLibrary();
try {
if ( renderTexture == null )
return Display.getImplementation().createPbuffer(width, height, pixel_format, null, null, shared_context_handle);
else
return Display.getImplementation().createPbuffer(width, height, pixel_format,
renderTexture.pixelFormatCaps,
renderTexture.pBufferAttribs,
shared_context_handle);
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
throw e;
}
private static PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException {
if ( renderTexture == null )
return Display.getImplementation().createPbuffer(width, height, pixel_format, null, null);
else
return Display.getImplementation().createPbuffer(width, height, pixel_format,
renderTexture.pixelFormatCaps,
renderTexture.pBufferAttribs);
}
public Context getContext() {
return context;
}
private void checkDestroyed() {
if (destroyed)
throw new IllegalStateException("Pbuffer is destroyed");
}
/**
@ -201,17 +215,18 @@ public final class Pbuffer {
*
* @return true if the buffer is lost and destroyed, false if the buffer is valid.
*/
public boolean isBufferLost() {
return Display.getImplementation().isBufferLost(handle);
public synchronized boolean isBufferLost() {
checkDestroyed();
return Display.getImplementation().isBufferLost(peer_info);
}
/**
* Method to make the Pbuffer context current. All subsequent OpenGL calls will go to this buffer.
* @throws LWJGLException if the context could not be made current
*/
public void makeCurrent() throws LWJGLException {
Display.getImplementation().makePbufferCurrent(handle);
GLContext.useContext(this);
public synchronized void makeCurrent() throws LWJGLException {
checkDestroyed();
context.makeCurrent();
}
/**
@ -227,17 +242,15 @@ public final class Pbuffer {
* Destroys the Pbuffer. After this call, there will be no valid GL rendering context - regardless of whether this Pbuffer was
* the current rendering context or not.
*/
public void destroy() {
public synchronized void destroy() {
if (destroyed)
return;
try {
makeCurrent();
int error = GL11.glGetError();
Display.getImplementation().destroyPbuffer(handle);
GLContext.useContext(null);
GLContext.unloadOpenGLLibrary();
if (error != GL11.GL_NO_ERROR)
throw new OpenGLException(error);
context.forceDestroy();
Display.getImplementation().destroyPbuffer(peer_info);
destroyed = true;
} catch (LWJGLException e) {
// ignore exception
Sys.log("Exception occurred while destroying pbuffer: " + e);
}
}
@ -257,8 +270,9 @@ public final class Pbuffer {
* @param attrib
* @param value
*/
public void setAttrib(int attrib, int value) {
Display.getImplementation().setPbufferAttrib(handle, attrib, value);
public synchronized void setAttrib(int attrib, int value) {
checkDestroyed();
Display.getImplementation().setPbufferAttrib(peer_info, attrib, value);
}
/**
@ -268,8 +282,9 @@ public final class Pbuffer {
*
* @param buffer
*/
public void bindTexImage(int buffer) {
Display.getImplementation().bindTexImageToPbuffer(handle, buffer);
public synchronized void bindTexImage(int buffer) {
checkDestroyed();
Display.getImplementation().bindTexImageToPbuffer(peer_info, buffer);
}
/**
@ -277,21 +292,24 @@ public final class Pbuffer {
*
* @param buffer
*/
public void releaseTexImage(int buffer) {
Display.getImplementation().releaseTexImageFromPbuffer(handle, buffer);
public synchronized void releaseTexImage(int buffer) {
checkDestroyed();
Display.getImplementation().releaseTexImageFromPbuffer(peer_info, buffer);
}
/**
* @return Returns the height.
*/
public int getHeight() {
public synchronized int getHeight() {
checkDestroyed();
return height;
}
/**
* @return Returns the width.
*/
public int getWidth() {
public synchronized int getWidth() {
checkDestroyed();
return width;
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2002-2004 LWJGL 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 'LWJGL' 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.opengl;
import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
abstract class PeerInfo {
private final ByteBuffer handle;
protected PeerInfo(ByteBuffer handle) {
this.handle = handle;
}
private final void lockAndInitHandle() throws LWJGLException {
doLockAndInitHandle();
}
public synchronized final void unlock() throws LWJGLException {
doUnlock();
}
protected abstract void doLockAndInitHandle() throws LWJGLException;
protected abstract void doUnlock() throws LWJGLException;
public synchronized final ByteBuffer lockAndGetHandle() throws LWJGLException {
lockAndInitHandle();
return getHandle();
}
protected final ByteBuffer getHandle() {
return handle;
}
}

View file

@ -58,25 +58,29 @@ final class Win32Display implements DisplayImplementation {
public native void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException;
public native String getAdapter();
public native String getVersion();
public native DisplayMode init();
public native DisplayMode init() throws LWJGLException;
public native void setTitle(String title);
public native boolean isCloseRequested();
public native boolean isVisible();
public native boolean isActive();
public native boolean isDirty();
public native void swapBuffers();
public native void makeCurrent() throws LWJGLException;
public native void createContext(PixelFormat pixel_format) throws LWJGLException;
public native void destroyContext();
// public native void swapBuffers();
// public native void makeCurrent() throws LWJGLException;
public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException {
throw new RuntimeException("Not supported yet");
}
// public native void createContext(PixelFormat pixel_format) throws LWJGLException;
// public native void destroyContext();
public native void destroyPeerInfo();
public native void update();
public native void setVSyncEnabled(boolean sync);
public native void reshape(int x, int y, int width, int height);
public native DisplayMode[] getAvailableDisplayModes();
public native DisplayMode[] getAvailableDisplayModes() throws LWJGLException;
/* Mouse */
public native boolean hasWheel();
public native int getButtonCount();
public native void createMouse();
public native void createMouse() throws LWJGLException;
public native void destroyMouse();
public native void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons);
public native int readMouse(IntBuffer buffer, int buffer_position);
@ -106,10 +110,17 @@ final class Win32Display implements DisplayImplementation {
public native void destroyCursor(Object cursorHandle);
public native int getPbufferCapabilities();
public native boolean isBufferLost(ByteBuffer handle);
public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException;
public native boolean isBufferLost(PeerInfo handle);
// public native boolean isBufferLost(ByteBuffer handle);
// public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException;
public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs) throws LWJGLException {
throw new RuntimeException("Not yet supported");
}
/* public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException {
ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE);
@ -120,10 +131,14 @@ final class Win32Display implements DisplayImplementation {
private native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException;
*/
public native void destroyPbuffer(PeerInfo handle);
// public native void destroyPbuffer(ByteBuffer handle);
public native void destroyPbuffer(ByteBuffer handle);
public native void setPbufferAttrib(ByteBuffer handle, int attrib, int value);
public native void setPbufferAttrib(PeerInfo handle, int attrib, int value);
public native void bindTexImageToPbuffer(PeerInfo handle, int buffer);
public native void releaseTexImageFromPbuffer(PeerInfo handle, int buffer);
/* public native void setPbufferAttrib(ByteBuffer handle, int attrib, int value);
public native void bindTexImageToPbuffer(ByteBuffer handle, int buffer);
public native void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);
public native void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);*/
}

View file

@ -54,7 +54,7 @@ public class DisplayTest {
/**
* Runs the tests
*/
public void executeTest() {
public void executeTest() throws LWJGLException {
currentTest();
queryModesTest();
setDisplayModeTest();
@ -80,7 +80,7 @@ public class DisplayTest {
/**
* Tests querying for modes
*/
private void queryModesTest() {
private void queryModesTest() throws LWJGLException {
DisplayMode[] modes = null;
System.out.println("==== Test query ====");
@ -109,7 +109,7 @@ public class DisplayTest {
/**
* Tests setting display modes
*/
private void setDisplayModeTest() {
private void setDisplayModeTest() throws LWJGLException {
DisplayMode mode = null;
DisplayMode[] modes = null;
@ -241,7 +241,7 @@ public class DisplayTest {
*
* @param args ignored
*/
public static void main(String[] args) {
public static void main(String[] args) throws LWJGLException {
new DisplayTest().executeTest();
}
}

View file

@ -33,6 +33,7 @@ package org.lwjgl.test;
import org.lwjgl.opengl.Display;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLException;
/**
* $Id$
@ -75,7 +76,11 @@ public class SysTest {
}
// get some display modes, to force some debug info
Display.getAvailableDisplayModes();
try {
Display.getAvailableDisplayModes();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
System.out.println("---- Test Debug ----\n");
}

View file

@ -50,7 +50,7 @@ public class WindowCreationTest {
*
* @param args ignored params to app
*/
public static void main(String[] args) {
public static void main(String[] args) throws LWJGLException {
// get avaialble modes, and print out
DisplayMode[] modes = Display.getAvailableDisplayModes();
System.out.println("Found " + modes.length + " display modes");
@ -138,10 +138,10 @@ public class WindowCreationTest {
* Sets the display mode for fullscreen mode
*/
protected static boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,

View file

@ -76,10 +76,10 @@ public class HWCursorTest {
* Sets the display mode for fullscreen mode
*/
protected boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,

View file

@ -78,10 +78,10 @@ public class KeyboardTest {
* Sets the display mode for fullscreen mode
*/
protected boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,

View file

@ -72,10 +72,10 @@ public class MouseCreationTest {
* Sets the display mode for fullscreen mode
*/
protected boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,

View file

@ -109,10 +109,10 @@ public abstract class BasicTest {
* Sets the display mode for fullscreen mode
*/
protected boolean setDisplayMode() {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,

View file

@ -233,7 +233,7 @@ public class FullScreenWindowedTest {
* Minimum required bits per pixel
* @return
*/
private DisplayMode findDisplayMode(int width, int height, int bpp) {
private DisplayMode findDisplayMode(int width, int height, int bpp) throws LWJGLException {
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++) {
if (modes[i].getWidth() == width && modes[i].getHeight() == height && modes[i].getBitsPerPixel() >= bpp && modes[i].getFrequency() <= 60) {

View file

@ -341,7 +341,7 @@ public class PbufferTest {
* @param bpp Minimum required bits per pixel
* @return
*/
private DisplayMode findDisplayMode(int width, int height, int bpp) {
private DisplayMode findDisplayMode(int width, int height, int bpp) throws LWJGLException {
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++) {
if (modes[i].getWidth() == width

View file

@ -5,9 +5,11 @@
package org.lwjgl.test.opengl.awt;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;
@ -30,31 +32,54 @@ public class AWTTest extends Frame {
/**
* C'tor
*/
public AWTTest() {
public AWTTest() throws LWJGLException {
setTitle("LWJGL AWT Canvas Test");
setSize(640, 320);
setLayout(null);
add(canvas0 = new AWTGLCanvas() {
protected void doPaint() {
GL11.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
public void paint(Graphics g) {
try {
makeCurrent();
GL11.glViewport(0, 0, getWidth(), getHeight());
GL11.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glColor3f(1f, 1f, 0f);
GL11.glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f);
GL11.glRotatef(angle, 0f, 0f, 1.0f);
GL11.glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
GL11.glPopMatrix();
swapBuffers();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
});
canvas0.setBounds(0, 0, 320, 320);
add(canvas1 = new AWTGLCanvas() {
protected void doPaint() {
GL11.glViewport(0, 0, getWidth(), getHeight());
GL11.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f);
GL11.glRotatef(angle, 0f, 0f, 1.0f);
GL11.glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
GL11.glPopMatrix();
public void paint(Graphics g) {
try {
makeCurrent();
GL11.glViewport(0, 0, getWidth(), getHeight());
GL11.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f);
GL11.glRotatef(2*angle, 0f, 0f, -1.0f);
GL11.glRectf(-50.0f, -50.0f, 50.0f, 50.0f);
GL11.glPopMatrix();
swapBuffers();
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
});
canvas1.setBounds(320, 0, 320, 320);
@ -64,6 +89,7 @@ public class AWTTest extends Frame {
}
});
setResizable(true);
setVisible(true);
new Thread() {
{
@ -72,6 +98,7 @@ public class AWTTest extends Frame {
public void run() {
for (;;) {
angle += 1.0f;
canvas0.repaint();
canvas1.repaint();
try {
sleep(20);
@ -83,7 +110,7 @@ public class AWTTest extends Frame {
}.start();
}
public static void main(String[] args) {
new AWTTest().setVisible(true);
public static void main(String[] args) throws LWJGLException {
new AWTTest();
}
}

View file

@ -345,7 +345,7 @@ public final class PbufferTest {
*
* @return
*/
private DisplayMode findDisplayMode(int width, int height, int bpp) {
private DisplayMode findDisplayMode(int width, int height, int bpp) throws LWJGLException {
DisplayMode[] modes = Display.getAvailableDisplayModes();
for ( int i = 0; i < modes.length; i++ ) {
if ( modes[i].getWidth() == width

View file

@ -122,19 +122,19 @@ public final class ShadersTest {
if ( args.length != 1 )
argsError();
DisplayMode[] modes = Display.getAvailableDisplayModes();
DisplayMode displayMode;
displayMode = chooseMode(modes, 1024, 768);
if ( displayMode == null )
displayMode = chooseMode(modes, 800, 600);
if ( displayMode == null )
displayMode = chooseMode(modes, 640, 480);
if ( displayMode == null )
kill("Failed to set an appropriate display mode.");
try {
DisplayMode[] modes = Display.getAvailableDisplayModes();
DisplayMode displayMode;
displayMode = chooseMode(modes, 1024, 768);
if ( displayMode == null )
displayMode = chooseMode(modes, 800, 600);
if ( displayMode == null )
displayMode = chooseMode(modes, 640, 480);
if ( displayMode == null )
kill("Failed to set an appropriate display mode.");
System.out.println("Setting display mode to: " + displayMode);
Display.setDisplayMode(displayMode);
Display.create(new PixelFormat(8, 24, 0));

View file

@ -37,6 +37,7 @@ import java.util.Arrays;
import java.util.Comparator;
import org.lwjgl.Sys;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;
/**
@ -65,7 +66,7 @@ public final class Display {
* @return an array of matching display modes
*/
public static DisplayMode[] getAvailableDisplayModes(int minWidth, int minHeight, int maxWidth, int maxHeight, int minBPP, int maxBPP,
int minFreq, int maxFreq)
int minFreq, int maxFreq) throws LWJGLException
{
// First get the available display modes
DisplayMode[] modes = org.lwjgl.opengl.Display.getAvailableDisplayModes();