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

View File

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

View File

@ -62,11 +62,6 @@ static bool created = false;
static bool should_grab = 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_y;
static int last_z;
@ -106,19 +101,6 @@ static void centerCursor() {
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) {
unsigned int best_width, best_height;
if (XQueryBestCursor(getCurrentDisplay(), getCurrentWindow(), 1, 1, &best_width, &best_height) == 0) {
@ -378,18 +360,29 @@ void handlePointerMotion(XMotionEvent *event) {
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_y = -(current_y - last_y);
int moved_z = current_z - last_z;
env->SetStaticIntField(clazz, fid_dx, (jint)moved_x);
env->SetStaticIntField(clazz, fid_dy, (jint)moved_y);
env->SetStaticIntField(clazz, fid_dwheel, (jint)moved_z);
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;
}
coords[0] = moved_x;
coords[1] = moved_y;
coords[2] = moved_z;
last_x = current_x;
last_y = current_y;
last_z = current_z;
jbyteArray buttons_array = (jbyteArray)env->GetStaticObjectField(clazz, fid_buttons);
env->SetByteArrayRegion(buttons_array, 0, NUM_BUTTONS, buttons);
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] = buttons[i];
warpPointer();
}

View File

@ -50,11 +50,6 @@ static const int NUM_BUTTONS = 7;
static const int NUM_COOKIES = NUM_BUTTONS + 3;
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 bool buffer_enabled;
/*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;
}
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) {
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;
//pollMouseDevice();
dz = last_dz*WHEEL_SCALE;
dx = last_dx;
dy = -last_dy;
env->SetStaticIntField(clazz, fid_dx, (jint)dx);
env->SetStaticIntField(clazz, fid_dy, (jint)dy);
env->SetStaticIntField(clazz, fid_dwheel, (jint)dz);
jbyteArray buttons_array = (jbyteArray)env->GetStaticObjectField(clazz, fid_buttons);
env->SetByteArrayRegion(buttons_array, 0, NUM_BUTTONS, button_states);
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;
}
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();
}

View File

@ -54,12 +54,6 @@ static bool mHaswheel; // Temporary wheel check
static bool mCreate_success; // bool used to determine successfull creation
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 RECT windowRect;
static bool usingNativeCursor;
@ -71,8 +65,7 @@ void ShutdownMouse();
void CreateMouse();
void SetupMouse();
void InitializeMouseFields();
void CacheMouseFields(JNIEnv *env, jclass clsMouse);
void UpdateMouseFields(JNIEnv *env, jclass clsMouse);
void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj);
static void getScreenClientRect(RECT* clientRect, RECT* windowRect)
{
@ -84,14 +77,6 @@ static void getScreenClientRect(RECT* clientRect, RECT* windowRect)
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) {
return mHaswheel;
}
@ -113,7 +98,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass cl
}
ShowCursor(FALSE);
CacheMouseFields(env, clazz);
/* skip enumeration, since we only want system mouse */
CreateMouse();
@ -312,9 +296,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv *env, jclass c
* Method: nPoll
* 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();
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
*/
static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) {
static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj) {
HRESULT hRes;
DIMOUSESTATE diMouseState; // State of Mouse
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
hRes = mDIDevice->GetDeviceState(sizeof(DIMOUSESTATE), &diMouseState);
if (hRes != DI_OK) {
@ -473,10 +466,9 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) {
}
dy = -dy;
env->SetStaticIntField(clsMouse, fidMDX, (jint)dx);
env->SetStaticIntField(clsMouse, fidMDY, (jint)dy);
env->SetStaticIntField(clsMouse, fidMDWheel, (jint)diMouseState.lZ);
coords[0] = dx;
coords[0] = dy;
coords[0] = diMouseState.lZ;
for (int i = 0; i < mButtoncount; i++) {
if (diMouseState.rgbButtons[i] != 0) {
diMouseState.rgbButtons[i] = JNI_TRUE;
@ -484,16 +476,9 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse) {
diMouseState.rgbButtons[i] = JNI_FALSE;
}
}
jbyteArray mButtonsArray = (jbyteArray) env->GetStaticObjectField(clsMouse, fidMButtons);
env->SetByteArrayRegion(mButtonsArray, 0, mButtoncount, (const signed char *) diMouseState.rgbButtons);
}
/**
* 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");
int num_buttons = mButtoncount;
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];
}