Skip to content

Commit

Permalink
Fix for resizing the window (issue 1191) (#1308)
Browse files Browse the repository at this point in the history
* Listen for framebuffer size callbacks and set the resolution from that

* Also check the window size on the framebuffer size callback
  • Loading branch information
tonihele authored and pspeed42 committed Mar 24, 2020
1 parent f0e09b2 commit 1e7e12e
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -121,6 +122,7 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {

private GLFWErrorCallback errorCallback;
private GLFWWindowSizeCallback windowSizeCallback;
private GLFWFramebufferSizeCallback framebufferSizeCallback;
private GLFWWindowFocusCallback windowFocusCallback;

private Thread mainThread;
Expand Down Expand Up @@ -250,16 +252,8 @@ public void invoke(int error, long description) {
throw new RuntimeException("Failed to create the GLFW window");
}

// Add a resize callback which delegates to the listener
glfwSetWindowSizeCallback(window, windowSizeCallback = new GLFWWindowSizeCallback() {
@Override
public void invoke(final long window, final int width, final int height) {
settings.setResolution(width, height);
listener.reshape(width, height);
}
});

glfwSetWindowFocusCallback(window, windowFocusCallback = new GLFWWindowFocusCallback() {

@Override
public void invoke(final long window, final boolean focus) {
if (wasActive != focus) {
Expand Down Expand Up @@ -294,6 +288,36 @@ public void invoke(final long window, final boolean focus) {
setWindowIcon(settings);
showWindow();

// Windows resize callback
glfwSetWindowSizeCallback(window, windowSizeCallback = new GLFWWindowSizeCallback() {

@Override
public void invoke(final long window, final int width, final int height) {

// This is the window size, never to passed to any pixel based stuff!
// https://www.glfw.org/docs/latest/window_guide.html#window_size
onWindowSizeChanged(width, height);
}
});

// Add a framebuffer resize callback which delegates to the listener
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback = new GLFWFramebufferSizeCallback() {

@Override
public void invoke(final long window, final int width, final int height) {

// The window size might be also changed, but the window size callback might not trigger
// Maybe a bug in graphics drivers or LWJGL 3...? So make sure we emulate the original JME behavior here
IntBuffer windowWidth = BufferUtils.createIntBuffer(1);
IntBuffer windowHeight = BufferUtils.createIntBuffer(1);
glfwGetWindowSize(window, windowWidth, windowHeight);
onWindowSizeChanged(windowWidth.get(), windowHeight.get());

// https://www.glfw.org/docs/latest/window_guide.html#window_fbsize
listener.reshape(width, height);
}
});

allowSwapBuffers = settings.isSwapBuffers();

// Create OpenCL
Expand All @@ -302,6 +326,10 @@ public void invoke(final long window, final boolean focus) {
}
}

private void onWindowSizeChanged(final int width, final int height) {
settings.setResolution(width, height);
}

protected void showWindow() {
glfwShowWindow(window);
}
Expand Down Expand Up @@ -406,6 +434,11 @@ protected void destroyContext() {
windowSizeCallback = null;
}

if (framebufferSizeCallback != null) {
framebufferSizeCallback.close();
framebufferSizeCallback = null;
}

if (windowFocusCallback != null) {
windowFocusCallback.close();
windowFocusCallback = null;
Expand Down

0 comments on commit 1e7e12e

Please sign in to comment.