diff --git a/src/java/org/lwjgl/input/Joystick.java b/src/java/org/lwjgl/input/Joystick.java index 5beb477a..7fc67dd0 100644 --- a/src/java/org/lwjgl/input/Joystick.java +++ b/src/java/org/lwjgl/input/Joystick.java @@ -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 diff --git a/src/java/org/lwjgl/input/test/JoystickTest.java b/src/java/org/lwjgl/input/test/JoystickTest.java index fd05f005..363eba0f 100644 --- a/src/java/org/lwjgl/input/test/JoystickTest.java +++ b/src/java/org/lwjgl/input/test/JoystickTest.java @@ -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; iRelease(); 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"); } \ No newline at end of file