This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_compat.c
395 lines (350 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
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
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
/* This file contains functions for backwards compatibility with SDL 1.2 */
#include "SDL.h"
#include "video/SDL_sysvideo.h"
static SDL_WindowID window;
static char *wm_title;
char *
35
SDL_AudioDriverName(char *namebuf, int maxlen)
36
{
37
const char *name = SDL_GetCurrentAudioDriver();
38
if (name) {
39
SDL_strlcpy(namebuf, name, maxlen);
40
41
42
43
44
45
return namebuf;
}
return NULL;
}
char *
46
SDL_VideoDriverName(char *namebuf, int maxlen)
47
{
48
const char *name = SDL_GetCurrentVideoDriver();
49
if (name) {
50
SDL_strlcpy(namebuf, name, maxlen);
51
52
53
54
55
56
return namebuf;
}
return NULL;
}
int
57
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
58
59
60
{
int i, actual_bpp = 0;
61
if (!SDL_GetVideoDevice()) {
62
63
64
65
return 0;
}
if (!(flags & SDL_FULLSCREEN)) {
66
return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
67
68
}
69
70
for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
71
72
73
74
if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
if (!mode->format) {
return bpp;
}
75
76
if (SDL_BITSPERPIXEL(mode->format) >= bpp) {
actual_bpp = SDL_BITSPERPIXEL(mode->format);
77
78
79
80
81
82
83
}
}
}
return actual_bpp;
}
SDL_Rect **
84
SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
85
86
87
88
{
int i, nmodes;
SDL_Rect **modes;
89
if (!SDL_GetVideoDevice()) {
90
91
92
93
94
95
96
97
98
return NULL;
}
if (!(flags & SDL_FULLSCREEN)) {
return (SDL_Rect **) (-1);
}
/* Memory leak, but this is a compatibility function, who cares? */
nmodes = 0;
99
100
for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
101
102
103
if (!mode->w || !mode->h) {
return (SDL_Rect **) (-1);
}
104
if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
105
106
107
108
109
110
111
continue;
}
if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
&& modes[nmodes - 1]->h == mode->h) {
continue;
}
112
modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
113
114
115
if (!modes) {
return NULL;
}
116
modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
if (!modes[nmodes]) {
return NULL;
}
modes[nmodes]->x = 0;
modes[nmodes]->y = 0;
modes[nmodes]->w = mode->w;
modes[nmodes]->h = mode->h;
++nmodes;
}
if (modes) {
modes[nmodes] = NULL;
}
return modes;
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
188
static int (*orig_eventfilter) (const SDL_Event * event);
static int
SDL_CompatEventFilter(const SDL_Event * event)
{
SDL_Event fake;
switch (event->type) {
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED:
fake.type = SDL_VIDEORESIZE;
fake.resize.w = event->window.data1;
fake.resize.h = event->window.data2;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_MINIMIZED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_RESTORED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_ENTER:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_LEAVE:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
}
}
return orig_eventfilter(event);
}
189
SDL_Surface *
190
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
191
{
192
193
int (*filter) (const SDL_Event * event);
const SDL_DisplayMode *desktop_mode;
194
195
196
197
198
199
SDL_DisplayMode mode;
int i;
Uint32 window_flags;
Uint32 desktop_format;
Uint32 desired_format;
200
201
if (!SDL_GetVideoDevice()) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
202
203
204
205
206
return NULL;
}
}
/* Destroy existing window */
207
SDL_DestroyWindow(window);
208
209
210
211
212
213
214
215
/* Set up the event filter */
filter = SDL_GetEventFilter();
if (filter != SDL_CompatEventFilter) {
orig_eventfilter = filter;
}
SDL_SetEventFilter(SDL_CompatEventFilter);
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Create a new window */
window_flags = SDL_WINDOW_SHOWN;
if (flags & SDL_FULLSCREEN) {
window_flags |= SDL_WINDOW_FULLSCREEN;
}
if (flags & SDL_OPENGL) {
window_flags |= SDL_WINDOW_OPENGL;
}
if (flags & SDL_RESIZABLE) {
window_flags |= SDL_WINDOW_RESIZABLE;
}
if (flags & SDL_NOFRAME) {
window_flags |= SDL_WINDOW_BORDERLESS;
}
230
window = SDL_CreateWindow(wm_title, 0, 0, width, height, window_flags);
231
232
233
234
235
if (!window) {
return NULL;
}
/* Set up the desired display mode */
236
237
238
239
desktop_mode = SDL_GetDesktopDisplayMode();
desktop_format = desktop_mode->format;
if (desktop_format && ((flags & SDL_ANYFORMAT)
|| (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
240
241
242
desired_format = desktop_format;
} else {
switch (bpp) {
243
244
245
246
247
248
249
case 0:
if (desktop_format) {
desired_format = desktop_format;
} else {
desired_format = SDL_PixelFormat_RGB888;
}
break;
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
case 8:
desired_format = SDL_PixelFormat_Index8;
break;
case 15:
desired_format = SDL_PixelFormat_RGB555;
break;
case 16:
desired_format = SDL_PixelFormat_RGB565;
break;
case 24:
desired_format = SDL_PixelFormat_RGB24;
break;
case 32:
desired_format = SDL_PixelFormat_RGB888;
break;
default:
266
SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
267
268
269
270
271
272
273
274
275
276
return NULL;
}
}
mode.format = desired_format;
mode.w = width;
mode.h = height;
mode.refresh_rate = 0;
/* Set the desired display mode */
if (flags & SDL_FULLSCREEN) {
277
if (!SDL_GetClosestDisplayMode(&mode, &mode)) {
278
279
280
return NULL;
}
} else {
281
282
283
284
285
286
287
288
if (desktop_format) {
mode.format = desktop_format;
}
if (desktop_mode->w && desktop_mode->h) {
mode.w = desktop_mode->w;
mode.h = desktop_mode->h;
}
mode.refresh_rate = desktop_mode->refresh_rate;
289
}
290
if (SDL_SetDisplayMode(&mode) < 0) {
291
292
293
294
return NULL;
}
/* Create the display surface */
295
return SDL_CreateWindowSurface(window, desired_format, flags);
296
297
298
}
SDL_Surface *
299
SDL_GetVideoSurface(void)
300
{
301
SDL_VideoDevice *_this = SDL_GetVideoDevice();
302
303
304
305
306
return SDL_VideoSurface;
}
void
307
SDL_WM_SetCaption(const char *title, const char *icon)
308
309
{
if (wm_title) {
310
SDL_free(wm_title);
311
} else {
312
wm_title = SDL_strdup(title);
313
}
314
SDL_SetWindowTitle(window, wm_title);
315
316
317
}
void
318
SDL_WM_GetCaption(char **title, char **icon)
319
320
321
322
323
324
325
326
327
328
{
if (title) {
*title = wm_title;
}
if (icon) {
*icon = "";
}
}
void
329
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
330
331
332
333
334
{
/* FIXME */
}
int
335
SDL_WM_IconifyWindow(void)
336
{
337
SDL_MinimizeWindow(window);
338
339
340
}
int
341
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
342
343
344
345
346
{
return 0;
}
SDL_GrabMode
347
SDL_WM_GrabInput(SDL_GrabMode mode)
348
349
{
if (mode != SDL_GRAB_QUERY) {
350
SDL_SetWindowGrab(window, mode);
351
}
352
return (SDL_GrabMode) SDL_GetWindowGrab(window);
353
354
355
}
Uint8
356
SDL_GetAppState(void)
357
358
359
360
{
Uint8 state = 0;
Uint32 flags = 0;
361
flags = SDL_GetWindowFlags(window);
362
363
364
365
366
367
368
369
370
371
372
373
374
if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
state |= SDL_APPACTIVE;
}
if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
state |= SDL_APPINPUTFOCUS;
}
if (flags & SDL_WINDOW_MOUSE_FOCUS) {
state |= SDL_APPMOUSEFOCUS;
}
return state;
}
const SDL_version *
375
SDL_Linked_Version(void)
376
377
{
static SDL_version version;
378
SDL_VERSION(&version);
379
380
381
382
return &version;
}
int
383
384
SDL_SetPalette(SDL_Surface * surface, int flags, SDL_Color * colors,
int firstcolor, int ncolors)
385
{
386
SDL_SetColors(surface, colors, firstcolor, ncolors);
387
388
389
}
int
390
SDL_GetWMInfo(SDL_SysWMinfo * info)
391
{
392
return SDL_GetWindowWMInfo(window, info);
393
394
395
}
/* vi: set ts=4 sw=4 expandtab: */