Converted Cursor and Pbuffer handles to ByteBuffers

This commit is contained in:
Elias Naur 2004-07-25 14:28:50 +00:00
parent efb28a1a85
commit bdf5a43d5e
9 changed files with 131 additions and 94 deletions

View file

@ -32,10 +32,10 @@
package org.lwjgl.input;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.BufferUtils;
import org.lwjgl.Sys;
/**
@ -49,8 +49,9 @@ import org.lwjgl.Sys;
*/
public class Cursor {
private final static int HANDLE_SIZE = 8;
/** First element to display */
private CursorElement[] cursors = null;
private final CursorElement[] cursors;
/** Index into list of cursors */
private int index = 0;
@ -88,15 +89,15 @@ public class Cursor {
yHotspot = height - 1 - yHotspot;
// create cursor (or cursors if multiple images supplied)
createCursors(width, height, xHotspot, yHotspot, numImages, images, delays);
cursors = createCursors(width, height, xHotspot, yHotspot, numImages, images, delays);
}
/**
* Creates the actual cursor, using a platform specific class
*/
private void createCursors(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
private static CursorElement[] createCursors(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
// create copy and flip images to match ogl
IntBuffer images_copy = ByteBuffer.allocateDirect(images.remaining()*4).order(ByteOrder.nativeOrder()).asIntBuffer();
IntBuffer images_copy = BufferUtils.createIntBuffer(images.remaining());
flipImages(width, height, numImages, images, images_copy);
// Win32 doesn't (afaik) allow for animation based cursors, except when they're
@ -107,25 +108,30 @@ public class Cursor {
// might want to split it into a X/Win/Mac cursor if it gets too cluttered
String osName = System.getProperty("os.name", "");
CursorElement[] cursors;
if (osName.startsWith("Win")) {
// create our cursor elements
cursors = new CursorElement[numImages];
for(int i=0; i<numImages; i++) {
cursors[i] = new CursorElement();
cursors[i].cursorHandle = nCreateCursor(width, height, xHotspot, yHotspot, 1, images_copy, images_copy.position(), null, 0);
cursors[i].delay = (delays != null) ? delays.get(i) : 0;
cursors[i].timeout = System.currentTimeMillis();
ByteBuffer handle = BufferUtils.createByteBuffer(HANDLE_SIZE);
nCreateCursor(handle, width, height, xHotspot, yHotspot, 1, images_copy, images_copy.position(), null, 0);
long delay = (delays != null) ? delays.get(i) : 0;
long timeout = System.currentTimeMillis();
cursors[i] = new CursorElement(handle, delay, timeout);
// offset to next image
images_copy.position(width*height*(i+1));
}
} else if (osName.startsWith("Lin")) {
// create our cursor elements
cursors = new CursorElement[1];
cursors[0] = new CursorElement();
cursors[0].cursorHandle = nCreateCursor(width, height, xHotspot, yHotspot, numImages, images_copy, images_copy.position(), delays, delays != null ? delays.position() : -1);
ByteBuffer handle = BufferUtils.createByteBuffer(HANDLE_SIZE);
nCreateCursor(handle, width, height, xHotspot, yHotspot, numImages, images_copy, images_copy.position(), delays, delays != null ? delays.position() : -1);
CursorElement cursor_element = new CursorElement(handle, -1, -1);
cursors = new CursorElement[]{cursor_element};
} else {
throw new RuntimeException("Unknown OS");
}
return cursors;
}
/**
@ -168,7 +174,7 @@ public class Cursor {
/**
* Gets the native handle associated with the cursor object.
*/
public long getHandle() {
ByteBuffer getHandle() {
return cursors[index].cursorHandle;
}
@ -215,24 +221,30 @@ public class Cursor {
/**
* Native method to create a native cursor
*/
private static native long nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset);
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;
/**
* Native method to destroy a native cursor
*/
private static native void nDestroyCursor(long cursorHandle);
private static native void nDestroyCursor(ByteBuffer cursorHandle);
/**
* A single cursor element, used when animating
*/
protected class CursorElement {
private static class CursorElement {
/** Handle to cursor */
long cursorHandle;
final ByteBuffer cursorHandle;
/** How long a delay this element should have */
long delay;
final long delay;
/** Absolute time this element times out */
long timeout;
CursorElement(ByteBuffer cursorHandle, long delay, long timeout) {
this.cursorHandle = cursorHandle;
this.delay = delay;
this.timeout = timeout;
}
}
}

View file

@ -200,14 +200,14 @@ public class Mouse {
nSetNativeCursor(currentCursor.getHandle());
currentCursor.setTimeout();
} else {
nSetNativeCursor(0);
nSetNativeCursor(null);
}
}
return oldCursor;
}
/** Native method to set the native cursor */
private static native void nSetNativeCursor(long handle) throws LWJGLException;
private static native void nSetNativeCursor(ByteBuffer handle) throws LWJGLException;
/**
* Gets the minimum size of a native cursor. Can only be called if

View file

@ -32,9 +32,11 @@
package org.lwjgl.opengl;
import org.lwjgl.Sys;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
/**
* $Id$
@ -134,10 +136,15 @@ public final class Pbuffer {
*/
public static final int DEPTH_BUFFER = RenderTexture.WGL_DEPTH_COMPONENT_NV;
/**
* The maximum number of bytes in the native handle
*/
private final static int HANDLE_SIZE = 24;
/**
* Handle to the native GL rendering context
*/
private final int handle;
private final ByteBuffer handle;
/**
* Width
@ -183,7 +190,7 @@ public final class Pbuffer {
public static Pbuffer createPbufferUsingDisplayContext(int width, int height, RenderTexture renderTexture) throws LWJGLException {
if (!Display.isCreated())
throw new IllegalStateException("The Display must be created before a shared Pbuffer can be created that use the Display context");
int handle = createPbuffer(true, width, height, null, renderTexture);
ByteBuffer handle = createPbuffer(true, width, height, null, renderTexture);
return new Pbuffer(width, height, Display.getContext(), handle);
}
@ -206,26 +213,28 @@ public final class Pbuffer {
* @param renderTexture
*/
public static Pbuffer createPbufferUsingUniqueContext(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException {
int handle = createPbuffer(false, width, height, pixel_format, renderTexture);
ByteBuffer handle = createPbuffer(false, width, height, pixel_format, renderTexture);
return new Pbuffer(width, height, null, handle);
}
private Pbuffer(int width, int height, Object display_context, int handle) {
private Pbuffer(int width, int height, Object display_context, ByteBuffer handle) {
this.width = width;
this.height = height;
this.display_context = display_context;
this.handle = handle;
}
private static int createPbuffer(boolean use_display_context, int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException {
private static ByteBuffer createPbuffer(boolean use_display_context, int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException {
GLContext.loadOpenGLLibrary();
try {
ByteBuffer handle = BufferUtils.createByteBuffer(HANDLE_SIZE);
if ( renderTexture == null )
return nCreate(use_display_context, width, height, pixel_format, null, null);
nCreate(handle, use_display_context, width, height, pixel_format, null, null);
else
return nCreate(use_display_context, width, height, pixel_format,
nCreate(handle, use_display_context, width, height, pixel_format,
renderTexture.pixelFormatCaps,
renderTexture.pBufferAttribs);
return handle;
} catch (LWJGLException e) {
GLContext.unloadOpenGLLibrary();
throw e;
@ -246,7 +255,7 @@ public final class Pbuffer {
/**
* Native method to test for buffer integrity
*/
private static native boolean nIsBufferLost(int handle);
private static native boolean nIsBufferLost(ByteBuffer handle);
/**
* Method to make the Pbuffer context current. All subsequent OpenGL calls will go to this buffer.
@ -263,7 +272,7 @@ public final class Pbuffer {
/**
* Native method to make a pbuffer current.
*/
private static native void nMakeCurrent(int handle) throws LWJGLException;
private static native void nMakeCurrent(ByteBuffer handle) throws LWJGLException;
/**
* Gets the Pbuffer capabilities.
@ -275,7 +284,7 @@ public final class Pbuffer {
/**
* Native method to create a Pbuffer
*/
private static native int nCreate(boolean shared, int width, int height, PixelFormat pixel_format,
private static native void nCreate(ByteBuffer handle, boolean shared, int width, int height, PixelFormat pixel_format,
IntBuffer pixelFormatCaps,
IntBuffer pBufferAttribs) throws LWJGLException;
@ -300,7 +309,7 @@ public final class Pbuffer {
/**
* Natively destroy any GL-related stuff
*/
private static native void nDestroy(int handle);
private static native void nDestroy(ByteBuffer handle);
// -----------------------------------------------------------------------------------------
// ------------------------------- Render-to-Texture Methods -------------------------------
@ -322,7 +331,7 @@ public final class Pbuffer {
nSetAttrib(handle, attrib, value);
}
private static native void nSetAttrib(int handle, int attrib, int value);
private static native void nSetAttrib(ByteBuffer handle, int attrib, int value);
/**
* Binds the currently bound texture to the buffer specified. The buffer can be one of the following:
@ -335,7 +344,7 @@ public final class Pbuffer {
nBindTexImage(handle, buffer);
}
private static native void nBindTexImage(int handle, int buffer);
private static native void nBindTexImage(ByteBuffer handle, int buffer);
/**
* Releases the currently bound texture from the buffer specified.
@ -346,7 +355,7 @@ public final class Pbuffer {
nReleaseTexImage(handle, buffer);
}
private static native void nReleaseTexImage(int handle, int buffer);
private static native void nReleaseTexImage(ByteBuffer handle, int buffer);
/**
* @return Returns the height.