GLFW Multi-Monitor Setup

Started by ShadowDragon, November 10, 2018, 00:00:05

Previous topic - Next topic

ShadowDragon

Hi there,

I'm wondering how one can take care of multiply monitors in LWJGL 3 while also making sure the resolution and  DPI settings are correct
as I've got a 24" FHD monitor and a 15" WQHD monitor and want to be able to launch the application on a specified monitor depending
whether it is connected or not, but also making sure everything looks the same size.

Note: It should work regardless of fullscreen mode or not (or what is left of fullscreen mode in Win10 1803 :P )

That's the code I've got in place currently:
Code: java
        // Create the window
        this.window = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);
        if(this.window == NULL) {
            glfwTerminate();
            glfwSetErrorCallback(null).free();
            throw new RuntimeException("Failed to create the GLFW window");
        }
        
        // Get the thread stack and push a new frame
        try(MemoryStack stack = stackPush()) { // thread-local lookup
            IntBuffer pWidth = stack.mallocInt(1); // int* ; ip instance eliminated
            IntBuffer pHeight = stack.mallocInt(1); // int* ; ip instance eliminated
            
            // Get the window size passed to glfwCreateWindow
            glfwGetWindowSize(this.window, pWidth, pHeight);
            
            // Get the resolution of the primary monitor
            GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
            
            // Center the window
            glfwSetWindowPos(this.window, 
                    (vidmode.width() - pWidth.get(0)) / 2, 
                    (vidmode.height() - pHeight.get(0)) / 2
                );
        } // the stack frame is popped automatically
        
        // Make the OpenGL context current
        glfwMakeContextCurrent(this.window);
        GL.createCapabilities();
        glfwShowWindow(this.window);

I don't know if this is relevant to this question, but I'm using OpenGL 4.6.

Thx in advance.
ShadowDragon

spasi

Have a look at the following GLFW APIs:

- glfwSetFramebufferSizeCallback (the framebuffer size can be different than the window size on HiDPI monitors)
- glfwGetMonitorContentScale/glfwGetWindowContentScale
- The new GLFW_SCALE_TO_MONITOR hint in LWJGL 3.2.1 snapshots. This fixes a lot of issues and has much better behavior when a window crosses monitors with different DPI settings.

I would also recommend separate testing for Windows/Linux and macOS, the latter handles HiDPI differently (see GLFW_COCOA_RETINA_FRAMEBUFFER). Finally, make sure to run your application on Java 9 or newer on Windows. Java 8 binaries are not configured with per-monitor DPI awareness.