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

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: 
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:        }
        !            54:        Dprintf(("Desktop resolution: %dx%d\n",DesktopWidth, DesktopHeight));
        !            55:        Dprintf(("Configured Max res: %dx%d\n",ConfigureParams.Screen.nMaxWidth,ConfigureParams.Screen.nMaxHeight));
        !            56: }
        !            57: 
        !            58: /**
        !            59:  * Get current desktop resolution
        !            60:  */
        !            61: void Resolution_GetDesktopSize(int *width, int *height)
        !            62: {
        !            63:        *width = DesktopWidth;
        !            64:        *height = DesktopHeight;
        !            65: }
1.1       root       66: 
                     67: /**
                     68:  * Select best resolution from given SDL video modes.
                     69:  * - If width and height are given, select the smallest mode larger
                     70:  *   or equal to requested size
                     71:  * - Otherwise select the largest available mode
                     72:  * return true for success and false if no matching mode was found.
                     73:  */
                     74: static bool Resolution_Select(SDL_Rect **modes, int *width, int *height)
                     75: {
                     76: #define TOO_LARGE 0x7fff
                     77:        int i, bestw, besth;
                     78: 
                     79:        if (!(*width && *height)) {
                     80:                /* search the largest mode (prefer wider ones) */
                     81:                for (i = 0; modes[i]; i++) {
                     82:                        if ((modes[i]->w > *width) && (modes[i]->h >= *height)) {
                     83:                                *width = modes[i]->w;
                     84:                                *height = modes[i]->h;
                     85:                        }
                     86:                }
1.1.1.2 ! root       87:                Dprintf(("resolution: largest found video mode: %dx%d\n",*width,*height));
1.1       root       88:                return true;
                     89:        }
                     90: 
                     91:        /* Search the smallest mode larger or equal to requested size */
                     92:        bestw = TOO_LARGE;
                     93:        besth = TOO_LARGE;
                     94:        for (i = 0; modes[i]; i++) {
                     95:                if ((modes[i]->w >= *width) && (modes[i]->h >= *height)) {
                     96:                        if ((modes[i]->w < bestw) || (modes[i]->h < besth)) {
                     97:                                bestw = modes[i]->w;
                     98:                                besth = modes[i]->h;
                     99:                        }
                    100:                }
                    101:        }
                    102:        if (bestw == TOO_LARGE || besth == TOO_LARGE) {
                    103:                return false;
                    104:        }
                    105:        *width = bestw;
                    106:        *height = besth;
1.1.1.2 ! root      107:        Dprintf(("resolution: video mode found: %dx%d\n",*width,*height));
1.1       root      108:        return true;
                    109: #undef TOO_LARGE
                    110: }
                    111: 
                    112: 
                    113: /**
                    114:  * Search video mode size that best suits the given width/height/bpp
                    115:  * constraints and set them into given arguments.  With zeroed arguments,
                    116:  * return largest video mode.
                    117:  */
                    118: void Resolution_Search(int *width, int *height, int *bpp)
                    119: {
                    120:        SDL_Rect **modes;
                    121:        SDL_PixelFormat pixelformat;
                    122:        Uint32 modeflags;
                    123: 
                    124:        /* Search in available modes the best suited */
1.1.1.2 ! root      125:        Dprintf(("resolution: video mode asked: %dx%dx%d\n",
1.1       root      126:                 *width, *height, *bpp));
1.1.1.2 ! root      127:        
1.1       root      128:        /* Read available video modes */
                    129:        modeflags = 0 /*SDL_HWSURFACE | SDL_HWPALETTE*/;
1.1.1.2 ! root      130:        if (bInFullScreen) {
        !           131:                /* resolution change not allowed? */
        !           132:                if (ConfigureParams.Screen.bKeepResolution) {
        !           133:                        Dprintf(("resolution: limit to desktop size\n"));
        !           134:                        Resolution_GetDesktopSize(width, height);
        !           135:                        return;
        !           136:                }
1.1       root      137:                modeflags |= SDL_FULLSCREEN;
1.1.1.2 ! root      138:        }
1.1       root      139: 
                    140:        /*--- Search a video mode with asked bpp ---*/
                    141:        if (*bpp != 0) {
                    142:                pixelformat.BitsPerPixel = *bpp;
                    143:                modes = SDL_ListModes(&pixelformat, modeflags);
                    144:                if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
1.1.1.2 ! root      145:                        Dprintf(("resolution: searching a good video mode (given bpp)\n"));
1.1       root      146:                        if (Resolution_Select(modes, width, height)) {
1.1.1.2 ! root      147:                                Dprintf(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      148:                                         *width, *height, *bpp));
                    149:                                return;
                    150:                        }
                    151:                }
                    152:        }
                    153: 
                    154:        /*--- Search a video mode with any bpp ---*/
                    155:        modes = SDL_ListModes(NULL, modeflags);
                    156:        if ((modes != (SDL_Rect **) 0) && (modes != (SDL_Rect **) -1)) {
1.1.1.2 ! root      157:                Dprintf(("resolution: searching a good video mode (any bpp)\n"));
1.1       root      158:                if (Resolution_Select(modes, width, height)) {
1.1.1.2 ! root      159:                        Dprintf(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      160:                                 *width, *height, *bpp));
                    161:                        return;
                    162:                }
                    163:        }
                    164: 
                    165:        if (modes == (SDL_Rect **) 0) {
                    166:                fprintf(stderr, "WARNING: No suitable video modes available!\n");
                    167:        }
                    168: 
                    169:        if (modes == (SDL_Rect **) -1) {
                    170:                /* Any mode available */
1.1.1.2 ! root      171:                Dprintf(("resolution: All resolutions available.\n"));
1.1       root      172:        }
                    173: 
1.1.1.2 ! root      174:        Dprintf(("resolution: video mode selected: %dx%dx%d\n",
1.1       root      175:                 *width, *height, *bpp));
                    176: }
                    177: 
                    178: 
                    179: /**
1.1.1.2 ! root      180:  * Set given width & height arguments to maximum size allowed in the
        !           181:  * configuration, or if that's too large for the requested bit depth,
        !           182:  * to the largest available video mode size.
1.1       root      183:  */
                    184: void Resolution_GetLimits(int *width, int *height, int *bpp)
                    185: {
                    186:        *width = *height = 0;
                    187:        /* constrain max size to what HW/SDL offers */
1.1.1.2 ! root      188:        Dprintf(("resolution: request limits for: %dx%dx%d\n", *width, *height, *bpp));
1.1       root      189:        Resolution_Search(width, height, bpp);
1.1.1.2 ! root      190: 
        !           191:        if (bInFullScreen && ConfigureParams.Screen.bKeepResolution) {
        !           192:                /* resolution change not allowed */
        !           193:                Dprintf(("resolution: limit to desktop size\n"));
        !           194:                Resolution_GetDesktopSize(width, height);
        !           195:                return;
        !           196:        }
        !           197:        if (!(*width && *height) ||
1.1       root      198:            (ConfigureParams.Screen.nMaxWidth < *width &&
                    199:             ConfigureParams.Screen.nMaxHeight < *height)) {
1.1.1.2 ! root      200:                Dprintf(("resolution: limit to user configured max\n"));
1.1       root      201:                *width = ConfigureParams.Screen.nMaxWidth;
                    202:                *height = ConfigureParams.Screen.nMaxHeight;
                    203:        }
                    204: }

unix.superglobalmegacorp.com

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