This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_ph_modes.c
357 lines (302 loc) · 10.2 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 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
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
13
Lesser General Public License for more details.
14
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
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
27
#include "SDL_ph_modes_c.h"
static PgVideoModeInfo_t mode_info;
static PgVideoModes_t mode_list;
28
29
/* The current list of available video modes */
30
31
SDL_Rect SDL_modelist[PH_MAX_VIDEOMODES];
SDL_Rect *SDL_modearray[PH_MAX_VIDEOMODES];
32
33
34
static int
compare_modes_by_res(const void *mode1, const void *mode2)
35
{
36
37
38
PgVideoModeInfo_t mode1_info;
PgVideoModeInfo_t mode2_info;
39
if (PgGetVideoModeInfo(*(unsigned short *) mode1, &mode1_info) < 0) {
40
41
return 0;
}
42
43
if (PgGetVideoModeInfo(*(unsigned short *) mode2, &mode2_info) < 0) {
44
45
return 0;
}
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
55
SDL_Rect **
ph_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
56
{
57
58
59
int i = 0;
int j = 0;
SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
60
61
62
for (i = 0; i < PH_MAX_VIDEOMODES; i++) {
SDL_modearray[i] = &SDL_modelist[i];
63
}
64
65
66
67
68
if (PgGetVideoModeList(&mode_list) < 0) {
SDL_SetError
("ph_ListModes(): PgGetVideoModeList() function failed !\n");
return NULL;
69
}
70
71
mode_info.bits_per_pixel = 0;
72
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]);
78
79
return NULL;
}
80
if (mode_info.bits_per_pixel == format->BitsPerPixel) {
81
82
83
84
Amodelist[j].w = mode_info.width;
Amodelist[j].h = mode_info.height;
Amodelist[j].x = 0;
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
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;
96
}
97
98
SDL_modearray[j] = NULL;
99
return SDL_modearray;
100
101
}
102
103
void
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
111
int
ph_GetVideoMode(int width, int height, int bpp)
112
{
113
int i;
114
115
int modestage = 0;
int closestmode = 0;
116
117
if (PgGetVideoModeList(&mode_list) < 0) {
118
119
return -1;
}
120
121
/* special case for the double-sized 320x200 mode */
122
123
if ((width == 640) && (height == 400)) {
modestage = 1;
124
125
}
126
/* search list for exact match */
127
128
for (i = 0; i < mode_list.num_modes; i++) {
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
129
130
return 0;
}
131
132
133
if ((mode_info.width == width) && (mode_info.height == height) &&
(mode_info.bits_per_pixel == bpp)) {
134
return mode_list.modes[i];
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];
}
142
143
144
145
}
}
/* if we are here, then no 640x400xbpp mode found and we'll emulate it via 640x480xbpp mode */
146
147
if (modestage == 2) {
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
156
int
get_mode_any_format(int width, int height, int bpp)
157
158
159
{
int i, closest, delta, min_delta;
160
if (PgGetVideoModeList(&mode_list) < 0) {
161
162
163
return -1;
}
164
165
SDL_qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short),
compare_modes_by_res);
166
167
168
for (i = 0; i < mode_list.num_modes; i++) {
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
169
170
return 0;
}
171
172
if ((mode_info.width == width) && (mode_info.height == height)) {
break;
173
174
175
}
}
176
if (i < mode_list.num_modes) {
177
178
/* get closest bpp */
closest = i++;
179
if (mode_info.bits_per_pixel == bpp) {
180
181
182
183
184
return mode_list.modes[closest];
}
min_delta = abs(mode_info.bits_per_pixel - bpp);
185
186
while (1) {
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) {
187
188
189
return 0;
}
190
191
if ((mode_info.width != width)
|| (mode_info.height != height)) {
192
break;
193
194
} else {
if (mode_info.bits_per_pixel == bpp) {
195
196
closest = i;
break;
197
} else {
198
delta = abs(mode_info.bits_per_pixel - bpp);
199
if (delta < min_delta) {
200
201
202
203
204
205
206
207
208
209
closest = i;
min_delta = delta;
}
i++;
}
}
}
return mode_list.modes[closest];
}
210
return 0;
211
212
}
213
214
int
ph_ToggleFullScreen(_THIS, int on)
215
{
216
return -1;
217
218
}
219
220
int
ph_EnterFullScreen(_THIS, SDL_Surface * screen, int fmode)
221
{
222
223
PgDisplaySettings_t settings;
int mode;
224
char *refreshrate;
225
int refreshratenum;
226
227
if (!currently_fullscreen) {
228
/* Get the video mode and set it */
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");
235
236
return 0;
}
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");
243
244
return 0;
}
245
246
247
if (PgGetVideoModeInfo(mode, &mode_info) < 0) {
SDL_SetError
("ph_EnterFullScreen(): can't get video mode capabilities !\n");
248
249
return 0;
}
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;
256
}
257
258
259
260
}
/* save old video mode caps */
PgGetVideoMode(&settings);
261
262
old_video_mode = settings.mode;
old_refresh_rate = settings.refresh;
263
264
265
266
267
268
/* setup new video mode */
settings.mode = mode;
settings.refresh = 0;
settings.flags = 0;
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;
}
274
275
}
276
277
278
if (PgSetVideoMode(&settings) < 0) {
SDL_SetError
("ph_EnterFullScreen(): PgSetVideoMode() call failed !\n");
279
280
281
return 0;
}
282
283
if (this->screen) {
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
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");
297
298
299
ph_LeaveFullScreen(this);
return 0;
}
300
}
301
OCImage.oldDC = PdDirectStart(OCImage.direct_context);
302
}
303
304
currently_fullscreen = 1;
305
}
306
PgFlush();
307
308
return 1;
309
310
}
311
312
int
ph_LeaveFullScreen(_THIS)
313
{
314
PgDisplaySettings_t oldmode_settings;
315
316
317
if (currently_fullscreen) {
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
327
PdDirectStop(OCImage.direct_context);
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
340
341
oldmode_settings.mode = (unsigned short) old_video_mode;
oldmode_settings.refresh = (unsigned short) old_refresh_rate;
oldmode_settings.flags = 0;
342
343
344
345
if (PgSetVideoMode(&oldmode_settings) < 0) {
SDL_SetError
("Ph_LeaveFullScreen(): PgSetVideoMode() function failed !\n");
346
return 0;
347
348
349
}
}
350
351
old_video_mode = -1;
old_refresh_rate = -1;
352
353
354
}
}
return 1;
355
}
356
357
/* vi: set ts=4 sw=4 expandtab: */