shithub: choc

Download patch

ref: 5d99eee5a4509f42d635156bfb1d6c495d8de221
parent: 895f440628c8c45aa98bd7af996ca6ae48065224
author: Simon Howard <[email protected]>
date: Sun Jun 7 13:10:05 EDT 2009

Make auto-adjust code switch to windowed mode if no fullscreen modes are
available.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 1571

--- a/src/i_video.c
+++ b/src/i_video.c
@@ -964,111 +964,110 @@
     return best_mode;
 }
 
-// If the video mode set in the configuration file is not available,
-// try to choose a different mode.
+// Adjust to an appropriate fullscreen mode.
+// Returns true if successful.
 
-static void I_AutoAdjustSettings(void)
+static boolean AutoAdjustFullscreen(void)
 {
-    if (fullscreen)
-    {
-        SDL_Rect **modes;
-        SDL_Rect *best_mode;
-        screen_mode_t *screen_mode;
-        int target_pixels, diff, best_diff;
-        int i;
+    SDL_Rect **modes;
+    SDL_Rect *best_mode;
+    screen_mode_t *screen_mode;
+    int target_pixels, diff, best_diff;
+    int i;
 
-        modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
+    modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
 
-        // Find the best mode that matches the mode specified in the
-        // configuration file
+    // No fullscreen modes available at all?
 
-        best_mode = NULL;
-        best_diff = INT_MAX;
-        target_pixels = screen_width * screen_height;
+    if (modes == NULL || modes == (SDL_Rect **) -1 || *modes == NULL)
+    {
+        return false;
+    }
 
-        for (i=0; modes[i] != NULL; ++i) 
-        {
-            //printf("%ix%i?\n", modes[i]->w, modes[i]->h);
+    // Find the best mode that matches the mode specified in the
+    // configuration file
 
-            // What screen_mode_t would be used for this video mode?
+    best_mode = NULL;
+    best_diff = INT_MAX;
+    target_pixels = screen_width * screen_height;
 
-            screen_mode = I_FindScreenMode(modes[i]->w, modes[i]->h);
+    for (i=0; modes[i] != NULL; ++i) 
+    {
+        //printf("%ix%i?\n", modes[i]->w, modes[i]->h);
 
-            // Never choose a screen mode that we cannot run in, or
-            // is poor quality for fullscreen
+        // What screen_mode_t would be used for this video mode?
 
-            if (screen_mode == NULL || screen_mode->poor_quality)
-            {
-            //    printf("\tUnsupported / poor quality\n");
-                continue;
-            }
+        screen_mode = I_FindScreenMode(modes[i]->w, modes[i]->h);
 
-            // Do we have the exact mode?
-            // If so, no autoadjust needed
+        // Never choose a screen mode that we cannot run in, or
+        // is poor quality for fullscreen
 
-            if (screen_width == modes[i]->w && screen_height == modes[i]->h)
-            {
-            //    printf("\tExact mode!\n");
-                return;
-            }
-
-            // Is this mode better than the current mode?
-
-            diff = (screen_width - modes[i]->w) 
-                     * (screen_width - modes[i]->w)
-                 + (screen_height - modes[i]->h)
-                     * (screen_height - modes[i]->h);
-
-            if (diff < best_diff)
-            {
-            //    printf("\tA valid mode\n");
-                best_mode = modes[i];
-                best_diff = diff;
-            }
+        if (screen_mode == NULL || screen_mode->poor_quality)
+        {
+        //    printf("\tUnsupported / poor quality\n");
+            continue;
         }
 
-        if (best_mode == NULL)
-        {
-            // Unable to find a valid mode!
+        // Do we have the exact mode?
+        // If so, no autoadjust needed
 
-            I_Error("Unable to find any valid video mode at all!");
+        if (screen_width == modes[i]->w && screen_height == modes[i]->h)
+        {
+        //    printf("\tExact mode!\n");
+            return true;
         }
 
-        printf("I_InitGraphics: %ix%i mode not supported on this machine.\n",
-               screen_width, screen_height);
+        // Is this mode better than the current mode?
 
-        screen_width = best_mode->w;
-        screen_height = best_mode->h;
+        diff = (screen_width - modes[i]->w) * (screen_width - modes[i]->w)
+             + (screen_height - modes[i]->h) * (screen_height - modes[i]->h);
 
+        if (diff < best_diff)
+        {
+        //    printf("\tA valid mode\n");
+            best_mode = modes[i];
+            best_diff = diff;
+        }
     }
-    else
+
+    if (best_mode == NULL)
     {
-        screen_mode_t *best_mode;
+        // Unable to find a valid mode!
 
-        //
-        // Windowed mode.
-        //
-        // Find a screen_mode_t to fit within the current settings
-        //
+        return false;
+    }
 
-        best_mode = I_FindScreenMode(screen_width, screen_height);
+    printf("I_InitGraphics: %ix%i mode not supported on this machine.\n",
+           screen_width, screen_height);
 
-        if (best_mode == NULL) 
-        {
-            // Nothing fits within the current settings.
-            // Pick the closest to 320x200 possible.
+    screen_width = best_mode->w;
+    screen_height = best_mode->h;
 
-            best_mode = I_FindScreenMode(SCREENWIDTH, SCREENHEIGHT_4_3);
-        }
+    return true;
+}
 
-        // Do we have the exact mode already?
+// Auto-adjust to a valid windowed mode.
 
-        if (best_mode->width == screen_width 
-         && best_mode->height == screen_height)
-        {
-            return;
-        }
+static void AutoAdjustWindowed(void)
+{
+    screen_mode_t *best_mode;
 
+    // Find a screen_mode_t to fit within the current settings
+
+    best_mode = I_FindScreenMode(screen_width, screen_height);
+
+    if (best_mode == NULL)
+    {
+        // Nothing fits within the current settings.
+        // Pick the closest to 320x200 possible.
+
+        best_mode = I_FindScreenMode(SCREENWIDTH, SCREENHEIGHT_4_3);
+    }
+
+    // Switch to the best mode if necessary.
+
+    if (best_mode->width != screen_width || best_mode->height != screen_height)
+    {
         printf("I_InitGraphics: Cannot run at specified mode: %ix%i\n",
                screen_width, screen_height);
 
@@ -1075,14 +1074,45 @@
         screen_width = best_mode->width;
         screen_height = best_mode->height;
     }
+}
 
-    printf("I_InitGraphics: Auto-adjusted to %ix%i.\n",
-           screen_width, screen_height);
+// If the video mode set in the configuration file is not available,
+// try to choose a different mode.
 
-    printf("NOTE: Your video settings have been adjusted.  "
-           "To disable this behavior,\n"
-           "set autoadjust_video_settings to 0 in your "
-           "configuration file.\n");
+static void I_AutoAdjustSettings(void)
+{
+    int old_screen_w, old_screen_h;
+
+    old_screen_w = screen_width;
+    old_screen_h = screen_height;
+
+    // If we are running fullscreen, try to autoadjust to a valid fullscreen
+    // mode.  If this is impossible, switch to windowed.
+
+    if (fullscreen && !AutoAdjustFullscreen())
+    {
+        fullscreen = 0;
+    }
+
+    // If we are running windowed, pick a valid window size.
+
+    if (!fullscreen)
+    {
+        AutoAdjustWindowed();
+    }
+
+    // Have the settings changed?  Show a message.
+
+    if (screen_width != old_screen_w || screen_height != old_screen_h)
+    {
+        printf("I_InitGraphics: Auto-adjusted to %ix%i.\n",
+               screen_width, screen_height);
+
+        printf("NOTE: Your video settings have been adjusted.  "
+               "To disable this behavior,\n"
+               "set autoadjust_video_settings to 0 in your "
+               "configuration file.\n");
+    }
 }
 
 // Set video size to a particular scale factor (1x, 2x, 3x, etc.)