Skip to content

Commit ffaed08

Browse files
committed
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
1 parent a1ae3fd commit ffaed08

2 files changed

Lines changed: 77 additions & 22 deletions

File tree

jme3-core/src/main/java/com/jme3/system/AppSettings.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ public final class AppSettings extends HashMap<String, Object> {
269269
defaults.put("CenterWindow", true);
270270
defaults.put("Width", 640);
271271
defaults.put("Height", 480);
272+
defaults.put("WindowWidth", Integer.MIN_VALUE);
273+
defaults.put("WindowHeight", Integer.MIN_VALUE);
272274
defaults.put("BitsPerPixel", 24);
273275
defaults.put("Frequency", 60);
274276
defaults.put("DepthBits", 24);
@@ -735,23 +737,24 @@ public void setAudioRenderer(String audioRenderer) {
735737
}
736738

737739
/**
738-
* @param value the width for the rendering display.
740+
* @param value the width for the default framebuffer.
739741
* (Default: 640)
740742
*/
741743
public void setWidth(int value) {
742744
putInteger("Width", value);
743745
}
744746

745747
/**
746-
* @param value the height for the rendering display.
748+
* @param value the height for the default framebuffer.
747749
* (Default: 480)
748750
*/
749751
public void setHeight(int value) {
750752
putInteger("Height", value);
751753
}
752754

753755
/**
754-
* Set the resolution for the rendering display
756+
* Set the resolution for the default framebuffer
757+
* Use {@link #setWindowSize(int, int)} instead, for HiDPI display support.
755758
* @param width The width
756759
* @param height The height
757760
* (Default: 640x480)
@@ -761,6 +764,18 @@ public void setResolution(int width, int height) {
761764
setHeight(height);
762765
}
763766

767+
768+
/**
769+
* Set the size of the window
770+
* @param width The width
771+
* @param height The height
772+
* (Default: 640x480)
773+
*/
774+
public void setWindowSize(int width,int height){
775+
putInteger("WindowWidth", width);
776+
putInteger("WindowHeight", height);
777+
}
778+
764779

765780
/**
766781
* @param value the minimum width the settings window will allow for the rendering display.
@@ -991,7 +1006,7 @@ public String getRenderer() {
9911006
/**
9921007
* Get the width
9931008
*
994-
* @return the width of the rendering display (in pixels)
1009+
* @return the width of the default framebuffer (in pixels)
9951010
* @see #setWidth(int)
9961011
*/
9971012
public int getWidth() {
@@ -1001,13 +1016,37 @@ public int getWidth() {
10011016
/**
10021017
* Get the height
10031018
*
1004-
* @return the height of the rendering display (in pixels)
1019+
* @return the height of the default framebuffer (in pixels)
10051020
* @see #setHeight(int)
10061021
*/
10071022
public int getHeight() {
10081023
return getInteger("Height");
10091024
}
10101025

1026+
/**
1027+
* Get the width of the window
1028+
*
1029+
* @return the width of the window (in pixels)
1030+
* @see #setWindowWidth(int)
1031+
*/
1032+
public int getWindowWidth(){
1033+
int w=getInteger("WindowWidth");
1034+
return w!=Integer.MIN_VALUE?w:getWidth();
1035+
}
1036+
1037+
/**
1038+
* Get the height of the window
1039+
*
1040+
* @return the height of the window (in pixels)
1041+
* @see #setWindowHeight(int)
1042+
*/
1043+
public int getWindowHeight(){
1044+
int h=getInteger("WindowHeight");
1045+
return h!=Integer.MIN_VALUE?h:getHeight();
1046+
}
1047+
1048+
1049+
10111050
/**
10121051
* Get the width
10131052
*

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,13 @@ public void invoke(int error, long description) {
277277

278278
final GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
279279

280-
if (settings.getWidth() <= 0 || settings.getHeight() <= 0) {
281-
settings.setResolution(videoMode.width(), videoMode.height());
280+
if (settings.getWindowWidth() <= 0 || settings.getWindowHeight() <= 0) {
281+
settings.setWindowSize(videoMode.width(), videoMode.height());
282282
}
283283

284-
window = glfwCreateWindow(settings.getWidth(), settings.getHeight(), settings.getTitle(), monitor, NULL);
284+
settings.setResolution(settings.getWindowWidth(), settings.getWindowHeight());// Assume default framebuffer size == window size
285+
286+
window = glfwCreateWindow(settings.getWindowWidth(), settings.getWindowHeight(), settings.getTitle(), monitor, NULL);
285287

286288
if (window == NULL) {
287289
throw new RuntimeException("Failed to create the GLFW window");
@@ -351,8 +353,7 @@ public void invoke(final long window, final int width, final int height) {
351353

352354
@Override
353355
public void invoke(final long window, final int width, final int height) {
354-
// https://www.glfw.org/docs/latest/window_guide.html#window_fbsize
355-
listener.reshape(width, height);
356+
updateDefaultFramebufferSize();
356357
}
357358
});
358359

@@ -363,9 +364,24 @@ public void invoke(final long window, final int width, final int height) {
363364
initOpenCL(window);
364365
}
365366

367+
368+
updateDefaultFramebufferSize();
369+
366370
framesAfterContextStarted = 0;
367371
}
368372

373+
private void updateDefaultFramebufferSize() {
374+
// If default framebuffer size is different than window size (eg. HiDPI)
375+
int[] width = new int[1];
376+
int[] height = new int[1];
377+
glfwGetFramebufferSize(window, width, height);
378+
if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
379+
settings.setResolution(width[0], height[0]);
380+
// https://www.glfw.org/docs/latest/window_guide.html#window_fbsize
381+
listener.reshape(width[0], height[0]);
382+
}
383+
}
384+
369385
private void onWindowSizeChanged(final int width, final int height) {
370386
settings.setResolution(width, height);
371387
}
@@ -584,18 +600,18 @@ protected void runLoop() {
584600

585601
// Update the frame buffer size from 2nd frame since the initial value
586602
// of frame buffer size from glfw maybe incorrect when HiDPI display is in use
587-
if (framesAfterContextStarted < 2) {
588-
framesAfterContextStarted++;
589-
if (framesAfterContextStarted == 2) {
590-
int[] width = new int[1];
591-
int[] height = new int[1];
592-
glfwGetFramebufferSize(window, width, height);
593-
594-
if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
595-
listener.reshape(width[0], height[0]);
596-
}
597-
}
598-
}
603+
// if (framesAfterContextStarted < 2) {
604+
// framesAfterContextStarted++;
605+
// if (framesAfterContextStarted == 2) {
606+
// int[] width = new int[1];
607+
// int[] height = new int[1];
608+
// glfwGetFramebufferSize(window, width, height);
609+
610+
// if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
611+
// listener.reshape(width[0], height[0]);
612+
// }
613+
// }
614+
// }
599615

600616
listener.update();
601617

0 commit comments

Comments
 (0)