From: Caspian Rychlik-P. <ci...@us...> - 2002-08-24 21:14:42
|
Update of /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input In directory usw-pr-cvs1:/tmp/cvs-serv9668/src/java/org/lwjgl/input Modified Files: GamePad.java Joystick.java Mouse.java Log Message: Buffering added Index: GamePad.java CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/java/org/lwjgl/input/GamePad.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input/GamePad.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- GamePad.java 20 Aug 2002 14:55:29 -0000 1.4 +++ GamePad.java 24 Aug 2002 21:14:40 -0000 1.5 @@ -167,7 +167,8 @@ 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."; Index: Joystick.java CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/java/org/lwjgl/input/Joystick.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input/Joystick.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- Joystick.java 20 Aug 2002 14:55:29 -0000 1.4 +++ Joystick.java 24 Aug 2002 21:14:40 -0000 1.5 @@ -32,6 +32,8 @@ package org.lwjgl.input; +import java.nio.ByteBuffer; + import org.lwjgl.Display; import org.lwjgl.Sys; @@ -72,6 +74,20 @@ 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. */ private Joystick() { @@ -166,4 +182,66 @@ * 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; + + } + + + + } Index: Mouse.java CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/java/org/lwjgl/input/Mouse.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input/Mouse.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Mouse.java 20 Aug 2002 14:55:29 -0000 1.6 +++ Mouse.java 24 Aug 2002 21:14:40 -0000 1.7 @@ -32,6 +32,8 @@ package org.lwjgl.input; +import java.nio.ByteBuffer; + import org.lwjgl.Display; import org.lwjgl.Sys; @@ -40,7 +42,6 @@ * * 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. @@ -70,6 +71,21 @@ 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. */ private Mouse() { @@ -164,4 +180,69 @@ * 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); + } + + + } |