From cf42282cfba20df08fe581f1e781880f1e381ba5 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 31 Dec 2012 03:04:04 +0100 Subject: [PATCH] Added generic video mode selection. --- src/internal.h | 3 +++ src/monitor.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/internal.h b/src/internal.h index 9fa11643..5c55d954 100644 --- a/src/internal.h +++ b/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); diff --git a/src/monitor.c b/src/monitor.c index ae324ca3..38fd9fed 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -32,6 +32,8 @@ #include #include +#include + #if defined(_MSC_VER) #include #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 //========================================================================