Implemented resolution change workaround on SDL2
authorStefanos A <stapostol@gmail.com>
Thu, 12 Dec 2013 23:07:13 +0000 (00:07 +0100)
committerStefanos A <stapostol@gmail.com>
Thu, 12 Dec 2013 23:07:13 +0000 (00:07 +0100)
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.

Source/OpenTK/Platform/SDL2/Sdl2DisplayDeviceDriver.cs
Source/OpenTK/Platform/SDL2/Sdl2Factory.cs
Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs

index 5bd3468..4cf5344 100644 (file)
@@ -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
index b9b3dd6..9f2447b 100644 (file)
@@ -37,8 +37,21 @@ namespace OpenTK.Platform.SDL2
         readonly IInputDriver2 InputDriver = new Sdl2InputDriver();
         bool disposed;
 
+        /// <summary>
+        /// 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.
+        /// </summary>
+        /// <remarks>>
+        /// This is a workaround for the lack of ChangeResolution support in SDL2.
+        /// When and if this changes upstream, we should remove this code.
+        /// </remarks>
+        public static bool UseFullscreenDesktop { get; set; }
+
         public Sdl2Factory()
         {
+            UseFullscreenDesktop = true;
         }
 
         #region IPlatformFactory implementation
index 204d482..5528d41 100644 (file)
@@ -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