/** * setup all connected joysticks */ public static void setup(){ int[] joys = new int[]{ GLFW.GLFW_JOYSTICK_1, GLFW.GLFW_JOYSTICK_2, GLFW.GLFW_JOYSTICK_3, GLFW.GLFW_JOYSTICK_4, GLFW.GLFW_JOYSTICK_5, GLFW.GLFW_JOYSTICK_6, GLFW.GLFW_JOYSTICK_7, GLFW.GLFW_JOYSTICK_8, GLFW.GLFW_JOYSTICK_9, GLFW.GLFW_JOYSTICK_10, GLFW.GLFW_JOYSTICK_11, GLFW.GLFW_JOYSTICK_12, GLFW.GLFW_JOYSTICK_13, GLFW.GLFW_JOYSTICK_14, GLFW.GLFW_JOYSTICK_15, GLFW.GLFW_JOYSTICK_16, }; for(int joy : joys){ if(GLFW.glfwJoystickPresent(joy)){ setupJoystick(joy); } } }
@Override public void update(float delta) { final float speedThisFrame = speed * delta; // Move forward if (Input.isKeyPressed(GLFW.GLFW_KEY_W)) camera.move(Camera.Direction.FORWARD, speedThisFrame); // Move backward if (Input.isKeyPressed(GLFW.GLFW_KEY_S)) camera.move(Camera.Direction.BACKWARD, speedThisFrame); // Move right if (Input.isKeyPressed(GLFW.GLFW_KEY_D)) camera.move(Camera.Direction.RIGHT, speedThisFrame); // Move left if (Input.isKeyPressed(GLFW.GLFW_KEY_A)) camera.move(Camera.Direction.LEFT, speedThisFrame); // Move up if (Input.isKeyPressed(GLFW.GLFW_KEY_SPACE)) camera.move(Camera.AXIS_Y, speedThisFrame); // Move down if (Input.isKeyPressed(GLFW.GLFW_KEY_LEFT_SHIFT)) camera.move(Camera.AXIS_Y, -speedThisFrame); camera.update(); world.update(camera, delta); }
/** * Creates a GLFWWindow and gives it OpenGL context */ private void createWindow() { // Hide the window during application initialization Window.hint(GLFW.GLFW_VISIBLE, GL11.GL_FALSE); // Setup MSAA 4x Window.hint(GLFW.GLFW_SAMPLES, 4); WINDOW.create(config.width, config.height, config.title); // Make this thread and window the current context for OpenGL WINDOW.makeContextCurrent(); GLContext.createFromCurrent(); // Set other specified window configurations WINDOW.setVSyncEnabled(config.vSyncEnabled); WINDOW.setPosition(config.position); }
/** Setup a specific joystick that is connected * * @param joy the joystick id to setup */ public static void setupJoystick(int joy){ //make sure the joystick is connected if(!GLFW.glfwJoystickPresent(joy)) return; //get the name of the joystick and normalize the casing String name = GLFW.glfwGetJoystickName(joy).toLowerCase(); JoystickType type = JoystickType.OTHER; //check the name for the type if(name.contains("xbox")) type = JoystickType.XBOX360; else if(name.contains("playstation")) type = JoystickType.PLAYSTATION; //setup the axis size for the controller FloatBuffer buf = GLFW.glfwGetJoystickAxes(joy); int axisCount = buf.capacity(); //setup the button size for the controller ByteBuffer bbuf = GLFW.glfwGetJoystickButtons(joy); int buttonCount = bbuf.capacity(); //add the controller to the static array for usage connected.add(new Joystick(joy, type, axisCount, buttonCount)); }
private void updateViewPort(ViewPort viewport) { if (viewport == this.defaultViewPort) { //FIXME: This is a fix (hack?) for Mac's weird hack to scale windows for compatibility try (MemoryStack stack = MemoryStack.stackPush()) { IntBuffer pWidth = stack.mallocInt(1); IntBuffer pHeight = stack.mallocInt(1); GLFW.glfwGetFramebufferSize(this.window.handle(), pWidth, pHeight); GL11.glViewport(0, 0, pWidth.get(0), pHeight.get(0)); } } else { GL11.glViewport(viewport.getX(), viewport.getY(), viewport.getWidth(), viewport.getHeight()); } }
public WindowHandle setPixelBuffer(PixelBufferHandle pbh) { this.setWindowHint(GLFW.GLFW_RED_BITS, pbh.getRedBits()); this.setWindowHint(GLFW.GLFW_ACCUM_RED_BITS, pbh.getRedBitsAccum()); this.setWindowHint(GLFW.GLFW_GREEN_BITS, pbh.getGreenBits()); this.setWindowHint(GLFW.GLFW_ACCUM_GREEN_BITS, pbh.getGreenBitsAccum()); this.setWindowHint(GLFW.GLFW_BLUE_BITS, pbh.getBlueBits()); this.setWindowHint(GLFW.GLFW_ACCUM_BLUE_BITS, pbh.getBlueBitsAccum()); this.setWindowHint(GLFW.GLFW_ALPHA_BITS, pbh.getAlphaBits()); this.setWindowHint(GLFW.GLFW_ACCUM_ALPHA_BITS, pbh.getAlphaBitsAccum()); this.setWindowHint(GLFW.GLFW_DEPTH_BITS, pbh.getDepthBits()); this.setWindowHint(GLFW.GLFW_STENCIL_BITS, pbh.getStencilBits()); this.setWindowHint(GLFW.GLFW_AUX_BUFFERS, pbh.getAuxBuffers()); this.setWindowHint(GLFW.GLFW_SAMPLES, pbh.getSamples()); this.setWindowHint(GLFW.GLFW_REFRESH_RATE, pbh.getRefreshRate()); this.setWindowHint(GLFW.GLFW_STEREO, pbh.getStereo()); this.setWindowHint(GLFW.GLFW_SRGB_CAPABLE, pbh.getSRGBCapable()); this.setWindowHint(GLFW.GLFW_DOUBLEBUFFER, pbh.getDoubleBuffer()); return this; }
public void makeWindowCentered() { // Get the thread stack and push a new frame try (MemoryStack stack = MemoryStack.stackPush()) { IntBuffer pWidth = stack.mallocInt(1); // int* IntBuffer pHeight = stack.mallocInt(1); // int* // Get the window size passed to glfwCreateWindow GLFW.glfwGetWindowSize(this.handle, pWidth, pHeight); this.width = pWidth.get(0); this.height = pHeight.get(0); // Get the resolution of the primary monitor GLFWVidMode vidmode = getCurrentVideoMode(); // Center the window GLFW.glfwSetWindowPos( this.handle, this.x = ((vidmode.width() - this.width) / 2), this.y = ((vidmode.height() - this.height) / 2) ); } // the stack frame is popped automatically }
@Override public boolean initialize() { System.out.println("OS " + System.getProperty("os.name")); System.out.println("JAVA " + System.getProperty("java.version")); System.out.println("LWJGL " + Version.getVersion()); System.out.println("GLFW " + GLFW.glfwGetVersionString()); System.out.println("JOML 1.9.2"); // Setup an error callback. The default implementation // will print the error message in System.err. GLFWErrorCallback.createPrint(System.err).set(); // Initialize GLFW. Most GLFW functions will not work before doing this. if (!GLFW.glfwInit()) { throw new IllegalStateException("Unable to initialize GLFW"); } return true; }
public ConsoleBase(Console console, Transform transform, Asset<TextureAtlas> textureAtlas, int width, int height) { this.console = console; this.transform = transform; this.view = new RasterizedView(width, height).setTextureAtlas(textureAtlas); this.background = new RasterizedView(width, height).setTextureAtlas(textureAtlas); this.backgroundOffset = new Matrix4f().translate(0.0625F, -0.0625F, 0); this.view.forEach((vector2ic, character, integer) -> this.view.draw(vector2ic.x(), vector2ic.y(), 'A', 0xFFFFFF)); this.background.forEach((vector2ic, character, integer) -> this.background.draw(vector2ic.x(), vector2ic.y(), 'A', 0x888888)); //this.view.clear((byte) 0, 0xFFFFFF); //this.background.clear((byte) 0, 0x888888); Console.getConsole().getInputEngine().getDefaultContext() .registerEvent("newline", Console.getConsole().getInputEngine().getKeyboard().getButton(GLFW.GLFW_KEY_ENTER)::getAction); this.textHandler = Console.getConsole().getInputEngine().getText(); this.initialize(); }
@Override public void onKey(int key, int action, int mods) { if (action == GLFW.GLFW_PRESS && key == GLFW.GLFW_KEY_TAB && menu == null) { displayMouse = !displayMouse; boolean display = displayMouse(); mouse.setGrabbed(!display); if (!display) mouse.setPosDirty(); Vec2i res = getResolution(); mouse.setPos(res.x/2.0f, res.y/2.0f); } interactionState.onKey(key, action, mods); }
void step(float dt) { float yaw = cameraController.getYaw(); float cosYaw = (float)Math.cos(yaw); float sinYaw = (float)Math.sin(yaw); Vec3 groundFwd = new Vec3(-sinYaw, 0.0f, -cosYaw); Vec3 groundRight = new Vec3(cosYaw, 0.0f, -sinYaw); Vec3 desiredVel = new Vec3(); if (keyboard.isKeyDown(GLFW.GLFW_KEY_W)) desiredVel.add(groundFwd); if (keyboard.isKeyDown(GLFW.GLFW_KEY_S)) desiredVel.sub(groundFwd); if (keyboard.isKeyDown(GLFW.GLFW_KEY_D)) desiredVel.add(groundRight); if (keyboard.isKeyDown(GLFW.GLFW_KEY_A)) desiredVel.sub(groundRight); boolean sprinting = keyboard.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT); float speed = sprinting ? SPEED_SPRINT : SPEED; if (!desiredVel.isZero(0.0f)) desiredVel.normalize().mult(speed); vel.move(desiredVel, dt*ACC); pos.madd(vel, dt); cameraController.update(); }
@Override public boolean onMouseAction(IGameInstance game, int button, float x, float y){ if(this.isMouseOver(game)){ if(game.getInput().isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)){ return RockBottomAPI.getInternalHooks().doDefaultShiftClicking(game, this.container, this); } else{ return RockBottomAPI.getInternalHooks().doDefaultSlotMovement(game, button, x, y, this); } } else{ return false; } }
@Override public void update(float delta, Window window, Camera camera, World world) { Vector2f movement = new Vector2f(); if (window.getInput().isKeyDown(GLFW.GLFW_KEY_A)){ movement.add(-10 * delta, 0); } if (window.getInput().isKeyDown(GLFW.GLFW_KEY_D)){ movement.add(10 * delta, 0); } if (window.getInput().isKeyDown(GLFW.GLFW_KEY_W)){ movement.add(0, 10 * delta * JUMP_STRENGTH); } movement.add(0, -10 * delta * GRAVITATION); move(movement); if(movement.y > 0){ useAnimation(ANIM_JETPACK); }else if (movement.x != 0){ useAnimation(ANIM_WALK); }else{ useAnimation(ANIM_IDLE); } camera.getPosition().lerp(transform.pos.mul(-world.getScale(), new Vector3f()), 0.2f); if (window.getInput().isKeyDown(GLFW.GLFW_KEY_SPACE)){ Rocket rocket = new Rocket(1, transform); } }
@Override public void handleEvents(List<Event> events) { for (Event e : events) { if (e.type == Event.Type.KEY_INPUT) { KeyInputEvent evt = (KeyInputEvent) e; if (evt.action == GLFW.GLFW_PRESS) { if (evt.mods == GLFW.GLFW_MOD_CONTROL && evt.key == GLFW.GLFW_KEY_ENTER) { // command // mode java.lang.System.out.print("Enter your command: "); String commandLine = consoleScanner.nextLine(); String command = commandLine; if (commandLine.indexOf(' ') != -1) commandLine.substring(0, commandLine.indexOf(' ')); if (command.equals("load")) { game.createEvent(new LoadSceneEvent("default"));// TODO // change // default // to // last // (or // sth). } else if (command.equals("save")) { game.createEvent(new Event(Event.Type.SAVE)); } else if (command.equals("exit")) { game.createEvent(new Event(Event.Type.EXIT)); } else {// unknown command Logger.warn("Unknown command " + command + ""); } } } } } }
@Override public void invoke(long window, int keycode, int scancode, int action, int mods) { // If the key was pressed if(action == GLFW.GLFW_PRESS) { keyPressed(keycode); // If the key was released } else if (action == GLFW.GLFW_RELEASE){ keyReleased(keycode); } }
/** * Refreshes the window by swapping buffers */ public void swapBuffers() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwSwapBuffers(handle); }
@Override public void invoke(long windowID, int button, int action, int mods) { if (this.windowID != windowID) return; this.button.set(button, (action != GLFW.GLFW_RELEASE)); if (action == GLFW.GLFW_RELEASE && this.ignore.get(button)) this.ignore.clear(button); }
/** * Sets the cursor position callback for the window * @param callback The callback that will recieve mouse movement input */ public void setCursorPosCallback(GLFWCursorPosCallback callback) throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwSetCursorPosCallback(handle, callback); }
/** * Sets the size of this window * @param width * @param height */ public void setSize(int width, int height) throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwSetWindowSize(handle, width, height); }
@Override public void update(float delta) { window.update(delta, AppUI.getMainWindow()); KeyboardHandler kbh = AppUI.getMainWindow().getKeyboardHandler(); if (kbh.isShiftPressed() && kbh.isKeyPressed(GLFW.GLFW_KEY_ESCAPE)) { running = false; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } StateMachine.stop(); } }
/** * Makes this window visible */ public void show() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwShowWindow(handle); }
/** * Makes this window invisible */ public void hide() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwHideWindow(handle); }
/** * Causes this window to obtain the current GLContext */ public void makeContextCurrent() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwMakeContextCurrent(handle); }
/** * Destroys the context of this window */ public void destroy() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwDestroyWindow(handle); }
/** * Returns whether or not this window is trying to close * @return true if closing */ public boolean isClosing() throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } return GLFW.glfwWindowShouldClose(handle) == 1; }
/** * Sets the specified input mode to the specified value * @param mode * @param value */ public void setInputMode(int mode, int value) throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } GLFW.glfwSetInputMode(handle, mode, value); }
/** * Returns the state of the specified mouse button * @param button The mouse button (0 = left, 1 = right, 2 = middle) * @return */ public int getMouseButton(int button) throws IllegalStateException { if(handle == 0) { throw new IllegalStateException("handle is NULL"); } return GLFW.glfwGetMouseButton(handle, button); }
/** * Initialize GLFW. Stops application if error occurs */ private void initGLFW() throws IllegalStateException { // If there is an error in initialization if(GLFW.glfwInit() == 0) { throw new IllegalStateException("GLFW failed to initialize"); } }
private String handleBackspace(String input) { long currentPress = 0l; if(this.isKeyPressed(GLFW.GLFW_KEY_BACKSPACE) && ((currentPress = System.currentTimeMillis()) - this.lastPress) > 150) { String result = input; this.lastPress = currentPress; if(input.length() > 0) result = input.substring(0, input.length() - 1); return result; } else return input; }
/** * Update the values of the joystick */ public void update(){ this.buttonsLastFrame = this.buttons.clone(); this.axesLastFrame = this.axes.clone(); FloatBuffer abuf = GLFW.glfwGetJoystickAxes(this.id); ByteBuffer bbuf = GLFW.glfwGetJoystickButtons(this.id); for(int i=0;i<abuf.capacity();i++) this.axes[i] = abuf.get(i); for(int i=0;i<bbuf.capacity();i++) this.buttons[i] = bbuf.get(i); }
/** WARNING: USE ONLY FOR DEBUGGING/MONITORING * * @return all buttons values in a string */ public String getButtons(){ StringBuilder but = new StringBuilder(); ByteBuffer b = GLFW.glfwGetJoystickButtons(this.id); for(int i=0;i<b.capacity();i++) but.append(b.get(i)+" "); return but.toString(); }
public MouseHandler(long windowID, AbstractWindow window) { this.enterCallback = new MouseEnterCallback(windowID); this.posCallback = new MousePosCallback(windowID); this.buttonCallback = new MouseButtonCallback(windowID); this.scrollCallback = new MouseScrollCallback(windowID); this.windowID = windowID; GLFW.glfwSetCursorEnterCallback(windowID, this.enterCallback); GLFW.glfwSetCursorPosCallback(windowID, this.posCallback); GLFW.glfwSetMouseButtonCallback(windowID, this.buttonCallback); GLFW.glfwSetScrollCallback(windowID, this.scrollCallback); this.window = window; }
public Window setPosition(int x, int y) { if (this.handle == MemoryUtil.NULL) { this.x = x; this.y = y; } else { GLFW.glfwSetWindowPos(this.handle, x, y); } return this; }
public Window setSize(int width, int height) { if (this.handle == MemoryUtil.NULL) { this.width = width; this.height = height; } else { GLFW.glfwSetWindowSize(this.handle, width, height); } return this; }
public Window setTitle(String title) { if (this.handle == MemoryUtil.NULL) { this.title = title; } else { GLFW.glfwSetWindowTitle(this.handle, this.title = title); } return this; }
public Window setVSync(boolean vsync) { if (this.handle == MemoryUtil.NULL) { this.vsync = vsync; } else { GLFW.glfwSwapInterval((this.vsync = vsync) ? 1 : 0); } return this; }
public void show() { if (this.handle == MemoryUtil.NULL) { this.createWindow(); } GLFW.glfwShowWindow(this.handle); GLFW.glfwFocusWindow(this.handle); }
public void hide() { if (this.handle == MemoryUtil.NULL) throw new IllegalStateException("window not yet initialized!"); GLFW.glfwHideWindow(this.handle); }
@Override public void terminate() { // Terminate GLFW and free the error callback GLFW.glfwTerminate(); GLFW.glfwSetErrorCallback(null).free(); }
public TextHandler(Window window) { this.window = window; GLFWCharCallbackI callback = null; try { callback = GLFW.glfwSetCharCallback(this.window.handle(), (handle, codepoint) -> { this.sb.append((char) codepoint); }); if (callback != null) { throw new IllegalStateException("another text handler is already registered!"); } } catch (Exception e) { //Reset to previous state GLFW.glfwSetCharCallback(this.window.handle(), callback); throw e; } }