From a95279e63de59db8448473a5747abccdd7e1ec53 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 13 Sep 2010 16:24:25 +0200 Subject: [PATCH] Replaced ad hoc bubble sort with libc qsort. --- src/fullscreen.c | 54 +++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/fullscreen.c b/src/fullscreen.c index b2b0117a..0cba18a0 100644 --- a/src/fullscreen.c +++ b/src/fullscreen.c @@ -30,6 +30,33 @@ #include "internal.h" +#include + + +static int compareVideoModes(const void* firstPtr, const void* secondPtr) +{ + GLFWvidmode* first = (GLFWvidmode*) firstPtr; + GLFWvidmode* second = (GLFWvidmode*) secondPtr; + + // First sort on color bits per pixel + + int firstBPP = first->redBits + + first->greenBits + + first->blueBits; + int secondBPP = second->redBits + + second->greenBits + + second->blueBits; + + if (firstBPP != secondBPP) + return firstBPP - secondBPP; + + // Then sort on screen area, in pixels + + int firstSize = first->width * first->height; + int secondSize = second->width * second->height; + + return firstSize - secondSize; +} ////////////////////////////////////////////////////////////////////////// ////// GLFW public API ////// @@ -41,8 +68,7 @@ GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount) { - int count, i, swap, res1, res2, depth1, depth2; - GLFWvidmode vm; + int count; if (!_glfwInitialized) { @@ -59,28 +85,8 @@ GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount) // Get list of video modes count = _glfwPlatformGetVideoModes(list, maxcount); - // Sort list (bubble sort) - do - { - swap = 0; - for (i = 0; i < count - 1; i++) - { - res1 = list[i].width * list[i].height; - depth1 = list[i].redBits + list[i].greenBits + list[i].blueBits; - res2 = list[i + 1].width * list[i + 1].height; - depth2 = list[i + 1].redBits + list[i + 1].greenBits + - list[i + 1].blueBits; - - if ((depth2 < depth1) || ((depth2 == depth1) && (res2 < res1))) - { - vm = list[i]; - list[i] = list[i + 1]; - list[i + 1] = vm; - swap = 1; - } - } - } - while (swap); + if (count > 0) + qsort(list, count, sizeof(GLFWvidmode), compareVideoModes); return count; }