shithub: choc

Download patch

ref: 6307e5e2519c165ea0c9496849e8013cd92315e1
parent: d417ce23d62443acea51fd11d24e83dd83d46188
author: Simon Howard <fraggle@gmail.com>
date: Mon Nov 29 15:18:10 EST 2010

Auto-adjust the screen color depth if the configured color depth is not
supported by the hardware.

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

--- a/setup/display.c
+++ b/setup/display.c
@@ -243,7 +243,7 @@
             return i;
         }
     }
-    
+
     // Shouldn't happen; fall back to the first in the list.
 
     return 0;
@@ -251,7 +251,7 @@
 
 // Set selected_bpp to match screen_bpp.
 
-static void SetSelectedBPP(void)
+static int TrySetSelectedBPP(void)
 {
     unsigned int num_depths = sizeof(pixel_depths) / sizeof(*pixel_depths);
     unsigned int i;
@@ -264,16 +264,41 @@
         if (pixel_depths[i].bpp == screen_bpp)
         {
             selected_bpp = GetSupportedBPPIndex(pixel_depths[i].description);
-            return;
+            return 1;
         }
     }
 
-    // screen_bpp does not match anything in pixel_depths.  Set selected_bpp
-    // to something that is supported, and reset screen_bpp to something
-    // sensible while we're at it.
+    return 0;
+}
 
-    selected_bpp = 0;
-    screen_bpp = GetSelectedBPP();
+static void SetSelectedBPP(void)
+{
+    const SDL_VideoInfo *info;
+
+    if (TrySetSelectedBPP())
+    {
+        return;
+    }
+
+    // screen_bpp does not match any supported pixel depth.  Query SDL
+    // to find out what it recommends using.
+
+    info = SDL_GetVideoInfo();
+
+    if (info != NULL && info->vfmt != NULL)
+    {
+        screen_bpp = info->vfmt->BitsPerPixel;
+    }
+
+    // Try again.
+
+    if (!TrySetSelectedBPP())
+    {
+        // Give up and just use the first in the list.
+
+        selected_bpp = 0;
+        screen_bpp = GetSelectedBPP();
+    }
 }
 
 static void ModeSelected(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(mode))
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -1207,16 +1207,62 @@
     }
 }
 
+// Auto-adjust to a valid color depth.
+
+static void AutoAdjustColorDepth(void)
+{
+    SDL_Rect **modes;
+    SDL_PixelFormat format;
+    const SDL_VideoInfo *info;
+    int flags;
+
+    if (fullscreen)
+    {
+        flags = SDL_FULLSCREEN;
+    }
+    else
+    {
+        flags = 0;
+    }
+
+    format.BitsPerPixel = screen_bpp;
+    format.BytesPerPixel = (screen_bpp + 7) / 8;
+
+    // Are any screen modes supported at the configured color depth?
+
+    modes = SDL_ListModes(&format, flags);
+
+    // If not, we must autoadjust to something sensible.
+
+    if (modes == NULL)
+    {
+        printf("I_InitGraphics: %ibpp color depth not supported.\n",
+               screen_bpp);
+
+        info = SDL_GetVideoInfo();
+
+        if (info != NULL && info->vfmt != NULL)
+        {
+            screen_bpp = info->vfmt->BitsPerPixel;
+        }
+    }
+}
+
 // If the video mode set in the configuration file is not available,
 // try to choose a different mode.
 
 static void I_AutoAdjustSettings(void)
 {
-    int old_screen_w, old_screen_h;
+    int old_screen_w, old_screen_h, old_screen_bpp;
 
     old_screen_w = screen_width;
     old_screen_h = screen_height;
+    old_screen_bpp = screen_bpp;
 
+    // Possibly adjust color depth.
+
+    AutoAdjustColorDepth();
+
     // If we are running fullscreen, try to autoadjust to a valid fullscreen
     // mode.  If this is impossible, switch to windowed.
 
@@ -1234,10 +1280,11 @@
 
     // Have the settings changed?  Show a message.
 
-    if (screen_width != old_screen_w || screen_height != old_screen_h)
+    if (screen_width != old_screen_w || screen_height != old_screen_h
+     || screen_bpp != old_screen_bpp)
     {
-        printf("I_InitGraphics: Auto-adjusted to %ix%i.\n",
-               screen_width, screen_height);
+        printf("I_InitGraphics: Auto-adjusted to %ix%ix%ibpp.\n",
+               screen_width, screen_height, screen_bpp);
 
         printf("NOTE: Your video settings have been adjusted.  "
                "To disable this behavior,\n"