Browse Source

Added generic video mode selection.

Camilla Berglund 12 years ago
parent
commit
cf42282cfb
2 changed files with 44 additions and 0 deletions
  1. 3 0
      src/internal.h
  2. 41 0
      src/monitor.c

+ 3 - 0
src/internal.h

@@ -378,6 +378,9 @@ void _glfwInputMonitorChange(void);
 //========================================================================
 
 // Fullscren management (fullscreen.c)
+const GLFWvidmode* _glfwChooseVideoMode(const GLFWvidmode* desired,
+                                        const GLFWvidmode* alternatives,
+                                        unsigned int count);
 int _glfwCompareVideoModes(const GLFWvidmode* first, const GLFWvidmode* second);
 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue);
 

+ 41 - 0
src/monitor.c

@@ -32,6 +32,8 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <limits.h>
+
 #if defined(_MSC_VER)
  #include <malloc.h>
  #define strdup _strdup
@@ -195,6 +197,45 @@ void _glfwDestroyMonitors(void)
 }
 
 
+//========================================================================
+// Returns the video mode closest to the desired one
+//========================================================================
+
+const GLFWvidmode* _glfwChooseVideoMode(const GLFWvidmode* desired,
+                                        const GLFWvidmode* alternatives,
+                                        unsigned int count)
+{
+    unsigned int i;
+    unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
+    unsigned int colorDiff, leastColorDiff = UINT_MAX;
+    const GLFWvidmode* current;
+    const GLFWvidmode* closest = NULL;
+
+    for (i = 0;  i < count;  i++)
+    {
+        current = alternatives + i;
+
+        colorDiff = abs((current->redBits + current->greenBits + current->blueBits) -
+                        (desired->redBits + desired->greenBits + desired->blueBits));
+
+        sizeDiff = abs((current->width - desired->width) *
+                       (current->width - desired->width) +
+                       (current->height - desired->height) *
+                       (current->height - desired->height));
+
+        if ((colorDiff < leastColorDiff) ||
+            (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff))
+        {
+            closest = current;
+            leastSizeDiff = sizeDiff;
+            leastColorDiff = colorDiff;
+        }
+    }
+
+    return closest;
+}
+
+
 //========================================================================
 // Lexical comparison of GLFW video modes
 //========================================================================