Skip to content

Commit

Permalink
Support for Apple Silicon/Retina (#125)
Browse files Browse the repository at this point in the history
* Support for Apple Silicon/Retina

* Fix window size
  • Loading branch information
valb3r authored Sep 17, 2023
1 parent d13e97c commit 11648bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 12 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
<lwjgl.natives>natives-macos</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-macos-aarch64</id>
<activation>
<os>
<family>mac</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-macos-arm64</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-windows-amd64</id>
<activation>
Expand Down
22 changes: 16 additions & 6 deletions demo/src/main/java/org/ode4j/drawstuff/internal/LwJGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private void createMainWindow (int _width, int _height, dsFunctions fn)
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // For windows and linux - fix window size

// Create the window
window = glfwCreateWindow(_width, _height, "Simulation", NULL, NULL);
Expand Down Expand Up @@ -169,11 +170,8 @@ private void createMainWindow (int _width, int _height, dsFunctions fn)
glfwSetCursorPosCallback(window, (window, xpos, ypos) -> {
handleMouseMove(xpos, ypos);
});
glfwSetWindowSizeCallback(window, (window, width, height) -> {
this.width = width;
this.height = height;
});

float xScale, yScale;
// Get the thread stack and push a new frame
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1); // int*
Expand All @@ -191,8 +189,20 @@ private void createMainWindow (int _width, int _height, dsFunctions fn)
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);

// Required for HiDPI monitors (i.e. Mac Retina)
FloatBuffer _xscale = stack.mallocFloat(1);
FloatBuffer _yscale = stack.mallocFloat(1);
glfwGetWindowContentScale(window, _xscale, _yscale);
xScale = _xscale.get();
yScale = _yscale.get();
} // the stack frame is popped automatically

glfwSetWindowSizeCallback(window, (window, width, height) -> {
this.width = (int) (width * xScale);
this.height = (int) (height * yScale);
});

// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
Expand Down Expand Up @@ -232,8 +242,8 @@ private void createMainWindow (int _width, int _height, dsFunctions fn)

// initialize variables
// win = 0;
width = _width;
height = _height;
width = (int) (_width * xScale);
height = (int) (_width * yScale);
// glx_context = 0;
last_key_pressed = 0;

Expand Down

0 comments on commit 11648bf

Please sign in to comment.