Add support for borderless fullscreen mode on supporting desktop OSes
See original GitHub issueIssue details
On desktop, libGDX currently offers windowed mode and fullscreen mode. There is a special third option, typically called borderless fullscreen, whereby a window with the exact characteristics of the monitor’s display mode is created. There are two main advantages to this. One is smoother switching between the ‘fullscreen’ window and other programs (no black flickering due to display mode changes). The other is that it prevents the ‘fullscreen’ window from being iconified when focusing on another window on a different monitor, which is what happens when using the traditional fullscreen mode with autoIconify set to true.
For Windows (only tested on Windows 10), one can activate borderless fullscreen as follows:
public static void setBorderlessFullscreenMode(Graphics.Monitor monitor) {
var window = ((Lwjgl3Graphics) Gdx.graphics).getWindow();
var mode = (Lwjgl3Graphics.Lwjgl3DisplayMode) Gdx.graphics.getDisplayMode(monitor);
GLFW.glfwSetWindowMonitor(window.getWindowHandle(), 0, 0, 0, mode.width, mode.height, mode.refreshRate);
}
The functionality is documented here: https://www.glfw.org/docs/latest/window_guide.html#window_windowed_full_screen What is not mentioned, however, is that one needs to set the monitor parameter to 0 for this to work (see https://github.com/glfw/glfw/issues/1267).
I think it would be great for this to be added as a method in Gdx.graphics
.
Someone should test whether this also works on macOS and Linux.
Version of libGDX and/or relevant dependencies
libGDX 1.10.0 with LWJGL3 backend
Please select the affected platforms
- Android
- iOS
- HTML/GWT
- Windows
- Linux
- MacOS
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14 (9 by maintainers)
Top GitHub Comments
Ah nice, that is good to know! I have opened a PR (#6649) to fix the issues mentioned in my comment above. After it is merged (reviews are welcome!), borderless fullscreen can be achieved as follows:
Or, if you want to use the ‘1 pixel wider’ trick, the last line has to be change to
Gdx.graphics.setWindowedMode(displayMode.width, displayMode.height + 1);
.Thanks @crykn for the thorough testing!
I did some more research and figured out that the flickering for undecorated windows is apparently due to OpenGL not having the same control over the swapchain as DirectX. See here: https://discourse.glfw.org/t/cannot-manage-to-create-borderless-windowed-form/1592/2
The workaround described in that discussion is to add one pixel to the resolution of the window. Lo and behold, the flickering disappears when doing that. This is the code I used:
This works on both the primary and other monitors. The only downside that I can see is that, well, the window is now one pixel larger than necessary. You would have to account for that in your game. Also, when making a screenshot or recording, the resolution of that would be one pixel larger as well.
(I also noticed that the flickering comes back when the window is already opened and I plug in a second monitor, but I guess monitor plug and play can always lead to strange issues.)