Search code examples
javascalalibgdx

LibGDX control multiple monitors


I am trying to create an application via LibGDX where I would like to control on which monitor the application starts.

I found a little workaround that sort of allows me to achieve this:

cfg.setMaximized(true)
cfg.setMaximizedMonitor(Lwjgl3ApplicationConfiguration.getMonitors()(1))
  

This will open the application in a maximized window on the secondary screen. However, I would like to be able to start the application on the secondary (or even a third monitor) without it being maximized.

Any ideas?


Solution

  • In multi monitor setups the monitors reside side by side and have virtual positions. Moving window from monitor to monitor is as simple as changing it's position to be inside the bounds of that monitor.

    In LibGDX monitor positions can be found with Monitor virtualX and virtualY fields. Their sizes can be found with config.getDisplayMode(Monitor)

    Here for example I'm centering the window on 2nd monitor on the list

        int width = 800;
        int height = 600;
    
        var mon = monitors[1];
        DisplayMode mode = Lwjgl3ApplicationConfiguration.getDisplayMode(mon);
        int posX = mon.virtualX + mode.width/2 - width/2;
        int posY = mon.virtualY + mode.height/2 - height/2;
        
        config.setWindowedMode(width, height);
        config.setWindowPosition(posX, posY);
    

    Although maximizing a window would probably be better user experience (who wants to play in a small window anyway?)