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

1.1       root        1: /*
                      2:   Hatari - resolution.c
                      3: 
1.1.1.4   root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        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: 
1.1.1.5 ! root       17: #define DEBUG 0
1.1       root       18: 
1.1.1.5 ! root       19: #if DEBUG
        !            20: # define DEBUGPRINT(x) printf x
1.1       root       21: #else
1.1.1.5 ! root       22: # define DEBUGPRINT(x)
1.1       root       23: #endif
                     24: 
1.1.1.2   root       25: static int DesktopWidth, DesktopHeight;
                     26: 
                     27: /**
                     28:  * Initilizes resolution settings (gets current desktop
                     29:  * resolution, sets max Falcon/TT Videl zooming resolution).
                     30:  */
                     31: void Resolution_Init(void)
                     32: {
                     33:        /* Needs to be called after SDL video and configuration
                     34:         * initialization, but before Hatari Screen init is called
                     35:         * for the first time!
                     36:         */
                     37:        const SDL_VideoInfo* info = SDL_GetVideoInfo();
                     38:        if (info->current_w >= 640 && info->current_h >= 400) {
                     39:                DesktopWidth = info->current_w;
                     40:                DesktopHeight = info->current_h;
                     41:        } else {
                     42:                /* target 800x600 screen with statusbar out of screen */
                     43:                DesktopWidth = 2*(48+320+48);
                     44:                DesktopHeight = 2*NUM_VISIBLE_LINES+24;
                     45:                fprintf(stderr, "WARNING: invalid desktop size %dx%d, defaulting to %dx%d!\n",
                     46:                        info->current_w, info->current_h, DesktopWidth, DesktopHeight);
                     47:        }
                     48:        /* if user hasn't set own max zoom size, use desktop size */
                     49:        if (!(ConfigureParams.Screen.nMaxWidth &&
                     50:              ConfigureParams.Screen.nMaxHeight)) {
                     51:                ConfigureParams.Screen.nMaxWidth = DesktopWidth;
                     52:                ConfigureParams.Screen.nMaxHeight = DesktopHeight;
                     53:        }
1.1.1.5 ! root       54:        DEBUGPRINT(("Desktop resolution: %dx%d\n",DesktopWidth, DesktopHeight));
1.1.1.4   root       55:        fprintf(stderr, "Configured max Hatari resolution = %dx%d.\n", ConfigureParams.Screen.nMaxWidth, ConfigureParams.Screen.nMaxHeight);
1.1.1.2   root       56: }
                     57: 
                     58: /**
                     59:  * Get current desktop resolution
                     60:  */
                     61: void Resolution_GetDesktopSize(int *width, int *height)
                     62: {
1.1.1.5 ! root       63:        DEBUGPRINT(("resolution: limit to desktop size\n"));
1.1.1.2   root       64:        *width = DesktopWidth;
                     65:        *height = DesktopHeight;
                     66: }
