private void handleKeyboardInputs() { // Kedboard.poll() checks for keyboard input, buffered Keyboard.poll(); while (Keyboard.next()) { // only consider KeyDown Events if (!Keyboard.getEventKeyState()) { continue; } switch (Keyboard.getEventKey()) { case Keyboard.KEY_ESCAPE: running = false; break; case Keyboard.KEY_S: gameView.drawSightRange = !gameView.drawSightRange; break; case Keyboard.KEY_M: gameView.drawMessages = !gameView.drawMessages; break; case Keyboard.KEY_PERIOD: ticksPerSecond *= 1.3; resetReferenceValues(); break; case Keyboard.KEY_COMMA: ticksPerSecond /= 1.3; resetReferenceValues(); break; case Keyboard.KEY_P: paused = !paused; break; } } }
/* */ public synchronized void pollDevice() throws IOException { /* 76 */ if (!org.lwjgl.input.Keyboard.isCreated()) /* 77 */ return; /* 78 */ org.lwjgl.input.Keyboard.poll(); /* 79 */ for (Component component : getComponents()) { /* 80 */ Key key = (Key) component; /* 81 */ key.update(); /* */ } /* */ }
static void pollDevices() { // Poll the input devices while we're here if (Mouse.isCreated()) { Mouse.poll(); Mouse.updateCursor(); } if (Keyboard.isCreated()) { Keyboard.poll(); } if (Controllers.isCreated()) { Controllers.poll(); } }
@Override public void draw() { GL11.glClearColor(0.8f, 0.8f, 0.8f, 0.0f); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); GLU.gluLookAt(5.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); GL11.glRotatef(rot, 0.0f, 1.0f, 0.0f); GL11.glRotatef(rot2, 1.0f, 0.0f, 0.0f); drawer.draw(); Keyboard.poll(); while (Keyboard.next()) { if (Keyboard.getEventKeyState()) { if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) { return; } if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT) { rot -= 15.0f; } if (Keyboard.getEventKey() == Keyboard.KEY_LEFT) { rot += 15.0f; } if (Keyboard.getEventKey() == Keyboard.KEY_UP) { rot2 -= 15.0f; } if (Keyboard.getEventKey() == Keyboard.KEY_DOWN) { rot2 += 15.0f; } if (Keyboard.getEventKey() == Keyboard.KEY_T) { if (renderMode == GL11.GL_POLYGON) { renderMode = GL11.GL_LINE_LOOP; } else { renderMode = GL11.GL_POLYGON; } } } } }
@Override public void update(Engine engine, double time, double dt) { Window window = engine.getGlobal(Window.class); window.closing = Display.isCloseRequested(); if (Display.wasResized()) { window.width = Display.getWidth(); window.height = Display.getHeight(); } KeyboardComponent keyboard = engine.getGlobal(KeyboardComponent.class); Keyboard.poll(); for (int i = 0; i < keyboard.down.length; i++) { keyboard.pressed[i] = false; keyboard.released[i] = false; keyboard.down[i] = Keyboard.isKeyDown(i); } while (Keyboard.next()) { int key = Keyboard.getEventKey(); keyboard.pressed[key] = Keyboard.getEventKeyState(); keyboard.released[key] = !keyboard.pressed[key]; } MouseComponent mouse = engine.getGlobal(MouseComponent.class); Mouse.poll(); for (int i = 0; i < Mouse.getButtonCount(); i++) { mouse.pressed[i] = false; mouse.pressed[i] = false; mouse.down[i] = Mouse.isButtonDown(i); mouse.dx = 0; mouse.dy = 0; } while (Mouse.next()) { int btn = Mouse.getEventButton(); if (btn != -1) { mouse.pressed[btn] = Mouse.getEventButtonState(); mouse.released[btn] = !mouse.pressed[btn]; } else { mouse.dx += Mouse.getEventDX(); mouse.dy += Mouse.getEventDY(); } mouse.x = Mouse.getEventX(); mouse.y = Mouse.getEventY(); } mouse.nx = ((float) mouse.x / (float) window.width) * 2.0f - 1.0f; mouse.ny = ((float) mouse.y / (float) window.height) * 2.0f - 1.0f; Vec3[] mp = MouseUtils.mouseToWorld(window, engine.getGlobal(Camera.class), mouse); mouse.near = mp[0]; mouse.far = mp[1]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (Map m : objects.values()) { m.clear(); } // calculate the world matrix of each renderable, // and add them to their respective "shader buckets" for (RenderNode node : engine.getNodeList(RenderNode.class)) { ID<Entity> id = node.getEntity().getId(); worlds.put(id, StateUtils.getMatrix(node.state)); Program program = resourceManager.getProgram(node.renderable.shader); Map<ID<Entity>, RenderNode> renderables = objects.get(program); if (renderables == null) { renderables = new HashMap<>(); objects.put(program, renderables); } renderables.put(id, node); } for (ViewportNode vpnode : engine.getNodeList(ViewportNode.class)) { Camera camera = vpnode.camera; glViewport( (int) (window.width * vpnode.viewport.x), (int) (window.height * vpnode.viewport.y), (int) (window.width * vpnode.viewport.width), (int) (window.height * vpnode.viewport.height)); // for each shader, draw each object that uses that shader for (Map.Entry<Program, Map<ID<Entity>, RenderNode>> e : objects.entrySet()) { Program program = e.getKey(); program.use(); program.setView(camera.viewMatrix); program.setProjection(camera.projection); program.setSun(engine.getGlobal(Sun.class).location); program.setEye(camera.eye); for (Map.Entry<ID<Entity>, RenderNode> e2 : e.getValue().entrySet()) { RenderNode node = e2.getValue(); program.setWorld(worlds.get(e2.getKey())); program.setColour(node.renderable.colour); program.setOpacity(node.renderable.opacity); Mesh mesh = resourceManager.getMesh(node.mesh); mesh.draw(); } } glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_CULL_FACE); Program program = resourceManager.getProgram("passthrough"); program.use(); program.setView(camera.viewMatrix); program.setProjection(camera.projection); for (BBoxNode node : engine.getNodeList(BBoxNode.class)) { program.setWorld(node.bbox.getMatrix()); Vec3 e = node.bbox.getExtents(); glBegin(GL_QUADS); { glColor3f(1, 0, 1); glVertex3f(e.getX(), e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), -e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), -e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), -e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), -e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), -e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), -e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(e.getX(), e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), -e.getY(), e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), -e.getY(), -e.getZ()); glColor3f(1, 0, 1); glVertex3f(-e.getX(), e.getY(), -e.getZ()); } glEnd(); } glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_CULL_FACE); } Display.update(); }
public void poll() { Keyboard.poll(); }
/** Runs the actual test, using supplied arguments */ protected void execute(String[] args) { if (args.length < 1) { System.out.println("no argument supplied, assuming Footsteps.wav"); args = new String[] {"Footsteps.wav"}; } try { setDisplayMode(); Display.create(); } catch (Exception e) { e.printStackTrace(); } int lastError; Vector3f sourcePosition = new Vector3f(); Vector3f listenerPosition = new Vector3f(); // initialize keyboard try { Keyboard.create(); } catch (Exception e) { e.printStackTrace(); exit(-1); } // create 1 buffer and 1 source IntBuffer buffers = BufferUtils.createIntBuffer(1); IntBuffer sources = BufferUtils.createIntBuffer(1); // al generate buffers and sources buffers.position(0).limit(1); AL10.alGenBuffers(buffers); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } sources.position(0).limit(1); AL10.alGenSources(sources); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // load wave data WaveData wavefile = WaveData.create(args[0]); // copy to buffers AL10.alBufferData(buffers.get(0), wavefile.format, wavefile.data, wavefile.samplerate); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // unload file again wavefile.dispose(); // set up source input AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffers.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } AL10.alSourcef(sources.get(0), AL10.AL_REFERENCE_DISTANCE, 1024.0f); AL10.alSourcef(sources.get(0), AL10.AL_ROLLOFF_FACTOR, 0.5f); // lets loop the sound AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, AL10.AL_TRUE); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // play source 0 AL10.alSourcePlay(sources.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } System.out.println( "Move source with arrow keys\nMove listener with right shift and arrowkeys\nExit with ESC"); while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { Display.update(); Keyboard.poll(); if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { listenerPosition.x -= MOVEMENT; AL10.alListener3f( AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); System.out.println("listenerx: " + listenerPosition.x); } else { sourcePosition.x -= MOVEMENT; AL10.alSource3f( sources.get(0), AL10.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); System.out.println("sourcex: " + sourcePosition.x); } } if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { listenerPosition.x += MOVEMENT; AL10.alListener3f( AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); System.out.println("listenerx: " + listenerPosition.x); } else { sourcePosition.x += MOVEMENT; AL10.alSource3f( sources.get(0), AL10.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); System.out.println("sourcex: " + sourcePosition.x); } } if (Display.isCloseRequested()) { break; } try { Thread.sleep(100); } catch (InterruptedException inte) { } } // stop source 0 AL10.alSourceStop(sources.get(0)); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // delete buffers and sources sources.position(0).limit(1); AL10.alDeleteSources(sources); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } buffers.position(0).limit(1); AL10.alDeleteBuffers(buffers); if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) { exit(lastError); } // shutdown alExit(); }