src/video/photon/SDL_ph_modes.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_ph_modes_c.h"
    25 
    26 static PgVideoModeInfo_t mode_info;
    27 static PgVideoModes_t mode_list;
    28 
    29 /* The current list of available video modes */
    30 SDL_Rect SDL_modelist[PH_MAX_VIDEOMODES];
    31 SDL_Rect *SDL_modearray[PH_MAX_VIDEOMODES];
    32 
    33 static int
    34 compare_modes_by_res(const void *mode1, const void *mode2)
    35 {
    36     PgVideoModeInfo_t mode1_info;
    37     PgVideoModeInfo_t mode2_info;
    38 
    39     if (PgGetVideoModeInfo(*(unsigned short *) mode1, &mode1_info) < 0) {
    40         return 0;
    41     }
    42 
    43     if (PgGetVideoModeInfo(*(unsigned short *) mode2, &mode2_info) < 0) {
    44         return 0;
    45     }
    46 
    47     if (mode1_info.width == mode2_info.width) {
    48         return mode2_info.height - mode1_info.height;
    49     } else {
    50         return mode2_info.width - mode1_info.width;
    51     }
    52 }
    53 
    54 SDL_Rect **
    55 ph_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
    56 {
    57     int i = 0;
    58     int j = 0;
    59     SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
    60 
    61     for (i = 0; i < PH_MAX_VIDEOMODES; i++) {
    62         SDL_modearray[i] = &SDL_modelist[i];
    63     }
    64 
    65     if (PgGetVideoModeList(&mode_list) < 0) {
    66         SDL_SetError
    67             ("ph_ListModes(): PgGetVideoModeList() function failed !\n");
    68         return NULL;
    69     }
    70 
    71     mode_info.bits_per_pixel = 0;
    72 
    73     for (i = 0; i < mode_list.num_modes; i++) {
    74         if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
    75             SDL_SetError
    76                 ("ph_ListModes(): PgGetVideoModeInfo() function failed on mode: 0x%X.\n",
    77                  mode_list.modes[i]);
    78             return NULL;
    79         }
    80         if (mode_info.bits_per_pixel == format->BitsPerPixel) {
    81             Amodelist[j].w = mode_info.width;
    82             Amodelist[j].h = mode_info.height;
    83             Amodelist[j].x = 0;
    84             Amodelist[j].y = 0;
    85             j++;
    86         }
    87     }
    88 
    89     /* reorder biggest for smallest, assume width dominates */
    90 
    91     for (i = 0; i < j; i++) {
    92         SDL_modelist[i].w = Amodelist[j - i - 1].w;
    93         SDL_modelist[i].h = Amodelist[j - i - 1].h;
    94         SDL_modelist[i].x = Amodelist[j - i - 1].x;
    95         SDL_modelist[i].y = Amodelist[j - i - 1].y;
    96     }
    97     SDL_modearray[j] = NULL;
    98 
    99     return SDL_modearray;
   100 }
   101 
   102 void
   103 ph_FreeVideoModes(_THIS)
   104 {
   105     return;
   106 }
   107 
   108 /* return the mode associated with width, height and bpp */
   109 /* if there is no mode then zero is returned             */
   110 int
   111 ph_GetVideoMode(int width, int height, int bpp)
   112 {
   113     int i;
   114     int modestage = 0;
   115     int closestmode = 0;
   116 
   117     if (PgGetVideoModeList(&mode_list) < 0) {
   118         return -1;
   119     }
   120 
   121     /* special case for the double-sized 320x200 mode */
   122     if ((width == 640) && (height == 400)) {
   123         modestage = 1;
   124     }
   125 
   126     /* search list for exact match */
   127     for (i = 0; i < mode_list.num_modes; i++) {
   128         if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
   129             return 0;
   130         }
   131 
   132         if ((mode_info.width == width) && (mode_info.height == height) &&
   133             (mode_info.bits_per_pixel == bpp)) {
   134             return mode_list.modes[i];
   135         } else {
   136             if ((modestage) && (mode_info.width == width)
   137                 && (mode_info.height == height + 80)
   138                 && (mode_info.bits_per_pixel == bpp)) {
   139                 modestage = 2;
   140                 closestmode = mode_list.modes[i];
   141             }
   142         }
   143     }
   144 
   145     /* if we are here, then no 640x400xbpp mode found and we'll emulate it via 640x480xbpp mode */
   146     if (modestage == 2) {
   147         return closestmode;
   148     }
   149 
   150     return (i == mode_list.num_modes) ? 0 : mode_list.modes[i];
   151 }
   152 
   153 /* return the mode associated with width, height and bpp               */
   154 /* if requested bpp is not found the mode with closest bpp is returned */
   155 int
   156 get_mode_any_format(int width, int height, int bpp)
   157 {
   158     int i, closest, delta, min_delta;
   159 
   160     if (PgGetVideoModeList(&mode_list) < 0) {
   161         return -1;
   162     }
   163 
   164     SDL_qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short),
   165               compare_modes_by_res);
   166 
   167     for (i = 0; i < mode_list.num_modes; i++) {
   168         if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
   169             return 0;
   170         }
   171         if ((mode_info.width == width) && (mode_info.height == height)) {
   172             break;
   173         }
   174     }
   175 
   176     if (i < mode_list.num_modes) {
   177         /* get closest bpp */
   178         closest = i++;
   179         if (mode_info.bits_per_pixel == bpp) {
   180             return mode_list.modes[closest];
   181         }
   182 
   183         min_delta = abs(mode_info.bits_per_pixel - bpp);
   184 
   185         while (1) {
   186             if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
   187                 return 0;
   188             }
   189 
   190             if ((mode_info.width != width)
   191                 || (mode_info.height != height)) {
   192                 break;
   193             } else {
   194                 if (mode_info.bits_per_pixel == bpp) {
   195                     closest = i;
   196                     break;
   197                 } else {
   198                     delta = abs(mode_info.bits_per_pixel - bpp);
   199                     if (delta < min_delta) {
   200                         closest = i;
   201                         min_delta = delta;
   202                     }
   203                     i++;
   204                 }
   205             }
   206         }
   207         return mode_list.modes[closest];
   208     }
   209 
   210     return 0;
   211 }
   212 
   213 int
   214 ph_ToggleFullScreen(_THIS, int on)
   215 {
   216     return -1;
   217 }
   218 
   219 int
   220 ph_EnterFullScreen(_THIS, SDL_Surface * screen, int fmode)
   221 {
   222     PgDisplaySettings_t settings;
   223     int mode;
   224     char *refreshrate;
   225     int refreshratenum;
   226 
   227     if (!currently_fullscreen) {
   228         /* Get the video mode and set it */
   229         if (screen->flags & SDL_ANYFORMAT) {
   230             if ((mode =
   231                  get_mode_any_format(screen->w, screen->h,
   232                                      screen->format->BitsPerPixel)) == 0) {
   233                 SDL_SetError
   234                     ("ph_EnterFullScreen(): can't find appropriate video mode !\n");
   235                 return 0;
   236             }
   237         } else {
   238             if ((mode =
   239                  ph_GetVideoMode(screen->w, screen->h,
   240                                  screen->format->BitsPerPixel)) == 0) {
   241                 SDL_SetError
   242                     ("ph_EnterFullScreen(): can't find appropriate video mode !\n");
   243                 return 0;
   244             }
   245             if (PgGetVideoModeInfo(mode, &mode_info) < 0) {
   246                 SDL_SetError
   247                     ("ph_EnterFullScreen(): can't get video mode capabilities !\n");
   248                 return 0;
   249             }
   250             if (mode_info.height != screen->h) {
   251                 if ((mode_info.height == 480) && (screen->h == 400)) {
   252                     videomode_emulatemode = 1;
   253                 }
   254             } else {
   255                 videomode_emulatemode = 0;
   256             }
   257         }
   258 
   259         /* save old video mode caps */
   260         PgGetVideoMode(&settings);
   261         old_video_mode = settings.mode;
   262         old_refresh_rate = settings.refresh;
   263 
   264         /* setup new video mode */
   265         settings.mode = mode;
   266         settings.refresh = 0;
   267         settings.flags = 0;
   268 
   269         refreshrate = SDL_getenv("SDL_PHOTON_FULLSCREEN_REFRESH");
   270         if (refreshrate != NULL) {
   271             if (SDL_sscanf(refreshrate, "%d", &refreshratenum) == 1) {
   272                 settings.refresh = refreshratenum;
   273             }
   274         }
   275 
   276         if (PgSetVideoMode(&settings) < 0) {
   277             SDL_SetError
   278                 ("ph_EnterFullScreen(): PgSetVideoMode() call failed !\n");
   279             return 0;
   280         }
   281 
   282         if (this->screen) {
   283             if (this->screen->flags & SDL_INTERNALOPENGL) {
   284 #if !SDL_VIDEO_OPENGL || (_NTO_VERSION < 630)
   285                 return 0;       /* 6.3.0 */
   286 #endif
   287             }
   288         }
   289 
   290         if (fmode == 0) {
   291             if (OCImage.direct_context == NULL) {
   292                 OCImage.direct_context =
   293                     (PdDirectContext_t *) PdCreateDirectContext();
   294                 if (!OCImage.direct_context) {
   295                     SDL_SetError
   296                         ("ph_EnterFullScreen(): Can't create direct context !\n");
   297                     ph_LeaveFullScreen(this);
   298                     return 0;
   299                 }
   300             }
   301             OCImage.oldDC = PdDirectStart(OCImage.direct_context);
   302         }
   303 
   304         currently_fullscreen = 1;
   305     }
   306     PgFlush();
   307 
   308     return 1;
   309 }
   310 
   311 int
   312 ph_LeaveFullScreen(_THIS)
   313 {
   314     PgDisplaySettings_t oldmode_settings;
   315 
   316     if (currently_fullscreen) {
   317         if ((this->screen) && (this->screen->flags & SDL_INTERNALOPENGL)) {
   318 #if !SDL_VIDEO_OPENGL || (_NTO_VERSION < 630)
   319             return 0;
   320 #endif
   321         }
   322 
   323         /* release routines starts here */
   324         {
   325             if (OCImage.direct_context) {
   326                 PdDirectStop(OCImage.direct_context);
   327                 PdReleaseDirectContext(OCImage.direct_context);
   328                 OCImage.direct_context = NULL;
   329             }
   330             if (OCImage.oldDC) {
   331                 PhDCSetCurrent(OCImage.oldDC);
   332                 OCImage.oldDC = NULL;
   333             }
   334 
   335             currently_fullscreen = 0;
   336 
   337             /* Restore old video mode */
   338             if (old_video_mode != -1) {
   339                 oldmode_settings.mode = (unsigned short) old_video_mode;
   340                 oldmode_settings.refresh = (unsigned short) old_refresh_rate;
   341                 oldmode_settings.flags = 0;
   342 
   343                 if (PgSetVideoMode(&oldmode_settings) < 0) {
   344                     SDL_SetError
   345                         ("Ph_LeaveFullScreen(): PgSetVideoMode() function failed !\n");
   346                     return 0;
   347                 }
   348             }
   349 
   350             old_video_mode = -1;
   351             old_refresh_rate = -1;
   352         }
   353     }
   354     return 1;
   355 }
   356 
   357 /* vi: set ts=4 sw=4 expandtab: */