fix: made joystick static, according to specs

This commit is contained in:
Brian Matzon 2002-11-18 17:50:21 +00:00
parent 3d083b62d1
commit d32ec087bc
3 changed files with 77 additions and 88 deletions

View File

@ -43,7 +43,7 @@ import org.lwjgl.Sys;
* *
* No buffering is available. * No buffering is available.
* *
* Currently n (native limits, currently 128) buttons, the x, y, z axis is supported along with a POV (or HAT), where the z axis * Currently n (native limits, currently 128 - might change) buttons, the x, y, z axis is supported along with a POV (or HAT), where the z axis
* represents a throttle. In the future the joystick may support more buttons and * represents a throttle. In the future the joystick may support more buttons and
* axises and other features. but this is a platform issue. * axises and other features. but this is a platform issue.
* *
@ -59,22 +59,22 @@ public class Joystick {
} }
/** Has the joystick been created? */ /** Has the joystick been created? */
private boolean created; private static boolean created;
/** The joystick buttons status */ /** The joystick buttons status */
private boolean[] buttons; private static boolean[] buttons;
/** X position, range -1000 to 1000 */ /** X position, range -1000 to 1000 */
public int x = -1; public static int x = -1;
/** Y position, range -1000 to 1000 */ /** Y position, range -1000 to 1000 */
public int y = -1; public static int y = -1;
/** Z position, range -1000 to 1000 */ /** Z position, range -1000 to 1000 */
public int z = -1; public static int z = -1;
/** Position of Point of View from -1 to 27000 (360 degrees) */ /** Position of Point of View from -1 to 27000 (360 degrees) */
public int pov; public static int pov;
/** Constant specifying centered POV */ /** Constant specifying centered POV */
public static final int POV_CENTER = -1; public static final int POV_CENTER = -1;
@ -92,14 +92,14 @@ public class Joystick {
public static final int POV_WEST = 9000; public static final int POV_WEST = 9000;
/* Joystick capabilities */ /* Joystick capabilities */
public int buttonCount = -1; public static int buttonCount = -1;
public boolean hasZAxis = false; public static boolean hasZAxis = false;
public boolean hasPOV = false; public static boolean hasPOV = false;
/** /**
* Joystick cannot be constructed. * Joystick cannot be constructed.
*/ */
public Joystick() { private Joystick() {
} }
/** /**
@ -114,7 +114,7 @@ public class Joystick {
* "Create" the joystick. The display must first have been created. * "Create" the joystick. The display must first have been created.
* @throws Exception if the joystick could not be created for any reason * @throws Exception if the joystick could not be created for any reason
*/ */
public void create() throws Exception { public static void create() throws Exception {
if (created) { if (created) {
return; return;
} }
@ -128,7 +128,7 @@ public class Joystick {
/** /**
* "Destroy" the joystick * "Destroy" the joystick
*/ */
public void destroy() { public static void destroy() {
if (!created) { if (!created) {
return; return;
} }
@ -140,7 +140,7 @@ public class Joystick {
/** /**
* Polls the joystick. * Polls the joystick.
*/ */
public void poll() { public static void poll() {
assert created : "The joystick has not been created."; assert created : "The joystick has not been created.";
nPoll(); nPoll();
} }
@ -152,7 +152,7 @@ public class Joystick {
* @return true if the specified button is down * @return true if the specified button is down
* @see #getNumButtons() * @see #getNumButtons()
*/ */
public boolean isButtonDown(int button) { public static boolean isButtonDown(int button) {
assert created : "The joystick has not been created."; assert created : "The joystick has not been created.";
return buttons[button]; return buttons[button];
} }
@ -160,19 +160,19 @@ public class Joystick {
/** /**
* Native method to poll the joystick * Native method to poll the joystick
*/ */
private native void nPoll(); private static native void nPoll();
/** /**
* Native method to create the joystick * Native method to create the joystick
* *
* @return true if the joystick was created * @return true if the joystick was created
*/ */
private native boolean nCreate(); private static native boolean nCreate();
/** /**
* Native method the destroy the joystick * Native method the destroy the joystick
*/ */
private native void nDestroy(); private static native void nDestroy();
/** /**
* Register fields with the native library * Register fields with the native library

View File

@ -16,20 +16,17 @@ import org.lwjgl.input.Joystick;
*/ */
public class JoystickTest extends Panel { public class JoystickTest extends Panel {
private Joystick joystick = null;
private int counter = 0; private int counter = 0;
public Thread animationThread; public Thread animationThread;
/** Creates a new instance of JoystickTest */ /** Creates a new instance of JoystickTest */
public JoystickTest() { public JoystickTest() {
joystick = new Joystick();
try { try {
joystick.create(); Joystick.create();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
joystick = null; return;
} }
animationThread = new Thread() { animationThread = new Thread() {
@ -58,48 +55,50 @@ public class JoystickTest extends Panel {
int y = 100; int y = 100;
int x = 100; int x = 100;
if (joystick != null) { Joystick.poll();
joystick.poll();
g.setColor(Color.blue); g.setColor(Color.blue);
g.drawString("Buttoncount: " + joystick.buttonCount, x, y); g.drawString("Buttoncount: " + Joystick.buttonCount, x, y);
y += 20; y += 20;
g.drawString("-----------------------------------------------", x, y); g.drawString("-----------------------------------------------", x, y);
y += 20; y += 20;
g.drawString("x : " + joystick.x, x, y); g.drawString("x : " + Joystick.x, x, y);
y += 20; y += 20;
g.drawString("y : " + joystick.y, x, y); g.drawString("y : " + Joystick.y, x, y);
y += 20; y += 20;
if(joystick.hasZAxis) { if(Joystick.hasZAxis) {
g.drawString("z : " + joystick.z, x, y); g.drawString("z : " + Joystick.z, x, y);
y += 20; y += 20;
} }
if(joystick.hasPOV) { if(Joystick.hasPOV) {
g.drawString("pov: " + joystick.pov, x, y); g.drawString("pov: " + Joystick.pov, x, y);
y += 20; y += 20;
} }
//paint buttons //paint buttons
g.drawString("btn: ", x, y); g.drawString("btn: ", x, y);
x += g.getFontMetrics().stringWidth("btn: "); x += g.getFontMetrics().stringWidth("btn: ");
for(int i=0; i<joystick.buttonCount; i++) { for(int i=0; i<Joystick.buttonCount; i++) {
if(joystick.isButtonDown(i)) { if(Joystick.isButtonDown(i)) {
g.drawString(i + ", ", x, y); g.drawString(i + ", ", x, y);
x+= 15; x+= 15;
} }
} }
}
} }
public void update(Graphics g) { public void update(Graphics g) {
paint(g); paint(g);
} }
public void disposing() {
Joystick.destroy();
}
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
final JoystickTest p = new JoystickTest();
final Frame f = new Frame(); final Frame f = new Frame();
f.setLayout(null); f.setLayout(null);
f.setSize(640, 480); f.setSize(640, 480);
@ -107,11 +106,11 @@ public class JoystickTest extends Panel {
f.addWindowListener(new WindowAdapter() { f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) { public void windowClosing(WindowEvent we) {
f.hide(); f.hide();
p.disposing();
f.dispose(); f.dispose();
} }
}); });
JoystickTest p = new JoystickTest();
p.setSize(640, 480); p.setSize(640, 480);
p.setLocation(0, 0); p.setLocation(0, 0);
p.setBackground(Color.RED); p.setBackground(Color.RED);

View File

@ -64,7 +64,6 @@ bool create_success; // bool used to determine successfull creati
// Cached fields of Joystick.java // Cached fields of Joystick.java
jclass clsJoystick; jclass clsJoystick;
jobject objJoystick;
jfieldID fidButtonCount; jfieldID fidButtonCount;
jfieldID fidHasZAxis; jfieldID fidHasZAxis;
jfieldID fidHasPOV; jfieldID fidHasPOV;
@ -83,9 +82,9 @@ void Shutdown();
void CreateJoystick(LPCDIDEVICEINSTANCE lpddi); void CreateJoystick(LPCDIDEVICEINSTANCE lpddi);
void SetupJoystick(); void SetupJoystick();
void InitializeFields(); void InitializeFields();
void CacheFields();
void UpdateFields(); void UpdateFields();
void SetCapabilities(); void SetCapabilities();
void CacheFields(jobject obj);
void PrintError(HRESULT error); void PrintError(HRESULT error);
/** /**
@ -93,15 +92,16 @@ void PrintError(HRESULT error);
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_initIDs(JNIEnv * env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_initIDs(JNIEnv * env, jclass clazz) {
environment = env; environment = env;
clsJoystick = clazz;
/* Cache fields in Joystick */
CacheFields();
} }
/** /**
* Called when the Joystick instance is to be created * Called when the Joystick instance is to be created
*/ */
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jobject obj) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jclass clazz) {
/* Cache fields in Joystick */
CacheFields(obj);
// Create the DirectInput object. // Create the DirectInput object.
HRESULT hr; HRESULT hr;
hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpDI, NULL); hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpDI, NULL);
@ -145,7 +145,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jo
* Method: nDestroy * Method: nDestroy
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jobject obj) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jclass clazz) {
Shutdown(); Shutdown();
} }
@ -154,7 +154,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jobje
* Method: nPoll * Method: nPoll
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nPoll(JNIEnv * env, jobject obj) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nPoll(JNIEnv * env, jclass clazz) {
HRESULT hRes; HRESULT hRes;
// poll the joystick to read the current state // poll the joystick to read the current state
@ -184,11 +184,6 @@ void Shutdown() {
lpDI->Release(); lpDI->Release();
lpDI = NULL; lpDI = NULL;
} }
// Delete are global reference to the Joystick
if(objJoystick != NULL) {
environment->DeleteGlobalRef(objJoystick);
}
} }
/** /**
@ -325,7 +320,7 @@ void SetupJoystick() {
void InitializeFields() { void InitializeFields() {
//set buttons array //set buttons array
jbooleanArray buttonsArray = environment->NewBooleanArray(buttoncount); jbooleanArray buttonsArray = environment->NewBooleanArray(buttoncount);
environment->SetObjectField(objJoystick, fidButtons, buttonsArray); environment->SetStaticObjectField(clsJoystick, fidButtons, buttonsArray);
} }
/** /**
@ -350,21 +345,21 @@ void UpdateFields() {
} }
//axis's //axis's
environment->SetIntField(objJoystick, fidX, js.lX); environment->SetStaticIntField(clsJoystick, fidX, js.lX);
environment->SetIntField(objJoystick, fidY, js.lY); environment->SetStaticIntField(clsJoystick, fidY, js.lY);
if(hasz) { if(hasz) {
environment->SetIntField(objJoystick, fidZ, js.lZ); environment->SetStaticIntField(clsJoystick, fidZ, js.lZ);
} }
//buttons //buttons
jbooleanArray buttonsArray = (jbooleanArray) environment->GetObjectField(objJoystick, fidButtons); jbooleanArray buttonsArray = (jbooleanArray) environment->GetStaticObjectField(clsJoystick, fidButtons);
BYTE * buttons = (BYTE *) environment->GetPrimitiveArrayCritical(buttonsArray, NULL); BYTE * buttons = (BYTE *) environment->GetPrimitiveArrayCritical(buttonsArray, NULL);
memcpy(buttons, js.rgbButtons, 4); memcpy(buttons, js.rgbButtons, 4);
environment->ReleasePrimitiveArrayCritical(buttonsArray, buttons, 0); environment->ReleasePrimitiveArrayCritical(buttonsArray, buttons, 0);
//pov //pov
if(haspov) { if(haspov) {
environment->SetIntField(objJoystick, fidPOV, js.rgdwPOV[0]); environment->SetStaticIntField(clsJoystick, fidPOV, js.rgdwPOV[0]);
} }
} }
@ -373,30 +368,25 @@ void UpdateFields() {
*/ */
void SetCapabilities() { void SetCapabilities() {
//set buttoncount //set buttoncount
environment->SetIntField(objJoystick, fidButtonCount, buttoncount); environment->SetStaticIntField(clsJoystick, fidButtonCount, buttoncount);
//set z axis //set z axis
environment->SetIntField(objJoystick, fidHasZAxis, hasz); environment->SetStaticIntField(clsJoystick, fidHasZAxis, hasz);
//set pov //set pov
environment->SetIntField(objJoystick, fidHasPOV, haspov); environment->SetStaticIntField(clsJoystick, fidHasPOV, haspov);
} }
/** /**
* Caches the field ids for quicker access * Caches the field ids for quicker access
*/ */
void CacheFields(jobject obj) { void CacheFields() {
/* make certain that we're caching from a global object, and not a local */ fidButtonCount = environment->GetStaticFieldID(clsJoystick, "buttonCount", "I");
objJoystick = environment->NewGlobalRef(obj); fidHasZAxis = environment->GetStaticFieldID(clsJoystick, "hasZAxis", "Z");
clsJoystick = environment->GetObjectClass(objJoystick); fidHasPOV = environment->GetStaticFieldID(clsJoystick, "hasPOV", "Z");
fidButtons = environment->GetStaticFieldID(clsJoystick, "buttons", "[Z");
/* cache fields */ fidX = environment->GetStaticFieldID(clsJoystick, "x", "I");
fidButtonCount = environment->GetFieldID(clsJoystick, "buttonCount", "I"); fidY = environment->GetStaticFieldID(clsJoystick, "y", "I");
fidHasZAxis = environment->GetFieldID(clsJoystick, "hasZAxis", "Z"); fidZ = environment->GetStaticFieldID(clsJoystick, "z", "I");
fidHasPOV = environment->GetFieldID(clsJoystick, "hasPOV", "Z"); fidPOV = environment->GetStaticFieldID(clsJoystick, "pov", "I");
fidButtons = environment->GetFieldID(clsJoystick, "buttons", "[Z");
fidX = environment->GetFieldID(clsJoystick, "x", "I");
fidY = environment->GetFieldID(clsJoystick, "y", "I");
fidZ = environment->GetFieldID(clsJoystick, "z", "I");
fidPOV = environment->GetFieldID(clsJoystick, "pov", "I");
} }