Skip to content

Latest commit

 

History

History
3073 lines (2677 loc) · 82.7 KB

SDL12_compat.c

File metadata and controls

3073 lines (2677 loc) · 82.7 KB
 
Mar 4, 2013
Mar 4, 2013
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* This file contains functions for backwards compatibility with SDL 1.2 */
#include "SDL.h"
#if !SDL_VERSION_ATLEAST(2,0,0)
#error You need to compile against SDL 2.0 headers.
#endif
#include "SDL_syswm.h"
Mar 7, 2013
Mar 7, 2013
32
33
#include <stdarg.h>
Mar 4, 2013
Mar 4, 2013
34
35
36
37
//#include "video/SDL_sysvideo.h"
//#include "video/SDL_pixels_c.h"
//#include "render/SDL_yuv_sw_c.h"
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// !!! IMPLEMENT_ME SDL_ConvertSurface
// !!! IMPLEMENT_ME SDL_CreateThread
// !!! IMPLEMENT_ME SDL_EventState
// !!! IMPLEMENT_ME SDL_FillRect
// !!! IMPLEMENT_ME SDL_GL_GetAttribute
// !!! IMPLEMENT_ME SDL_GL_Lock
// !!! IMPLEMENT_ME SDL_GL_SetAttribute
// !!! IMPLEMENT_ME SDL_GL_Unlock
// !!! IMPLEMENT_ME SDL_GL_UpdateRects
// !!! IMPLEMENT_ME SDL_GetKeyName
// !!! IMPLEMENT_ME SDL_GetKeyState
// !!! IMPLEMENT_ME SDL_GetModState
// !!! IMPLEMENT_ME SDL_GetMouseState
// !!! IMPLEMENT_ME SDL_GetRelativeMouseState
// !!! IMPLEMENT_ME SDL_LockSurface
// !!! IMPLEMENT_ME SDL_LowerBlit
// !!! IMPLEMENT_ME SDL_SetColorKey
// !!! IMPLEMENT_ME SDL_SetModState
// !!! IMPLEMENT_ME SDL_SoftStretch
// !!! IMPLEMENT_ME SDL_UnlockSurface
// !!! IMPLEMENT_ME SDL_UpperBlit
// !!! IMPLEMENT_ME SDL_WaitEvent
// !!! IMPLEMENT_ME X11_KeyToUnicode
Mar 4, 2013
Mar 4, 2013
61
Mar 6, 2013
Mar 6, 2013
62
Mar 6, 2013
Mar 6, 2013
63
64
65
#define SDL20_SYM(rc,fn,params,args,ret) \
typedef rc (*SDL20_##fn##_t) params; \
static SDL20_##fn##_t SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
66
67
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
68
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
69
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 2013
70
71
#undef SDL20_SYM
Mar 7, 2013
Mar 7, 2013
72
73
typedef void (*SDL20_SetError_t)(const char *fmt, ...);
static SDL20_SetError_t SDL20_SetError = NULL;
Mar 6, 2013
Mar 6, 2013
74
75
76
77
78
79
80
81
82
83
84
85
#define SDL12_INIT_TIMER 0x00000001
#define SDL12_INIT_AUDIO 0x00000010
#define SDL12_INIT_VIDEO 0x00000020
#define SDL12_INIT_CDROM 0x00000100
#define SDL12_INIT_JOYSTICK 0x00000200
#define SDL12_INIT_NOPARACHUTE 0x00100000
#define SDL12_INIT_EVENTTHREAD 0x01000000
#define SDL12_INIT_EVERYTHING 0x0000FFFF
Mar 8, 2013
Mar 8, 2013
86
typedef struct SDL12_Palette
Mar 6, 2013
Mar 6, 2013
87
{
Mar 8, 2013
Mar 8, 2013
88
89
int ncolors;
SDL_Color *colors;
Mar 6, 2013
Mar 6, 2013
90
91
} SDL12_Palette;
Mar 8, 2013
Mar 8, 2013
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
typedef struct SDL12_PixelFormat
{
SDL12_Palette *palette;
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
Uint8 Rloss;
Uint8 Gloss;
Uint8 Bloss;
Uint8 Aloss;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
Uint8 Ashift;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Uint32 colorkey;
Uint8 alpha;
Mar 6, 2013
Mar 6, 2013
111
112
} SDL12_PixelFormat;
Apr 1, 2013
Apr 1, 2013
113
114
115
116
117
118
119
120
121
typedef struct SDL12_Surface
{
Uint32 flags;
SDL12_PixelFormat *format;
int w;
int h;
Uint16 pitch;
void *pixels;
int offset;
Apr 11, 2013
Apr 11, 2013
122
SDL_Surface *hwdata; /* the real SDL 1.2 has an opaque pointer to a platform-specific thing here. */
Apr 1, 2013
Apr 1, 2013
123
124
125
126
127
128
129
130
SDL_Rect clip_rect;
Uint32 unused1;
Uint32 locked;
void *blitmap;
unsigned int format_version;
int refcount;
} SDL12_Surface;
Mar 8, 2013
Mar 8, 2013
131
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
typedef struct
{
Uint32 hw_available :1;
Uint32 wm_available :1;
Uint32 UnusedBits1 :6;
Uint32 UnusedBits2 :1;
Uint32 blit_hw :1;
Uint32 blit_hw_CC :1;
Uint32 blit_hw_A :1;
Uint32 blit_sw :1;
Uint32 blit_sw_CC :1;
Uint32 blit_sw_A :1;
Uint32 blit_fill :1;
Uint32 UnusedBits3 :16;
Uint32 video_mem;
SDL_PixelFormat *vfmt;
int current_w;
int current_h;
} SDL12_VideoInfo;
#define SDL12_HWSURFACE 0x00000001
#define SDL12_ASYNCBLIT 0x00000004
#define SDL12_ANYFORMAT 0x10000000
#define SDL12_HWPALETTE 0x20000000
#define SDL12_DOUBLEBUF 0x40000000
#define SDL12_FULLSCREEN 0x80000000
#define SDL12_OPENGL 0x00000002
#define SDL12_OPENGLBLIT 0x0000000A
#define SDL12_RESIZABLE 0x00000010
#define SDL12_NOFRAME 0x00000020
#define SDL12_HWACCEL 0x00000100
#define SDL12_SRCCOLORKEY 0x00001000
#define SDL12_RLEACCELOK 0x00002000
#define SDL12_RLEACCEL 0x00004000
#define SDL12_SRCALPHA 0x00010000
#define SDL12_PREALLOC 0x01000000
Apr 1, 2013
Apr 1, 2013
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
typedef int (SDLCALL *SDL12_EventFilter)(const SDL12_Event *event12);
static int EventFilter20to12(void *data, SDL_Event *event20);
typedef enum
{
SDL12_NOEVENT = 0,
SDL12_ACTIVEEVENT,
SDL12_KEYDOWN,
SDL12_KEYUP,
SDL12_MOUSEMOTION,
SDL12_MOUSEBUTTONDOWN,
SDL12_MOUSEBUTTONUP,
SDL12_JOYAXISMOTION,
SDL12_JOYBALLMOTION,
SDL12_JOYHATMOTION,
SDL12_JOYBUTTONDOWN,
SDL12_JOYBUTTONUP,
SDL12_QUIT,
SDL12_SYSWMEVENT,
SDL12_EVENT_RESERVEDA,
SDL12_EVENT_RESERVEDB,
SDL12_VIDEORESIZE,
SDL12_VIDEOEXPOSE,
SDL12_USEREVENT = 24,
SDL12_NUMEVENTS = 32
} SDL12_EventType;
typedef struct
{
Uint8 type;
Uint8 gain;
Uint8 state;
} SDL12_ActiveEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 state;
SDL12_keysym keysym;
} SDL12_KeyboardEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 state;
Uint16 x, y;
Sint16 xrel;
Sint16 yrel;
} SDL12_MouseMotionEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 button;
Uint8 state;
Uint16 x, y;
} SDL12_MouseButtonEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 axis;
Sint16 value;
} SDL12_JoyAxisEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 ball;
Sint16 xrel;
Sint16 yrel;
} SDL12_JoyBallEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 hat;
Uint8 value;
} SDL12_JoyHatEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 button;
Uint8 state;
} SDL12_JoyButtonEvent;
typedef struct
{
Uint8 type;
int w;
int h;
} SDL12_ResizeEvent;
typedef struct
{
Uint8 type;
} SDL12_ExposeEvent;
typedef struct
{
Uint8 type;
} SDL12_QuitEvent;
typedef struct
{
Uint8 type;
int code;
void *data1;
void *data2;
} SDL12_UserEvent;
typedef struct
{
Uint8 type;
void *msg;
} SDL12_SysWMEvent;
typedef union
{
Uint8 type;
SDL12_ActiveEvent active;
SDL12_KeyboardEvent key;
SDL12_MouseMotionEvent motion;
SDL12_MouseButtonEvent button;
SDL12_JoyAxisEvent jaxis;
SDL12_JoyBallEvent jball;
SDL12_JoyHatEvent jhat;
SDL12_JoyButtonEvent jbutton;
SDL12_ResizeEvent resize;
SDL12_ExposeEvent expose;
SDL12_QuitEvent quit;
SDL12_UserEvent user;
SDL12_SysWMEvent syswm;
} SDL12_Event;
Mar 8, 2013
Mar 8, 2013
311
Apr 11, 2013
Apr 11, 2013
312
313
314
315
316
317
318
319
320
321
322
323
typedef struct
`{
SDL_Rect area;
Sint16 hot_x;
Sint16 hot_y;
Uint8 *data;
Uint8 *mask;
Uint8 *save[2];
SDL_Cursor *wm_cursor; /* the real SDL 1.2 has an opaque pointer to a platform-specific cursor here. */
} SDL12_Cursor;
Mar 8, 2013
Mar 8, 2013
324
static SDL12_VideoInfo VideoInfo;
Apr 1, 2013
Apr 1, 2013
325
326
327
328
329
static SDL_Window *VideoWindow20 = NULL;
static SDL12_Surface *WindowSurface = NULL;
static SDL12_Surface *VideoSurface = NULL;
static SDL12_Surface *ShadowSurface = NULL;
static SDL12_Surface *PublicSurface = NULL;
Mar 8, 2013
Mar 8, 2013
330
331
332
333
334
335
336
337
338
static SDL_GLContext *VideoContext = NULL;
static Uint32 VideoFlags = 0;
static SDL_Rect VideoViewport;
static char *WindowTitle = NULL;
static char *WindowIconTitle = NULL;
static SDL_Surface *VideoIcon;
static int EnabledUnicode = 0;
static int VideoDisplayIndex = 0;
static int CDRomInit = 0;
Apr 1, 2013
Apr 1, 2013
339
static SDL12_EventFilter EventFilter12 = NULL;
Apr 11, 2013
Apr 11, 2013
340
static SDL12_Cursor *CurrentCursor = NULL;
Apr 1, 2013
Apr 1, 2013
341
342
343
344
// !!! FIXME: need a mutex for the event queue.
#define SDL12_MAXEVENTS 128
Apr 10, 2013
Apr 10, 2013
345
346
347
348
349
350
351
352
353
354
typedef struct EventQueueType
{
SDL12_Event event12;
struct EventQueueType *next;
} EventQueueType;
static EventQueueType EventQueuePool[SDL12_MAXEVENTS];
static EventQueueType *EventQueueHead = NULL;
static EventQueueType *EventQueueTail = NULL;
static EventQueueType *EventQueueAvailable = NULL;
Mar 6, 2013
Mar 6, 2013
355
356
357
/* Obviously we can't use SDL_LoadObject() to load SDL2. :) */
#if defined(_WINDOWS)
Apr 1, 2013
Apr 1, 2013
358
359
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
Mar 6, 2013
Mar 6, 2013
360
361
362
363
364
#define SDL20_LIBNAME "SDL2.dll"
static HANDLE Loaded_SDL20 = NULL;
#define LoadSDL20Library() ((Loaded_SDL20 = LoadLibraryA(SDL20_LIBNAME)) != NULL)
#define LookupSDL20Sym(sym) GetProcAddress(Loaded_SDL20, sym)
#define CloseSDL20Library() { { if (Loaded_SDL20) { FreeLibrary(Loaded_SDL20); Loaded_SDL20 = NULL; } }
Apr 1, 2013
Apr 1, 2013
365
366
#elif defined(unix) || defined(__APPLE__)
#include <dlfcn.h>
Mar 6, 2013
Mar 6, 2013
367
368
369
370
371
372
373
374
375
376
377
378
#ifdef __APPLE__
#define SDL20_LIBNAME "libSDL2.dylib"
#else
#define SDL20_LIBNAME "libSDL2-2.0.so.0"
#endif
static void *Loaded_SDL20 = NULL;
#define LoadSDL20Library() ((Loaded_SDL20 = dlopen(SDL20_LIBNAME, RTLD_LOCAL)) != NULL)
#define LookupSDL20Sym(sym) dlsym(Loaded_SDL20, sym)
#define CloseSDL20Library() { if (Loaded_SDL20) { dlclose(Loaded_SDL20); Loaded_SDL20 = NULL; } }
#else
#error Please define your platform.
#endif
Mar 4, 2013
Mar 4, 2013
379
Mar 6, 2013
Mar 6, 2013
380
381
static void *
LoadSDL20Symbol(const char *fn, int *okay)
Mar 4, 2013
Mar 4, 2013
382
{
Mar 6, 2013
Mar 6, 2013
383
384
385
386
387
388
389
390
void *retval = NULL;
if (*okay) /* only bother trying if we haven't previously failed. */
{
retval = LookupSDL20Sym(fn);
if (retval == NULL)
*okay = 0;
}
return retval;
Mar 4, 2013
Mar 4, 2013
391
392
}
Mar 6, 2013
Mar 6, 2013
393
394
static void
UnloadSDL20(void)
Mar 4, 2013
Mar 4, 2013
395
{
Mar 6, 2013
Mar 6, 2013
396
#define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
397
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
398
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
399
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 2013
400
#undef SDL20_SYM
Mar 7, 2013
Mar 7, 2013
401
SDL20_SetError = NULL;
Mar 6, 2013
Mar 6, 2013
402
403
404
405
406
407
408
409
410
411
412
CloseSDL20Library();
}
static int
LoadSDL20(void)
{
int okay = 1;
if (!Loaded_SDL20)
{
okay = LoadSDL20Library();
#define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = (SDL20_##fn##_t) LoadSDL20Symbol("SDL_" #fn, &okay);
Mar 6, 2013
Mar 6, 2013
413
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
414
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
415
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 2013
416
#undef SDL20_SYM
Mar 7, 2013
Mar 7, 2013
417
SDL20_SetError = (SDL20_SetError_t) LoadSDL20Symbol("SDL_SetError", &okay);
Mar 6, 2013
Mar 6, 2013
418
419
if (!okay)
UnloadSDL20();
Mar 4, 2013
Mar 4, 2013
420
}
Mar 6, 2013
Mar 6, 2013
421
return okay;
Mar 4, 2013
Mar 4, 2013
422
423
}
Mar 6, 2013
Mar 6, 2013
424
Mar 4, 2013
Mar 4, 2013
425
426
427
static int
GetVideoDisplay()
{
Mar 6, 2013
Mar 6, 2013
428
// !!! FIXME: cache this value during SDL_Init() so it doesn't change.
Mar 4, 2013
Mar 4, 2013
429
430
431
432
433
434
435
436
437
438
439
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
if ( !variable ) {
variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
}
if ( variable ) {
return SDL_atoi(variable);
} else {
return 0;
}
}
Mar 7, 2013
Mar 7, 2013
440
441
static int
DoSDLInit(const int justsubs, Uint32 sdl12flags)
Mar 6, 2013
Mar 6, 2013
442
443
444
445
446
447
448
{
Uint32 sdl20flags = 0;
int rc;
if (!LoadSDL20())
return -1;
Apr 1, 2013
Apr 1, 2013
449
#define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
Mar 6, 2013
Mar 6, 2013
450
451
452
453
454
455
456
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
#undef SETFLAG
Mar 7, 2013
Mar 7, 2013
457
458
// There's no CDROM in 2.0, but we'll just pretend it succeeded.
if (sdl12flags & SDL12_INIT_CDROM)
Mar 8, 2013
Mar 8, 2013
459
CDRomInit = 1;
Mar 7, 2013
Mar 7, 2013
460
Mar 6, 2013
Mar 6, 2013
461
462
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
Mar 7, 2013
Mar 7, 2013
463
rc = justsubs ? SDL20_InitSubSystem(sdl20flags) : SDL20_Init(sdl20flags);
Mar 6, 2013
Mar 6, 2013
464
if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO))
Apr 1, 2013
Apr 1, 2013
465
{
Apr 10, 2013
Apr 10, 2013
466
467
468
469
470
471
472
int i;
for (i = 0; i < SDL12_MAXEVENTS-1; i++)
EventQueuePool[i].next = &EventQueuePool[i+1];
EventQueuePool[SDL12_MAXEVENTS-1].next = NULL;
EventQueueHead = EventQueueTail = NULL;
EventQueueAvailable = EventQueuePool;
Apr 1, 2013
Apr 1, 2013
473
SDL20_SetEventFilter(EventFilter20to12, NULL);
Mar 8, 2013
Mar 8, 2013
474
VideoDisplayIndex = GetVideoDisplay();
Apr 1, 2013
Apr 1, 2013
475
476
}
Mar 6, 2013
Mar 6, 2013
477
478
479
return rc;
}
Mar 7, 2013
Mar 7, 2013
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
int
SDL_InitSubSystem(Uint32 sdl12flags)
{
return DoSDLInit(1, sdl12flags);
}
int
SDL_Init(Uint32 sdl12flags)
{
return DoSDLInit(0, sdl12flags);
}
Uint32
SDL_WasInit(Uint32 sdl12flags)
{
// !!! FIXME: this is cut and pasted several places.
Uint32 sdl20flags = 0;
Uint32 extraflags = 0;
Apr 1, 2013
Apr 1, 2013
499
#define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
Mar 7, 2013
Mar 7, 2013
500
501
502
503
504
505
506
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
#undef SETFLAG
Mar 8, 2013
Mar 8, 2013
507
if ((sdl12flags & SDL12_INIT_CDROM) && (CDRomInit))
Mar 7, 2013
Mar 7, 2013
508
509
510
511
512
513
514
515
516
517
518
519
520
extraflags |= SDL12_INIT_CDROM;
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
// !!! FIXME: convert back to 1.2
return SDL20_WasInit(sdl20flags) | extraflags;
}
void
SDL_QuitSubSystem(Uint32 sdl12flags)
{
Uint32 sdl20flags = 0;
Apr 1, 2013
Apr 1, 2013
521
#define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
Mar 7, 2013
Mar 7, 2013
522
523
524
525
526
527
528
529
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
// There's no CDROM in 2.0, but we'll just pretend it succeeded.
#undef SETFLAG
Mar 8, 2013
Mar 8, 2013
530
531
532
if (sdl12flags & SDL12_INIT_CDROM)
CDRomInit = 0;
Mar 8, 2013
Mar 8, 2013
533
534
// !!! FIXME: reset a bunch of other global variables too.
if (sdl12flags & SDL12_INIT_VIDEO) {
Apr 1, 2013
Apr 1, 2013
535
EventFilter12 = NULL;
Apr 10, 2013
Apr 10, 2013
536
EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
Apr 11, 2013
Apr 11, 2013
537
CurrentCursor = NULL;
Mar 8, 2013
Mar 8, 2013
538
539
540
541
SDL20_FreeFormat(VideoInfo.vfmt);
SDL_zero(VideoInfo);
}
Mar 7, 2013
Mar 7, 2013
542
543
544
545
546
547
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
SDL20_QuitSubSystem(sdl20flags);
// !!! FIXME: UnloadSDL20() ?
}
Mar 6, 2013
Mar 6, 2013
548
549
550
void
SDL_Quit(void)
{
Mar 8, 2013
Mar 8, 2013
551
// !!! FIXME: reset a bunch of other global variables too.
Apr 1, 2013
Apr 1, 2013
552
EventFilter12 = NULL;
Apr 10, 2013
Apr 10, 2013
553
EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
Apr 11, 2013
Apr 11, 2013
554
CurrentCursor = NULL;
Mar 8, 2013
Mar 8, 2013
555
556
SDL20_FreeFormat(VideoInfo.vfmt);
SDL_zero(VideoInfo);
Mar 8, 2013
Mar 8, 2013
557
CDRomInit = 0;
Mar 6, 2013
Mar 6, 2013
558
559
560
561
SDL20_Quit();
UnloadSDL20();
}
Mar 7, 2013
Mar 7, 2013
562
563
564
void
SDL_SetError(const char *fmt, ...)
{
Mar 7, 2013
Mar 7, 2013
565
566
567
568
569
570
571
572
573
574
575
576
char *str = NULL;
va_list ap;
va_start(ap, fmt);
vasprintf(&str, fmt, ap);
va_end(ap);
if (!str)
SDL20_OutOfMemory();
else
{
SDL20_SetError("%s", str);
free(str);
}
Mar 7, 2013
Mar 7, 2013
577
}
Mar 6, 2013
Mar 6, 2013
578
Apr 1, 2013
Apr 1, 2013
579
const char *
Mar 6, 2013
Mar 6, 2013
580
581
582
583
SDL_GetError(void)
{
if (!Loaded_SDL20)
{
Apr 1, 2013
Apr 1, 2013
584
static const char noload_errstr[] = "Failed to load SDL 2.0 shared library";
Mar 6, 2013
Mar 6, 2013
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
return noload_errstr;
}
return SDL20_GetError();
}
static const char *
GetDriverName(const char *name, char *namebuf, int maxlen)
{
if (name) {
if (namebuf) {
SDL20_strlcpy(namebuf, name, maxlen);
return namebuf;
} else {
return name;
}
}
return NULL;
}
const char *
SDL_AudioDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
}
const char *
SDL_VideoDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
}
Apr 1, 2013
Apr 1, 2013
617
618
static int
EventFilter20to12(void *data, SDL_Event *event20)
Mar 8, 2013
Mar 8, 2013
619
{
Apr 1, 2013
Apr 1, 2013
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
const int maxUserEvents12 = SDL12_NUMEVENTS - SDL12_USEREVENT;
SDL12_Event event12;
SDL_assert(data == NULL); /* currently unused. */
SDL_zero(event12);
switch (event20->type)
{
case SDL_QUIT:
event12->type = SDL12_QUIT;
break;
case SDL_WINDOWEVENT:
switch (event20->window.event)
{
case SDL_WINDOWEVENT_CLOSE:
event12->type = SDL12_QUIT;
break;
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED:
event12->type = SDL12_VIDEOEXPOSE;
break;
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED: // !!! FIXME: what's the difference between RESIZED and SIZE_CHANGED?
event12->type = SDL12_VIDEORESIZE;
event12->resize.w = event20->window.data1;
event12->resize.h = event20->window.data2;
break;
case SDL_WINDOWEVENT_MINIMIZED:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 0;
event12->active.state = SDL_APPACTIVE;
break;
case SDL_WINDOWEVENT_RESTORED:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 1;
event12->active.state = SDL_APPACTIVE;
break;
case SDL_WINDOWEVENT_ENTER:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 1;
event12->active.state = SDL_APPMOUSEFOCUS;
break;
case SDL_WINDOWEVENT_LEAVE:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 0;
event12->active.state = SDL_APPMOUSEFOCUS;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 1;
event12->active.state = SDL_APPINPUTFOCUS;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
event12->type = SDL_ACTIVEEVENT;
event12->active.gain = 0;
event12->active.state = SDL_APPINPUTFOCUS;
break;
}
break;
// !!! FIXME: this is sort of a mess to convert.
//case SDL_SYSWMEVENT:
// !!! FIXME: write me
case SDL_KEYDOWN:
case SDL_KEYUP:
return 0;
// !!! FIXME: write me
case SDL_TEXTEDITING:
case SDL_TEXTINPUT:
return 0;
case SDL_MOUSEMOTION:
event12->type = SDL12_MOUSEMOTION;
event12->motion.which = (Uint8) event20->motion.which;
event12->motion.state = event20->motion.state;
event12->motion.x = (Uint16) event20->motion.x;
event12->motion.y = (Uint16) event20->motion.y;
event12->motion.xrel = (Sint16) event20->motion.xrel;
event12->motion.yrel = (Sint16) event20->motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
event12->type = SDL12_MOUSEBUTTONDOWN;
event12->button.which = (Uint8) event20->button.which;
event12->button.button = event20->button.button;
event12->button.state = event20->button.state;
event12->button.x = (Uint16) event20->button.x;
event12->button.y = (Uint16) event20->button.y;
break;
case SDL_MOUSEBUTTONUP:
event12->type = SDL12_MOUSEBUTTONUP;
event12->button.which = (Uint8) event20->button.which;
event12->button.button = event20->button.button;
event12->button.state = event20->button.state;
event12->button.x = (Uint16) event20->button.x;
event12->button.y = (Uint16) event20->button.y;
break;
case SDL_MOUSEWHEEL:
return 0; // !!! FIXME
//event12->type = SDL12_MOUSEBUTTONDOWN;
//check app filter, push event
//event12->type = SDL12_MOUSEBUTTONUP;
break;
case SDL_JOYAXISMOTION:
event12->type = SDL12_JOYAXISMOTION;
event12->jaxis.which = (Uint8) event20->jaxis.which;
event12->jaxis.axis = event20->jaxis.axis;
event12->jaxis.value = event20->jaxis.value;
break;
case SDL_JOYBALLMOTION:
event12->type = SDL12_JOYBALLMOTION;
event12->jball.which = (Uint8) event20->jball.which;
event12->jball.ball = event20->jball.ball;
event12->jball.xrel = event20->jball.xrel;
event12->jball.yrel = event20->jball.yrel;
break;
case SDL_JOYHATMOTION:
event12->type = SDL12_JOYHATMOTION;
event12->jhat.which = (Uint8) event20->jhat.which;
event12->jhat.hat = event20->jhat.hat;
event12->jhat.value = event20->jhat.xrel;
break;
case SDL_JOYBUTTONDOWN:
event12->type = SDL12_JOYBUTTONDOWN;
event12->jbutton.which = (Uint8) event20->jbutton.which;
event12->jbutton.button = event20->jbutton.button;
event12->jbutton.state = event20->jbutton.state;
break;
case SDL_JOYBUTTONUP:
event12->type = SDL12_JOYBUTTONUP;
event12->jbutton.which = (Uint8) event20->jbutton.which;
event12->jbutton.button = event20->jbutton.button;
event12->jbutton.state = event20->jbutton.state;
break;
//case SDL_JOYDEVICEADDED:
//case SDL_JOYDEVICEREMOVED:
//case SDL_CONTROLLERAXISMOTION:
//case SDL_CONTROLLERBUTTONDOWN:
//case SDL_CONTROLLERBUTTONUP:
//case SDL_CONTROLLERDEVICEADDED:
//case SDL_CONTROLLERDEVICEREMOVED:
//case SDL_CONTROLLERDEVICEREMAPPED:
//case SDL_FINGERDOWN:
//case SDL_FINGERUP:
//case SDL_FINGERMOTION:
//case SDL_DOLLARGESTURE:
//case SDL_DOLLARRECORD:
//case SDL_MULTIGESTURE:
//case SDL_CLIPBOARDUPDATE:
//case SDL_DROPFILE:
default:
return 0; /* drop everything else. */
}
if ((!EventFilter12) || (EventFilter12(&event12)))
SDL_PushEvent(&event12);
return 0; /* always drop it from the 2.0 event queue. */
}
int
SDL_PollEvent(SDL12_Event *event12)
{
Apr 10, 2013
Apr 10, 2013
804
805
EventQueueType *next;
Apr 1, 2013
Apr 1, 2013
806
807
SDL20_PumpEvents(); /* this will run our filter and build our 1.2 queue. */
Apr 10, 2013
Apr 10, 2013
808
if (EventQueueHead == NULL)
Apr 1, 2013
Apr 1, 2013
809
810
return 0; /* no events at the moment. */
Apr 10, 2013
Apr 10, 2013
811
812
813
814
815
SDL_memcpy(event12, &EventQueueHead->event12, sizeof (SDL12_Event));
next = EventQueueHead->next;
EventQueueHead->next = EventQueueAvailable;
EventQueueAvailable = EventQueueHead;
EventQueueHead = next;
Apr 1, 2013
Apr 1, 2013
816
817
818
819
820
821
return 1;
}
int
SDL_PushEvent(SDL12_Event *event12)
{
Apr 10, 2013
Apr 10, 2013
822
823
EventQueueType *item = EventQueueAvailable;
if (item == NULL)
Apr 1, 2013
Apr 1, 2013
824
825
return -1; /* no space available at the moment. */
Apr 10, 2013
Apr 10, 2013
826
827
828
829
830
831
832
833
EventQueueAvailable = item->next;
if (EventQueueTail)
EventQueueTail->next = item;
else
EventQueueHead = EventQueueTail = item;
item->next = NULL;
SDL_memcpy(&item->event12, event12, sizeof (SDL12_Event));
Apr 1, 2013
Apr 1, 2013
834
835
836
837
838
839
840
841
842
843
844
845
return 0;
}
int
SDL_PeepEvents(SDL12_Event *events12, int numevents, SDL_eventaction action, Uint32 mask)
{
int i;
if (action == SDL_ADDEVENT)
{
for (i = 0; i < numevents; i++)
{
Apr 10, 2013
Apr 10, 2013
846
if (SDL_PushEvent(&events12[i]) == -1)
Apr 1, 2013
Apr 1, 2013
847
848
849
850
851
852
break; /* out of space for more events. */
}
return i;
}
else if ((action == SDL_PEEKEVENT) || (action == SDL_GETEVENT))
{
Apr 10, 2013
Apr 10, 2013
853
854
855
856
const int isGet = (action == SDL_GETEVENT);
EventQueueType *prev = NULL;
EventQueueType *item = EventQueueHead;
EventQueueType *next = NULL;
Apr 1, 2013
Apr 1, 2013
857
858
859
int chosen = 0;
while (chosen < numevents)
{
Apr 10, 2013
Apr 10, 2013
860
861
EventQueueType *nextPrev = item;
if (!item)
Apr 1, 2013
Apr 1, 2013
862
863
break; /* no more events at the moment. */
Apr 10, 2013
Apr 10, 2013
864
next = item->next; /* copy, since we might overwrite item->next */
Apr 1, 2013
Apr 1, 2013
865
866
867
if (mask & (1<<event12->type))
{
Apr 10, 2013
Apr 10, 2013
868
869
870
871
872
873
874
875
876
877
878
879
880
881
SDL_memcpy(&events12[chosen++], &item->event12, sizeof (SDL12_Event));
if (isGet) /* remove from list? */
{
if (prev != NULL)
prev->next = next;
if (item == EventQueueHead)
EventQueueHead = next;
if (item == EventQueueTail)
EventQueueTail = prev;
/* put it back in the free pool. */
item->next = EventQueueAvailable;
EventQueueAvailable = item;
nextPrev = prev; /* previous item doesn't change. */
Apr 1, 2013
Apr 1, 2013
882
883
884
}
}
Apr 10, 2013
Apr 10, 2013
885
886
item = next;
prev = nextPrev;
Apr 1, 2013
Apr 1, 2013
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
}
return chosen;
}
return 0;
}
void
SDL_SetEventFilter(SDL12_EventFilter filter12)
{
/* We always have a filter installed, but will call the app's too. */
EventFilter12 = filter12;
}
SDL12_EventFilter
SDL_GetEventFilter(void)
{
return EventFilter12;
}
Mar 8, 2013
Mar 8, 2013
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
SDL12_Surface *
Surface20to12(SDL_Surface *surface20)
{
SDL12_Surface *surface12 = NULL;
SDL12_Palette *palette12 = NULL;
SDL12_PixelFormat *format12 = NULL;
UInt32 flags = 0;
if (!surface20)
return NULL;
surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
if (!surface12)
goto failed;
palette12 = (SDL12_Palette *) SDL20_malloc(sizeof (SDL12_Palette));
if (!palette12)
goto failed;
format12 = (SDL12_PixelFormat *) SDL20_malloc(sizeof (SDL12_PixelFormat));
if (!format12)
goto failed;
SDL_zerop(palette12);
palette12->ncolors = surface20->palette->ncolors;
palette12->colors = surface20->palette->colors;
SDL_zerop(format12);
format12->palette = palette12;
format12->BitsPerPixel = surface20->format->BitsPerPixel;
format12->BytesPerPixel = surface20->format->BytesPerPixel;
format12->Rloss = surface20->format->Rloss;
format12->Gloss = surface20->format->Gloss;
format12->Bloss = surface20->format->Bloss;
format12->Aloss = surface20->format->Aloss;
format12->Rshift = surface20->format->Rshift;
format12->Gshift = surface20->format->Gshift;
format12->Bshift = surface20->format->Bshift;
format12->Ashift = surface20->format->Ashift;
format12->Rmask = surface20->format->Rmask;
format12->Gmask = surface20->format->Gmask;
format12->Bmask = surface20->format->Bmask;
format12->Amask = surface20->format->Amask;
/* !!! FIXME: format12->colorkey; */
/* !!! FIXME: format12->alpha; */
SDL_zerop(surface12);
flags = surface20->flags;
#define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
MAPSURFACEFLAGS(PREALLOC);
MAPSURFACEFLAGS(RLEACCEL);
MAPSURFACEFLAGS(DONTFREE);
#undef MAPSURFACEFLAGS
Mar 8, 2013
Mar 8, 2013
961
assert(flags == 0); /* non-zero if there's a flag we didn't map. */
Mar 8, 2013
Mar 8, 2013
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
surface12->format = format12;
surface12->w = surface20->w;
surface12->h = surface20->h;
surface12->pitch = (Uint16) surface20->pitch; /* !!! FIXME: make sure this fits in a Uint16 */
surface12->pixels = surface20->pixels;
surface12->offset = 0;
surface12->hwdata = surface20;
SDL20_memcpy(&surface12->clip_rect, &surface20->clip_rect, sizeof (SDL_Rect));
surface12->refcount = surface20->refcount;
return surface12;
failed:
SDL20_free(surface12);
SDL20_free(palette12);
SDL20_free(format12);
return NULL;
}
SDL12_Surface *
SDL_CreateRGBSurface(Uint32 sdl12flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
SDL_Surface *surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
SDL12_Surface *surface12 = Surface20to12(surface20);
if (!surface12) {
SDL20_FreeSurface(surface20);
return NULL;
}
Mar 8, 2013
Mar 8, 2013
992
assert(surface12->flags == 0); // shouldn't have prealloc, rleaccel, or dontfree.
Mar 8, 2013
Mar 8, 2013
993
994
995
996
997
998
999
1000
return surface12;
}
SDL12_Surface *
SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
SDL_Surface *surface20 = SDL20_CreateRGBSurfaceFrom(pixels, width, height, depth, Rmask, Gmask, Bmask, Amask);
SDL12_Surface *surface12 = Surface20to12(surface20);