From ed6dba0d2a4f521ae516f4ced7ec6860d0d04d74 Mon Sep 17 00:00:00 2001 From: arcade_kappa Date: Mon, 26 Aug 2024 12:02:07 +0800 Subject: [PATCH] Port scale fix from https://github.com/GTNewHorizons/lwjgl3ify/pull/171 --- src/main/java/org/lwjgl/input/Mouse.java | 11 ++++++++++- src/main/java/org/lwjgl/opengl/Display.java | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/lwjgl/input/Mouse.java b/src/main/java/org/lwjgl/input/Mouse.java index 5126c601e..472f2f9a8 100644 --- a/src/main/java/org/lwjgl/input/Mouse.java +++ b/src/main/java/org/lwjgl/input/Mouse.java @@ -53,6 +53,9 @@ public static void addMoveEvent(double mouseX, double mouseY) { ignoreNextMove--; return; } + float scale = Display.getPixelScaleFactor(); + mouseX *= scale; + mouseY *= scale; dx += (int) mouseX - latestX; dy += Display.getHeight() - (int) mouseY - latestY; latestX = (int) mouseX; @@ -280,7 +283,13 @@ public static void setCursorPosition(int new_x, int new_y) { if (grabbed) { return; } - GLFW.glfwSetCursorPos(Display.getWindow(), new_x, new_y); + // convert back from framebuffer coordinates to screen-space coordinates + float inv_scale = 1.0f / Display.getPixelScaleFactor(); + new_x *= inv_scale; + new_y *= inv_scale; + GLFW.glfwSetCursorPos(Display.getWindow(), new_x * inv_scale, new_y * inv_scale); + // this might lose accuracy, since we just went from fb->screen and this will + // undo that change. Yay floating point numbers! addMoveEvent(new_x, new_y); } diff --git a/src/main/java/org/lwjgl/opengl/Display.java b/src/main/java/org/lwjgl/opengl/Display.java index 3ce9dc9eb..e2f37ed16 100644 --- a/src/main/java/org/lwjgl/opengl/Display.java +++ b/src/main/java/org/lwjgl/opengl/Display.java @@ -420,11 +420,11 @@ public static int getY() { } public static int getWidth() { - return displayWidth; + return displayFramebufferWidth; } public static int getHeight() { - return displayHeight; + return displayFramebufferHeight; } public static int getFramebufferWidth() { @@ -439,6 +439,16 @@ public static String getTitle() { return windowTitle; } + public static float getPixelScaleFactor() { + if (!isCreated()) { + return 1.0f; + } + float[] xScale = new float[1]; + float[] yScale = new float[1]; + glfwGetWindowContentScale(getWindow(), xScale, yScale); + return Math.max(xScale[0], yScale[0]); + } + public static void setTitle(String title) { if (getWindow() != 0) { org.lwjgl3.glfw.GLFW.glfwSetWindowTitle(Window.handle, title);