From 4974a66398485239777469d41d412086df66dacb Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 7 Apr 2004 07:55:42 +0000 Subject: [PATCH] Folded read into poll to simplify interface --- src/java/org/lwjgl/input/Controller.java | 8 --- src/java/org/lwjgl/input/Keyboard.java | 52 ++++++++----------- src/java/org/lwjgl/input/Mouse.java | 42 +++++++-------- src/java/org/lwjgl/opengl/Window.java | 9 ---- .../org/lwjgl/test/input/KeyboardTest.java | 1 - 5 files changed, 41 insertions(+), 71 deletions(-) diff --git a/src/java/org/lwjgl/input/Controller.java b/src/java/org/lwjgl/input/Controller.java index 97158008..851e9958 100644 --- a/src/java/org/lwjgl/input/Controller.java +++ b/src/java/org/lwjgl/input/Controller.java @@ -228,14 +228,6 @@ public class Controller { return false; } - /** - * Read the controller's input buffer. This is not yet implemented in LWJGL so - * it always throws a RuntimeException. - */ - public static void read() { - throw new UnsupportedOperationException("Buffering is not implemented for Controllers."); - } - /** * Gets a button's name * @param button The button diff --git a/src/java/org/lwjgl/input/Keyboard.java b/src/java/org/lwjgl/input/Keyboard.java index 888c2bbf..f26d9be2 100644 --- a/src/java/org/lwjgl/input/Keyboard.java +++ b/src/java/org/lwjgl/input/Keyboard.java @@ -326,15 +326,37 @@ public class Keyboard { * poll fast enough. To receive all events, enable buffering by calling * enableBuffer, and read those events by calling read * + * This method also reads all keyboard events since last read if keyboard buffering is enabled. + * To use these values, you have to call next for each event you + * want to read. You can query which key caused the event by using + * getEventKey. To get the state of that key, for that event, use + * getEventKeyState - finally use getEventCharacter to get the + * character for that event. + * * @see org.lwjgl.input.Keyboard#isKeyDown(int key) * @see org.lwjgl.input.Keyboard#isStateKeySet(int key) + * @see org.lwjgl.input.Keyboard#next() * @see org.lwjgl.input.Keyboard#enableBuffer() - * @see org.lwjgl.input.Keyboard#read() + * @see org.lwjgl.input.Keyboard#getEventKey() + * @see org.lwjgl.input.Keyboard#getEventKeyState() + * @see org.lwjgl.input.Keyboard#getEventCharacter() */ public static void poll() { if (!created) throw new IllegalStateException("Keyboard must be created before you can poll the device"); nPoll(keyDownBuffer); + if (readBuffer != null) + read(); + } + + private static void read() { + readBuffer.compact(); + int numEvents = nRead(readBuffer, readBuffer.position()); + if (translationEnabled) + readBuffer.position(readBuffer.position() + numEvents*4); + else + readBuffer.position(readBuffer.position() + numEvents*2); + readBuffer.flip(); } /** @@ -345,34 +367,6 @@ public class Keyboard { */ private static native void nPoll(ByteBuffer keyDownBuffer); - /** - * Reads all keyboard events since last read. - * To use these values, you have to call next for each event you - * want to read. You can query which key caused the event by using - * getEventKey. To get the state of that key, for that event, use - * getEventKeyState - finally use getEventCharacter to get the - * character for that event. - * - * @see org.lwjgl.input.Keyboard#next() - * @see org.lwjgl.input.Keyboard#enableBuffer() - * @see org.lwjgl.input.Keyboard#getEventKey() - * @see org.lwjgl.input.Keyboard#getEventKeyState() - * @see org.lwjgl.input.Keyboard#getEventCharacter() - */ - public static void read() { - if (!created) - throw new IllegalStateException("Keyboard must be created before you can read events"); - if (readBuffer == null) - throw new IllegalStateException("Event buffering must be enabled before you can read events"); - readBuffer.compact(); - int numEvents = nRead(readBuffer, readBuffer.position()); - if (translationEnabled) - readBuffer.position(readBuffer.position() + numEvents*4); - else - readBuffer.position(readBuffer.position() + numEvents*2); - readBuffer.flip(); - } - /** * Native method to read the keyboard buffer * @return the total number of events read. diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index 3196fb30..301a41cb 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -323,6 +323,15 @@ public class Mouse { * poll fast enough. To receive all button events, enable buffering by calling * enableBuffer, and read those events by calling read * + * If buffering is enabled, this method also reads all button events since last read. + * To use these values, you have to call next for each event you + * want to read. You can query which button caused the event by using + * getEventButton. To get the state of that button, for that event, use + * getEventButtonState. + * + * @see org.lwjgl.input.Mouse#next() + * @see org.lwjgl.input.Mouse#getEventButton() + * @see org.lwjgl.input.Mouse#getEventButtonState() * @see org.lwjgl.input.Mouse#isButtonDown(int button) * @see org.lwjgl.input.Mouse#getX() * @see org.lwjgl.input.Mouse#getY() @@ -330,7 +339,6 @@ public class Mouse { * @see org.lwjgl.input.Mouse#getDY() * @see org.lwjgl.input.Mouse#getDWheel() * @see org.lwjgl.input.Mouse#enableBuffer() - * @see org.lwjgl.input.Mouse#read() */ public static void poll() { if (!created) @@ -362,6 +370,15 @@ public class Mouse { y = Window.getHeight(); } } + if (readBuffer != null) + read(); + } + + private static void read() { + readBuffer.compact(); + int numEvents = nRead(readBuffer, readBuffer.position()); + readBuffer.position(readBuffer.position() + numEvents*2); + readBuffer.flip(); } /** @@ -426,29 +443,6 @@ public class Mouse { */ private static native void nEnableBuffer() throws LWJGLException; - /** - * Reads all button events since last read. - * To use these values, you have to call next for each event you - * want to read. You can query which button caused the event by using - * getEventButton. To get the state of that button, for that event, use - * getEventButtonState. - * - * @see org.lwjgl.input.Mouse#next() - * @see org.lwjgl.input.Mouse#enableBuffer() - * @see org.lwjgl.input.Mouse#getEventButton() - * @see org.lwjgl.input.Mouse#getEventButtonState() - */ - public static void read() { - if (!created) - throw new IllegalStateException("Mouse must be created before you can read events"); - if (readBuffer == null) - throw new IllegalStateException("Event buffering must be enabled before you can read events"); - readBuffer.compact(); - int numEvents = nRead(readBuffer, readBuffer.position()); - readBuffer.position(readBuffer.position() + numEvents*2); - readBuffer.flip(); - } - /** * Native method to read the keyboard buffer * @return the total number of events read. diff --git a/src/java/org/lwjgl/opengl/Window.java b/src/java/org/lwjgl/opengl/Window.java index c6c1342a..b9ae59b9 100644 --- a/src/java/org/lwjgl/opengl/Window.java +++ b/src/java/org/lwjgl/opengl/Window.java @@ -241,22 +241,13 @@ public final class Window { // Poll the input devices while we're here if (Mouse.isCreated()) { Mouse.poll(); - if (Mouse.isBuffered()) { - Mouse.read(); - } Mouse.updateCursor(); } if (Keyboard.isCreated()) { Keyboard.poll(); - if (Keyboard.isBuffered()) { - Keyboard.read(); - } } if (Controller.isCreated()) { Controller.poll(); - if (Controller.isBuffered()) { - Controller.read(); - } } } diff --git a/src/java/org/lwjgl/test/input/KeyboardTest.java b/src/java/org/lwjgl/test/input/KeyboardTest.java index 935a02b7..2fcf3eae 100644 --- a/src/java/org/lwjgl/test/input/KeyboardTest.java +++ b/src/java/org/lwjgl/test/input/KeyboardTest.java @@ -128,7 +128,6 @@ public class KeyboardTest { //check keys, buffered Keyboard.poll(); - Keyboard.read(); int count = Keyboard.getNumKeyboardEvents(); while (Keyboard.next()) {