diff --git a/src/java/org/lwjgl/input/GamePad.java b/src/java/org/lwjgl/input/GamePad.java index 10469eb4..3d869859 100644 --- a/src/java/org/lwjgl/input/GamePad.java +++ b/src/java/org/lwjgl/input/GamePad.java @@ -167,7 +167,8 @@ public class GamePad { private static native void nPoll(int keyDownBufferAddress); /** - * Reads the gamepad buffer. + * Reads the gamepad buffer. Call next() to read the events one by one. + * @see #next() */ public static void read() { assert created : "The gamepad has not been created."; diff --git a/src/java/org/lwjgl/input/Joystick.java b/src/java/org/lwjgl/input/Joystick.java index 285c8832..4067f889 100644 --- a/src/java/org/lwjgl/input/Joystick.java +++ b/src/java/org/lwjgl/input/Joystick.java @@ -32,6 +32,8 @@ package org.lwjgl.input; +import java.nio.ByteBuffer; + import org.lwjgl.Display; import org.lwjgl.Sys; @@ -71,6 +73,20 @@ public class Joystick { /** Z position, range 0.0f to 1.0f */ public static float z; + /** + * The joystick events from the last read: a sequence of Events + */ + private static ByteBuffer readBuffer; + + /** Address of the read buffer */ + private static int readBufferAddress; + + /** The size in bytes of a single joystick event */ + private static final int JOYSTICK_EVENT_SIZE = 20; + + /** The stride in bytes of a single joystick event */ + private static final int JOYSTICK_EVENT_STRIDE = 32; + /** * Joystick cannot be constructed. */ @@ -166,4 +182,66 @@ public class Joystick { * Native implementation of hasZValue() */ private static native boolean nHasZValue(); + + /** + * Enable joystick buffering. Must be called after the joystick is created. + * @return the size of the joystick buffer in events, or 0 if no buffering + * can be enabled for any reason + */ + public static int enableBuffer() { + assert created : "The joystick has not been created."; + return nEnableBuffer(); + } + + /** + * Native method to read the joystick buffer + * + * @param readBufferAddress the address of the joystick buffer + * @return the number of joystick events read + */ + private static native int nRead(int readBufferAddress); + + /** + * Reads the joystick buffer. + */ + public static void read() { + assert created : "The joystick has not been created."; + assert readBuffer != null : "Joystick buffering has not been enabled."; + readBuffer.clear(); + readBuffer.limit(nRead(readBufferAddress) << 1); + } + + /** + * Native method to enable the buffer + * @return the size of the buffer allocated, in events (1 event is 2 bytes), + * or 0 if no buffer can be allocated + */ + private static native int nEnableBuffer(); + + /** + * Gets the next joystick event. This returns its results as if a poll() had + * been called. + * + * @return true if a joystick event was read, false otherwise + */ + public static boolean next() { + assert created : "The joystick has not been created."; + assert readBuffer != null : "Joystick buffering has not been enabled."; + + if (readBuffer.hasRemaining()) { + x = readBuffer.getFloat(); + y = readBuffer.getFloat(); + z = readBuffer.getFloat(); + for (int i = 0; i < button.length; i ++) + button[i] = readBuffer.get() != (byte)0; + readBuffer.position(readBuffer.position() + (JOYSTICK_EVENT_STRIDE - JOYSTICK_EVENT_SIZE)); + return true; + } else + return false; + + } + + + + } diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index e566eca5..6f2f3fa4 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -32,6 +32,8 @@ package org.lwjgl.input; +import java.nio.ByteBuffer; + import org.lwjgl.Display; import org.lwjgl.Sys; @@ -40,7 +42,6 @@ import org.lwjgl.Sys; * * A raw Mouse interface. This can be used to poll the current state of the * mouse buttons, and determine the mouse movement delta since the last poll. - * No buffering is available. * * Up to 8 buttons are available. A scrolly wheel, if present, is the z * value. This will be in the range of -10000 to +10000. @@ -69,6 +70,21 @@ public class Mouse { /** Delta Z */ public static int dz; + /** + * The mouse events from the last read: a sequence of Events + */ + private static ByteBuffer readBuffer; + + /** Address of the read buffer */ + private static int readBufferAddress; + + /** The size in bytes of a single mouse event */ + private static final int MOUSE_EVENT_SIZE = 20; + + /** The stride in bytes of a single mouse event */ + private static final int MOUSE_EVENT_STRIDE = 32; + + /** * Mouse cannot be constructed. */ @@ -164,4 +180,69 @@ public class Mouse { * Native implementation of hasZValue() */ private static native boolean nHasZValue(); + + /** + * Enable mouse buffering. Must be called after the mouse is created. + * @return the size of the mouse buffer in events, or 0 if no buffering + * can be enabled for any reason + */ + public static int enableBuffer() { + assert created : "The mouse has not been created."; + return nEnableBuffer(); + } + + + /** + * Native method to enable the buffer + * @return the size of the buffer allocated, in events (1 event is 2 bytes), + * or 0 if no buffer can be allocated + */ + private static native int nEnableBuffer(); + + + /** + * Gets the next mouse event. This returns its results as if a poll() had + * been called. + * + * @return true if a mouse event was read, false otherwise + */ + public static boolean next() { + assert created : "The mouse has not been created."; + assert readBuffer != null : "Mouse buffering has not been enabled."; + + if (readBuffer.hasRemaining()) { + dx = readBuffer.getInt(); + dy = readBuffer.getInt(); + dz = readBuffer.getInt(); + for (int i = 0; i < button.length; i ++) + button[i] = readBuffer.get() != (byte)0; + readBuffer.position(readBuffer.position() + (MOUSE_EVENT_STRIDE - MOUSE_EVENT_SIZE)); + return true; + } else + return false; + + } + + + /** + * Native method to read the gamepad buffer + * + * @param readBufferAddress the address of the mouse buffer + * @return the number of mouse events read + */ + private static native int nRead(int readBufferAddress); + + + /** + * Reads the mouse buffer. + */ + public static void read() { + assert created : "The mouse has not been created."; + assert readBuffer != null : "Mouse buffering has not been enabled."; + readBuffer.clear(); + readBuffer.limit(nRead(readBufferAddress) * MOUSE_EVENT_SIZE); + } + + + }