Skip to content

Commit

Permalink
Port scale fix from GTNewHorizons/lwjgl3ify#171
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Aug 26, 2024
1 parent b1f8e2f commit ed6dba0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/lwjgl/input/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/lwjgl/opengl/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down

0 comments on commit ed6dba0

Please sign in to comment.