From cafa7452aa74f86fb266e673254e3fc1f896d4d5 Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Tue, 17 Jun 2003 21:18:44 +0000 Subject: [PATCH] add simple keyboard test - for completeness sake - needs to be made better --- .../org/lwjgl/test/input/KeyboardTest.java | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 src/java/org/lwjgl/test/input/KeyboardTest.java diff --git a/src/java/org/lwjgl/test/input/KeyboardTest.java b/src/java/org/lwjgl/test/input/KeyboardTest.java new file mode 100644 index 00000000..8fd8cf83 --- /dev/null +++ b/src/java/org/lwjgl/test/input/KeyboardTest.java @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.input; + +import org.lwjgl.DisplayMode; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GLU; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + *
+ * Keyboard test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class KeyboardTest { + + /** OpenGL instance */ + private GL gl; + + /** GLU instance */ + private GLU glu; + + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + private boolean bufferedKeyboard; + private boolean translatedKeyboard; + private int bufferSize; + + /** Creates a new instance of MouseTest */ + public KeyboardTest() { + } + + private void initialize() { + // create display and opengl + setupDisplay(false); + + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void setupDisplay(boolean fullscreen) { + try { + gl = new GL("KeyboardTest", 50, 50, 640, 480, 16, 0, 0, 0); + gl.create(); + + glu = new GLU(gl); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f); + glu.ortho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(); + + createKeyboard(); + + wiggleKeyboard(); + + Keyboard.destroy(); + gl.destroy(); + } + + private void createKeyboard() { + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void wiggleKeyboard() { + Keyboard.enableBuffer(); + Keyboard.enableTranslation(); + + while (!gl.isCloseRequested()) { + gl.tick(); + + if(gl.isMinimized()) { + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + inte.printStackTrace(); + } + continue; + } + + //check keys, buffered + Keyboard.read(); + + int count = Keyboard.getNumKeyboardEvents(); + System.out.println("Read " + count + " events"); + while(Keyboard.next()) { + System.out.println("Checking key:" + Keyboard.getKeyName(Keyboard.key)); + if(Keyboard.key == Keyboard.KEY_ESCAPE) { + return; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { + position.x += 1; + } + + if (Keyboard.key == Keyboard.KEY_RIGHT) { + position.x += 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { + position.x -= 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { + position.y += 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { + position.y -= 1; + } + + } + if (count > 0) { + System.out.println(); + } + + if(position.x<0) { + position.x = 0; + } else if (position.x>640-60) { + position.x = 640-60; + } + + if(position.y < 0) { + position.y = 0; + } else if (position.y>480-30) { + position.y = 480-30; + } + + + render(); + + gl.paint(); + + try { + Thread.sleep(0); + } catch (Exception e) { + } + } + } + + private void render() { + gl.clear(GL.COLOR_BUFFER_BIT); + + gl.begin(GL.POLYGON); + { + float color = 1.0f; + int buttonDown = 0; + gl.color3f(color, color, color); + + gl.vertex2f(position.x + 0.0f, position.y + 0.0f); + gl.vertex2f(position.x + 0.0f, position.y + 30.0f); + gl.vertex2f(position.x + 40.0f, position.y + 30.0f); + gl.vertex2f(position.x + 60.0f, position.y + 15.f); + gl.vertex2f(position.x + 40.0f, position.y + 0.0f); + } + gl.end(); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + KeyboardTest kt = new KeyboardTest(); + kt.executeTest(); + } +}