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.
*
* 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
* axises and other features. but this is a platform issue.
*
@ -59,22 +59,22 @@ public class Joystick {
}
/** Has the joystick been created? */
private boolean created;
private static boolean created;
/** The joystick buttons status */
private boolean[] buttons;
private static boolean[] buttons;
/** X position, range -1000 to 1000 */
public int x = -1;
public static int x = -1;
/** Y position, range -1000 to 1000 */
public int y = -1;
public static int y = -1;
/** 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) */
public int pov;
public static int pov;
/** Constant specifying centered POV */
public static final int POV_CENTER = -1;
@ -92,14 +92,14 @@ public class Joystick {
public static final int POV_WEST = 9000;
/* Joystick capabilities */
public int buttonCount = -1;
public boolean hasZAxis = false;
public boolean hasPOV = false;
public static int buttonCount = -1;
public static boolean hasZAxis = false;
public static boolean hasPOV = false;
/**
* 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.
* @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) {
return;
}
@ -128,7 +128,7 @@ public class Joystick {
/**
* "Destroy" the joystick
*/
public void destroy() {
public static void destroy() {
if (!created) {
return;
}
@ -140,7 +140,7 @@ public class Joystick {
/**
* Polls the joystick.
*/
public void poll() {
public static void poll() {
assert created : "The joystick has not been created.";
nPoll();
}
@ -152,7 +152,7 @@ public class Joystick {
* @return true if the specified button is down
* @see #getNumButtons()
*/
public boolean isButtonDown(int button) {
public static boolean isButtonDown(int button) {
assert created : "The joystick has not been created.";
return buttons[button];
}
@ -160,19 +160,19 @@ public class Joystick {
/**
* Native method to poll the joystick
*/
private native void nPoll();
private static native void nPoll();
/**
* Native method to create the joystick
*
* @return true if the joystick was created
*/
private native boolean nCreate();
private static native boolean nCreate();
/**
* Native method the destroy the joystick
*/
private native void nDestroy();
private static native void nDestroy();
/**
* Register fields with the native library

View File

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

View File

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