Added GamePad and Joystick and some query methods

This commit is contained in:
Caspian Rychlik-Prince 2002-08-14 14:31:09 +00:00
parent d9ed524c54
commit 630362c462
4 changed files with 396 additions and 5 deletions

View File

@ -0,0 +1,225 @@
package org.lwjgl.input;
import java.nio.ByteBuffer;
import org.lwjgl.Display;
import org.lwjgl.Sys;
/**
* (C) 2002 Shaven Puppy Ltd
*
* GamePad Created on Aug 14, 2002 by foo
*/
/**
* A raw GamePad interface. This can be used to poll the current state of the
* buttons, or read all the gamepad presses / releases since the last read.
* Buffering must be explicitly enabled; the size of the buffer is determined
* by the native implementation at its discretion.
*
* @author foo
*/
public class GamePad {
// Button codes
public static int PAD_UP = 1;
public static int PAD_DOWN = 2;
public static int PAD_LEFT = 3;
public static int PAD_RIGHT = 4;
public static int PAD_BUTTON0 = 5;
public static int PAD_BUTTON1 = 6;
public static int PAD_BUTTON2 = 7;
public static int PAD_BUTTON3 = 8;
public static int PAD_BUTTON4 = 9;
public static int PAD_BUTTON5 = 10;
public static int PAD_BUTTON6 = 11;
public static int PAD_BUTTON7 = 12;
public static int PAD_BUTTON8 = 13;
static {
initialize();
}
/** Has the gamepad been created? */
private static boolean created;
/** The buttons status from the last poll */
private static final ByteBuffer buttonDownBuffer = ByteBuffer.allocateDirect(256);
/** Address of the buttonDown buffer */
private static final int buttonDownAddress = Sys.getDirectBufferAddress(buttonDownBuffer);
/**
* The button events from the last read: a sequence of pairs of button number,
* followed by state.
*/
private static ByteBuffer readBuffer;
/** Address of the read buffer */
private static int readBufferAddress;
/** The current gamepad event button being examined */
public static int button;
/** The current state of the button being examined in the event queue */
public static boolean state;
/**
* GamePad cannot be constructed.
*/
private GamePad() {
}
/**
* Static initialization
*/
private static void initialize() {
System.loadLibrary(Sys.LIBRARY_NAME);
initIDs();
}
/**
* Register fields with the native library
*/
private static native void initIDs();
/**
* "Create" the gamepad. The display must first have been created. The
* reason for this is so the gamepad has a window to "focus" in.
*
* @throw Exception if the gamepad could not be created for any reason
*/
public static void create() throws Exception {
if (created)
return;
if (!Display.isCreated())
throw new Exception("The display has not yet been created.");
if (!nCreate())
throw new Exception("The gamepad could not be created.");
created = true;
}
/**
* Native method to create the gamepad
*
* @return true if the gamepad was created
*/
private static native boolean nCreate();
/**
* "Destroy" the gamepad
*/
public static void destroy() {
if (!created)
return;
created = false;
nDestroy();
}
/**
* Native method the destroy the gamepad
*/
private static native void nDestroy();
/**
* Polls the gamepad.
*/
public static void poll() {
assert created : "The gamepad has not been created.";
nPoll(buttonDownAddress);
}
/**
* Native method to poll the gamepad.
*
* @param keyDownBufferAddress the address of a 256-byte buffer to place
* key states in.
*/
private static native void nPoll(int keyDownBufferAddress);
/**
* Reads the gamepad buffer.
*/
public static void read() {
assert created : "The gamepad has not been created.";
assert readBuffer != null : "GamePad buffering has not been enabled.";
readBuffer.clear();
readBuffer.limit(nRead(readBufferAddress) << 1);
}
/**
* Native method to read the gamepad buffer
*
* @param readBufferAddress the address of the gamepad buffer
* @return the number of gamepad events read
*/
private static native int nRead(int readBufferAddress);
/**
* Enable gamepad buffering. Must be called after the gamepad is created.
* @return the size of the gamepad buffer in events, or 0 if no buffering
* can be enabled for any reason
*/
public static int enableBuffer() {
assert created : "The gamepad 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();
/**
* Checks to see if a button is down.
* @param button The button code to check
* @return true if the button is down according to the last poll()
*/
public static boolean isButtonDown(int button) {
assert created : "The gamepad has not been created.";
return buttonDownBuffer.get(button) != 0;
}
/**
* Gets the number of gamepad events waiting after doing a read().
* @return the number of gamepad events
*/
public static int getNumGamePadEvents() {
return readBuffer.limit() >> 1;
}
/**
* Gets the next gamepad event. This is stored in the publicly accessible
* static fields button and state.
* @return true if a gamepad event was read, false otherwise
*/
public static boolean next() {
assert created : "The gamepad has not been created.";
assert readBuffer != null : "GamePad buffering has not been enabled.";
if (readBuffer.hasRemaining()) {
button = readBuffer.get();
state = readBuffer.get() != 0;
return true;
} else
return false;
}
/**
* Queries the number of buttons the gamepad has (excluding up, down, left, right)
* @return the number of buttons the gamepad has
*/
public static int getNumButtons() {
assert created : "The gamepad has not been created.";
return nGetNumButtons();
}
/**
* Native implementation of getNumButtons()
*/
private static native int nGetNumButtons();
}

View File

@ -0,0 +1,139 @@
package org.lwjgl.input;
import org.lwjgl.Display;
import org.lwjgl.Sys;
/**
* (C) 2002 Shaven Puppy Ltd
*
* Joystick.java Created on Aug 14, 2002 by foo
*/
/**
* A raw Joystick interface. This can be used to poll the current state of the
* joystick buttons, and determine the joystick position. The joystick position
* is returned as floats in the range -1.0f to 1.0f.
*
* No buffering is available.
*
* Up to 8 buttons are available. A scrolly wheel or paddle, if present, is the z
* value. This will be in the range of 0.0f to 1.0f.
*
* @author foo
*/
public class Joystick {
static {
initialize();
}
/** Has the joystick been created? */
private static boolean created;
/** The joystick buttons status from the last poll */
private static final boolean[] button = new boolean[8];
/** X position, range -1.0f to 1.0f */
public static float x;
/** Y position, range -1.0f to 1.0f */
public static float y;
/** Z position, range 0.0f to 1.0f */
public static float z;
/**
* Joystick cannot be constructed.
*/
private Joystick() {
}
/**
* Static initialization
*/
private static void initialize() {
System.loadLibrary(Sys.LIBRARY_NAME);
initIDs();
}
/**
* Register fields with the native library
*/
private static native void initIDs();
/**
* "Create" the joystick. The display must first have been created.
* @throw Exception if the joystick could not be created for any reason
*/
public static void create() throws Exception {
if (created)
return;
if (!Display.isCreated())
throw new Exception("The display has not yet been created.");
if (!nCreate())
throw new Exception("The joystick could not be created.");
created = true;
}
/**
* Native method to create the joystick
*
* @return true if the joystick was created
*/
private static native boolean nCreate();
/**
* "Destroy" the joystick
*/
public static void destroy() {
if (!created)
return;
created = false;
nDestroy();
}
/**
* Native method the destroy the joystick
*/
private static native void nDestroy();
/**
* Polls the joystick.
*/
public static void poll() {
assert created : "The joystick has not been created.";
nPoll();
}
/**
* Native method to poll the joystick
*/
private static native void nPoll();
/**
* Queries the number of buttons the joystick has
* @return the number of buttons the joystick has
*/
public static int getNumButtons() {
assert created : "The joystick has not been created.";
return nGetNumButtons();
}
/**
* Native implementation of getNumButtons()
*/
private static native int nGetNumButtons();
/**
* Queries whether the joystick has a Z value
* @return true if the joystick has a Z value
*/
public static boolean hasZValue() {
assert created : "The joystick has not been created.";
return nHasZValue();
}
/**
* Native implementation of hasZValue()
*/
private static native boolean nHasZValue();
}

View File

@ -19,9 +19,6 @@ import org.lwjgl.Sys;
* @author foo
*/
public class Keyboard {
// Keyboard key codes.
public static final int KEY_ESCAPE = 0x01;
public static final int KEY_1 = 0x02;
public static final int KEY_2 = 0x03;

View File

@ -13,8 +13,8 @@ import org.lwjgl.Sys;
* mouse buttons, and determine the mouse movement delta since the last poll.
* No buffering is available.
*
* Up to 8 buttons are available. The scrolly wheel, if present, is the z
* value.
* 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.
*
* @author foo
*/
@ -106,4 +106,34 @@ public class Mouse {
* Native method to poll the mouse
*/
private static native void nPoll();
/**
* Queries the number of buttons the mouse has
* @return the number of buttons the mouse has
*/
public static int getNumButtons() {
assert created : "The mouse has not been created.";
return nGetNumButtons();
}
/**
* Native implementation of getNumButtons()
*/
private static native int nGetNumButtons();
/**
* Queries whether the joystick has a Z value
* @return true if the joystick has a Z value
*/
public static boolean hasZValue() {
assert created : "The mouse has not been created.";
return nHasZValue();
}
/**
* Native implementation of hasZValue()
*/
private static native boolean nHasZValue();
}