/** Enters the Main-Loop */ private void beginLoop() { this.isRunning = true; this.timing.init(); while (this.isRunning) { int delta = this.timing.getDelta(); this.handleDisplayResize(); if (Display.isCloseRequested()) this.isRunning = false; // Mouse updates this.profiler.start("Mouse updates"); this.mouseX = Mouse.getX(); this.mouseY = this.height - Mouse.getY(); if (!this.isGamePaused) { this.input.mousePos(this.mouseX, this.mouseY); if (Mouse.isButtonDown(0) && !this.lastButtonState) this.input.mouseClick(this.mouseX, this.mouseY); this.lastButtonState = Mouse.isButtonDown(0); } // Scrolling if (!this.isGamePaused) { int scroll = Mouse.getDWheel(); if (scroll > 0) this.camera.zoom(-1); else if (scroll < 0) this.camera.zoom(1); } // Keyboard updates this.profiler.endStart("Keyboard updates"); this.keyboardListener.update(); // Audio this.profiler.endStart("Audio updates"); this.soundEngine.update(delta); // Rendering this.profiler.endStart("Render game"); this.camera.update(delta); this.renderer.render(this.camera, this.logic.getGameState(), delta); this.profiler.endStart("Render GUI"); this.guiRenderer.render(this.width, this.height, this.mouseX, this.mouseY); this.profiler.end(); Display.update(); Display.sync(60); int error = glGetError(); if (error != GL_NO_ERROR) System.out.println("GLError " + error + ": " + GLU.gluErrorString(error)); this.timing.updateFPS(); } this.soundEngine.dispose(); Settings.saveSettings(this.settings); if (Display.isCreated()) Display.destroy(); }
// XXX: Need another look at this to make it generic? private void giveBackContext() throws LWJGLException { if (Display.isCreated()) { Display.makeCurrent(); ContextManager.switchContext(_oldContext.getContextKey()); } else if (_oldContext.getContextKey() instanceof AWTGLCanvas) { ((AWTGLCanvas) _oldContext.getContextKey()).makeCurrent(); ContextManager.switchContext(_oldContext.getContextKey()); } }
public static void setWindowResolution(int width, int height) { Globals.windowWidth = width; Globals.windowHeight = height; Globals.aspectRatio = (float) Globals.windowWidth / (float) Globals.windowHeight; Globals.windowMatrix.set( 1 / Globals.aspectRatio, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); // for(IWindowChangeListener lis : Globals.windowChangeListener) // { // lis.onWindowResolutionChange(Globals.windowWidth, Globals.windowHeight); // } try { DisplayMode[] modes = Display.getAvailableDisplayModes(); DisplayMode finalMode = new DisplayMode(Globals.getWindowWidth(), Globals.getWindowHeight()); for (int i = 0; i < modes.length; i++) { DisplayMode current = modes[i]; if (current.getWidth() == Globals.getWindowWidth() && current.getHeight() == Globals.getWindowHeight() && current.getBitsPerPixel() == 32 && current.getFrequency() == 60) finalMode = current; } Display.setDisplayMode(finalMode); if (!Display.isCreated()) { PixelFormat pixelFormat = new PixelFormat(); ContextAttribs contextAtrributes = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true); Display.create(pixelFormat, contextAtrributes); } GL11.glViewport(0, 0, Globals.getWindowWidth(), Globals.getWindowHeight()); } catch (LWJGLException e) { e.printStackTrace(); System.exit(-1); } Display.update(); }
public int showDialog(Frame parentWindow, Throwable error) { if (this._dialog != null) { throw new IllegalStateException("CustomOreGen Config Error Dialog is already open!"); } else { this._dialog = new Dialog(parentWindow, "CustomOreGen Config Error", false); this._dialog.addWindowListener(this); TextArea text = new TextArea(this.getMessage(error), 30, 120, 1); text.setEditable(false); text.setBackground(Color.WHITE); text.setFont(new Font("Monospaced", 0, 12)); this._dialog.add(text); Panel buttonPanel = new Panel(); this._abort = new Button("Abort"); this._abort.addActionListener(this); buttonPanel.add(this._abort); this._retry = new Button("Retry"); this._retry.addActionListener(this); buttonPanel.add(this._retry); this._ignore = new Button("Ignore"); this._ignore.addActionListener(this); buttonPanel.add(this._ignore); buttonPanel.setLayout(new BoxLayout(buttonPanel, 0)); this._dialog.add(buttonPanel); this._dialog.setLayout(new BoxLayout(this._dialog, 1)); this._dialog.pack(); Point loc = parentWindow.getLocation(); Dimension parentSize = parentWindow.getSize(); Dimension size = this._dialog.getSize(); loc.x += (parentSize.width - size.width) / 2; loc.y += (parentSize.height - size.height) / 2; this._dialog.setLocation(loc); this._waiting = true; this._returnVal = 0; this._dialog.setVisible(true); boolean usingLWJGL = false; // CustomOreGenBase.isClassLoaded("org.lwjgl.opengl.Display"); while (this._waiting) { if (usingLWJGL && Display.isCreated()) { Display.processMessages(); } } this._abort = null; this._retry = null; this._ignore = null; this._dialog.setVisible(false); this._dialog.dispose(); this._dialog = null; return this._returnVal; } }
@Override public void setMouseCursor(@Nullable MouseCursor cursor) { if (!Display.isCreated()) { throw new IllegalStateException("The game display was not yet created."); } try { if (cursor == null) { Mouse.setNativeCursor(null); } else if (cursor instanceof GdxLwjglCursor) { Mouse.setNativeCursor(((GdxLwjglCursor) cursor).getLwjglCursor()); } } catch (@Nonnull LWJGLException ignored) { // nothing to do } }
/** Does LWJGL display initialization in the OpenGL thread */ protected boolean initInThread() { try { if (!JmeSystem.isLowPermissions()) { // Enable uncaught exception handler only for current thread Thread.currentThread() .setUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread thread, Throwable thrown) { listener.handleError( "Uncaught exception thrown in " + thread.toString(), thrown); if (needClose.get()) { // listener.handleError() has requested the // context to close. Satisfy request. deinitInThread(); } } }); } // For canvas, this will create a pbuffer, // allowing us to query information. // When the canvas context becomes available, it will // be replaced seamlessly. createContext(settings); printContextInitInfo(); created.set(true); super.internalCreate(); } catch (Exception ex) { try { if (Display.isCreated()) Display.destroy(); } catch (Exception ex2) { logger.log(Level.WARNING, null, ex2); } listener.handleError("Failed to create display", ex); return false; // if we failed to create display, do not continue } listener.initialize(); return true; }
public static void setGameSettings(GameSettings var0) { if (gameSettings == null) { if (!Display.isCreated()) { return; } checkOpenGlCaps(); startVersionCheckThread(); } gameSettings = var0; minecraft = gameSettings.mc; minecraftThread = Thread.currentThread(); if (gameSettings != null) { antialiasingLevel = gameSettings.ofAaLevel; } updateThreadPriorities(); }
/** * Sets one or more icons for the Display. * * <ul> * <li>On Windows you should supply at least one 16x16 icon and one 32x32. * <li>Linux (and similar platforms) expect one 32x32 icon. * <li>Mac OS X should be supplied one 128x128 icon * </ul> * * The implementation will use the supplied ByteBuffers with image data in RGBA (size must be a * power of two) and perform any conversions nescesarry for the specific platform. * * <p><b>NOTE:</b> The display will make a deep copy of the supplied byte buffer array, for the * purpose of recreating the icons when you go back and forth fullscreen mode. You therefore only * need to set the icon once per instance. * * @param icons Array of icons in RGBA mode. Pass the icons in order of preference. * @return number of icons used, or 0 if display hasn't been created */ public static int setIcon(ByteBuffer[] icons) { synchronized (GlobalLock.lock) { // make deep copy so we dont rely on the supplied buffers later on // don't recache! if (cached_icons != icons) { cached_icons = new ByteBuffer[icons.length]; for (int i = 0; i < icons.length; i++) { cached_icons[i] = BufferUtils.createByteBuffer(icons[i].capacity()); int old_position = icons[i].position(); cached_icons[i].put(icons[i]); icons[i].position(old_position); cached_icons[i].flip(); } } if (Display.isCreated() && parent == null) { return display_impl.setIcon(cached_icons); } else { return 0; } } }
public static void tick() { // Mouse.setCursorPosition(Display.getWidth()/2, Display.getHeight()/2); if (Display.isCreated()) { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { exit = true; } if (Keyboard.isKeyDown(Keyboard.KEY_R)) { Mouse.setGrabbed(false); } if (Mouse.isButtonDown(0)) { Mouse.setGrabbed(true); } Display.update(); if (Display.isCloseRequested() || exit) { game.close(); Display.destroy(); } } for (int i = 0; i < world.entities.size(); i++) { world.entities.get(i).onTick(); } }
/** * Poll the state of the input * * @param width The width of the game view * @param height The height of the game view */ public void poll(int width, int height) { if (paused) { clearControlPressedRecord(); clearKeyPressedRecord(); clearMousePressedRecord(); while (Keyboard.next()) {} while (Mouse.next()) {} return; } if (!Display.isActive()) { clearControlPressedRecord(); clearKeyPressedRecord(); clearMousePressedRecord(); } // add any listeners requested since last time for (int i = 0; i < keyListenersToAdd.size(); i++) { addKeyListenerImpl((KeyListener) keyListenersToAdd.get(i)); } keyListenersToAdd.clear(); for (int i = 0; i < mouseListenersToAdd.size(); i++) { addMouseListenerImpl((MouseListener) mouseListenersToAdd.get(i)); } mouseListenersToAdd.clear(); if (doubleClickTimeout != 0) { if (System.currentTimeMillis() > doubleClickTimeout) { doubleClickTimeout = 0; } } this.height = height; Iterator allStarts = allListeners.iterator(); while (allStarts.hasNext()) { ControlledInputReciever listener = (ControlledInputReciever) allStarts.next(); listener.inputStarted(); } while (Keyboard.next()) { if (Keyboard.getEventKeyState()) { int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter()); keys[eventKey] = Keyboard.getEventCharacter(); pressed[eventKey] = true; nextRepeat[eventKey] = System.currentTimeMillis() + keyRepeatInitial; consumed = false; for (int i = 0; i < keyListeners.size(); i++) { KeyListener listener = (KeyListener) keyListeners.get(i); if (listener.isAcceptingInput()) { listener.keyPressed(eventKey, Keyboard.getEventCharacter()); if (consumed) { break; } } } } else { int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter()); nextRepeat[eventKey] = 0; consumed = false; for (int i = 0; i < keyListeners.size(); i++) { KeyListener listener = (KeyListener) keyListeners.get(i); if (listener.isAcceptingInput()) { listener.keyReleased(eventKey, keys[eventKey]); if (consumed) { break; } } } } } while (Mouse.next()) { if (Mouse.getEventButton() >= 0 && Mouse.getEventButton() < mousePressed.length) { if (Mouse.getEventButtonState()) { consumed = false; mousePressed[Mouse.getEventButton()] = true; pressedX = (int) (xoffset + (Mouse.getEventX() * scaleX)); pressedY = (int) (yoffset + ((height - Mouse.getEventY()) * scaleY)); for (int i = 0; i < mouseListeners.size(); i++) { MouseListener listener = (MouseListener) mouseListeners.get(i); if (listener.isAcceptingInput()) { listener.mousePressed(Mouse.getEventButton(), pressedX, pressedY); if (consumed) { break; } } } } else { consumed = false; mousePressed[Mouse.getEventButton()] = false; int releasedX = (int) (xoffset + (Mouse.getEventX() * scaleX)); int releasedY = (int) (yoffset + ((height - Mouse.getEventY()) * scaleY)); if ((pressedX != -1) && (pressedY != -1) && (Math.abs(pressedX - releasedX) < mouseClickTolerance) && (Math.abs(pressedY - releasedY) < mouseClickTolerance)) { considerDoubleClick(Mouse.getEventButton(), releasedX, releasedY); pressedX = pressedY = -1; } for (int i = 0; i < mouseListeners.size(); i++) { MouseListener listener = (MouseListener) mouseListeners.get(i); if (listener.isAcceptingInput()) { listener.mouseReleased(Mouse.getEventButton(), releasedX, releasedY); if (consumed) { break; } } } } } else { if (Mouse.isGrabbed() && displayActive) { if ((Mouse.getEventDX() != 0) || (Mouse.getEventDY() != 0)) { consumed = false; for (int i = 0; i < mouseListeners.size(); i++) { MouseListener listener = (MouseListener) mouseListeners.get(i); if (listener.isAcceptingInput()) { if (anyMouseDown()) { listener.mouseDragged(0, 0, Mouse.getEventDX(), -Mouse.getEventDY()); } else { listener.mouseMoved(0, 0, Mouse.getEventDX(), -Mouse.getEventDY()); } if (consumed) { break; } } } } } int dwheel = Mouse.getEventDWheel(); wheel += dwheel; if (dwheel != 0) { consumed = false; for (int i = 0; i < mouseListeners.size(); i++) { MouseListener listener = (MouseListener) mouseListeners.get(i); if (listener.isAcceptingInput()) { listener.mouseWheelMoved(dwheel); if (consumed) { break; } } } } } } if (!displayActive || Mouse.isGrabbed()) { lastMouseX = getMouseX(); lastMouseY = getMouseY(); } else { if ((lastMouseX != getMouseX()) || (lastMouseY != getMouseY())) { consumed = false; for (int i = 0; i < mouseListeners.size(); i++) { MouseListener listener = (MouseListener) mouseListeners.get(i); if (listener.isAcceptingInput()) { if (anyMouseDown()) { listener.mouseDragged(lastMouseX, lastMouseY, getMouseX(), getMouseY()); } else { listener.mouseMoved(lastMouseX, lastMouseY, getMouseX(), getMouseY()); } if (consumed) { break; } } } lastMouseX = getMouseX(); lastMouseY = getMouseY(); } } if (controllersInited) { for (int i = 0; i < getControllerCount(); i++) { int count = ((Controller) controllers.get(i)).getButtonCount() + 3; count = Math.min(count, 24); for (int c = 0; c <= count; c++) { try { if (controls[i][c] && !isControlDwn(c, i)) { controls[i][c] = false; fireControlRelease(c, i); } else if (!controls[i][c] && isControlDwn(c, i)) { controllerPressed[i][c] = true; controls[i][c] = true; fireControlPress(c, i); } } catch (ArrayIndexOutOfBoundsException e) { break; } } } } if (keyRepeat) { for (int i = 0; i < 1024; i++) { if (pressed[i] && (nextRepeat[i] != 0)) { if (System.currentTimeMillis() > nextRepeat[i]) { nextRepeat[i] = System.currentTimeMillis() + keyRepeatInterval; consumed = false; for (int j = 0; j < keyListeners.size(); j++) { KeyListener listener = (KeyListener) keyListeners.get(j); if (listener.isAcceptingInput()) { listener.keyPressed(i, keys[i]); if (consumed) { break; } } } } } } } Iterator all = allListeners.iterator(); while (all.hasNext()) { ControlledInputReciever listener = (ControlledInputReciever) all.next(); listener.inputEnded(); } if (Display.isCreated()) { displayActive = Display.isActive(); } }
protected int determineMaxSamples(int requestedSamples) { boolean displayWasCurrent = false; try { // If we already have a valid context, determine samples using current // context. if (Display.isCreated() && Display.isCurrent()) { if (GLContext.getCapabilities().GL_ARB_framebuffer_object) { return GL11.glGetInteger(ARBFramebufferObject.GL_MAX_SAMPLES); } else if (GLContext.getCapabilities().GL_EXT_framebuffer_multisample) { return GL11.glGetInteger(EXTFramebufferMultisample.GL_MAX_SAMPLES_EXT); } // Doesn't support any of the needed extensions .. continue down. displayWasCurrent = true; } } catch (LWJGLException ex) { listener.handleError("Failed to check if display is current", ex); } if ((Pbuffer.getCapabilities() & Pbuffer.PBUFFER_SUPPORTED) == 0) { // No pbuffer, assume everything is supported. return Integer.MAX_VALUE; } else { Pbuffer pb = null; if (!displayWasCurrent) { // OpenGL2 method: Create pbuffer and query samples // from GL_ARB_framebuffer_object or GL_EXT_framebuffer_multisample. try { pb = new Pbuffer(1, 1, new PixelFormat(0, 0, 0), null); pb.makeCurrent(); if (GLContext.getCapabilities().GL_ARB_framebuffer_object) { return GL11.glGetInteger(ARBFramebufferObject.GL_MAX_SAMPLES); } else if (GLContext.getCapabilities().GL_EXT_framebuffer_multisample) { return GL11.glGetInteger(EXTFramebufferMultisample.GL_MAX_SAMPLES_EXT); } // OpenGL2 method failed. } catch (LWJGLException ex) { // Something else failed. return Integer.MAX_VALUE; } finally { if (pb != null) { pb.destroy(); pb = null; } } } // OpenGL1 method (DOESNT WORK RIGHT NOW ..) requestedSamples = FastMath.nearestPowerOfTwo(requestedSamples); try { requestedSamples = Integer.MAX_VALUE; /* while (requestedSamples > 1) { try { pb = new Pbuffer(1, 1, new PixelFormat(16, 0, 8, 0, requestedSamples), null); } catch (LWJGLException ex) { if (ex.getMessage().startsWith("Failed to find ARB pixel format")) { // Unsupported format, so continue. requestedSamples = FastMath.nearestPowerOfTwo(requestedSamples / 2); } else { // Something else went wrong .. return Integer.MAX_VALUE; } } finally { if (pb != null){ pb.destroy(); pb = null; } } }*/ } finally { if (displayWasCurrent) { try { Display.makeCurrent(); } catch (LWJGLException ex) { listener.handleError("Failed to make display current after checking samples", ex); } } } return requestedSamples; } }
/** * Poll the state of the input * * @param width The width of the game view * @param height The height of the game view */ public void poll(int width, int height) { if (paused) { while (Keyboard.next()) {} while (Mouse.next()) {} return; } this.height = height; while (Keyboard.next()) { if (Keyboard.getEventKeyState()) { int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter()); keys[eventKey] = Keyboard.getEventCharacter(); pressed[eventKey] = true; nextRepeat[eventKey] = System.currentTimeMillis() + keyRepeatInitial; consumed = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.keyPressed(eventKey, Keyboard.getEventCharacter()); if (consumed) { break; } } } } else { int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter()); nextRepeat[eventKey] = 0; consumed = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.keyReleased(eventKey, keys[eventKey]); if (consumed) { break; } } } } } while (Mouse.next()) { if (Mouse.getEventButton() >= 0) { if (Mouse.getEventButtonState()) { consumed = false; mousePressed[Mouse.getEventButton()] = true; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.mousePressed( Mouse.getEventButton(), (int) (xoffset + (Mouse.getEventX() * scaleX)), (int) (yoffset + ((height - Mouse.getEventY()) * scaleY))); if (consumed) { break; } } } } else { consumed = false; mousePressed[Mouse.getEventButton()] = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.mouseReleased( Mouse.getEventButton(), (int) (xoffset + (Mouse.getEventX() * scaleX)), (int) (yoffset + ((height - Mouse.getEventY()) * scaleY))); if (consumed) { break; } } } } } else { if (Mouse.isGrabbed()) { if ((Mouse.getEventDX() != 0) || (Mouse.getEventDY() != 0)) { consumed = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.mouseMoved(0, 0, Mouse.getEventDX(), -Mouse.getEventDY()); if (consumed) { break; } } } } } int dwheel = Mouse.getEventDWheel(); wheel += dwheel; if (dwheel != 0) { consumed = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.mouseWheelMoved(dwheel); if (consumed) { break; } } } } } } if (!displayActive) { lastMouseX = getMouseX(); lastMouseY = getMouseY(); } else { if ((lastMouseX != getMouseX()) || (lastMouseY != getMouseY())) { consumed = false; for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); if (listener.isAcceptingInput()) { listener.mouseMoved(lastMouseX, lastMouseY, getMouseX(), getMouseY()); if (consumed) { break; } } } lastMouseX = getMouseX(); lastMouseY = getMouseY(); } } if (controllersInited) { for (int i = 0; i < getControllerCount(); i++) { int count = ((Controller) controllers.get(i)).getButtonCount() + 3; count = Math.min(count, 24); for (int c = 0; c <= count; c++) { if (controls[i][c] && !isControlDwn(c, i)) { controls[i][c] = false; fireControlRelease(c, i); } else if (!controls[i][c] && isControlDwn(c, i)) { controllerPressed[c] = true; controls[i][c] = true; fireControlPress(c, i); } } } } if (keyRepeat) { for (int i = 0; i < 1024; i++) { if (pressed[i] && (nextRepeat[i] != 0)) { if (System.currentTimeMillis() > nextRepeat[i]) { nextRepeat[i] = System.currentTimeMillis() + keyRepeatInterval; consumed = false; for (int j = 0; j < listeners.size(); j++) { InputListener listener = (InputListener) listeners.get(j); if (listener.isAcceptingInput()) { listener.keyPressed(i, keys[i]); if (consumed) { break; } } } } } } } for (int i = 0; i < listeners.size(); i++) { InputListener listener = (InputListener) listeners.get(i); listener.inputEnded(); } if (Display.isCreated()) { displayActive = Display.isActive(); } }