[PATCH] SDL_GetVideoMode() do not find the best video mode
authorPatrice Mandin <patmandin@gmail.com>
Sun, 12 Jun 2005 16:12:55 +0000
changeset 10768d3b95ece376
parent 1075 1f5ef94e8bef
child 1077 f122afdfa025
[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
     1.1 --- a/src/video/SDL_video.c	Tue Jun 07 13:30:06 2005 +0000
     1.2 +++ b/src/video/SDL_video.c	Sun Jun 12 16:12:55 2005 +0000
     1.3 @@ -462,33 +462,29 @@
     1.4  	SDL_closest_depths[table][0] = *BitsPerPixel;
     1.5  	SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
     1.6  	for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
     1.7 +		int best;
     1.8 +
     1.9  		format.BitsPerPixel = SDL_closest_depths[table][b];
    1.10  		sizes = SDL_ListModes(&format, flags);
    1.11  		if ( sizes == (SDL_Rect **)0 ) {
    1.12  			/* No sizes supported at this bit-depth */
    1.13  			continue;
    1.14  		}
    1.15 +		best=0;
    1.16  		for ( i=0; sizes[i]; ++i ) {
    1.17 -			if ((sizes[i]->w < *w) || (sizes[i]->h < *h)) {
    1.18 -				if ( i > 0 ) {
    1.19 -					--i;
    1.20 -					*w = sizes[i]->w;
    1.21 -					*h = sizes[i]->h;
    1.22 -					*BitsPerPixel = SDL_closest_depths[table][b];
    1.23 +			/* Mode with both dimensions bigger or equal than asked ? */
    1.24 +			if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
    1.25 +				/* Mode with any dimension smaller or equal than current best ? */
    1.26 +				if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
    1.27 +					best=i;
    1.28  					supported = 1;
    1.29 -				} else {
    1.30 -					/* Largest mode too small... */;
    1.31  				}
    1.32 -				break;
    1.33  			}
    1.34  		}
    1.35 -		if ( (i > 0) && ! sizes[i] ) {
    1.36 -			/* The smallest mode was larger than requested, OK */
    1.37 -			--i;
    1.38 -			*w = sizes[i]->w;
    1.39 -			*h = sizes[i]->h;
    1.40 +		if (supported) {
    1.41 +			*w=sizes[best]->w;
    1.42 +			*h=sizes[best]->h;
    1.43  			*BitsPerPixel = SDL_closest_depths[table][b];
    1.44 -			supported = 1;
    1.45  		}
    1.46  	}
    1.47  	if ( ! supported ) {