/
SDL_ph_modes.c
314 lines (266 loc) · 7.62 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
26
27
28
29
30
31
32
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include "SDL_ph_modes_c.h"
static unsigned long key1, key2;
static PgVideoModeInfo_t mode_info;
static PgVideoModes_t mode_list;
33
34
/* The current list of available video modes */
35
36
SDL_Rect SDL_modelist[PH_MAX_VIDEOMODES];
SDL_Rect* SDL_modearray[PH_MAX_VIDEOMODES];
37
38
39
static int compare_modes_by_res(const void* mode1, const void* mode2)
{
40
41
42
43
44
45
if (PgGetVideoModeInfo(*(unsigned short*)mode1, &mode_info) < 0)
{
fprintf(stderr,"error: In compare_modes_by_res PgGetVideoModeInfo failed on mode: 0x%x\n",
*(unsigned short*)mode1);
return 0;
}
46
47
key1 = mode_info.width * mode_info.height;
48
49
50
51
52
53
54
if (PgGetVideoModeInfo(*(unsigned short*)mode2, &mode_info) < 0)
{
fprintf(stderr,"error: In compare_modes_by_res PgGetVideoModeInfo failed on mode: 0x%x\n",
*(unsigned short*)mode2);
return 0;
}
55
56
57
58
59
60
61
62
63
key2 = mode_info.width * mode_info.height;
if (key1 > key2)
return 1;
else if (key1 == key2)
return 0;
else
return -1;
64
65
}
66
SDL_Rect **ph_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
67
{
68
69
70
int i = 0;
int j = 0;
SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
71
72
73
74
75
for (i=0; i<PH_MAX_VIDEOMODES; i++)
{
SDL_modearray[i]=&SDL_modelist[i];
}
76
77
78
79
80
81
if (PgGetVideoModeList( &mode_list ) < 0)
{
fprintf(stderr,"error: PgGetVideoModeList failed\n");
return NULL;
}
82
83
mode_info.bits_per_pixel = 0;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
for (i=0; i < mode_list.num_modes; i++)
{
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
{
fprintf(stderr,"error: PgGetVideoModeInfo failed on mode: 0x%x\n", mode_list.modes[i]);
return NULL;
}
if(mode_info.bits_per_pixel == format->BitsPerPixel)
{
Amodelist[j].w = mode_info.width;
Amodelist[j].h = mode_info.height;
Amodelist[j].x = 0;
Amodelist[j].y = 0;
j++;
}
}
101
102
/* reorder biggest for smallest, assume width dominates */
103
104
105
106
107
108
109
110
111
for(i=0; i<j; i++)
{
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;
}
SDL_modearray[j]=NULL;
112
113
return SDL_modearray;
114
115
116
117
}
void ph_FreeVideoModes(_THIS)
{
118
return;
119
120
121
}
/* return the mode associated with width, height and bpp */
122
123
/* if there is no mode then zero is returned */
int get_mode(int width, int height, int bpp)
124
{
125
int i;
126
127
128
129
130
131
132
133
134
if(width<640)
{
width=640;
}
if(height<480)
{
height=480;
}
135
136
137
138
139
140
if (PgGetVideoModeList(&mode_list) < 0)
{
fprintf(stderr,"error: PgGetVideoModeList failed\n");
return -1;
}
141
142
143
144
145
146
147
148
149
/* search list for exact match */
for (i=0;i<mode_list.num_modes;i++)
{
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
{
fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
return 0;
}
150
151
152
153
154
155
156
157
if ((mode_info.width == width) &&
(mode_info.height == height) &&
(mode_info.bits_per_pixel == bpp))
{
return mode_list.modes[i];
}
}
158
159
return (i == mode_list.num_modes) ? 0 : mode_list.modes[i];
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
}
int get_mode_any_format(int width, int height, int bpp)
/* return the mode associated with width, height and bpp */
/* if requested bpp is not found the mode with closest bpp is returned */
{
int i, closest, delta, min_delta;
if (PgGetVideoModeList( &mode_list ) < 0)
{
fprintf(stderr,"error: PgGetVideoModeList failed\n");
return -1;
}
qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_res);
for(i=0;i<mode_list.num_modes;i++)
{
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
{
fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
return 0;
}
if ((mode_info.width == width) &&
(mode_info.height == height))
break;
}
if (i<mode_list.num_modes)
{
188
/* get closest bpp */
189
190
closest = i++;
if (mode_info.bits_per_pixel == bpp)
191
return mode_list.modes[closest];
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
min_delta = abs(mode_info.bits_per_pixel - bpp);
while(1)
{
if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
{
fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
return 0;
}
if ((mode_info.width != width) ||
(mode_info.height != height))
break;
else if (mode_info.bits_per_pixel == bpp)
{
closest = i;
break;
}
else
{
delta = abs(mode_info.bits_per_pixel - bpp);
if (delta < min_delta)
{
closest = i;
min_delta = delta;
}
i++;
}
}
221
return mode_list.modes[closest];
222
223
}
else
224
return 0;
225
226
227
228
}
int ph_ToggleFullScreen(_THIS, int on)
{
229
230
231
232
233
234
235
236
if (currently_fullscreen)
{
return ph_LeaveFullScreen(this);
}
else
{
return ph_EnterFullScreen(this);
}
237
238
return 0;
239
240
241
242
}
int ph_EnterFullScreen(_THIS)
{
243
244
if (!currently_fullscreen)
{
245
if (this->screen)
246
{
247
248
if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
{
249
250
#ifdef HAVE_OPENGL
#endif /* HAVE_OPENGL */
251
252
return 0;
}
253
}
254
255
if (OCImage.direct_context==NULL)
256
{
257
258
OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
}
259
260
261
if (!OCImage.direct_context)
{
262
263
fprintf(stderr, "ph_EnterFullScreen(): Can't create direct context !\n");
return 0;
264
}
265
266
OCImage.oldDC=PdDirectStart(OCImage.direct_context);
267
268
currently_fullscreen = 1;
269
}
270
271
return 1;
272
273
}
274
int ph_LeaveFullScreen(_THIS)
275
{
276
PgDisplaySettings_t mymode_settings;
277
278
279
280
281
282
283
284
285
286
287
288
289
if (currently_fullscreen)
{
if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
{
#ifdef HAVE_OPENGL
#endif /* HAVE_OPENGL */
return 0;
}
else
{
PdDirectStop(OCImage.direct_context);
PdReleaseDirectContext(OCImage.direct_context);
290
291
PhDCSetCurrent(OCImage.oldDC);
292
currently_fullscreen=0;
293
294
295
296
297
298
/* Restore old video mode */
if (old_video_mode != -1)
{
mymode_settings.mode= (unsigned short) old_video_mode;
mymode_settings.refresh= (unsigned short) old_refresh_rate;
299
300
mymode_settings.flags= 0;
301
302
if (PgSetVideoMode(&mymode_settings) < 0)
{
303
304
fprintf(stderr, "Ph_LeaveFullScreen(): PgSetVideoMode failed !\n");
return 0;
305
306
307
308
}
}
old_video_mode=-1;
309
old_refresh_rate=-1;
310
311
312
313
}
}
return 1;
314
}