Annotation of hatari/src/resolution.c, revision 1.1.1.1

1.1       root        1: /*
                      2:   Hatari - resolution.c
                      3: 
                      4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
                      6: 
                      7:   SDL resolution limitation and selection routines.
                      8: */
                      9: const char Resolution_fileid[] = "Hatari resolution.c : " __DATE__ " " __TIME__;
                     10: 
                     11: #include <SDL.h>
                     12: #include "main.h"
                     13: #include "configuration.h"
                     14: #include "resolution.h"
                     15: #include "screen.h"
                     16: 
                     17: #define RESOLUTION_DEBUG 0
                     18: 
                     19: #if RESOLUTION_DEBUG
                     20: #define Dprintf(a) printf a
                     21: #else
                     22: #define Dprintf(a)
                     23: #endif
                     24: 
                     25: 
                     26: /**
                     27:  * Select best resolution from given SDL video modes.
                     28:  * - If width and height are given, select the smallest mode larger
                     29:  *   or equal to requested size
                     30:  * - Otherwise select the largest available mode
                     31:  * return true for success and false if no matching mode was found.
                     32:  */
                     33: static bool Resolution_Select(SDL_Rect **modes, int *width, int *height)
                     34: {
                     35: #define TOO_LARGE 0x7fff
                     36:        int i, bestw, besth;
                     37: 
                     38:        if (!(*width && *height)) {
                     39:                /* search the largest mode (prefer wider ones) */
                     40:                for (i = 0; modes[i]; i++) {
                     41:                        if ((modes[i]->w > *width) && (modes[i]->h >= *height)) {
                     42:                                *width = modes[i]->w;
                     43:                                *height = modes[i]->h;
                     44:                        }
                     45:                }
                     46:                Dprintf(("hostscreen: largest found video mode: %dx%d\n",*width,*height));
                     47:                return true;
                     48:        }
                     49: 
                     50:        /* Search the smallest mode larger or equal to requested size */
                     51:        bestw = TOO_LARGE;
                     52:        besth = TOO_LARGE;
                     53:        for (i = 0; modes[i]; i++) {
                     54:                if ((modes[i]->w >= *width) && (modes[i]->h >= *height)) {
                     55:                        if ((modes[i]->w < bestw) || (modes[i]->h < besth)) {
                     56:                                bestw = modes[i]->w;
                     57:                                besth = modes[i]->h;
                     58:                        }
                     59:                }
                     60:        }
                     61:        if (bestw == TOO_LARGE || besth == TOO_LARGE) {
                     62:                return false;
                     63:        }
                     64:        *width = bestw;
                     65:        *height = besth;
                     66:        Dprintf(("hostscreen: video mode found: %dx%d\n",*width,*height));
                     67:        return true;
                     68: #undef TOO_LARGE
                     69: }
                     70: 
                     71: 
                     72: /**
                     73:  * Search video mode size that best suits the given width/height/bpp
                     74:  * constraints and set them into given arguments.  With zeroed arguments,
                     75:  * return largest video mode.
                     76:  */
                     77: void Resolution_Search(int *width, int *height, int *bpp)
                     78: {
                     79:        SDL_Rect **modes;
                     80:        SDL_PixelFormat pixelformat;
                     81:        Uint32 modeflags;
                     82: 
                     83:        /* Search in available modes the best suited */
                     84:        Dprintf(("hostscreen: video mode asked: %dx%dx%d\n",
                     85:                 *width, *height, *bpp));
                     86: 
                     87:        /* Read available video modes */
                     88:        modeflags = 0 /*SDL_HWSURFACE | SDL_HWPALETTE*/;
                     89:        if (bInFullScreen)
                     90:                modeflags |= SDL_FULLSCREEN;
                     91: 
                     92:        /*--- Search a video mode with asked bpp ---*/
                     93:        if (*bpp != 0) {
                     94:                pixelformat.BitsPerPixel = *bpp;
                     95:                modes = SDL_ListModes(&pixelformat, modeflags);
                     96:                if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
                     97:                        Dprintf(("hostscreen: searching a good video mode (any bpp)\n"));
                     98:                        if (Resolution_Select(modes, width, height)) {
                     99:                                Dprintf(("hostscreen: video mode selected: %dx%dx%d\n",
                    100:                                         *width, *height, *bpp));
                    101:                                return;
                    102:                        }
                    103:                }
                    104:        }
                    105: 
                    106:        /*--- Search a video mode with any bpp ---*/
                    107:        modes = SDL_ListModes(NULL, modeflags);
                    108:        if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
                    109:                Dprintf(("hostscreen: searching a good video mode\n"));
                    110:                if (Resolution_Select(modes, width, height)) {
                    111:                        Dprintf(("hostscreen: video mode selected: %dx%dx%d\n",
                    112:                                 *width, *height, *bpp));
                    113:                        return;
                    114:                }
                    115:        }
                    116: 
                    117:        if (modes == (SDL_Rect **) 0) {
                    118:                fprintf(stderr, "WARNING: No suitable video modes available!\n");
                    119:        }
                    120: 
                    121:        if (modes == (SDL_Rect **) -1) {
                    122:                /* Any mode available */
                    123:                Dprintf(("hostscreen: All resolutions available.\n"));
                    124:        }
                    125: 
                    126:        Dprintf(("hostscreen: video mode selected: %dx%dx%d\n",
                    127:                 *width, *height, *bpp));
                    128: }
                    129: 
                    130: 
                    131: /**
                    132:  * Set given width & height arguments to maximum size in the configuration,
                    133:  * or if that's too large for the requested bit depth, to the largest
                    134:  * available video mode size.
                    135:  */
                    136: void Resolution_GetLimits(int *width, int *height, int *bpp)
                    137: {
                    138:        *width = *height = 0;
                    139:        /* constrain max size to what HW/SDL offers */
                    140:        Resolution_Search(width, height, bpp);
                    141:        if (!(*width && *width) ||
                    142:            (ConfigureParams.Screen.nMaxWidth < *width &&
                    143:             ConfigureParams.Screen.nMaxHeight < *height)) {
                    144:                /* already within it, use user provided value */
                    145:                *width = ConfigureParams.Screen.nMaxWidth;
                    146:                *height = ConfigureParams.Screen.nMaxHeight;
                    147:        }
                    148: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.