Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
357 lines (302 loc) · 10.2 KB

SDL_ph_modes.c

File metadata and controls

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