foxygit / doom Log in
commit 6307e5e2519c165ea0c9496849e8013cd92315e1
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Mon Nov 29 20:18:10 2010 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Mon Nov 29 20:18:10 2010 +0000

    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
---
 setup/display.c | 41 +++++++++++++++++++++++++++++++++--------
 src/i_video.c   | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 84 insertions(+), 12 deletions(-)

diff --git a/setup/display.c b/setup/display.c
index b20b6667..42a1dced 100644
--- a/setup/display.c
+++ b/setup/display.c
@@ -243,7 +243,7 @@ static int GetSupportedBPPIndex(char *description)
             return i;
         }
     }
-
+
     // Shouldn't happen; fall back to the first in the list.

     return 0;
@@ -251,7 +251,7 @@ static int GetSupportedBPPIndex(char *description)

 // 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 @@ static void SetSelectedBPP(void)
         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))
diff --git a/src/i_video.c b/src/i_video.c
index 62c74b73..488c08a0 100644
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -1207,15 +1207,61 @@ static void AutoAdjustWindowed(void)
     }
 }

+// 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 @@ static void I_AutoAdjustSettings(void)

     // 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"