From 67057e7ce829c86190851fd3af777b15096cccb5 Mon Sep 17 00:00:00 2001 From: Patrice Mandin Date: Sun, 12 Jun 2005 16:12:55 +0000 Subject: [PATCH] [PATCH] SDL_GetVideoMode() do not find the best video mode The current GetVideoMode() function stops at the first mode which has any dimensions smaller than the one asked, and gives the previous in the list. If I ask 336x224 with this list: 768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200 SDL will give me 640x400, because 640x200 as height smaller than what I asked. However the best mode is the smaller which has both dimensions bigger than the one asked (384x240 in my example). This patch fixes this, plus it does not rely on a sorted video mode list. --- src/video/SDL_video.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 3cd9c9436..5df6ffbc9 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -462,33 +462,29 @@ static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags) SDL_closest_depths[table][0] = *BitsPerPixel; SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel; for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) { + int best; + format.BitsPerPixel = SDL_closest_depths[table][b]; sizes = SDL_ListModes(&format, flags); if ( sizes == (SDL_Rect **)0 ) { /* No sizes supported at this bit-depth */ continue; } + best=0; for ( i=0; sizes[i]; ++i ) { - if ((sizes[i]->w < *w) || (sizes[i]->h < *h)) { - if ( i > 0 ) { - --i; - *w = sizes[i]->w; - *h = sizes[i]->h; - *BitsPerPixel = SDL_closest_depths[table][b]; + /* Mode with both dimensions bigger or equal than asked ? */ + if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) { + /* Mode with any dimension smaller or equal than current best ? */ + if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) { + best=i; supported = 1; - } else { - /* Largest mode too small... */; } - break; } } - if ( (i > 0) && ! sizes[i] ) { - /* The smallest mode was larger than requested, OK */ - --i; - *w = sizes[i]->w; - *h = sizes[i]->h; + if (supported) { + *w=sizes[best]->w; + *h=sizes[best]->h; *BitsPerPixel = SDL_closest_depths[table][b]; - supported = 1; } } if ( ! supported ) {