From 694869dc0534f039fb7a7f78781198dfaeb2cdbd Mon Sep 17 00:00:00 2001 From: Stefanos A Date: Fri, 13 Dec 2013 00:07:13 +0100 Subject: [PATCH] Implemented resolution change workaround on SDL2 SDL2 does not support changing display resolutions independently of an SDL window. As a workaround, if the user uses ChangeResolution and then makes a GameWindow fullscreen, we use old-style SDL fullscreen which changes the resolution. If the user makes a GameWindow fullscreen without calling ChangeResolution first, we use the new fullscreen-desktop mode to match the other OpenTK backends. --- .../Platform/SDL2/Sdl2DisplayDeviceDriver.cs | 14 ++------------ Source/OpenTK/Platform/SDL2/Sdl2Factory.cs | 13 +++++++++++++ Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs | 17 +++++++++++------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs b/Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs index 5bd34689..4cf53446 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs @@ -94,24 +94,14 @@ namespace OpenTK.Platform.SDL2 public override bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution) { - // Todo: we need a temporary window to change resolutions, most probably - Trace.WriteLine("SDL2 driver does not implement TryChangeResolution"); + Sdl2Factory.UseFullscreenDesktop = false; return true; - - //SDL2.SDL_DisplayMode desired, closest; - //desired.w = resolution.Width; - //desired.h = resolution.Height; - //desired.format = SDL.SDL_PIXELFORMAT_BGRA8888; - - //SDL2.SDL_GetClosestDisplayMode((int)device.Id, ref desired, out closest); - //SDL2.SDL_SetWindowDisplayMode(IntPtr.Zero, ref closest); } public override bool TryRestoreResolution(DisplayDevice device) { - Trace.WriteLine("SDL2 driver does not support TryRestoreResolution"); + Sdl2Factory.UseFullscreenDesktop = true; return true; - //throw new NotImplementedException(); } #endregion diff --git a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs index b9b3dd6b..9f2447ba 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2Factory.cs @@ -37,8 +37,21 @@ namespace OpenTK.Platform.SDL2 readonly IInputDriver2 InputDriver = new Sdl2InputDriver(); bool disposed; + /// + /// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode + /// for fullscreen windows. When true, then GameWindow instances will not change + /// DisplayDevice resolutions when going fullscreen. When false, fullscreen GameWindows + /// will change the device resolution to match their size. + /// + /// > + /// This is a workaround for the lack of ChangeResolution support in SDL2. + /// When and if this changes upstream, we should remove this code. + /// + public static bool UseFullscreenDesktop { get; set; } + public Sdl2Factory() { + UseFullscreenDesktop = true; } #region IPlatformFactory implementation diff --git a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index 204d482f..5528d410 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -112,8 +112,11 @@ namespace OpenTK.Platform.SDL2 switch (flags) { case GameWindowFlags.Fullscreen: - return WindowFlags.FULLSCREEN_DESKTOP; - + if (Sdl2Factory.UseFullscreenDesktop) + return WindowFlags.FULLSCREEN_DESKTOP; + else + return WindowFlags.FULLSCREEN; + default: return WindowFlags.Default; } @@ -626,13 +629,15 @@ namespace OpenTK.Platform.SDL2 { case WindowState.Fullscreen: RestoreWindow(); - if (SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN_DESKTOP) < 0) + bool success = Sdl2Factory.UseFullscreenDesktop ? + SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN_DESKTOP) < 0 : + SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN) < 0; + + if (!success) { - if (SDL.SetWindowFullscreen(window.Handle, (uint)WindowFlags.FULLSCREEN) < 0) - { Debug.Print("SDL2 failed to enter fullscreen mode: {0}", SDL.GetError()); - } } + SDL.RaiseWindow(window.Handle); // There is no "fullscreen" message in the event loop // so we have to mark that ourselves