wip fix: buttons array now being ref'ed from native side to avoid GC'ing

This commit is contained in:
Brian Matzon 2003-03-06 20:55:13 +00:00
parent c0bc4b1899
commit 310526a254
2 changed files with 31 additions and 9 deletions

View File

@ -70,6 +70,7 @@ JNIEnv* cEnvironment; // JNIEnvironment copy
bool cCreate_success; // bool used to determine successfull creation bool cCreate_success; // bool used to determine successfull creation
bool cFirstTimeInitialization = true; // boolean to determine first time initialization bool cFirstTimeInitialization = true; // boolean to determine first time initialization
jobject cButtonsReference = NULL; // reference to buttons array so it won't get GC'ed
// Cached fields of Controller.java // Cached fields of Controller.java
jclass clsController; jclass clsController;
@ -179,6 +180,9 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Controller_nCreate(JNIEnv *env,
if(cCreate_success) { if(cCreate_success) {
/* Do setup of Controller */ /* Do setup of Controller */
SetupController(); SetupController();
/* Initialize any fields on the Controller */
InitializeControllerFields();
} }
} }
@ -241,6 +245,9 @@ void ShutdownController() {
cDIDevice->Release(); cDIDevice->Release();
cDIDevice = NULL; cDIDevice = NULL;
} }
//delete global reference, since we're done
cEnvironment->DeleteGlobalRef(cButtonsReference);
} }
/** /**
@ -461,9 +468,14 @@ void SetupController() {
* Sets the fields on the Controller * Sets the fields on the Controller
*/ */
void InitializeControllerFields() { void InitializeControllerFields() {
//create buttons array
jbooleanArray cButtonsArray = cEnvironment->NewBooleanArray(cButtoncount);
//create reference so it won't get GC'ed
cButtonsReference = cEnvironment->NewGlobalRef(cButtonsArray);
//set buttons array //set buttons array
jbooleanArray buttonsArray = cEnvironment->NewBooleanArray(cButtoncount); cEnvironment->SetStaticObjectField(clsController, fidCButtons, (jbooleanArray) cButtonsReference);
cEnvironment->SetStaticObjectField(clsController, fidCButtons, buttonsArray);
} }
/** /**

View File

@ -60,6 +60,7 @@ JNIEnv* mEnvironment; // JNIEnvironment copy
bool mCreate_success; // bool used to determine successfull creation bool mCreate_success; // bool used to determine successfull creation
bool mFirstTimeInitialization = true; // boolean to determine first time initialization bool mFirstTimeInitialization = true; // boolean to determine first time initialization
jobject mButtonsReference = NULL; // reference to buttons array so it won't get GC'ed
// Cached fields of Mouse.java // Cached fields of Mouse.java
jclass clsMouse; jclass clsMouse;
@ -129,6 +130,9 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclas
if(mCreate_success) { if(mCreate_success) {
/* Do setup of Mouse */ /* Do setup of Mouse */
SetupMouse(); SetupMouse();
/* Initialize any fields on the Mouse */
InitializeMouseFields();
} }
} }
@ -171,6 +175,9 @@ void ShutdownMouse() {
mDIDevice->Unacquire(); mDIDevice->Unacquire();
mDIDevice->Release(); mDIDevice->Release();
} }
//delete global reference, since we're done
mEnvironment->DeleteGlobalRef(mButtonsReference);
} }
/** /**
* Enumerates the capabilities of the Mouse attached to the system * Enumerates the capabilities of the Mouse attached to the system
@ -253,9 +260,14 @@ void SetupMouse() {
* Sets the fields on the Mouse * Sets the fields on the Mouse
*/ */
void InitializeMouseFields() { void InitializeMouseFields() {
//create buttons array
jbooleanArray mButtonsArray = mEnvironment->NewBooleanArray(mButtoncount);
//create reference so it won't get GC'ed
mButtonsReference = mEnvironment->NewGlobalRef(mButtonsArray);
//set buttons array //set buttons array
jbooleanArray buttonsArray = mEnvironment->NewBooleanArray(mButtoncount); mEnvironment->SetStaticObjectField(clsMouse, fidMButtons, (jbooleanArray) mButtonsReference);
mEnvironment->SetStaticObjectField(clsMouse, fidMButtons, buttonsArray);
} }
/** /**
@ -271,7 +283,6 @@ void UpdateMouseFields() {
diMouseState.lX = 0; diMouseState.lX = 0;
diMouseState.lY = 0; diMouseState.lY = 0;
diMouseState.lZ = 0; diMouseState.lZ = 0;
// did the read fail because we lost input for some reason? // did the read fail because we lost input for some reason?
// if so, then attempt to reacquire. // if so, then attempt to reacquire.
if(hRes == DIERR_INPUTLOST || hRes == DIERR_NOTACQUIRED) { if(hRes == DIERR_INPUTLOST || hRes == DIERR_NOTACQUIRED) {
@ -290,7 +301,6 @@ void UpdateMouseFields() {
mEnvironment->SetStaticIntField(clsMouse, fidMDY, (jint) diMouseState.lY); mEnvironment->SetStaticIntField(clsMouse, fidMDY, (jint) diMouseState.lY);
mEnvironment->SetStaticIntField(clsMouse, fidMDWheel, (jint) diMouseState.lZ); mEnvironment->SetStaticIntField(clsMouse, fidMDWheel, (jint) diMouseState.lZ);
jbooleanArray buttonsArray = (jbooleanArray) mEnvironment->GetStaticObjectField(clsMouse, fidMButtons);
for (int i = 0; i < mButtoncount; i++) { for (int i = 0; i < mButtoncount; i++) {
if (diMouseState.rgbButtons[i] != 0) { if (diMouseState.rgbButtons[i] != 0) {
diMouseState.rgbButtons[i] = JNI_TRUE; diMouseState.rgbButtons[i] = JNI_TRUE;
@ -298,7 +308,7 @@ void UpdateMouseFields() {
diMouseState.rgbButtons[i] = JNI_FALSE; diMouseState.rgbButtons[i] = JNI_FALSE;
} }
} }
mEnvironment->SetBooleanArrayRegion(buttonsArray, 0, mButtoncount, diMouseState.rgbButtons); mEnvironment->SetBooleanArrayRegion((jbooleanArray) mButtonsReference, 0, mButtoncount, diMouseState.rgbButtons);
} }
/** /**