1.1       root       67: 
                     68: /**
1.1.1.3   root       69:  * Get max resolution
                     70:  */
                     71: static void Resolution_GetMaxSize(int *width, int *height)
                     72: {
1.1.1.5 ! root       73:        DEBUGPRINT(("resolution: force to specified max size\n"));
1.1.1.3   root       74:        *width = ConfigureParams.Screen.nMaxWidth;
                     75:        *height = ConfigureParams.Screen.nMaxHeight;
                     76: }
                     77: 
                     78: /**
1.1       root       79:  * Select best resolution from given SDL video modes.
                     80:  * - If width and height are given, select the smallest mode larger
                     81:  *   or equal to requested size
                     82:  * - Otherwise select the largest available mode
                     83:  * return true for success and false if no matching mode was found.
                     84:  */
                     85: static bool Resolution_Select(SDL_Rect **modes, int *width, int *height)
                     86: {
                     87: #define TOO_LARGE 0x7fff
                     88:        int i, bestw, besth;
                     89: 
                     90:        if (!(*width && *height)) {
                     91:                /* search the largest mode (prefer wider ones) */
                     92:                for (i = 0; modes[i]; i++) {
                     93:                        if ((modes[i]->w > *width) && (modes[i]->h >= *height)) {
                     94:                                *width = modes[i]->w;
                     95:                                *height = modes[i]->h;
                     96:                        }
                     97:                }
1.1.1.5 ! root       98:                DEBUGPRINT(("resolution: largest found video mode: %dx%d\n",*width,*height));
1.1       root       99:                return true;
                    100:        }
                    101: 
                    102:        /* Search the smallest mode larger or equal to requested size */
                    103:        bestw = TOO_LARGE;
                    104:        besth = TOO_LARGE;
                    105:        for (i = 0; modes[i]; i++) {
                    106:                if ((modes[i]->w >= *width) && (modes[i]->h >= *height)) {
                    107:                        if ((modes[i]->w < bestw) || (modes[i]->h < besth)) {
                    108:                                bestw = modes[i]->w;
                    109:                                besth = modes[i]->h;
                    110:                        }
                    111:                }
                    112:        }
                    113:        if (bestw == TOO_LARGE || besth == TOO_LARGE) {
                    114:                return false;
                    115:        }
                    116:        *width = bestw;
                    117:        *height = besth;
1.1.1.5 ! root      118:        DEBUGPRINT(("resolution: video mode found: %dx%d\n",*width,*height));
1.1       root      119:        return true;
                    120: #undef TOO_LARGE
                    121: }
                    122: 
                    123: 
                    124: /**
                    125:  * Search video mode size that best suits the given width/height/bpp
                    126:  * constraints and set them into given arguments.  With zeroed arguments,
1.1.1.5 ! root      127:  * set largest video mode.
        !           128:  * 
        !           129:  * Return true if mode is forced (shouldn't be further limited).
1.1       root      130:  */
1.1.1.5 ! root      131: bool Resolution_Search(int *width, int *height, int *bpp, bool keep)
1.1       root      132: {
                    133:        SDL_Rect **modes;
                    134:        SDL_PixelFormat pixelformat;
                    135:        Uint32 modeflags;
                    136: 
                    137:        /* Search in available modes the best suited */
1.1.1.5 ! root      138:        DEBUGPRINT(("resolution: video mode asked: %dx%dx%d (%s)\n",
        !           139:                 *width, *height, *bpp, bInFullScreen ? "fullscreen" : "windowed"));
1.1.1.3   root      140: 
1.1       root      141:        modeflags = 0 /*SDL_HWSURFACE | SDL_HWPALETTE*/;
1.1.1.3   root      142: 
1.1.1.2   root      143:        if (bInFullScreen) {
                    144:                /* resolution change not allowed? */
1.1.1.5 ! root      145:                if (keep) {
1.1.1.2   root      146:                        Resolution_GetDesktopSize(width, height);
1.1.1.5 ! root      147:                        return true;
1.1.1.2   root      148:                }
1.1       root      149:                modeflags |= SDL_FULLSCREEN;
1.1.1.2   root      150:        }
1.1.1.3   root      151:        if (ConfigureParams.Screen.bForceMax) {
                    152:                /* force given max size */
                    153:                Resolution_GetMaxSize(width, height);
1.1.1.5 ! root      154:                return true;
1.1.1.3   root      155:        }
                    156: 
                    157:        /* Read available video modes */
1.1       root      158: 
                    159:        /*--- Search a video mode with asked bpp ---*/
                    160:        if (*bpp != 0) {
                    161:                pixelformat.BitsPerPixel = *bpp;
                    162:                modes = SDL_ListModes(&pixelformat, modeflags);
                    163:                if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
1.1.1.5 ! root      164:                        DEBUGPRINT(("resolution: searching a good video mode (given bpp)\n"));
1.1       root      165:                        if (Resolution_Select(modes, width, height)) {
1.1.1.5 ! root      166:                                DEBUGPRINT(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      167:                                         *width, *height, *bpp));
1.1.1.5 ! root      168:                                return false;
1.1       root      169:                        }
                    170:                }
                    171:        }
                    172: 
                    173:        /*--- Search a video mode with any bpp ---*/
                    174:        modes = SDL_ListModes(NULL, modeflags);
                    175:        if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
1.1.1.5 ! root      176:                DEBUGPRINT(("resolution: searching a good video mode (any bpp)\n"));
1.1       root      177:                if (Resolution_Select(modes, width, height)) {
1.1.1.5 ! root      178:                        DEBUGPRINT(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      179:                                 *width, *height, *bpp));
1.1.1.5 ! root      180:                        return false;
1.1       root      181:                }
                    182:        }
                    183: 
                    184:        if (modes == (SDL_Rect **) 0) {
                    185:                fprintf(stderr, "WARNING: No suitable video modes available!\n");
                    186:        }
                    187: 
                    188:        if (modes == (SDL_Rect **) -1) {
                    189:                /* Any mode available */
1.1.1.5 ! root      190:                DEBUGPRINT(("resolution: All resolutions available.\n"));
1.1       root      191:        }
                    192: 
1.1.1.5 ! root      193:        DEBUGPRINT(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      194:                 *width, *height, *bpp));
1.1.1.5 ! root      195:        return false;
1.1       root      196: }
                    197: 
                    198: 
                    199: /**
1.1.1.2   root      200:  * Set given width & height arguments to maximum size allowed in the
                    201:  * configuration, or if that's too large for the requested bit depth,
                    202:  * to the largest available video mode size.
1.1       root      203:  */
1.1.1.4   root      204: void Resolution_GetLimits(int *width, int *height, int *bpp, bool keep)
1.1       root      205: {
                    206:        *width = *height = 0;
1.1.1.5 ! root      207: 
1.1       root      208:        /* constrain max size to what HW/SDL offers */
1.1.1.5 ! root      209:        DEBUGPRINT(("resolution: request limits for: %dx%dx%d\n", *width, *height, *bpp));
        !           210: 
        !           211:        /* forced resolution? */
        !           212:        if (Resolution_Search(width, height, bpp, keep)) {
1.1.1.3   root      213:                return;
                    214:        }
                    215: 
1.1.1.2   root      216:        if (!(*width && *height) ||
1.1       root      217:            (ConfigureParams.Screen.nMaxWidth < *width &&
                    218:             ConfigureParams.Screen.nMaxHeight < *height)) {
1.1.1.5 ! root      219:                DEBUGPRINT(("resolution: limit to user configured max\n"));
1.1       root      220:                *width = ConfigureParams.Screen.nMaxWidth;
                    221:                *height = ConfigureParams.Screen.nMaxHeight;
                    222:        }
                    223: }

unix.superglobalmegacorp.com

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