Make poll deltas accumulative, and only reset on getD*

This commit is contained in:
Elias Naur 2004-04-06 14:23:19 +00:00
parent 65c16c3741
commit bf8cce9b59
5 changed files with 93 additions and 104 deletions

View File

@ -33,6 +33,7 @@
package org.lwjgl.input; package org.lwjgl.input;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -72,7 +73,7 @@ public class Mouse {
private static boolean created; private static boolean created;
/** The mouse buttons status from the last poll */ /** The mouse buttons status from the last poll */
private static byte[] buttons; private static ByteBuffer buttons;
/** X */ /** X */
private static int x; private static int x;
@ -80,6 +81,9 @@ public class Mouse {
/** Y */ /** Y */
private static int y; private static int y;
/** Buffer to hold the deltas dx, dy and dwheel */
private static IntBuffer coord_buffer;
/** Delta X */ /** Delta X */
private static int dx; private static int dx;
@ -177,8 +181,8 @@ public class Mouse {
if (currentCursor != null) { if (currentCursor != null) {
nSetNativeCursor(currentCursor.getHandle()); nSetNativeCursor(currentCursor.getHandle());
currentCursor.setTimeout(); currentCursor.setTimeout();
x = Window.getWidth() / 2; x = Window.getWidth() / 2;
y = Window.getHeight() / 2; y = Window.getHeight() / 2;
} else { } else {
nSetNativeCursor(0); nSetNativeCursor(0);
} }
@ -221,7 +225,6 @@ public class Mouse {
*/ */
private static void initialize() { private static void initialize() {
Sys.initialize(); Sys.initialize();
initIDs();
// Assign names to all the buttons // Assign names to all the buttons
buttonName = new String[16]; buttonName = new String[16];
@ -233,14 +236,9 @@ public class Mouse {
initialized = true; initialized = true;
} }
/**
* Register fields with the native library
*/
private static native void initIDs();
/** /**
* "Create" the mouse. The display must first have been created. * "Create" the mouse. The display must first have been created.
* *
* @throws LWJGLException if the mouse could not be created for any reason * @throws LWJGLException if the mouse could not be created for any reason
*/ */
public static void create() throws LWJGLException { public static void create() throws LWJGLException {
@ -256,10 +254,12 @@ public class Mouse {
hasWheel = nHasWheel(); hasWheel = nHasWheel();
created = true; created = true;
currentCursor = null; currentCursor = null;
dx = dy = dwheel = 0;
// set mouse buttons // set mouse buttons
buttonCount = nGetButtonCount(); buttonCount = nGetButtonCount();
buttons = new byte[buttonCount]; buttons = BufferUtils.createByteBuffer(buttonCount);
coord_buffer = BufferUtils.createIntBuffer(3);
} }
/** Native query of wheel support */ /** Native query of wheel support */
@ -305,6 +305,7 @@ public class Mouse {
return; return;
created = false; created = false;
buttons = null; buttons = null;
coord_buffer = null;
currentCursor = null; currentCursor = null;
nDestroy(); nDestroy();
@ -334,11 +335,17 @@ public class Mouse {
public static void poll() { public static void poll() {
if (!created) if (!created)
throw new IllegalStateException("Mouse must be created before you can poll it"); throw new IllegalStateException("Mouse must be created before you can poll it");
nPoll(); nPoll(coord_buffer, buttons);
int poll_dx = coord_buffer.get(0);
int poll_dy = coord_buffer.get(1);
int poll_dwheel = coord_buffer.get(2);
// set absolute position // set absolute position
x += dx; x += poll_dx;
y += dy; y += poll_dy;
dx += poll_dx;
dy += poll_dy;
dwheel += poll_dwheel;
// if window has been created, clamp to edges // if window has been created, clamp to edges
if(Window.isCreated()) { if(Window.isCreated()) {
@ -360,7 +367,7 @@ public class Mouse {
/** /**
* Native method to poll the mouse * Native method to poll the mouse
*/ */
private static native void nPoll(); private static native void nPoll(IntBuffer coord_buffer, ByteBuffer buttons);
/** /**
* See if a particular mouse button is down. * See if a particular mouse button is down.
@ -374,7 +381,7 @@ public class Mouse {
if (button >= buttonCount || button < 0) if (button >= buttonCount || button < 0)
return false; return false;
else else
return buttons[button] == 1; return buttons.get(button) == 1;
} }
/** /**
@ -505,24 +512,30 @@ public class Mouse {
} }
/** /**
* @return Movement on the x axis since last poll * @return Movement on the x axis since last time getDX() was called
*/ */
public static int getDX() { public static int getDX() {
return dx; int result = dx;
dx = 0;
return result;
} }
/** /**
* @return Movement on the y axis since last poll * @return Movement on the y axis since last time getDY() was called
*/ */
public static int getDY() { public static int getDY() {
return dy; int result = dy;
dy = 0;
return result;
} }
/** /**
* @return Movement of the wheel since last poll * @return Movement of the wheel since last time getDWheel() was called
*/ */
public static int getDWheel() { public static int getDWheel() {
return dwheel; int result = dwheel;
dwheel = 0;
return result;
} }
/** /**

View File

@ -7,7 +7,6 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* Inaccessible static: _00024assertionsDisabled */
#undef org_lwjgl_input_Mouse_CURSOR_ONE_BIT_TRANSPARENCY #undef org_lwjgl_input_Mouse_CURSOR_ONE_BIT_TRANSPARENCY
#define org_lwjgl_input_Mouse_CURSOR_ONE_BIT_TRANSPARENCY 1L #define org_lwjgl_input_Mouse_CURSOR_ONE_BIT_TRANSPARENCY 1L
#undef org_lwjgl_input_Mouse_CURSOR_8_BIT_ALPHA #undef org_lwjgl_input_Mouse_CURSOR_8_BIT_ALPHA
@ -18,6 +17,7 @@ extern "C" {
/* Inaccessible static: buttons */ /* Inaccessible static: buttons */
/* Inaccessible static: x */ /* Inaccessible static: x */
/* Inaccessible static: y */ /* Inaccessible static: y */
/* Inaccessible static: coord_buffer */
/* Inaccessible static: dx */ /* Inaccessible static: dx */
/* Inaccessible static: dy */ /* Inaccessible static: dy */
/* Inaccessible static: dwheel */ /* Inaccessible static: dwheel */
@ -32,7 +32,6 @@ extern "C" {
/* Inaccessible static: eventState */ /* Inaccessible static: eventState */
#undef org_lwjgl_input_Mouse_BUFFER_SIZE #undef org_lwjgl_input_Mouse_BUFFER_SIZE
#define org_lwjgl_input_Mouse_BUFFER_SIZE 50L #define org_lwjgl_input_Mouse_BUFFER_SIZE 50L
/* Inaccessible static: class_00024org_00024lwjgl_00024input_00024Mouse */
/* /*
* Class: org_lwjgl_input_Mouse * Class: org_lwjgl_input_Mouse
* Method: nGetNativeCursorCaps * Method: nGetNativeCursorCaps
@ -108,10 +107,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy
/* /*
* Class: org_lwjgl_input_Mouse * Class: org_lwjgl_input_Mouse
* Method: nPoll * Method: nPoll
* Signature: ()V * Signature: (Ljava/nio/IntBuffer;Ljava/nio/ByteBuffer;)V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll
(JNIEnv *, jclass); (JNIEnv *, jclass, jobject, jobject);
/* /*
* Class: org_lwjgl_input_Mouse * Class: org_lwjgl_input_Mouse

View File

@ -62,11 +62,6 @@ static bool created = false;
static bool should_grab = false; static bool should_grab = false;
static bool native_cursor = false; static bool native_cursor = false;
static jfieldID fid_buttons = NULL;
static jfieldID fid_dx = NULL;
static jfieldID fid_dy = NULL;
static jfieldID fid_dwheel = NULL;
static int last_x; static int last_x;
static int last_y; static int last_y;
static int last_z; static int last_z;
@ -106,19 +101,6 @@ static void centerCursor() {
last_y = current_y; last_y = current_y;
} }
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs
(JNIEnv * env, jclass clazz)
{
if (fid_buttons == NULL)
fid_buttons = env->GetStaticFieldID(clazz, "buttons", "[B");
if (fid_dx == NULL)
fid_dx = env->GetStaticFieldID(clazz, "dx", "I");
if (fid_dy == NULL)
fid_dy = env->GetStaticFieldID(clazz, "dy", "I");
if (fid_dwheel == NULL)
fid_dwheel = env->GetStaticFieldID(clazz, "dwheel", "I");
}
static bool blankCursor(void) { static bool blankCursor(void) {
unsigned int best_width, best_height; unsigned int best_width, best_height;
if (XQueryBestCursor(getCurrentDisplay(), getCurrentWindow(), 1, 1, &best_width, &best_height) == 0) { if (XQueryBestCursor(getCurrentDisplay(), getCurrentWindow(), 1, 1, &best_width, &best_height) == 0) {
@ -378,18 +360,29 @@ void handlePointerMotion(XMotionEvent *event) {
setCursorPos(event->x, event->y); setCursorPos(event->x, event->y);
} }
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz, jobject coord_buffer_obj, jobject button_buffer_obj) {
int moved_x = current_x - last_x; int moved_x = current_x - last_x;
int moved_y = -(current_y - last_y); int moved_y = -(current_y - last_y);
int moved_z = current_z - last_z; int moved_z = current_z - last_z;
env->SetStaticIntField(clazz, fid_dx, (jint)moved_x); int *coords = (int *)env->GetDirectBufferAddress(coord_buffer_obj);
env->SetStaticIntField(clazz, fid_dy, (jint)moved_y); int coords_length = env->GetDirectBufferCapacity(coord_buffer_obj);
env->SetStaticIntField(clazz, fid_dwheel, (jint)moved_z); unsigned char *buttons_buffer = (unsigned char *)env->GetDirectBufferAddress(button_buffer_obj);
int buttons_length = env->GetDirectBufferCapacity(button_buffer_obj);
if (coords_length < 3) {
printfDebug("ERROR: Not enough space in coords array: %d < 3\n", coords_length);
return;
}
coords[0] = moved_x;
coords[1] = moved_y;
coords[2] = moved_z;
last_x = current_x; last_x = current_x;
last_y = current_y; last_y = current_y;
last_z = current_z; last_z = current_z;
jbyteArray buttons_array = (jbyteArray)env->GetStaticObjectField(clazz, fid_buttons); int num_buttons = NUM_BUTTONS;
env->SetByteArrayRegion(buttons_array, 0, NUM_BUTTONS, buttons); if (num_buttons > buttons_length)
num_buttons = buttons_length;
for (int i = 0; i < num_buttons; i++)
buttons_buffer[i] = buttons[i];
warpPointer(); warpPointer();
} }

View File

@ -50,11 +50,6 @@ static const int NUM_BUTTONS = 7;
static const int NUM_COOKIES = NUM_BUTTONS + 3; static const int NUM_COOKIES = NUM_BUTTONS + 3;
static const int WHEEL_SCALE = 120; static const int WHEEL_SCALE = 120;
static jfieldID fid_dx;
static jfieldID fid_dy;
static jfieldID fid_dwheel;
static jfieldID fid_buttons;
static jbyte button_states[NUM_BUTTONS]; static jbyte button_states[NUM_BUTTONS];
static bool buffer_enabled; static bool buffer_enabled;
/*static int x_axis_index = NUM_BUTTONS; /*static int x_axis_index = NUM_BUTTONS;
@ -176,13 +171,6 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetButtonCount(JNIEnv *, jcla
return NUM_BUTTONS; return NUM_BUTTONS;
} }
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs(JNIEnv * env, jclass clazz) {
fid_dx = env->GetStaticFieldID(clazz, "dx", "I");
fid_dy = env->GetStaticFieldID(clazz, "dy", "I");
fid_dwheel = env->GetStaticFieldID(clazz, "dwheel", "I");
fid_buttons = env->GetStaticFieldID(clazz, "buttons", "[B");
}
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps(JNIEnv *env, jclass clazz) { JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps(JNIEnv *env, jclass clazz) {
return 0; return 0;
} }
@ -235,17 +223,28 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv * env, jclass
} }
} }
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz, jobject coord_buffer_obj, jobject button_buffer_obj) {
int dx, dy, dz; int dx, dy, dz;
//pollMouseDevice(); //pollMouseDevice();
dz = last_dz*WHEEL_SCALE; dz = last_dz*WHEEL_SCALE;
dx = last_dx; dx = last_dx;
dy = -last_dy; dy = -last_dy;
env->SetStaticIntField(clazz, fid_dx, (jint)dx); int *coords = (int *)env->GetDirectBufferAddress(coord_buffer_obj);
env->SetStaticIntField(clazz, fid_dy, (jint)dy); int coords_length = env->GetDirectBufferCapacity(coord_buffer_obj);
env->SetStaticIntField(clazz, fid_dwheel, (jint)dz); unsigned char *buttons_buffer = (unsigned char *)env->GetDirectBufferAddress(button_buffer_obj);
jbyteArray buttons_array = (jbyteArray)env->GetStaticObjectField(clazz, fid_buttons); int buttons_length = env->GetDirectBufferCapacity(button_buffer_obj);
env->SetByteArrayRegion(buttons_array, 0, NUM_BUTTONS, button_states); if (coords_length < 3) {
printfDebug("ERROR: Not enough space in coords array: %d < 3\n", coords_length);
return;
}
coords[0] = dx;
coords[1] = dy;
coords[2] = dz;
int num_buttons = NUM_BUTTONS;
if (num_buttons > buttons_length)
num_buttons = buttons_length;
for (int i = 0; i < num_buttons; i++)
buttons_buffer[i] = button_states[i];
resetDeltas(); resetDeltas();
} }

View File

@ -54,12 +54,6 @@ static bool mHaswheel; // Temporary wheel check
static bool mCreate_success; // bool used to determine successfull creation static bool mCreate_success; // bool used to determine successfull creation
static bool mFirstTimeInitialization = true; // boolean to determine first time initialization static bool mFirstTimeInitialization = true; // boolean to determine first time initialization
// Cached fields of Mouse.java
static jfieldID fidMButtons;
static jfieldID fidMDX;
static jfieldID fidMDY;
static jfieldID fidMDWheel;
static POINT cursorPos; static POINT cursorPos;
static RECT windowRect; static RECT windowRect;
static bool usingNativeCursor; static bool usingNativeCursor;
@ -71,8 +65,7 @@ void ShutdownMouse();
void CreateMouse(); void CreateMouse();
void SetupMouse(); void SetupMouse();
void InitializeMouseFields(); void InitializeMouseFields();
void CacheMouseFields(JNIEnv *env, jclass clsMouse); void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj);
void UpdateMouseFields(JNIEnv *env, jclass clsMouse);
static void getScreenClientRect(RECT* clientRect, RECT* windowRect) static void getScreenClientRect(RECT* clientRect, RECT* windowRect)
{ {
@ -84,14 +77,6 @@ static void getScreenClientRect(RECT* clientRect, RECT* windowRect)
clientRect->right += clientRect->left; clientRect->right += clientRect->left;
} }
/**
* Initializes any field ids
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs(JNIEnv * env, jclass clazz) {
/* Cache fields in Mouse */
CacheMouseFields(env, clazz);
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) {
return mHaswheel; return mHaswheel;
} }
@ -113,7 +98,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass cl
} }
ShowCursor(FALSE); ShowCursor(FALSE);
CacheMouseFields(env, clazz);
/* skip enumeration, since we only want system mouse */ /* skip enumeration, since we only want system mouse */
CreateMouse(); CreateMouse();
@ -312,9 +296,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv *env, jclass c
* Method: nPoll * Method: nPoll
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz, jobject coord_buffer_obj, jobject button_buffer_obj) {
mDIDevice->Acquire(); mDIDevice->Acquire();
UpdateMouseFields(env, clazz); UpdateMouseFields(env, clazz, coord_buffer_obj, button_buffer_obj);
} }
/** /**
@ -444,11 +428,20 @@ static void getGDICursorDelta(int* return_dx, int* return_dy) {
/** /**
* Updates the fields on the Mouse * Updates the fields on the Mouse
*/ */
static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) { static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj) {
HRESULT hRes; HRESULT hRes;
DIMOUSESTATE diMouseState; // State of Mouse DIMOUSESTATE diMouseState; // State of Mouse
int dx, dy; int dx, dy;
int *coords = (int *)env->GetDirectBufferAddress(coord_buffer_obj);
int coords_length = env->GetDirectBufferCapacity(coord_buffer_obj);
unsigned char *buttons_buffer = (unsigned char *)env->GetDirectBufferAddress(button_buffer_obj);
int buttons_length = env->GetDirectBufferCapacity(button_buffer_obj);
if (coords_length < 3) {
printfDebug("ERROR: Not enough space in coords array: %d < 3\n", coords_length);
return;
}
// get data from the Mouse // get data from the Mouse
hRes = mDIDevice->GetDeviceState(sizeof(DIMOUSESTATE), &diMouseState); hRes = mDIDevice->GetDeviceState(sizeof(DIMOUSESTATE), &diMouseState);
if (hRes != DI_OK) { if (hRes != DI_OK) {
@ -473,10 +466,9 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) {
} }
dy = -dy; dy = -dy;
env->SetStaticIntField(clsMouse, fidMDX, (jint)dx); coords[0] = dx;
env->SetStaticIntField(clsMouse, fidMDY, (jint)dy); coords[0] = dy;
env->SetStaticIntField(clsMouse, fidMDWheel, (jint)diMouseState.lZ); coords[0] = diMouseState.lZ;
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;
@ -484,16 +476,9 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) {
diMouseState.rgbButtons[i] = JNI_FALSE; diMouseState.rgbButtons[i] = JNI_FALSE;
} }
} }
jbyteArray mButtonsArray = (jbyteArray) env->GetStaticObjectField(clsMouse, fidMButtons); int num_buttons = mButtoncount;
env->SetByteArrayRegion(mButtonsArray, 0, mButtoncount, (const signed char *) diMouseState.rgbButtons); if (num_buttons > buttons_length)
} num_buttons = buttons_length;
for (int i = 0; i < num_buttons; i++)
/** buttons_buffer[i] = (unsigned char)diMouseState.rgbButtons[i];
* Caches the field ids for quicker access
*/
void CacheMouseFields(JNIEnv* env, jclass clsMouse) {
fidMButtons = env->GetStaticFieldID(clsMouse, "buttons", "[B");
fidMDX = env->GetStaticFieldID(clsMouse, "dx", "I");
fidMDY = env->GetStaticFieldID(clsMouse, "dy", "I");
fidMDWheel = env->GetStaticFieldID(clsMouse, "dwheel", "I");
} }