Sfoglia il codice sorgente

Separate concept of window size from default framebuffer size that are not always interchangeable in modern platforms. Add methods to query both sizes from AppSettings

Riccardo Balbo 3 anni fa
parent
commit
08f8d08601

+ 42 - 5
jme3-core/src/main/java/com/jme3/system/AppSettings.java

@@ -269,6 +269,8 @@ public final class AppSettings extends HashMap<String, Object> {
         defaults.put("CenterWindow", true);
         defaults.put("Width", 640);
         defaults.put("Height", 480);
+        defaults.put("WindowWidth", Integer.MIN_VALUE);
+        defaults.put("WindowHeight", Integer.MIN_VALUE);
         defaults.put("BitsPerPixel", 24);
         defaults.put("Frequency", 60);
         defaults.put("DepthBits", 24);
@@ -735,7 +737,7 @@ public final class AppSettings extends HashMap<String, Object> {
     }
 
     /**
-     * @param value the width for the rendering display.
+     * @param value the width for the default framebuffer.
      * (Default: 640)
      */
     public void setWidth(int value) {
@@ -743,7 +745,7 @@ public final class AppSettings extends HashMap<String, Object> {
     }
 
     /**
-     * @param value the height for the rendering display.
+     * @param value the height for the default framebuffer.
      * (Default: 480)
      */
     public void setHeight(int value) {
@@ -751,7 +753,8 @@ public final class AppSettings extends HashMap<String, Object> {
     }
 
     /**
-     * Set the resolution for the rendering display
+     * Set the resolution for the default framebuffer
+     * Use {@link #setWindowSize(int, int)} instead, for HiDPI display support.
      * @param width The width
      * @param height The height
      * (Default: 640x480)
@@ -761,6 +764,18 @@ public final class AppSettings extends HashMap<String, Object> {
         setHeight(height);
     }
 
+    /**
+     * Set the size of the window
+     * 
+     * @param width
+     *            The width
+     * @param height
+     *            The height (Default: 640x480)
+     */
+    public void setWindowSize(int width, int height) {
+        putInteger("WindowWidth", width);
+        putInteger("WindowHeight", height);
+    }
 
     /**
      * @param value the minimum width the settings window will allow for the rendering display.
@@ -991,7 +1006,7 @@ public final class AppSettings extends HashMap<String, Object> {
     /**
      * Get the width
      *
-     * @return the width of the rendering display (in pixels)
+     * @return the width of the default framebuffer (in pixels)
      * @see #setWidth(int)
      */
     public int getWidth() {
@@ -1001,13 +1016,35 @@ public final class AppSettings extends HashMap<String, Object> {
     /**
      * Get the height
      *
-     * @return the height of the rendering display (in pixels)
+     * @return the height of the default framebuffer (in pixels)
      * @see #setHeight(int)
      */
     public int getHeight() {
         return getInteger("Height");
     }
 
+    /**
+     * Get the width of the window
+     *
+     * @return the width of the window (in pixels)
+     * @see #setWindowWidth(int)
+     */
+    public int getWindowWidth() {
+        int w = getInteger("WindowWidth");
+        return w != Integer.MIN_VALUE ? w : getWidth();
+    }
+
+    /**
+     * Get the height of the window
+     *
+     * @return the height of the window (in pixels)
+     * @see #setWindowHeight(int)
+     */
+    public int getWindowHeight() {
+        int h = getInteger("WindowHeight");
+        return h != Integer.MIN_VALUE ? h : getHeight();
+    }
+
     /**
      * Get the width
      *

+ 27 - 11
jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java

@@ -281,11 +281,16 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
 
         final GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
 
-        if (settings.getWidth() <= 0 || settings.getHeight() <= 0) {
-            settings.setResolution(videoMode.width(), videoMode.height());
+        if (settings.getWindowWidth() <= 0 || settings.getWindowHeight() <= 0) {
+            settings.setWindowSize(videoMode.width(), videoMode.height());
+        } else {
+            settings.setWindowSize(settings.getWindowWidth(), settings.getWindowHeight());
         }
 
-        window = glfwCreateWindow(settings.getWidth(), settings.getHeight(), settings.getTitle(), monitor, NULL);
+        // Assume default framebuffer size == window size
+        settings.setResolution(settings.getWindowWidth(), settings.getWindowHeight());
+
+        window = glfwCreateWindow(settings.getWindowWidth(), settings.getWindowHeight(), settings.getTitle(), monitor, NULL);
 
         if (window == NULL) {
             throw new RuntimeException("Failed to create the GLFW window");
@@ -355,8 +360,7 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
 
             @Override
             public void invoke(final long window, final int width, final int height) {
-                // https://www.glfw.org/docs/latest/window_guide.html#window_fbsize
-                listener.reshape(width, height);
+                updateDefaultFramebufferSize();
             }
         });
 
@@ -367,9 +371,24 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
             initOpenCL(window);
         }
 
+
+        updateDefaultFramebufferSize();
+
         framesAfterContextStarted = 0;
     }
 
+    private void updateDefaultFramebufferSize() {
+        // If default framebuffer size is different than window size (eg. HiDPI)
+        int[] width = new int[1];
+        int[] height = new int[1];
+        glfwGetFramebufferSize(window, width, height);
+        if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
+            settings.setResolution(width[0], height[0]);
+            // https://www.glfw.org/docs/latest/window_guide.html#window_fbsize
+            listener.reshape(width[0], height[0]);
+        }
+    }
+
     private void onWindowSizeChanged(final int width, final int height) {
         settings.setResolution(width, height);
     }
@@ -587,15 +606,12 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
         }
 
         // Update the frame buffer size from 2nd frame since the initial value
-        // of frame buffer size from glfw maybe incorrect when HiDPI display is in use
+        // of frame buffer size from glfw maybe incorrect when HiDPI display is in use 
+        // and GLFW_COCOA_RETINA_FRAMEBUFFER is false
         if (framesAfterContextStarted < 2) {
             framesAfterContextStarted++;
             if (framesAfterContextStarted == 2) {
-                glfwGetFramebufferSize(window, width, height);
-
-                if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
-                    listener.reshape(width[0], height[0]);
-                }
+                updateDefaultFramebufferSize();
             }
         }