2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #include "SDL_ph_modes_c.h"
26 static PgVideoModeInfo_t mode_info;
27 static PgVideoModes_t mode_list;
29 /* The current list of available video modes */
30 SDL_Rect SDL_modelist[PH_MAX_VIDEOMODES];
31 SDL_Rect* SDL_modearray[PH_MAX_VIDEOMODES];
33 static int compare_modes_by_res(const void* mode1, const void* mode2)
35 PgVideoModeInfo_t mode1_info;
36 PgVideoModeInfo_t mode2_info;
38 if (PgGetVideoModeInfo(*(unsigned short*)mode1, &mode1_info) < 0)
43 if (PgGetVideoModeInfo(*(unsigned short*)mode2, &mode2_info) < 0)
48 if (mode1_info.width == mode2_info.width)
50 return mode2_info.height - mode1_info.height;
54 return mode2_info.width - mode1_info.width;
58 SDL_Rect **ph_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
62 SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
64 for (i=0; i<PH_MAX_VIDEOMODES; i++)
66 SDL_modearray[i]=&SDL_modelist[i];
69 if (PgGetVideoModeList(&mode_list) < 0)
71 SDL_SetError("ph_ListModes(): PgGetVideoModeList() function failed !\n");
75 mode_info.bits_per_pixel = 0;
77 for (i=0; i < mode_list.num_modes; i++)
79 if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
81 SDL_SetError("ph_ListModes(): PgGetVideoModeInfo() function failed on mode: 0x%X.\n", mode_list.modes[i]);
84 if(mode_info.bits_per_pixel == format->BitsPerPixel)
86 Amodelist[j].w = mode_info.width;
87 Amodelist[j].h = mode_info.height;
94 /* reorder biggest for smallest, assume width dominates */
98 SDL_modelist[i].w = Amodelist[j - i - 1].w;
99 SDL_modelist[i].h = Amodelist[j - i - 1].h;
100 SDL_modelist[i].x = Amodelist[j - i - 1].x;
101 SDL_modelist[i].y = Amodelist[j - i - 1].y;
103 SDL_modearray[j]=NULL;
105 return SDL_modearray;
108 void ph_FreeVideoModes(_THIS)
113 /* return the mode associated with width, height and bpp */
114 /* if there is no mode then zero is returned */
115 int ph_GetVideoMode(int width, int height, int bpp)
121 if (PgGetVideoModeList(&mode_list) < 0)
126 /* special case for the double-sized 320x200 mode */
127 if ((width==640) && (height==400))
132 /* search list for exact match */
133 for (i=0; i<mode_list.num_modes; i++)
135 if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
140 if ((mode_info.width == width) && (mode_info.height == height) &&
141 (mode_info.bits_per_pixel == bpp))
143 return mode_list.modes[i];
147 if ((modestage) && (mode_info.width == width) && (mode_info.height == height+80) &&
148 (mode_info.bits_per_pixel == bpp))
151 closestmode=mode_list.modes[i];
156 /* if we are here, then no 640x400xbpp mode found and we'll emulate it via 640x480xbpp mode */
162 return (i == mode_list.num_modes) ? 0 : mode_list.modes[i];
165 /* return the mode associated with width, height and bpp */
166 /* if requested bpp is not found the mode with closest bpp is returned */
167 int get_mode_any_format(int width, int height, int bpp)
169 int i, closest, delta, min_delta;
171 if (PgGetVideoModeList(&mode_list) < 0)
176 SDL_qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_res);
178 for(i=0;i<mode_list.num_modes;i++)
180 if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
184 if ((mode_info.width == width) && (mode_info.height == height))
190 if (i<mode_list.num_modes)
192 /* get closest bpp */
194 if (mode_info.bits_per_pixel == bpp)
196 return mode_list.modes[closest];
199 min_delta = abs(mode_info.bits_per_pixel - bpp);
203 if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
208 if ((mode_info.width != width) || (mode_info.height != height))
214 if (mode_info.bits_per_pixel == bpp)
221 delta = abs(mode_info.bits_per_pixel - bpp);
222 if (delta < min_delta)
231 return mode_list.modes[closest];
237 int ph_ToggleFullScreen(_THIS, int on)
242 int ph_EnterFullScreen(_THIS, SDL_Surface* screen, int fmode)
244 PgDisplaySettings_t settings;
249 if (!currently_fullscreen)
251 /* Get the video mode and set it */
252 if (screen->flags & SDL_ANYFORMAT)
254 if ((mode = get_mode_any_format(screen->w, screen->h, screen->format->BitsPerPixel)) == 0)
256 SDL_SetError("ph_EnterFullScreen(): can't find appropriate video mode !\n");
262 if ((mode = ph_GetVideoMode(screen->w, screen->h, screen->format->BitsPerPixel)) == 0)
264 SDL_SetError("ph_EnterFullScreen(): can't find appropriate video mode !\n");
267 if (PgGetVideoModeInfo(mode, &mode_info) < 0)
269 SDL_SetError("ph_EnterFullScreen(): can't get video mode capabilities !\n");
272 if (mode_info.height != screen->h)
274 if ((mode_info.height==480) && (screen->h==400))
276 videomode_emulatemode=1;
281 videomode_emulatemode=0;
285 /* save old video mode caps */
286 PgGetVideoMode(&settings);
287 old_video_mode=settings.mode;
288 old_refresh_rate=settings.refresh;
290 /* setup new video mode */
291 settings.mode = mode;
292 settings.refresh = 0;
295 refreshrate=SDL_getenv("SDL_PHOTON_FULLSCREEN_REFRESH");
296 if (refreshrate!=NULL)
298 if (SDL_sscanf(refreshrate, "%d", &refreshratenum)==1)
300 settings.refresh = refreshratenum;
304 if (PgSetVideoMode(&settings) < 0)
306 SDL_SetError("ph_EnterFullScreen(): PgSetVideoMode() call failed !\n");
312 if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
314 #if !SDL_VIDEO_OPENGL || (_NTO_VERSION < 630)
315 return 0; /* 6.3.0 */
322 if (OCImage.direct_context==NULL)
324 OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
325 if (!OCImage.direct_context)
327 SDL_SetError("ph_EnterFullScreen(): Can't create direct context !\n");
328 ph_LeaveFullScreen(this);
332 OCImage.oldDC=PdDirectStart(OCImage.direct_context);
335 currently_fullscreen = 1;
342 int ph_LeaveFullScreen(_THIS)
344 PgDisplaySettings_t oldmode_settings;
346 if (currently_fullscreen)
348 if ((this->screen) && ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
350 #if !SDL_VIDEO_OPENGL || (_NTO_VERSION < 630)
355 /* release routines starts here */
357 if (OCImage.direct_context)
359 PdDirectStop(OCImage.direct_context);
360 PdReleaseDirectContext(OCImage.direct_context);
361 OCImage.direct_context=NULL;
365 PhDCSetCurrent(OCImage.oldDC);
369 currently_fullscreen=0;
371 /* Restore old video mode */
372 if (old_video_mode != -1)
374 oldmode_settings.mode = (unsigned short) old_video_mode;
375 oldmode_settings.refresh = (unsigned short) old_refresh_rate;
376 oldmode_settings.flags = 0;
378 if (PgSetVideoMode(&oldmode_settings) < 0)
380 SDL_SetError("Ph_LeaveFullScreen(): PgSetVideoMode() function failed !\n");