Browse Source

Instead of reverting to windowed if exact resolution match not found, use closest matching fullscreen resolution in Graphics::SetMode().

Lasse Öörni 11 years ago
parent
commit
1229b6cf1d

+ 17 - 6
Source/Engine/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -391,18 +391,29 @@ bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless,
     // Note: GetMultiSample() will not reflect the actual hardware multisample mode, but rather what the caller wanted.
     multiSample_ = multiSample;
     
-    // Check fullscreen mode validity. If not valid, revert to windowed
+    // Check fullscreen mode validity. Use a closest match if not found
     if (fullscreen)
     {
         PODVector<IntVector2> resolutions = GetResolutions();
-        fullscreen = false;
-        for (unsigned i = 0; i < resolutions.Size(); ++i)
+        if (resolutions.Empty())
+            fullscreen = false;
+        else
         {
-            if (width == resolutions[i].x_ && height == resolutions[i].y_)
+            unsigned best = 0;
+            unsigned bestError = M_MAX_UNSIGNED;
+
+            for (unsigned i = 0; i < resolutions.Size(); ++i)
             {
-                fullscreen = true;
-                break;
+                unsigned error = Abs(resolutions[i].x_ - width) * Abs(resolutions[i].y_ - height);
+                if (error < bestError)
+                {
+                    best = i;
+                    bestError = error;
+                }
             }
+            
+            width = resolutions[best].x_;
+            height = resolutions[best].y_;
         }
     }
     

+ 17 - 6
Source/Engine/Graphics/OpenGL/OGLGraphics.cpp

@@ -315,19 +315,30 @@ bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless,
         }
     }
     
-    // Check fullscreen mode validity (desktop only). If not valid, revert to windowed
+    // Check fullscreen mode validity (desktop only). Use a closest match if not found
     #if !defined(ANDROID) && !defined(IOS) && !defined(RASPI)
     if (fullscreen)
     {
         PODVector<IntVector2> resolutions = GetResolutions();
-        fullscreen = false;
-        for (unsigned i = 0; i < resolutions.Size(); ++i)
+        if (resolutions.Empty())
+            fullscreen = false;
+        else
         {
-            if (width == resolutions[i].x_ && height == resolutions[i].y_)
+            unsigned best = 0;
+            unsigned bestError = M_MAX_UNSIGNED;
+
+            for (unsigned i = 0; i < resolutions.Size(); ++i)
             {
-                fullscreen = true;
-                break;
+                unsigned error = Abs(resolutions[i].x_ - width) * Abs(resolutions[i].y_ - height);
+                if (error < bestError)
+                {
+                    best = i;
+                    bestError = error;
+                }
             }
+            
+            width = resolutions[best].x_;
+            height = resolutions[best].y_;
         }
     }
     #endif