src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 19 Feb 2019 16:41:05 -0500
changeset 74 4e3e77f78520
parent 73 040f36454e95
child 75 00631178b6d5
permissions -rw-r--r--
Implemented SDL_SetColorKey.

Now testsprite renders perfectly!
icculus@9
     1
/*
icculus@9
     2
  Simple DirectMedia Layer
icculus@46
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
icculus@9
     4
icculus@9
     5
  This software is provided 'as-is', without any express or implied
icculus@9
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@9
     7
  arising from the use of this software.
icculus@9
     8
icculus@9
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@9
    10
  including commercial applications, and to alter it and redistribute it
icculus@9
    11
  freely, subject to the following restrictions:
icculus@9
    12
icculus@9
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@9
    14
     claim that you wrote the original software. If you use this software
icculus@9
    15
     in a product, an acknowledgment in the product documentation would be
icculus@9
    16
     appreciated but is not required.
icculus@9
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@9
    18
     misrepresented as being the original software.
icculus@9
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@9
    20
*/
icculus@9
    21
icculus@9
    22
/* This file contains functions for backwards compatibility with SDL 1.2 */
icculus@9
    23
icculus@49
    24
// !!! FIXME: clean up code conventions
icculus@54
    25
// !!! FIXME: grep for VideoWindow20 places that might care if it's NULL
icculus@49
    26
icculus@31
    27
#include "SDL20_include_wrapper.h"
icculus@9
    28
icculus@9
    29
#if !SDL_VERSION_ATLEAST(2,0,0)
icculus@9
    30
#error You need to compile against SDL 2.0 headers.
icculus@9
    31
#endif
icculus@9
    32
icculus@33
    33
/*
icculus@33
    34
 * We report the library version as 1.2.$(SDL12_COMPAT_VERSION). This number
icculus@33
    35
 *  should be way ahead of what SDL-1.2 Classic would report, so apps can
icculus@33
    36
 *  decide if they're running under the compat layer, if they really care.
icculus@33
    37
 */
icculus@33
    38
#define SDL12_COMPAT_VERSION 50
icculus@33
    39
icculus@11
    40
#include <stdarg.h>
icculus@11
    41
icculus@49
    42
// !!! IMPLEMENT_ME SDL_CDClose
icculus@49
    43
// !!! IMPLEMENT_ME SDL_CDEject
icculus@49
    44
// !!! IMPLEMENT_ME SDL_CDName
icculus@49
    45
// !!! IMPLEMENT_ME SDL_CDNumDrives
icculus@49
    46
// !!! IMPLEMENT_ME SDL_CDOpen
icculus@49
    47
// !!! IMPLEMENT_ME SDL_CDPause
icculus@49
    48
// !!! IMPLEMENT_ME SDL_CDPlay
icculus@49
    49
// !!! IMPLEMENT_ME SDL_CDPlayTracks
icculus@49
    50
// !!! IMPLEMENT_ME SDL_CDResume
icculus@49
    51
// !!! IMPLEMENT_ME SDL_CDStatus
icculus@49
    52
// !!! IMPLEMENT_ME SDL_CDStop
icculus@49
    53
// !!! IMPLEMENT_ME SDL_CreateYUVOverlay
icculus@49
    54
icculus@49
    55
// !!! IMPLEMENT_ME SDL_DisplayFormatAlpha
icculus@49
    56
// !!! IMPLEMENT_ME SDL_DisplayYUVOverlay
icculus@49
    57
// !!! IMPLEMENT_ME SDL_EnableKeyRepeat
icculus@49
    58
// !!! IMPLEMENT_ME SDL_EnableUNICODE
icculus@49
    59
// !!! IMPLEMENT_ME SDL_FreeYUVOverlay
icculus@49
    60
icculus@49
    61
// !!! IMPLEMENT_ME SDL_GL_Lock
icculus@49
    62
// !!! IMPLEMENT_ME SDL_GL_Unlock
icculus@49
    63
// !!! IMPLEMENT_ME SDL_GL_UpdateRects
icculus@49
    64
icculus@9
    65
// !!! IMPLEMENT_ME SDL_GetKeyName
icculus@9
    66
// !!! IMPLEMENT_ME SDL_GetKeyState
icculus@9
    67
// !!! IMPLEMENT_ME SDL_GetModState
icculus@9
    68
// !!! IMPLEMENT_ME SDL_GetRelativeMouseState
icculus@49
    69
icculus@49
    70
// !!! IMPLEMENT_ME SDL_GetVideoSurface
icculus@49
    71
// !!! IMPLEMENT_ME SDL_GetWMInfo
icculus@49
    72
icculus@9
    73
// !!! IMPLEMENT_ME SDL_LockSurface
icculus@49
    74
// !!! IMPLEMENT_ME SDL_LockYUVOverlay
icculus@9
    75
// !!! IMPLEMENT_ME SDL_LowerBlit
icculus@49
    76
icculus@49
    77
// !!! IMPLEMENT_ME SDL_SetAlpha
icculus@9
    78
// !!! IMPLEMENT_ME SDL_SetColorKey
icculus@49
    79
// !!! IMPLEMENT_ME SDL_SetColors
icculus@49
    80
icculus@9
    81
// !!! IMPLEMENT_ME SDL_SetModState
icculus@49
    82
// !!! IMPLEMENT_ME SDL_SetPalette
icculus@49
    83
// !!! IMPLEMENT_ME SDL_SetVideoMode
icculus@9
    84
// !!! IMPLEMENT_ME SDL_SoftStretch
icculus@9
    85
// !!! IMPLEMENT_ME SDL_UnlockSurface
icculus@49
    86
// !!! IMPLEMENT_ME SDL_UnlockYUVOverlay
icculus@49
    87
// !!! IMPLEMENT_ME SDL_UpdateRects
icculus@9
    88
// !!! IMPLEMENT_ME SDL_UpperBlit
icculus@49
    89
icculus@49
    90
// !!! FIXME: should SDL_VideoInit really be a passthrough?
icculus@49
    91
// !!! FIXME: should SDL_VideoQuit really be a passthrough?
icculus@49
    92
icculus@49
    93
// !!! IMPLEMENT_ME SDL_WM_SetIcon
icculus@49
    94
// !!! IMPLEMENT_ME SDL_WM_ToggleFullScreen
icculus@49
    95
icculus@9
    96
// !!! IMPLEMENT_ME X11_KeyToUnicode
icculus@9
    97
icculus@64
    98
#define SDL_BlitSurface SDL_UpperBlit
icculus@64
    99
icculus@54
   100
icculus@54
   101
#if 0
icculus@54
   102
#define FIXME(x) do {} while (0)
icculus@54
   103
#else
icculus@54
   104
#define FIXME(x) \
icculus@54
   105
    do { \
icculus@54
   106
        static SDL_bool seen = SDL_FALSE; \
icculus@54
   107
        if (!seen) { \
icculus@54
   108
            fprintf(stderr, "FIXME: %s (%s:%d)\n", x, __FILE__, __LINE__); \
icculus@54
   109
            seen = SDL_TRUE; \
icculus@54
   110
        } \
icculus@54
   111
    } while (0)
icculus@54
   112
#endif
icculus@54
   113
icculus@9
   114
#define SDL20_SYM(rc,fn,params,args,ret) \
icculus@35
   115
    typedef rc (SDLCALL *SDL20_##fn##_t) params; \
icculus@9
   116
    static SDL20_##fn##_t SDL20_##fn = NULL;
icculus@9
   117
#include "SDL20_syms.h"
icculus@48
   118
icculus@34
   119
/* Things that _should_ be binary compatible pass right through... */
icculus@34
   120
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
icculus@35
   121
    DECLSPEC rc SDLCALL SDL_##fn params { ret SDL20_##fn args; }
icculus@34
   122
#include "SDL20_syms.h"
icculus@34
   123
icculus@34
   124
icculus@34
   125
/* these are macros (etc) in the SDL headers, so make our own. */
icculus@34
   126
#define SDL20_OutOfMemory() SDL20_Error(SDL_ENOMEM)
icculus@34
   127
#define SDL20_Unsupported() SDL20_Error(SDL_UNSUPPORTED)
icculus@34
   128
#define SDL20_InvalidParamError(param) SDL20_SetError("Parameter '%s' is invalid", (param))
icculus@34
   129
#define SDL20_zero(x) SDL20_memset(&(x), 0, sizeof((x)))
icculus@34
   130
#define SDL20_zerop(x) SDL20_memset((x), 0, sizeof(*(x)))
icculus@34
   131
#define SDL_ReportAssertion SDL20_ReportAssertion
icculus@34
   132
icculus@34
   133
#define SDL12_DEFAULT_REPEAT_DELAY 500
icculus@34
   134
#define SDL12_DEFAULT_REPEAT_INTERVAL 30
icculus@9
   135
icculus@9
   136
#define SDL12_INIT_TIMER       0x00000001
icculus@9
   137
#define SDL12_INIT_AUDIO       0x00000010
icculus@9
   138
#define SDL12_INIT_VIDEO       0x00000020
icculus@9
   139
#define SDL12_INIT_CDROM       0x00000100
icculus@9
   140
#define SDL12_INIT_JOYSTICK    0x00000200
icculus@9
   141
#define SDL12_INIT_NOPARACHUTE 0x00100000
icculus@9
   142
#define SDL12_INIT_EVENTTHREAD 0x01000000
icculus@9
   143
#define SDL12_INIT_EVERYTHING  0x0000FFFF
icculus@9
   144
icculus@73
   145
typedef struct SDL12_Rect
icculus@73
   146
{
icculus@73
   147
    Sint16 x;
icculus@73
   148
    Sint16 y;
icculus@73
   149
    Uint16 w;
icculus@73
   150
    Uint16 h;
icculus@73
   151
} SDL12_Rect;
icculus@73
   152
icculus@15
   153
typedef struct SDL12_Palette
icculus@15
   154
{
icculus@15
   155
    int       ncolors;
icculus@15
   156
    SDL_Color *colors;
icculus@15
   157
} SDL12_Palette;
icculus@15
   158
icculus@15
   159
typedef struct SDL12_PixelFormat
icculus@15
   160
{
icculus@15
   161
    SDL12_Palette *palette;
icculus@15
   162
    Uint8 BitsPerPixel;
icculus@15
   163
    Uint8 BytesPerPixel;
icculus@15
   164
    Uint8 Rloss;
icculus@15
   165
    Uint8 Gloss;
icculus@15
   166
    Uint8 Bloss;
icculus@15
   167
    Uint8 Aloss;
icculus@15
   168
    Uint8 Rshift;
icculus@15
   169
    Uint8 Gshift;
icculus@15
   170
    Uint8 Bshift;
icculus@15
   171
    Uint8 Ashift;
icculus@15
   172
    Uint32 Rmask;
icculus@15
   173
    Uint32 Gmask;
icculus@15
   174
    Uint32 Bmask;
icculus@15
   175
    Uint32 Amask;
icculus@15
   176
    Uint32 colorkey;
icculus@15
   177
    Uint8 alpha;
icculus@15
   178
} SDL12_PixelFormat;
icculus@15
   179
icculus@18
   180
typedef struct SDL12_Surface
icculus@18
   181
{
icculus@18
   182
    Uint32 flags;
icculus@18
   183
    SDL12_PixelFormat *format;
icculus@18
   184
    int w;
icculus@18
   185
    int h;
icculus@18
   186
    Uint16 pitch;
icculus@18
   187
    void *pixels;
icculus@18
   188
    int offset;
icculus@34
   189
    SDL_Surface *surface20; /* the real SDL 1.2 has an opaque pointer to a platform-specific thing here named "hwdata". */
icculus@73
   190
    SDL12_Rect clip_rect;
icculus@18
   191
    Uint32 unused1;
icculus@18
   192
    Uint32 locked;
icculus@18
   193
    void *blitmap;
icculus@18
   194
    unsigned int format_version;
icculus@18
   195
    int refcount;
icculus@18
   196
} SDL12_Surface;
icculus@18
   197
icculus@57
   198
typedef struct SDL12_Overlay
icculus@57
   199
{
icculus@57
   200
    Uint32 format;
icculus@57
   201
    int w;
icculus@57
   202
    int h;
icculus@57
   203
    int planes;
icculus@57
   204
    Uint16 *pitches;
icculus@57
   205
    Uint8 **pixels;
icculus@57
   206
    void *hwfuncs;
icculus@57
   207
    void *hwdata;
icculus@57
   208
    Uint32 hw_overlay :1;
icculus@57
   209
    Uint32 UnusedBits :31;
icculus@57
   210
} SDL12_Overlay;
icculus@57
   211
icculus@9
   212
typedef struct
icculus@9
   213
{
icculus@15
   214
    Uint32 hw_available :1;
icculus@15
   215
    Uint32 wm_available :1;
icculus@15
   216
    Uint32 UnusedBits1  :6;
icculus@15
   217
    Uint32 UnusedBits2  :1;
icculus@15
   218
    Uint32 blit_hw      :1;
icculus@15
   219
    Uint32 blit_hw_CC   :1;
icculus@15
   220
    Uint32 blit_hw_A    :1;
icculus@15
   221
    Uint32 blit_sw      :1;
icculus@15
   222
    Uint32 blit_sw_CC   :1;
icculus@15
   223
    Uint32 blit_sw_A    :1;
icculus@15
   224
    Uint32 blit_fill    :1;
icculus@15
   225
    Uint32 UnusedBits3  :16;
icculus@15
   226
    Uint32 video_mem;
icculus@15
   227
    SDL_PixelFormat *vfmt;
icculus@15
   228
    int current_w;
icculus@15
   229
    int current_h;
icculus@9
   230
} SDL12_VideoInfo;
icculus@9
   231
icculus@9
   232
icculus@15
   233
#define SDL12_HWSURFACE 0x00000001
icculus@15
   234
#define SDL12_ASYNCBLIT 0x00000004
icculus@15
   235
#define SDL12_ANYFORMAT 0x10000000
icculus@15
   236
#define SDL12_HWPALETTE 0x20000000
icculus@15
   237
#define SDL12_DOUBLEBUF 0x40000000
icculus@15
   238
#define SDL12_FULLSCREEN 0x80000000
icculus@15
   239
#define SDL12_OPENGL 0x00000002
icculus@15
   240
#define SDL12_OPENGLBLIT 0x0000000A
icculus@15
   241
#define SDL12_RESIZABLE 0x00000010
icculus@15
   242
#define SDL12_NOFRAME 0x00000020
icculus@15
   243
#define SDL12_HWACCEL 0x00000100
icculus@15
   244
#define SDL12_SRCCOLORKEY 0x00001000
icculus@15
   245
#define SDL12_RLEACCELOK 0x00002000
icculus@15
   246
#define SDL12_RLEACCEL 0x00004000
icculus@15
   247
#define SDL12_SRCALPHA 0x00010000
icculus@15
   248
#define SDL12_PREALLOC 0x01000000
icculus@9
   249
icculus@18
   250
typedef enum
icculus@18
   251
{
icculus@18
   252
    SDL12_NOEVENT = 0,
icculus@18
   253
    SDL12_ACTIVEEVENT,
icculus@18
   254
    SDL12_KEYDOWN,
icculus@18
   255
    SDL12_KEYUP,
icculus@18
   256
    SDL12_MOUSEMOTION,
icculus@18
   257
    SDL12_MOUSEBUTTONDOWN,
icculus@18
   258
    SDL12_MOUSEBUTTONUP,
icculus@18
   259
    SDL12_JOYAXISMOTION,
icculus@18
   260
    SDL12_JOYBALLMOTION,
icculus@18
   261
    SDL12_JOYHATMOTION,
icculus@18
   262
    SDL12_JOYBUTTONDOWN,
icculus@18
   263
    SDL12_JOYBUTTONUP,
icculus@18
   264
    SDL12_QUIT,
icculus@18
   265
    SDL12_SYSWMEVENT,
icculus@18
   266
    SDL12_EVENT_RESERVEDA,
icculus@18
   267
    SDL12_EVENT_RESERVEDB,
icculus@18
   268
    SDL12_VIDEORESIZE,
icculus@18
   269
    SDL12_VIDEOEXPOSE,
icculus@18
   270
    SDL12_USEREVENT = 24,
icculus@18
   271
    SDL12_NUMEVENTS = 32
icculus@18
   272
} SDL12_EventType;
icculus@18
   273
icculus@31
   274
icculus@31
   275
#define SDL12_APPMOUSEFOCUS (1<<0)
icculus@31
   276
#define SDL12_APPINPUTFOCUS (1<<1)
icculus@31
   277
#define SDL12_APPACTIVE     (1<<2)
icculus@31
   278
icculus@18
   279
typedef struct
icculus@18
   280
{
icculus@18
   281
    Uint8 type;
icculus@18
   282
    Uint8 gain;
icculus@18
   283
    Uint8 state;
icculus@18
   284
} SDL12_ActiveEvent;
icculus@18
   285
icculus@18
   286
typedef struct
icculus@18
   287
{
icculus@18
   288
    Uint8 type;
icculus@18
   289
    Uint8 which;
icculus@18
   290
    Uint8 state;
icculus@31
   291
    //FIXME: SDL12_keysym keysym;
icculus@18
   292
} SDL12_KeyboardEvent;
icculus@18
   293
icculus@18
   294
typedef struct
icculus@18
   295
{
icculus@18
   296
    Uint8 type;
icculus@18
   297
    Uint8 which;
icculus@18
   298
    Uint8 state;
icculus@18
   299
    Uint16 x, y;
icculus@18
   300
    Sint16 xrel;
icculus@18
   301
    Sint16 yrel;
icculus@18
   302
} SDL12_MouseMotionEvent;
icculus@18
   303
icculus@18
   304
typedef struct
icculus@18
   305
{
icculus@18
   306
    Uint8 type;
icculus@18
   307
    Uint8 which;
icculus@18
   308
    Uint8 button;
icculus@18
   309
    Uint8 state;
icculus@18
   310
    Uint16 x, y;
icculus@18
   311
} SDL12_MouseButtonEvent;
icculus@18
   312
icculus@18
   313
typedef struct
icculus@18
   314
{
icculus@18
   315
    Uint8 type;
icculus@18
   316
    Uint8 which;
icculus@18
   317
    Uint8 axis;
icculus@18
   318
    Sint16 value;
icculus@18
   319
} SDL12_JoyAxisEvent;
icculus@18
   320
icculus@18
   321
typedef struct
icculus@18
   322
{
icculus@18
   323
    Uint8 type;
icculus@18
   324
    Uint8 which;
icculus@18
   325
    Uint8 ball;
icculus@18
   326
    Sint16 xrel;
icculus@18
   327
    Sint16 yrel;
icculus@18
   328
} SDL12_JoyBallEvent;
icculus@18
   329
icculus@18
   330
typedef struct
icculus@18
   331
{
icculus@18
   332
    Uint8 type;
icculus@18
   333
    Uint8 which;
icculus@18
   334
    Uint8 hat;
icculus@18
   335
    Uint8 value;
icculus@18
   336
} SDL12_JoyHatEvent;
icculus@18
   337
icculus@18
   338
typedef struct
icculus@18
   339
{
icculus@18
   340
    Uint8 type;
icculus@18
   341
    Uint8 which;
icculus@18
   342
    Uint8 button;
icculus@18
   343
    Uint8 state;
icculus@18
   344
} SDL12_JoyButtonEvent;
icculus@18
   345
icculus@18
   346
typedef struct
icculus@18
   347
{
icculus@18
   348
    Uint8 type;
icculus@18
   349
    int w;
icculus@18
   350
    int h;
icculus@18
   351
} SDL12_ResizeEvent;
icculus@18
   352
icculus@18
   353
typedef struct
icculus@18
   354
{
icculus@18
   355
    Uint8 type;
icculus@18
   356
} SDL12_ExposeEvent;
icculus@18
   357
icculus@18
   358
typedef struct
icculus@18
   359
{
icculus@18
   360
    Uint8 type;
icculus@18
   361
} SDL12_QuitEvent;
icculus@18
   362
icculus@18
   363
typedef struct
icculus@18
   364
{
icculus@18
   365
    Uint8 type;
icculus@18
   366
    int code;
icculus@18
   367
    void *data1;
icculus@18
   368
    void *data2;
icculus@18
   369
} SDL12_UserEvent;
icculus@18
   370
icculus@18
   371
typedef struct
icculus@18
   372
{
icculus@18
   373
    Uint8 type;
icculus@18
   374
    void *msg;
icculus@18
   375
} SDL12_SysWMEvent;
icculus@18
   376
icculus@18
   377
typedef union
icculus@18
   378
{
icculus@18
   379
    Uint8 type;
icculus@18
   380
    SDL12_ActiveEvent active;
icculus@18
   381
    SDL12_KeyboardEvent key;
icculus@18
   382
    SDL12_MouseMotionEvent motion;
icculus@18
   383
    SDL12_MouseButtonEvent button;
icculus@18
   384
    SDL12_JoyAxisEvent jaxis;
icculus@18
   385
    SDL12_JoyBallEvent jball;
icculus@18
   386
    SDL12_JoyHatEvent jhat;
icculus@18
   387
    SDL12_JoyButtonEvent jbutton;
icculus@18
   388
    SDL12_ResizeEvent resize;
icculus@18
   389
    SDL12_ExposeEvent expose;
icculus@18
   390
    SDL12_QuitEvent quit;
icculus@18
   391
    SDL12_UserEvent user;
icculus@18
   392
    SDL12_SysWMEvent syswm;
icculus@18
   393
} SDL12_Event;
icculus@9
   394
icculus@31
   395
typedef int (SDLCALL *SDL12_EventFilter)(const SDL12_Event *event12);
icculus@31
   396
static int EventFilter20to12(void *data, SDL_Event *event20);
icculus@31
   397
icculus@31
   398
typedef Uint32 (SDLCALL *SDL12_TimerCallback)(Uint32 interval);
icculus@31
   399
typedef SDL_TimerCallback SDL12_NewTimerCallback;
icculus@31
   400
icculus@23
   401
typedef struct
icculus@31
   402
{
icculus@73
   403
    SDL12_Rect area;
icculus@23
   404
    Sint16 hot_x;
icculus@23
   405
    Sint16 hot_y;
icculus@23
   406
    Uint8 *data;
icculus@23
   407
    Uint8 *mask;
icculus@23
   408
    Uint8 *save[2];
icculus@23
   409
    SDL_Cursor *wm_cursor;  /* the real SDL 1.2 has an opaque pointer to a platform-specific cursor here. */
icculus@23
   410
} SDL12_Cursor;
icculus@23
   411
icculus@30
   412
typedef enum
icculus@30
   413
{
icculus@30
   414
    SDL12_GL_RED_SIZE,
icculus@30
   415
    SDL12_GL_GREEN_SIZE,
icculus@30
   416
    SDL12_GL_BLUE_SIZE,
icculus@30
   417
    SDL12_GL_ALPHA_SIZE,
icculus@30
   418
    SDL12_GL_BUFFER_SIZE,
icculus@30
   419
    SDL12_GL_DOUBLEBUFFER,
icculus@30
   420
    SDL12_GL_DEPTH_SIZE,
icculus@30
   421
    SDL12_GL_STENCIL_SIZE,
icculus@30
   422
    SDL12_GL_ACCUM_RED_SIZE,
icculus@30
   423
    SDL12_GL_ACCUM_GREEN_SIZE,
icculus@30
   424
    SDL12_GL_ACCUM_BLUE_SIZE,
icculus@30
   425
    SDL12_GL_ACCUM_ALPHA_SIZE,
icculus@30
   426
    SDL12_GL_STEREO,
icculus@30
   427
    SDL12_GL_MULTISAMPLEBUFFERS,
icculus@30
   428
    SDL12_GL_MULTISAMPLESAMPLES,
icculus@30
   429
    SDL12_GL_ACCELERATED_VISUAL,
icculus@30
   430
    SDL12_GL_SWAP_CONTROL,
icculus@30
   431
    SDL12_GL_MAX_ATTRIBUTE
icculus@30
   432
} SDL12_GLattr;
icculus@23
   433
icculus@39
   434
icculus@39
   435
typedef struct
icculus@39
   436
{
icculus@39
   437
    Uint32 format;
icculus@73
   438
    SDL12_Rect *modeslist12;
icculus@73
   439
    SDL12_Rect **modes12;  /* ptrs to each item in modeslist, for SDL_ListModes() */
icculus@39
   440
} VideoModeList;
icculus@39
   441
icculus@54
   442
// !!! FIXME: go through all of these.
icculus@39
   443
static VideoModeList *VideoModes = NULL;
icculus@39
   444
static int VideoModesCount = 0;
icculus@56
   445
static SDL12_VideoInfo VideoInfo12;
icculus@19
   446
static SDL_Window *VideoWindow20 = NULL;
icculus@56
   447
static SDL_Renderer *VideoRenderer20 = NULL;
icculus@56
   448
static SDL_Texture *VideoTexture20 = NULL;
icculus@56
   449
static SDL12_Surface *VideoSurface12 = NULL;
icculus@56
   450
static SDL_Surface *VideoConvertSurface20 = NULL;
icculus@56
   451
static SDL_GLContext *VideoGLContext20 = NULL;
icculus@16
   452
static char *WindowTitle = NULL;
icculus@16
   453
static char *WindowIconTitle = NULL;
icculus@56
   454
static SDL12_Surface *VideoIcon12;
icculus@16
   455
static int EnabledUnicode = 0;
icculus@16
   456
static int VideoDisplayIndex = 0;
icculus@16
   457
static int CDRomInit = 0;
icculus@18
   458
static SDL12_EventFilter EventFilter12 = NULL;
icculus@56
   459
static SDL12_Cursor *CurrentCursor12 = NULL;
icculus@27
   460
static Uint8 EventStates[SDL12_NUMEVENTS];
icculus@30
   461
static int SwapInterval = 0;
icculus@18
   462
icculus@18
   463
// !!! FIXME: need a mutex for the event queue.
icculus@18
   464
#define SDL12_MAXEVENTS 128
icculus@20
   465
typedef struct EventQueueType
icculus@20
   466
{
icculus@20
   467
    SDL12_Event event12;
icculus@20
   468
    struct EventQueueType *next;
icculus@20
   469
} EventQueueType;
icculus@20
   470
icculus@20
   471
static EventQueueType EventQueuePool[SDL12_MAXEVENTS];
icculus@20
   472
static EventQueueType *EventQueueHead = NULL;
icculus@20
   473
static EventQueueType *EventQueueTail = NULL;
icculus@20
   474
static EventQueueType *EventQueueAvailable = NULL;
icculus@9
   475
icculus@33
   476
icculus@9
   477
/* Obviously we can't use SDL_LoadObject() to load SDL2.  :)  */
icculus@9
   478
#if defined(_WINDOWS)
icculus@19
   479
    #define WIN32_LEAN_AND_MEAN 1
icculus@19
   480
    #include <windows.h>
icculus@9
   481
    #define SDL20_LIBNAME "SDL2.dll"
icculus@9
   482
    static HANDLE Loaded_SDL20 = NULL;
icculus@9
   483
    #define LoadSDL20Library() ((Loaded_SDL20 = LoadLibraryA(SDL20_LIBNAME)) != NULL)
icculus@9
   484
    #define LookupSDL20Sym(sym) GetProcAddress(Loaded_SDL20, sym)
icculus@9
   485
    #define CloseSDL20Library() { { if (Loaded_SDL20) { FreeLibrary(Loaded_SDL20); Loaded_SDL20 = NULL; } }
icculus@19
   486
#elif defined(unix) || defined(__APPLE__)
icculus@19
   487
    #include <dlfcn.h>
icculus@9
   488
    #ifdef __APPLE__
icculus@9
   489
    #define SDL20_LIBNAME "libSDL2.dylib"
icculus@9
   490
    #else
icculus@9
   491
    #define SDL20_LIBNAME "libSDL2-2.0.so.0"
icculus@9
   492
    #endif
icculus@9
   493
    static void *Loaded_SDL20 = NULL;
icculus@9
   494
    #define LoadSDL20Library() ((Loaded_SDL20 = dlopen(SDL20_LIBNAME, RTLD_LOCAL)) != NULL)
icculus@9
   495
    #define LookupSDL20Sym(sym) dlsym(Loaded_SDL20, sym)
icculus@9
   496
    #define CloseSDL20Library() { if (Loaded_SDL20) { dlclose(Loaded_SDL20); Loaded_SDL20 = NULL; } }
icculus@9
   497
#else
icculus@9
   498
    #error Please define your platform.
icculus@9
   499
#endif
icculus@9
   500
icculus@9
   501
static void *
icculus@9
   502
LoadSDL20Symbol(const char *fn, int *okay)
icculus@9
   503
{
icculus@9
   504
    void *retval = NULL;
icculus@9
   505
    if (*okay)  /* only bother trying if we haven't previously failed. */
icculus@9
   506
    {
icculus@9
   507
        retval = LookupSDL20Sym(fn);
icculus@55
   508
if (!retval) { fprintf(stderr, "WARNING: LOAD FAILED: %s\n", fn); }
icculus@55
   509
//        if (retval == NULL)
icculus@55
   510
//            *okay = 0;
icculus@9
   511
    }
icculus@9
   512
    return retval;
icculus@9
   513
}
icculus@9
   514
icculus@9
   515
static void
icculus@9
   516
UnloadSDL20(void)
icculus@9
   517
{
icculus@9
   518
    #define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = NULL;
icculus@9
   519
    #include "SDL20_syms.h"
icculus@9
   520
    CloseSDL20Library();
icculus@9
   521
}
icculus@9
   522
icculus@9
   523
static int
icculus@9
   524
LoadSDL20(void)
icculus@9
   525
{
icculus@9
   526
    int okay = 1;
icculus@9
   527
    if (!Loaded_SDL20)
icculus@9
   528
    {
icculus@9
   529
        okay = LoadSDL20Library();
icculus@9
   530
        #define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = (SDL20_##fn##_t) LoadSDL20Symbol("SDL_" #fn, &okay);
icculus@9
   531
        #include "SDL20_syms.h"
icculus@9
   532
        if (!okay)
icculus@9
   533
            UnloadSDL20();
icculus@9
   534
    }
icculus@9
   535
    return okay;
icculus@9
   536
}
icculus@9
   537
icculus@35
   538
DECLSPEC const SDL_version * SDLCALL
icculus@35
   539
SDL_Linked_Version(void)
icculus@35
   540
{
icculus@35
   541
    static const SDL_version version = { 1, 2, SDL12_COMPAT_VERSION };
icculus@35
   542
    return &version;
icculus@35
   543
}
icculus@9
   544
icculus@60
   545
DECLSPEC int SDLCALL
icculus@60
   546
SDL_sscanf(const char *text, const char *fmt, ...)
icculus@60
   547
{
icculus@60
   548
    int retval;
icculus@60
   549
    va_list ap;
icculus@60
   550
    va_start(ap, fmt);
icculus@60
   551
    retval = (int) SDL20_sscanf(text, fmt, ap);
icculus@60
   552
    va_end(ap);
icculus@60
   553
    return retval;
icculus@60
   554
}
icculus@60
   555
icculus@60
   556
DECLSPEC int SDLCALL
icculus@60
   557
SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
icculus@60
   558
{
icculus@60
   559
    int retval;
icculus@60
   560
    va_list ap;
icculus@60
   561
    va_start(ap, fmt);
icculus@60
   562
    retval = (int) SDL20_vsnprintf(text, maxlen, fmt, ap);
icculus@60
   563
    va_end(ap);
icculus@60
   564
    return retval;
icculus@60
   565
}
icculus@60
   566
icculus@60
   567
DECLSPEC SDL_bool SDLCALL
icculus@60
   568
SDL_HasMMXExt(void)
icculus@60
   569
{
icculus@60
   570
    /* this isn't accurate, but SDL2 doesn't have this for some reason.
icculus@60
   571
        MMXExt is available in all SSE1 machines, except early Athlon chips,
icculus@60
   572
        so we'll just say it's available if they have SSE1. Oh well. */
icculus@60
   573
    return SDL20_HasSSE();
icculus@60
   574
}
icculus@60
   575
icculus@60
   576
DECLSPEC SDL_bool SDLCALL
icculus@60
   577
SDL_Has3DNowExt(void)
icculus@60
   578
{
icculus@60
   579
    FIXME("check this");
icculus@60
   580
    return SDL20_HasSSE();
icculus@60
   581
}
icculus@60
   582
icculus@60
   583
DECLSPEC int SDLCALL SDL_JoystickOpened(int device_index)
icculus@60
   584
{
icculus@60
   585
    FIXME("write me");
icculus@60
   586
    return 0;
icculus@60
   587
}
icculus@60
   588
icculus@9
   589
static int
icculus@9
   590
GetVideoDisplay()
icculus@9
   591
{
icculus@58
   592
    FIXME("cache this value during SDL_Init() so it doesn't change.");
icculus@58
   593
    const char *variable = SDL20_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
icculus@9
   594
    if ( !variable ) {
icculus@58
   595
        variable = SDL20_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
icculus@9
   596
    }
icculus@9
   597
    if ( variable ) {
icculus@34
   598
        return SDL20_atoi(variable);
icculus@9
   599
    } else {
icculus@9
   600
        return 0;
icculus@9
   601
    }
icculus@9
   602
}
icculus@9
   603
icculus@39
   604
/* This sets up VideoModes and VideoModesCount. You end up with arrays by pixel
icculus@39
   605
    format, each with a value that 1.2's SDL_ListModes() can return. */
icculus@39
   606
static int
icculus@39
   607
Init12VidModes(void)
icculus@39
   608
{
icculus@39
   609
    const int total = SDL20_GetNumDisplayModes(VideoDisplayIndex);
icculus@39
   610
    VideoModeList *vmode = NULL;
icculus@39
   611
    int num_modes = 0;
icculus@39
   612
    void *ptr = NULL;
icculus@39
   613
    int i, j;
icculus@39
   614
icculus@39
   615
    SDL_assert(VideoModes == NULL);
icculus@39
   616
    SDL_assert(VideoModesCount == 0);
icculus@39
   617
icculus@39
   618
    for (i = 0; i < total; ++i) {
icculus@39
   619
        SDL_DisplayMode mode;
icculus@39
   620
icculus@39
   621
        if (SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode) == -1) {
icculus@39
   622
            continue;
icculus@39
   623
        } else if (!mode.w || !mode.h) {
icculus@39
   624
            SDL_assert(0 && "Can this actually happen?");
icculus@39
   625
            continue;
icculus@39
   626
        }
icculus@39
   627
icculus@39
   628
        if (!vmode || (mode.format != vmode->format)) {  // SDL20_GetDisplayMode() sorts on bpp first. We know when to change arrays.
icculus@39
   629
            if (VideoModesCount > 0) {
icculus@73
   630
                VideoModes[VideoModesCount-1].modes12[num_modes] = NULL;
icculus@39
   631
            }
icculus@39
   632
            ptr = (VideoModeList *) SDL20_realloc(VideoModes, sizeof (VideoModeList) * (VideoModesCount+1));
icculus@39
   633
            if (!ptr) {
icculus@39
   634
                return SDL20_OutOfMemory();
icculus@39
   635
            }
icculus@39
   636
            VideoModes = (VideoModeList *) ptr;
icculus@39
   637
            vmode = &VideoModes[VideoModesCount];
icculus@39
   638
            vmode->format = mode.format;
icculus@73
   639
            vmode->modeslist12 = NULL;
icculus@73
   640
            vmode->modes12 = NULL;
icculus@39
   641
            VideoModesCount++;
icculus@39
   642
            num_modes = 0;
icculus@39
   643
        }
icculus@39
   644
icculus@39
   645
        /* make sure we don't have this one already (with a different refresh rate, etc). */
icculus@39
   646
        for (j = 0; j < num_modes; j++) {
icculus@73
   647
            if ((vmode->modeslist12[j].w == mode.w) && (vmode->modeslist12[j].h == mode.h)) {
icculus@39
   648
                break;
icculus@39
   649
            }
icculus@39
   650
        }
icculus@39
   651
icculus@39
   652
        if (j < num_modes) {
icculus@39
   653
            continue;  /* already have this one. */
icculus@39
   654
        }
icculus@39
   655
icculus@73
   656
        FIXME("Make sure mode dimensions fit in 16-bits for SDL12_Rect");
icculus@73
   657
icculus@73
   658
        ptr = SDL20_realloc(vmode->modes12, sizeof (SDL12_Rect *) * (num_modes + 2));
icculus@39
   659
        if (ptr == NULL) {
icculus@39
   660
            return SDL20_OutOfMemory();
icculus@39
   661
        }
icculus@73
   662
        vmode->modes12 = (SDL12_Rect **) ptr;
icculus@73
   663
icculus@73
   664
        ptr = SDL20_realloc(vmode->modeslist12, sizeof (SDL12_Rect) * (num_modes + 1));
icculus@39
   665
        if (ptr == NULL) {
icculus@39
   666
            return SDL20_OutOfMemory();
icculus@39
   667
        }
icculus@73
   668
        vmode->modeslist12 = (SDL12_Rect *) ptr;
icculus@73
   669
icculus@73
   670
        vmode->modeslist12[num_modes].x = 0;
icculus@73
   671
        vmode->modeslist12[num_modes].y = 0;
icculus@73
   672
        vmode->modeslist12[num_modes].w = mode.w;
icculus@73
   673
        vmode->modeslist12[num_modes].h = mode.h;
icculus@73
   674
icculus@73
   675
        vmode->modes12[num_modes] = &vmode->modeslist12[num_modes];
icculus@39
   676
icculus@39
   677
        num_modes++;
icculus@39
   678
    }
icculus@39
   679
icculus@39
   680
    if (VideoModesCount > 0) {
icculus@73
   681
        VideoModes[VideoModesCount-1].modes12[num_modes] = NULL;
icculus@39
   682
    }
icculus@39
   683
icculus@39
   684
    return 0;
icculus@39
   685
}
icculus@39
   686
icculus@39
   687
static int
icculus@39
   688
Init12Video(void)
icculus@39
   689
{
icculus@39
   690
    int i;
icculus@39
   691
icculus@39
   692
    for (i = 0; i < SDL12_MAXEVENTS-1; i++)
icculus@39
   693
        EventQueuePool[i].next = &EventQueuePool[i+1];
icculus@39
   694
    EventQueuePool[SDL12_MAXEVENTS-1].next = NULL;
icculus@39
   695
icculus@39
   696
    EventQueueHead = EventQueueTail = NULL;
icculus@39
   697
    EventQueueAvailable = EventQueuePool;
icculus@39
   698
icculus@39
   699
    SDL_memset(EventStates, SDL_ENABLE, sizeof (EventStates)); /* on by default */
icculus@39
   700
    EventStates[SDL12_SYSWMEVENT] = SDL_IGNORE;  /* off by default. */
icculus@39
   701
icculus@39
   702
    SDL20_SetEventFilter(EventFilter20to12, NULL);
icculus@39
   703
icculus@39
   704
    VideoDisplayIndex = GetVideoDisplay();
icculus@39
   705
    SwapInterval = 0;
icculus@39
   706
icculus@39
   707
    if (Init12VidModes() == -1) {
icculus@39
   708
        return -1;
icculus@39
   709
    }
icculus@39
   710
icculus@39
   711
    return 0;
icculus@39
   712
}
icculus@39
   713
icculus@39
   714
icculus@37
   715
DECLSPEC int SDLCALL
icculus@37
   716
SDL_InitSubSystem(Uint32 sdl12flags)
icculus@9
   717
{
icculus@54
   718
    FIXME("there is never a parachute in SDL2, should we catch segfaults ourselves?");
icculus@40
   719
icculus@54
   720
    FIXME("support event thread where it makes sense to do so?");
icculus@54
   721
icculus@40
   722
	if ( (sdl12flags & SDL12_INIT_EVENTTHREAD) == SDL12_INIT_EVENTTHREAD ) {
icculus@40
   723
		return SDL20_SetError("OS doesn't support threaded events");
icculus@40
   724
	}
icculus@40
   725
icculus@9
   726
    Uint32 sdl20flags = 0;
icculus@9
   727
    int rc;
icculus@9
   728
icculus@9
   729
    if (!LoadSDL20())
icculus@9
   730
        return -1;
icculus@9
   731
icculus@62
   732
#ifdef __MACOSX__
icculus@62
   733
    extern void sdl12_compat_macos_init(void);
icculus@62
   734
    sdl12_compat_macos_init();
icculus@62
   735
#endif
icculus@62
   736
icculus@19
   737
    #define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
icculus@9
   738
    SETFLAG(TIMER);
icculus@9
   739
    SETFLAG(AUDIO);
icculus@9
   740
    SETFLAG(VIDEO);
icculus@9
   741
    SETFLAG(JOYSTICK);
icculus@9
   742
    SETFLAG(NOPARACHUTE);
icculus@11
   743
    #undef SETFLAG
icculus@11
   744
icculus@11
   745
    // There's no CDROM in 2.0, but we'll just pretend it succeeded.
icculus@11
   746
    if (sdl12flags & SDL12_INIT_CDROM)
icculus@16
   747
        CDRomInit = 1;
icculus@11
   748
icculus@54
   749
    FIXME("do something about SDL12_INIT_EVENTTHREAD");
icculus@11
   750
icculus@37
   751
    rc = SDL20_Init(sdl20flags);
icculus@39
   752
    if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO)) {
icculus@39
   753
        if (Init12Video() == -1) {
icculus@54
   754
            FIXME("should we deinit other subsystems?");
icculus@54
   755
            return -1;
icculus@39
   756
        }
icculus@18
   757
    }
icculus@18
   758
icculus@11
   759
    return rc;
icculus@11
   760
}
icculus@11
   761
icculus@35
   762
DECLSPEC int SDLCALL
icculus@11
   763
SDL_Init(Uint32 sdl12flags)
icculus@11
   764
{
icculus@54
   765
    FIXME("what was different in 1.2?");
icculus@41
   766
    return SDL_InitSubSystem(sdl12flags);   /* there's no difference betwee Init and InitSubSystem in SDL2. */
icculus@11
   767
}
icculus@11
   768
icculus@38
   769
icculus@38
   770
static void
icculus@38
   771
InitFlags12To20(const Uint32 flags12, Uint32 *_flags20, Uint32 *_extraflags)
icculus@11
   772
{
icculus@38
   773
    Uint32 flags20 = 0;
icculus@11
   774
    Uint32 extraflags = 0;
icculus@11
   775
icculus@38
   776
    #define SETFLAG(flag) if (flags12 & SDL12_INIT_##flag) flags20 |= SDL_INIT_##flag
icculus@11
   777
    SETFLAG(TIMER);
icculus@11
   778
    SETFLAG(AUDIO);
icculus@11
   779
    SETFLAG(VIDEO);
icculus@11
   780
    SETFLAG(JOYSTICK);
icculus@11
   781
    SETFLAG(NOPARACHUTE);
icculus@11
   782
    #undef SETFLAG
icculus@11
   783
icculus@38
   784
    if ((flags12 & SDL12_INIT_CDROM) && (CDRomInit)) {
icculus@11
   785
        extraflags |= SDL12_INIT_CDROM;
icculus@38
   786
    }
icculus@11
   787
icculus@54
   788
    FIXME("do something about SDL12_INIT_EVENTTHREAD");
icculus@11
   789
icculus@38
   790
    *_flags20 = flags20;
icculus@38
   791
    *_extraflags = extraflags;
icculus@11
   792
}
icculus@11
   793
icculus@38
   794
static Uint32
icculus@38
   795
InitFlags20to12(const Uint32 flags20)
icculus@11
   796
{
icculus@38
   797
    Uint32 flags12 = 0;
icculus@38
   798
icculus@38
   799
    #define SETFLAG(flag) if (flags20 & SDL_INIT_##flag) flags12 |= SDL12_INIT_##flag
icculus@11
   800
    SETFLAG(TIMER);
icculus@11
   801
    SETFLAG(AUDIO);
icculus@11
   802
    SETFLAG(VIDEO);
icculus@11
   803
    SETFLAG(JOYSTICK);
icculus@11
   804
    SETFLAG(NOPARACHUTE);
icculus@9
   805
    #undef SETFLAG
icculus@9
   806
icculus@38
   807
    return flags12;
icculus@38
   808
}
icculus@38
   809
icculus@38
   810
icculus@38
   811
DECLSPEC Uint32 SDLCALL
icculus@38
   812
SDL_WasInit(Uint32 sdl12flags)
icculus@38
   813
{
icculus@38
   814
    Uint32 sdl20flags, extraflags;
icculus@38
   815
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   816
icculus@38
   817
    return InitFlags20to12(SDL20_WasInit(sdl20flags)) | extraflags;
icculus@38
   818
}
icculus@38
   819
icculus@39
   820
static void
icculus@39
   821
Quit12Video(void)
icculus@39
   822
{
icculus@39
   823
    int i;
icculus@39
   824
icculus@39
   825
    for (i = 0; i < VideoModesCount; i++) {
icculus@73
   826
        SDL20_free(VideoModes[i].modeslist12);
icculus@73
   827
        SDL20_free(VideoModes[i].modes12);
icculus@39
   828
    }
icculus@39
   829
    SDL20_free(VideoModes);
icculus@39
   830
icculus@56
   831
    SDL20_FreeFormat(VideoInfo12.vfmt);
icculus@56
   832
    SDL20_zero(VideoInfo12);
icculus@39
   833
icculus@39
   834
    EventFilter12 = NULL;
icculus@39
   835
    EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
icculus@56
   836
    CurrentCursor12 = NULL;
icculus@39
   837
    VideoModes = NULL;
icculus@39
   838
    VideoModesCount = 0;
icculus@39
   839
}
icculus@39
   840
icculus@38
   841
DECLSPEC void SDLCALL
icculus@38
   842
SDL_QuitSubSystem(Uint32 sdl12flags)
icculus@38
   843
{
icculus@38
   844
    Uint32 sdl20flags, extraflags;
icculus@38
   845
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   846
icculus@38
   847
    if (extraflags & SDL12_INIT_CDROM) {
icculus@16
   848
        CDRomInit = 0;
icculus@38
   849
    }
icculus@16
   850
icculus@54
   851
    FIXME("reset a bunch of other global variables too.");
icculus@17
   852
    if (sdl12flags & SDL12_INIT_VIDEO) {
icculus@39
   853
        Quit12Video();
icculus@17
   854
    }
icculus@17
   855
icculus@54
   856
    FIXME("do something about SDL12_INIT_EVENTTHREAD");
icculus@11
   857
    SDL20_QuitSubSystem(sdl20flags);
icculus@9
   858
icculus@42
   859
    if ((SDL20_WasInit(0) == 0) && (!CDRomInit)) {
icculus@42
   860
        SDL20_Quit();
icculus@42
   861
        UnloadSDL20();
icculus@42
   862
    }
icculus@9
   863
}
icculus@9
   864
icculus@35
   865
DECLSPEC void SDLCALL
icculus@9
   866
SDL_Quit(void)
icculus@9
   867
{
icculus@42
   868
    SDL_QuitSubSystem(SDL_WasInit(0) | SDL12_INIT_CDROM);
icculus@9
   869
}
icculus@9
   870
icculus@35
   871
DECLSPEC void SDLCALL
icculus@11
   872
SDL_SetError(const char *fmt, ...)
icculus@11
   873
{
icculus@34
   874
    char ch;
icculus@12
   875
    char *str = NULL;
icculus@34
   876
    size_t len = 0;
icculus@12
   877
    va_list ap;
icculus@12
   878
    va_start(ap, fmt);
icculus@34
   879
    len = SDL20_vsnprintf(&ch, 1, fmt, ap);
icculus@12
   880
    va_end(ap);
icculus@34
   881
icculus@34
   882
    str = (char *) SDL20_malloc(len + 1);
icculus@12
   883
    if (!str)
icculus@12
   884
        SDL20_OutOfMemory();
icculus@12
   885
    else
icculus@12
   886
    {
icculus@34
   887
        va_start(ap, fmt);
icculus@34
   888
        SDL20_vsnprintf(str, len + 1, fmt, ap);
icculus@34
   889
        va_end(ap);
icculus@12
   890
        SDL20_SetError("%s", str);
icculus@34
   891
        SDL20_free(str);
icculus@12
   892
    }
icculus@11
   893
}
icculus@9
   894
icculus@35
   895
DECLSPEC const char * SDLCALL
icculus@9
   896
SDL_GetError(void)
icculus@9
   897
{
icculus@60
   898
    if (SDL20_GetError == NULL)
icculus@9
   899
    {
icculus@60
   900
        static const char noload_errstr[] = "The SDL 2.0 library that the 1.2 compatibility layer needs isn't loaded";
icculus@9
   901
        return noload_errstr;
icculus@9
   902
    }
icculus@9
   903
    return SDL20_GetError();
icculus@9
   904
}
icculus@9
   905
icculus@9
   906
icculus@9
   907
static const char *
icculus@9
   908
GetDriverName(const char *name, char *namebuf, int maxlen)
icculus@9
   909
{
icculus@9
   910
    if (name) {
icculus@9
   911
        if (namebuf) {
icculus@9
   912
            SDL20_strlcpy(namebuf, name, maxlen);
icculus@9
   913
            return namebuf;
icculus@9
   914
        } else {
icculus@9
   915
            return name;
icculus@9
   916
        }
icculus@9
   917
    }
icculus@9
   918
    return NULL;
icculus@9
   919
}
icculus@9
   920
icculus@35
   921
DECLSPEC const char * SDLCALL
icculus@9
   922
SDL_AudioDriverName(char *namebuf, int maxlen)
icculus@9
   923
{
icculus@9
   924
    return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
icculus@9
   925
}
icculus@9
   926
icculus@35
   927
DECLSPEC const char * SDLCALL
icculus@9
   928
SDL_VideoDriverName(char *namebuf, int maxlen)
icculus@9
   929
{
icculus@9
   930
    return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
icculus@9
   931
}
icculus@9
   932
icculus@35
   933
DECLSPEC int SDLCALL
icculus@31
   934
SDL_PollEvent(SDL12_Event *event12)
icculus@31
   935
{
icculus@31
   936
    EventQueueType *next;
icculus@31
   937
icculus@71
   938
    SDL20_PumpEvents();  /* this will run our filter and build our 1.2 queue. */
icculus@31
   939
icculus@31
   940
    if (EventQueueHead == NULL)
icculus@31
   941
        return 0;  /* no events at the moment. */
icculus@31
   942
icculus@31
   943
    SDL_memcpy(event12, &EventQueueHead->event12, sizeof (SDL12_Event));
icculus@31
   944
    next = EventQueueHead->next;
icculus@31
   945
    EventQueueHead->next = EventQueueAvailable;
icculus@31
   946
    EventQueueAvailable = EventQueueHead;
icculus@31
   947
    EventQueueHead = next;
icculus@31
   948
    return 1;
icculus@31
   949
}
icculus@31
   950
icculus@35
   951
DECLSPEC int SDLCALL
icculus@31
   952
SDL_PushEvent(SDL12_Event *event12)
icculus@31
   953
{
icculus@31
   954
    EventQueueType *item = EventQueueAvailable;
icculus@31
   955
    if (item == NULL)
icculus@31
   956
        return -1;  /* no space available at the moment. */
icculus@31
   957
icculus@31
   958
    EventQueueAvailable = item->next;
icculus@31
   959
    if (EventQueueTail)
icculus@31
   960
        EventQueueTail->next = item;
icculus@31
   961
    else
icculus@31
   962
        EventQueueHead = EventQueueTail = item;
icculus@31
   963
    item->next = NULL;
icculus@31
   964
icculus@31
   965
    SDL_memcpy(&item->event12, event12, sizeof (SDL12_Event));
icculus@31
   966
    return 0;
icculus@31
   967
}
icculus@31
   968
icculus@35
   969
DECLSPEC int SDLCALL
icculus@31
   970
SDL_PeepEvents(SDL12_Event *events12, int numevents, SDL_eventaction action, Uint32 mask)
icculus@31
   971
{
icculus@31
   972
    if (action == SDL_ADDEVENT)
icculus@31
   973
    {
icculus@31
   974
        int i;
icculus@31
   975
        for (i = 0; i < numevents; i++)
icculus@31
   976
        {
icculus@31
   977
            if (SDL_PushEvent(&events12[i]) == -1)
icculus@31
   978
                break;  /* out of space for more events. */
icculus@31
   979
        }
icculus@31
   980
        return i;
icculus@31
   981
    }
icculus@31
   982
    else if ((action == SDL_PEEKEVENT) || (action == SDL_GETEVENT))
icculus@31
   983
    {
icculus@31
   984
        const SDL_bool isGet = (action == SDL_GETEVENT);
icculus@31
   985
        EventQueueType *prev = NULL;
icculus@31
   986
        EventQueueType *item = EventQueueHead;
icculus@31
   987
        EventQueueType *next = NULL;
icculus@31
   988
        int chosen = 0;
icculus@31
   989
        while (chosen < numevents)
icculus@31
   990
        {
icculus@31
   991
            EventQueueType *nextPrev = item;
icculus@31
   992
            if (!item)
icculus@31
   993
                break;  /* no more events at the moment. */
icculus@31
   994
icculus@31
   995
            next = item->next;  /* copy, since we might overwrite item->next */
icculus@31
   996
icculus@31
   997
            if (mask & (1<<item->event12.type))
icculus@31
   998
            {
icculus@31
   999
                SDL_memcpy(&events12[chosen++], &item->event12, sizeof (SDL12_Event));
icculus@31
  1000
                if (isGet)  /* remove from list? */
icculus@31
  1001
                {
icculus@31
  1002
                    if (prev != NULL)
icculus@31
  1003
                        prev->next = next;
icculus@31
  1004
                    if (item == EventQueueHead)
icculus@31
  1005
                        EventQueueHead = next;
icculus@31
  1006
                    if (item == EventQueueTail)
icculus@31
  1007
                        EventQueueTail = prev;
icculus@31
  1008
icculus@31
  1009
                    /* put it back in the free pool. */
icculus@31
  1010
                    item->next = EventQueueAvailable;
icculus@31
  1011
                    EventQueueAvailable = item;
icculus@31
  1012
                    nextPrev = prev;  /* previous item doesn't change. */
icculus@31
  1013
                }
icculus@31
  1014
            }
icculus@31
  1015
icculus@31
  1016
            item = next;
icculus@31
  1017
            prev = nextPrev;
icculus@31
  1018
        }
icculus@31
  1019
        return chosen;
icculus@31
  1020
    }
icculus@31
  1021
icculus@31
  1022
    return 0;
icculus@31
  1023
}
icculus@31
  1024
icculus@35
  1025
DECLSPEC int SDLCALL
icculus@31
  1026
SDL_WaitEvent(SDL12_Event *event12)
icculus@31
  1027
{
icculus@54
  1028
    FIXME("In 1.2, this only fails (-1) if you haven't SDL_Init()'d.");
icculus@31
  1029
    while (!SDL_PollEvent(event12))
icculus@51
  1030
        SDL20_Delay(10);
icculus@31
  1031
    return 1;
icculus@31
  1032
}
icculus@31
  1033
icculus@32
  1034
static SDL_bool
icculus@32
  1035
PushEventIfNotFiltered(SDL12_Event *event12)
icculus@32
  1036
{
icculus@32
  1037
    if (event12->type != SDL12_NOEVENT)
icculus@32
  1038
    {
icculus@32
  1039
        if (EventStates[event12->type] != SDL_IGNORE)
icculus@32
  1040
        {
icculus@32
  1041
            if ((!EventFilter12) || (EventFilter12(event12)))
icculus@32
  1042
                return (SDL_PushEvent(event12) == 0);
icculus@32
  1043
        }
icculus@32
  1044
    }
icculus@32
  1045
    return SDL_FALSE;
icculus@32
  1046
}
icculus@32
  1047
icculus@35
  1048
DECLSPEC Uint8 SDLCALL
icculus@31
  1049
SDL_EventState(Uint8 type, int state)
icculus@31
  1050
{
icculus@31
  1051
    /* the values of "state" match between 1.2 and 2.0 */
icculus@31
  1052
    const Uint8 retval = EventStates[type];
icculus@31
  1053
    SDL12_Event e;
icculus@31
  1054
icculus@31
  1055
    if (state != SDL_QUERY)
icculus@31
  1056
        EventStates[type] = state;
icculus@31
  1057
    if (state == SDL_IGNORE)  /* drop existing events of this type. */
icculus@31
  1058
        while (SDL_PeepEvents(&e, 1, SDL_GETEVENT, (1<<type))) {}
icculus@31
  1059
icculus@31
  1060
    return retval;
icculus@31
  1061
}
icculus@31
  1062
icculus@35
  1063
DECLSPEC Uint8 SDLCALL
icculus@35
  1064
SDL_GetMouseState(int *x, int *y)
icculus@35
  1065
{
icculus@35
  1066
    const Uint32 state20 = SDL20_GetMouseState(x, y);
icculus@35
  1067
    Uint8 retval = (state20 & 0x7);  /* left, right, and middle will match. */
icculus@35
  1068
icculus@35
  1069
    /* the X[12] buttons are different in 1.2; mousewheel was in the way. */
icculus@35
  1070
    if (state20 & SDL_BUTTON(SDL_BUTTON_X1))
icculus@35
  1071
        retval |= (1<<5);
icculus@35
  1072
    if (state20 & SDL_BUTTON(SDL_BUTTON_X2))
icculus@35
  1073
        retval |= (1<<6);
icculus@35
  1074
icculus@35
  1075
    return retval;
icculus@35
  1076
}
icculus@35
  1077
icculus@18
  1078
static int
icculus@18
  1079
EventFilter20to12(void *data, SDL_Event *event20)
icculus@15
  1080
{
icculus@43
  1081
    //const int maxUserEvents12 = SDL12_NUMEVENTS - SDL12_USEREVENT;
icculus@18
  1082
    SDL12_Event event12;
icculus@32
  1083
    int x, y;
icculus@18
  1084
icculus@18
  1085
    SDL_assert(data == NULL);  /* currently unused. */
icculus@18
  1086
icculus@34
  1087
    SDL20_zero(event12);
icculus@18
  1088
icculus@18
  1089
    switch (event20->type)
icculus@18
  1090
    {
icculus@18
  1091
        case SDL_QUIT:
icculus@31
  1092
            event12.type = SDL12_QUIT;
icculus@18
  1093
            break;
icculus@18
  1094
icculus@18
  1095
        case SDL_WINDOWEVENT:
icculus@18
  1096
            switch (event20->window.event)
icculus@18
  1097
            {
icculus@18
  1098
                case SDL_WINDOWEVENT_CLOSE:
icculus@31
  1099
                    event12.type = SDL12_QUIT;
icculus@18
  1100
                    break;
icculus@18
  1101
icculus@18
  1102
                case SDL_WINDOWEVENT_SHOWN:
icculus@18
  1103
                case SDL_WINDOWEVENT_EXPOSED:
icculus@31
  1104
                    event12.type = SDL12_VIDEOEXPOSE;
icculus@18
  1105
                    break;
icculus@18
  1106
icculus@18
  1107
                case SDL_WINDOWEVENT_RESIZED:
icculus@54
  1108
                case SDL_WINDOWEVENT_SIZE_CHANGED:
icculus@54
  1109
                    FIXME("what's the difference between RESIZED and SIZE_CHANGED?");
icculus@31
  1110
                    event12.type = SDL12_VIDEORESIZE;
icculus@31
  1111
                    event12.resize.w = event20->window.data1;
icculus@31
  1112
                    event12.resize.h = event20->window.data2;
icculus@18
  1113
                    break;
icculus@18
  1114
icculus@18
  1115
                case SDL_WINDOWEVENT_MINIMIZED:
icculus@31
  1116
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1117
                    event12.active.gain = 0;
icculus@31
  1118
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
  1119
                    break;
icculus@18
  1120
icculus@18
  1121
                case SDL_WINDOWEVENT_RESTORED:
icculus@31
  1122
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1123
                    event12.active.gain = 1;
icculus@31
  1124
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
  1125
                    break;
icculus@18
  1126
icculus@18
  1127
                case SDL_WINDOWEVENT_ENTER:
icculus@31
  1128
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1129
                    event12.active.gain = 1;
icculus@31
  1130
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1131
                    break;
icculus@18
  1132
icculus@18
  1133
                case SDL_WINDOWEVENT_LEAVE:
icculus@31
  1134
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1135
                    event12.active.gain = 0;
icculus@31
  1136
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1137
                    break;
icculus@18
  1138
icculus@18
  1139
                case SDL_WINDOWEVENT_FOCUS_GAINED:
icculus@31
  1140
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1141
                    event12.active.gain = 1;
icculus@31
  1142
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1143
                    break;
icculus@18
  1144
icculus@18
  1145
                case SDL_WINDOWEVENT_FOCUS_LOST:
icculus@31
  1146
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1147
                    event12.active.gain = 0;
icculus@31
  1148
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1149
                    break;
icculus@18
  1150
            }
icculus@18
  1151
            break;
icculus@18
  1152
icculus@18
  1153
        // !!! FIXME: this is sort of a mess to convert.
icculus@18
  1154
        //case SDL_SYSWMEVENT:
icculus@18
  1155
icculus@54
  1156
        case SDL_KEYDOWN: FIXME("write me"); return 0;
icculus@54
  1157
        case SDL_KEYUP: FIXME("write me"); return 0;
icculus@18
  1158
icculus@54
  1159
        case SDL_TEXTEDITING: FIXME("write me"); return 0;
icculus@54
  1160
        case SDL_TEXTINPUT: FIXME("write me"); return 0;
icculus@18
  1161
icculus@18
  1162
        case SDL_MOUSEMOTION:
icculus@31
  1163
        	event12.type = SDL12_MOUSEMOTION;
icculus@31
  1164
            event12.motion.which = (Uint8) event20->motion.which;
icculus@31
  1165
            event12.motion.state = event20->motion.state;
icculus@31
  1166
            event12.motion.x = (Uint16) event20->motion.x;
icculus@31
  1167
            event12.motion.y = (Uint16) event20->motion.y;
icculus@31
  1168
            event12.motion.xrel = (Sint16) event20->motion.xrel;
icculus@31
  1169
            event12.motion.yrel = (Sint16) event20->motion.yrel;
icculus@18
  1170
            break;
icculus@18
  1171
icculus@18
  1172
        case SDL_MOUSEBUTTONDOWN:
icculus@31
  1173
        	event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@31
  1174
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1175
            event12.button.button = event20->button.button;
icculus@31
  1176
            event12.button.state = event20->button.state;
icculus@31
  1177
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1178
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1179
            break;
icculus@18
  1180
icculus@18
  1181
        case SDL_MOUSEBUTTONUP:
icculus@31
  1182
        	event12.type = SDL12_MOUSEBUTTONUP;
icculus@31
  1183
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1184
            event12.button.button = event20->button.button;
icculus@31
  1185
            event12.button.state = event20->button.state;
icculus@31
  1186
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1187
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1188
            break;
icculus@18
  1189
icculus@18
  1190
        case SDL_MOUSEWHEEL:
icculus@32
  1191
            if (event20->wheel.y == 0)
icculus@32
  1192
                break;  /* don't support horizontal wheels in 1.2. */
icculus@32
  1193
icculus@32
  1194
            event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@32
  1195
            event12.button.which = (Uint8) event20->wheel.which;
icculus@32
  1196
            event12.button.button = (event20->wheel.y > 0) ? 4 : 5;  /* wheelup is 4, down is 5. */
icculus@32
  1197
            event12.button.state = SDL_GetMouseState(&x, &y);
icculus@32
  1198
            event12.button.x = (Uint16) x;
icculus@32
  1199
            event12.button.y = (Uint16) y;
icculus@32
  1200
            PushEventIfNotFiltered(&event12);
icculus@32
  1201
icculus@32
  1202
            event12.type = SDL12_MOUSEBUTTONUP;  /* immediately release mouse "button" at the end of this switch. */
icculus@18
  1203
            break;
icculus@18
  1204
icculus@18
  1205
        case SDL_JOYAXISMOTION:
icculus@31
  1206
            event12.type = SDL12_JOYAXISMOTION;
icculus@31
  1207
            event12.jaxis.which = (Uint8) event20->jaxis.which;
icculus@31
  1208
            event12.jaxis.axis = event20->jaxis.axis;
icculus@31
  1209
            event12.jaxis.value = event20->jaxis.value;
icculus@18
  1210
            break;
icculus@18
  1211
icculus@18
  1212
        case SDL_JOYBALLMOTION:
icculus@31
  1213
            event12.type = SDL12_JOYBALLMOTION;
icculus@31
  1214
            event12.jball.which = (Uint8) event20->jball.which;
icculus@31
  1215
            event12.jball.ball = event20->jball.ball;
icculus@31
  1216
            event12.jball.xrel = event20->jball.xrel;
icculus@31
  1217
            event12.jball.yrel = event20->jball.yrel;
icculus@18
  1218
            break;
icculus@18
  1219
icculus@18
  1220
        case SDL_JOYHATMOTION:
icculus@31
  1221
            event12.type = SDL12_JOYHATMOTION;
icculus@31
  1222
            event12.jhat.which = (Uint8) event20->jhat.which;
icculus@31
  1223
            event12.jhat.hat = event20->jhat.hat;
icculus@31
  1224
            event12.jhat.value = event20->jhat.value;
icculus@18
  1225
            break;
icculus@18
  1226
icculus@18
  1227
        case SDL_JOYBUTTONDOWN:
icculus@31
  1228
            event12.type = SDL12_JOYBUTTONDOWN;
icculus@31
  1229
            event12.jbutton.which = (Uint8) event20->jbutton.which;
icculus@31
  1230
            event12.jbutton.button = event20->jbutton.button;
icculus@31
  1231
            event12.jbutton.state = event20->jbutton.state;
icculus@18
  1232
            break;
icculus@18
  1233
icculus@18
  1234
        case SDL_JOYBUTTONUP:
icculus@31
  1235
            event12.type = SDL12_JOYBUTTONUP;
icculus@31
  1236
            event12.jbutton.which = (Uint8) event20->jbutton.which;
icculus@31
  1237
            event12.jbutton.button = event20->jbutton.button;
icculus@31
  1238
            event12.jbutton.state = event20->jbutton.state;
icculus@18
  1239
            break;
icculus@18
  1240
icculus@18
  1241
        //case SDL_JOYDEVICEADDED:
icculus@18
  1242
        //case SDL_JOYDEVICEREMOVED:
icculus@18
  1243
	    //case SDL_CONTROLLERAXISMOTION:
icculus@18
  1244
	    //case SDL_CONTROLLERBUTTONDOWN:
icculus@18
  1245
	    //case SDL_CONTROLLERBUTTONUP:
icculus@18
  1246
	    //case SDL_CONTROLLERDEVICEADDED:
icculus@18
  1247
	    //case SDL_CONTROLLERDEVICEREMOVED:
icculus@18
  1248
	    //case SDL_CONTROLLERDEVICEREMAPPED:
icculus@18
  1249
        //case SDL_FINGERDOWN:
icculus@18
  1250
        //case SDL_FINGERUP:
icculus@18
  1251
        //case SDL_FINGERMOTION:
icculus@18
  1252
        //case SDL_DOLLARGESTURE:
icculus@18
  1253
        //case SDL_DOLLARRECORD:
icculus@18
  1254
        //case SDL_MULTIGESTURE:
icculus@18
  1255
        //case SDL_CLIPBOARDUPDATE:
icculus@18
  1256
        //case SDL_DROPFILE:
icculus@18
  1257
icculus@18
  1258
        default:
icculus@18
  1259
            return 0;  /* drop everything else. */
icculus@18
  1260
    }
icculus@18
  1261
icculus@32
  1262
    PushEventIfNotFiltered(&event12);
icculus@18
  1263
icculus@18
  1264
    return 0;  /* always drop it from the 2.0 event queue. */
icculus@18
  1265
}
icculus@18
  1266
icculus@35
  1267
DECLSPEC void SDLCALL
icculus@18
  1268
SDL_SetEventFilter(SDL12_EventFilter filter12)
icculus@18
  1269
{
icculus@18
  1270
    /* We always have a filter installed, but will call the app's too. */
icculus@18
  1271
    EventFilter12 = filter12;
icculus@18
  1272
}
icculus@18
  1273
icculus@35
  1274
DECLSPEC SDL12_EventFilter SDLCALL
icculus@18
  1275
SDL_GetEventFilter(void)
icculus@18
  1276
{
icculus@18
  1277
    return EventFilter12;
icculus@18
  1278
}
icculus@18
  1279
icculus@15
  1280
icculus@73
  1281
static SDL12_Rect *
icculus@73
  1282
Rect20to12(const SDL_Rect *rect20, SDL12_Rect *rect12)
icculus@73
  1283
{
icculus@73
  1284
    rect12->x = (Sint16) rect20->x;
icculus@73
  1285
    rect12->y = (Sint16) rect20->y;
icculus@73
  1286
    rect12->w = (Uint16) rect20->w;
icculus@73
  1287
    rect12->h = (Uint16) rect20->h;
icculus@73
  1288
    return rect12;
icculus@73
  1289
}
icculus@73
  1290
icculus@73
  1291
static SDL_Rect *
icculus@73
  1292
Rect12to20(const SDL12_Rect *rect12, SDL_Rect *rect20)
icculus@73
  1293
{
icculus@73
  1294
    rect20->x = (int) rect12->x;
icculus@73
  1295
    rect20->y = (int) rect12->y;
icculus@73
  1296
    rect20->w = (int) rect12->w;
icculus@73
  1297
    rect20->h = (int) rect12->h;
icculus@73
  1298
    return rect20;
icculus@73
  1299
}
icculus@73
  1300
icculus@34
  1301
static SDL12_Surface *
icculus@15
  1302
Surface20to12(SDL_Surface *surface20)
icculus@15
  1303
{
icculus@15
  1304
    SDL12_Surface *surface12 = NULL;
icculus@15
  1305
    SDL12_Palette *palette12 = NULL;
icculus@15
  1306
    SDL12_PixelFormat *format12 = NULL;
icculus@31
  1307
    Uint32 flags = 0;
icculus@15
  1308
icculus@15
  1309
    if (!surface20)
icculus@15
  1310
        return NULL;
icculus@15
  1311
icculus@15
  1312
    surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
icculus@15
  1313
    if (!surface12)
icculus@15
  1314
        goto failed;
icculus@15
  1315
icculus@15
  1316
    palette12 = (SDL12_Palette *) SDL20_malloc(sizeof (SDL12_Palette));
icculus@15
  1317
    if (!palette12)
icculus@15
  1318
        goto failed;
icculus@15
  1319
icculus@15
  1320
    format12 = (SDL12_PixelFormat *) SDL20_malloc(sizeof (SDL12_PixelFormat));
icculus@15
  1321
    if (!format12)
icculus@15
  1322
        goto failed;
icculus@15
  1323
icculus@34
  1324
    SDL20_zerop(palette12);
icculus@31
  1325
    palette12->ncolors = surface20->format->palette->ncolors;
icculus@31
  1326
    palette12->colors = surface20->format->palette->colors;
icculus@15
  1327
icculus@34
  1328
    SDL20_zerop(format12);
icculus@15
  1329
    format12->palette = palette12;
icculus@15
  1330
    format12->BitsPerPixel = surface20->format->BitsPerPixel;
icculus@15
  1331
    format12->BytesPerPixel = surface20->format->BytesPerPixel;
icculus@15
  1332
    format12->Rloss = surface20->format->Rloss;
icculus@15
  1333
    format12->Gloss = surface20->format->Gloss;
icculus@15
  1334
    format12->Bloss = surface20->format->Bloss;
icculus@15
  1335
    format12->Aloss = surface20->format->Aloss;
icculus@15
  1336
    format12->Rshift = surface20->format->Rshift;
icculus@15
  1337
    format12->Gshift = surface20->format->Gshift;
icculus@15
  1338
    format12->Bshift = surface20->format->Bshift;
icculus@15
  1339
    format12->Ashift = surface20->format->Ashift;
icculus@15
  1340
    format12->Rmask = surface20->format->Rmask;
icculus@15
  1341
    format12->Gmask = surface20->format->Gmask;
icculus@15
  1342
    format12->Bmask = surface20->format->Bmask;
icculus@15
  1343
    format12->Amask = surface20->format->Amask;
icculus@54
  1344
    FIXME("format12->colorkey");
icculus@54
  1345
    FIXME("format12->alpha");
icculus@15
  1346
icculus@34
  1347
    SDL20_zerop(surface12);
icculus@15
  1348
    flags = surface20->flags;
icculus@66
  1349
    #ifdef SDL_SIMD_ALIGNED
icculus@66
  1350
    flags &= ~SDL_SIMD_ALIGNED;  /* we don't need to map this to 1.2 */
icculus@66
  1351
    #endif
icculus@15
  1352
    #define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
icculus@15
  1353
    MAPSURFACEFLAGS(PREALLOC);
icculus@15
  1354
    MAPSURFACEFLAGS(RLEACCEL);
icculus@34
  1355
    /*MAPSURFACEFLAGS(DONTFREE);*/
icculus@15
  1356
    #undef MAPSURFACEFLAGS
icculus@31
  1357
    SDL_assert(flags == 0);  /* non-zero if there's a flag we didn't map. */
icculus@15
  1358
icculus@15
  1359
    surface12->format = format12;
icculus@15
  1360
    surface12->w = surface20->w;
icculus@15
  1361
    surface12->h = surface20->h;
icculus@54
  1362
    surface12->pitch = (Uint16) surface20->pitch;  FIXME("make sure this fits in a Uint16");
icculus@15
  1363
    surface12->pixels = surface20->pixels;
icculus@15
  1364
    surface12->offset = 0;
icculus@34
  1365
    surface12->surface20 = surface20;
icculus@73
  1366
    Rect20to12(&surface20->clip_rect, &surface12->clip_rect);
icculus@15
  1367
    surface12->refcount = surface20->refcount;
icculus@15
  1368
icculus@15
  1369
    return surface12;
icculus@15
  1370
icculus@15
  1371
failed:
icculus@15
  1372
    SDL20_free(surface12);
icculus@15
  1373
    SDL20_free(palette12);
icculus@15
  1374
    SDL20_free(format12);
icculus@15
  1375
    return NULL;
icculus@15
  1376
}
icculus@15
  1377
icculus@35
  1378
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1379
SDL_CreateRGBSurface(Uint32 sdl12flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1380
{
icculus@15
  1381
    SDL_Surface *surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
icculus@15
  1382
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1383
    if (!surface12) {
icculus@15
  1384
        SDL20_FreeSurface(surface20);
icculus@15
  1385
        return NULL;
icculus@15
  1386
    }
icculus@15
  1387
icculus@31
  1388
    SDL_assert(surface12->flags == 0);  // shouldn't have prealloc, rleaccel, or dontfree.
icculus@15
  1389
    return surface12;
icculus@15
  1390
}
icculus@15
  1391
icculus@35
  1392
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1393
SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1394
{
icculus@31
  1395
    SDL_Surface *surface20 = SDL20_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
icculus@15
  1396
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1397
    if (!surface12) {
icculus@15
  1398
        SDL20_FreeSurface(surface20);
icculus@15
  1399
        return NULL;
icculus@15
  1400
    }
icculus@15
  1401
icculus@31
  1402
    SDL_assert(surface12->flags == SDL12_PREALLOC);  // should _only_ have prealloc.
icculus@15
  1403
    return surface12;
icculus@15
  1404
}
icculus@15
  1405
icculus@35
  1406
DECLSPEC void SDLCALL
icculus@35
  1407
SDL_FreeSurface(SDL12_Surface *surface12)
icculus@15
  1408
{
icculus@15
  1409
    if (surface12) {
icculus@34
  1410
        SDL20_FreeSurface(surface12->surface20);
icculus@15
  1411
        if (surface12->format) {
icculus@15
  1412
            SDL20_free(surface12->format->palette);
icculus@15
  1413
            SDL20_free(surface12->format);
icculus@15
  1414
        }
icculus@15
  1415
        SDL20_free(surface12);
icculus@15
  1416
    }
icculus@15
  1417
}
icculus@15
  1418
icculus@35
  1419
DECLSPEC void SDLCALL
icculus@73
  1420
SDL_GetClipRect(SDL12_Surface *surface12, SDL12_Rect *rect)
icculus@24
  1421
{
icculus@73
  1422
    if (surface12 && rect) {
icculus@73
  1423
        SDL_memcpy(rect, &surface12->clip_rect, sizeof (SDL12_Rect));
icculus@73
  1424
    }
icculus@24
  1425
}
icculus@24
  1426
icculus@35
  1427
DECLSPEC SDL_bool SDLCALL
icculus@73
  1428
SDL_SetClipRect(SDL12_Surface *surface12, const SDL12_Rect *rect12)
icculus@24
  1429
{
icculus@24
  1430
    SDL_bool retval = SDL_FALSE;
icculus@24
  1431
    if (surface12)
icculus@24
  1432
    {
icculus@73
  1433
        SDL_Rect rect20;
icculus@73
  1434
        retval = SDL20_SetClipRect(surface12->surface20, rect12 ? Rect12to20(rect12, &rect20) : NULL);
icculus@73
  1435
        SDL20_GetClipRect(surface12->surface20, &rect20);
icculus@73
  1436
        Rect20to12(&rect20, &surface12->clip_rect);
icculus@24
  1437
    }
icculus@24
  1438
    return retval;
icculus@24
  1439
}
icculus@24
  1440
icculus@35
  1441
DECLSPEC int SDLCALL
icculus@73
  1442
SDL_FillRect(SDL12_Surface *dst, SDL12_Rect *dstrect12, Uint32 color)
icculus@25
  1443
{
icculus@73
  1444
    SDL_Rect dstrect20;
icculus@73
  1445
    const int retval = SDL20_FillRect(dst->surface20, dstrect12 ? Rect12to20(dstrect12, &dstrect20) : NULL, color);
icculus@25
  1446
    if (retval != -1)
icculus@25
  1447
    {
icculus@73
  1448
        if (dstrect12) {  /* 1.2 stores the clip intersection in dstrect */
icculus@73
  1449
            SDL_Rect intersected20;
icculus@73
  1450
            SDL20_IntersectRect(&dstrect20, &dst->surface20->clip_rect, &intersected20);
icculus@73
  1451
            Rect20to12(&intersected20, dstrect12);
icculus@67
  1452
        }
icculus@25
  1453
    }
icculus@25
  1454
    return retval;
icculus@25
  1455
}
icculus@25
  1456
icculus@25
  1457
icculus@17
  1458
static SDL_PixelFormat *
icculus@31
  1459
PixelFormat12to20(SDL_PixelFormat *format20, SDL_Palette *palette20, const SDL12_PixelFormat *format12)
icculus@17
  1460
{
icculus@17
  1461
    palette20->ncolors = format12->palette->ncolors;
icculus@17
  1462
    palette20->colors = format12->palette->colors;
icculus@17
  1463
    palette20->version = 1;
icculus@17
  1464
    palette20->refcount = 1;
icculus@34
  1465
    format20->format = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@17
  1466
    format20->palette = palette20;
icculus@17
  1467
    format20->BitsPerPixel = format12->BitsPerPixel;
icculus@17
  1468
    format20->BytesPerPixel = format12->BytesPerPixel;
icculus@17
  1469
    format20->Rmask = format12->Rmask;
icculus@17
  1470
    format20->Gmask = format12->Gmask;
icculus@17
  1471
    format20->Bmask = format12->Bmask;
icculus@17
  1472
    format20->Amask = format12->Amask;
icculus@17
  1473
    format20->Rloss = format12->Rloss;
icculus@17
  1474
    format20->Gloss = format12->Gloss;
icculus@17
  1475
    format20->Bloss = format12->Bloss;
icculus@17
  1476
    format20->Aloss = format12->Aloss;
icculus@17
  1477
    format20->Rshift = format12->Rshift;
icculus@17
  1478
    format20->Gshift = format12->Gshift;
icculus@17
  1479
    format20->Bshift = format12->Bshift;
icculus@17
  1480
    format20->Ashift = format12->Ashift;
icculus@17
  1481
    format20->refcount = 1;
icculus@17
  1482
    format20->next = NULL;
icculus@17
  1483
    return format20;
icculus@17
  1484
}
icculus@17
  1485
icculus@35
  1486
DECLSPEC Uint32 SDLCALL
icculus@17
  1487
SDL_MapRGB(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b)
icculus@17
  1488
{
icculus@17
  1489
    /* This is probably way slower than apps expect. */
icculus@17
  1490
    SDL_PixelFormat format20;
icculus@17
  1491
    SDL_Palette palette20;
icculus@17
  1492
    return SDL20_MapRGB(PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1493
}
icculus@17
  1494
icculus@35
  1495
DECLSPEC Uint32 SDLCALL
icculus@17
  1496
SDL_MapRGBA(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
icculus@17
  1497
{
icculus@17
  1498
    /* This is probably way slower than apps expect. */
icculus@17
  1499
    SDL_PixelFormat format20;
icculus@17
  1500
    SDL_Palette palette20;
icculus@17
  1501
    return SDL20_MapRGBA(PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1502
}
icculus@17
  1503
icculus@35
  1504
DECLSPEC void SDLCALL
icculus@17
  1505
SDL_GetRGB(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b)
icculus@17
  1506
{
icculus@17
  1507
    /* This is probably way slower than apps expect. */
icculus@17
  1508
    SDL_PixelFormat format20;
icculus@17
  1509
    SDL_Palette palette20;
icculus@17
  1510
    return SDL20_GetRGB(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1511
}
icculus@17
  1512
icculus@35
  1513
DECLSPEC void SDLCALL
icculus@17
  1514
SDL_GetRGBA(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
icculus@17
  1515
{
icculus@17
  1516
    /* This is probably way slower than apps expect. */
icculus@17
  1517
    SDL_PixelFormat format20;
icculus@17
  1518
    SDL_Palette palette20;
icculus@31
  1519
    return SDL20_GetRGBA(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1520
}
icculus@15
  1521
icculus@35
  1522
DECLSPEC const SDL12_VideoInfo * SDLCALL
icculus@9
  1523
SDL_GetVideoInfo(void)
icculus@9
  1524
{
icculus@9
  1525
    SDL_DisplayMode mode;
icculus@9
  1526
icculus@56
  1527
    FIXME("calculate this in Init12Video(), then this just does: return VideoInfo.vfmt ? &VideoInfo : NULL;");
icculus@39
  1528
icculus@56
  1529
    if (!VideoInfo12.vfmt && SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode) == 0) {
icculus@56
  1530
        VideoInfo12.vfmt = SDL20_AllocFormat(mode.format);
icculus@56
  1531
        VideoInfo12.current_w = mode.w;
icculus@56
  1532
        VideoInfo12.current_h = mode.h;
icculus@56
  1533
        FIXME("vidinfo details commented out");
icculus@56
  1534
        //VideoInfo12.wm_available = 1;
icculus@56
  1535
        //VideoInfo12.video_mem = 1024 * 256;
icculus@9
  1536
    }
icculus@56
  1537
    return &VideoInfo12;
icculus@9
  1538
}
icculus@9
  1539
icculus@35
  1540
DECLSPEC int SDLCALL
icculus@9
  1541
SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
icculus@9
  1542
{
icculus@9
  1543
    int i, nummodes, actual_bpp = 0;
icculus@9
  1544
icculus@9
  1545
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1546
        return 0;
icculus@9
  1547
    }
icculus@9
  1548
icculus@9
  1549
    if (!(sdl12flags & SDL12_FULLSCREEN)) {
icculus@9
  1550
        SDL_DisplayMode mode;
icculus@16
  1551
        SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode);
icculus@9
  1552
        return SDL_BITSPERPIXEL(mode.format);
icculus@9
  1553
    }
icculus@9
  1554
icculus@16
  1555
    nummodes = SDL20_GetNumDisplayModes(VideoDisplayIndex);
icculus@9
  1556
    for (i = 0; i < nummodes; ++i) {
icculus@9
  1557
        SDL_DisplayMode mode;
icculus@16
  1558
        SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode);
icculus@9
  1559
        if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
icculus@9
  1560
            if (!mode.format) {
icculus@9
  1561
                return bpp;
icculus@9
  1562
            }
icculus@9
  1563
            if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
icculus@9
  1564
                actual_bpp = SDL_BITSPERPIXEL(mode.format);
icculus@9
  1565
            }
icculus@9
  1566
        }
icculus@9
  1567
    }
icculus@9
  1568
    return actual_bpp;
icculus@9
  1569
}
icculus@9
  1570
icculus@73
  1571
DECLSPEC SDL12_Rect ** SDLCALL
icculus@39
  1572
SDL_ListModes(const SDL12_PixelFormat *format12, Uint32 flags)
icculus@9
  1573
{
icculus@39
  1574
    Uint32 fmt;
icculus@39
  1575
    int i;
icculus@9
  1576
icculus@9
  1577
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1578
        return NULL;
icculus@9
  1579
    }
icculus@9
  1580
icculus@56
  1581
    if ((!format12) && (!VideoInfo12.vfmt)) {
icculus@39
  1582
        SDL20_SetError("No pixel format specified");
icculus@39
  1583
        return NULL;
icculus@39
  1584
    }
icculus@39
  1585
icculus@9
  1586
    if (!(flags & SDL12_FULLSCREEN)) {
icculus@73
  1587
        return (SDL12_Rect **) (-1);  /* any resolution is fine. */
icculus@9
  1588
    }
icculus@9
  1589
icculus@39
  1590
    if (format12) {
icculus@39
  1591
        fmt = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@39
  1592
    } else {
icculus@56
  1593
        fmt = VideoInfo12.vfmt->format;
icculus@9
  1594
    }
icculus@9
  1595
icculus@39
  1596
    for (i = 0; i < VideoModesCount; i++) {
icculus@39
  1597
        VideoModeList *modes = &VideoModes[i];
icculus@39
  1598
        if (modes->format == fmt) {
icculus@73
  1599
            return modes->modes12;
icculus@9
  1600
        }
icculus@9
  1601
    }
icculus@39
  1602
icculus@39
  1603
    SDL20_SetError("No modes support requested pixel format");
icculus@39
  1604
    return NULL;
icculus@9
  1605
}
icculus@9
  1606
icculus@35
  1607
DECLSPEC void SDLCALL
icculus@31
  1608
SDL_FreeCursor(SDL12_Cursor *cursor12)
icculus@31
  1609
{
icculus@51
  1610
    if (cursor12) {
icculus@31
  1611
        if (cursor12->wm_cursor)
icculus@31
  1612
            SDL20_FreeCursor(cursor12->wm_cursor);
icculus@31
  1613
        SDL20_free(cursor12->data);
icculus@31
  1614
        SDL20_free(cursor12->mask);
icculus@31
  1615
        SDL20_free(cursor12);
icculus@31
  1616
    }
icculus@31
  1617
}
icculus@23
  1618
icculus@35
  1619
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1620
SDL_CreateCursor(Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
icculus@23
  1621
{
icculus@23
  1622
    const size_t datasize = h * (w / 8);
icculus@23
  1623
    SDL_Cursor *cursor20 = NULL;
icculus@23
  1624
    SDL12_Cursor *retval = NULL;
icculus@23
  1625
icculus@23
  1626
    retval = (SDL12_Cursor *) SDL20_malloc(sizeof (SDL12_Cursor));
icculus@23
  1627
    if (!retval)
icculus@23
  1628
        goto outofmem;
icculus@23
  1629
icculus@34
  1630
    SDL20_zerop(retval);
icculus@23
  1631
icculus@23
  1632
    retval->data = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1633
    if (!retval->data)
icculus@23
  1634
        goto outofmem;
icculus@23
  1635
icculus@23
  1636
    retval->mask = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1637
    if (!retval->mask)
icculus@23
  1638
        goto outofmem;
icculus@23
  1639
icculus@23
  1640
    cursor20 = SDL20_CreateCursor(data, mask, w, h, hot_x, hot_y);
icculus@23
  1641
    if (!cursor20)
icculus@23
  1642
        goto failed;
icculus@23
  1643
icculus@23
  1644
    retval->area.w = w;
icculus@23
  1645
    retval->area.h = h;
icculus@23
  1646
    retval->hot_x = hot_x;
icculus@23
  1647
    retval->hot_y = hot_y;
icculus@23
  1648
    retval->wm_cursor = cursor20;
icculus@23
  1649
    /* we always leave retval->save as null pointers. */
icculus@23
  1650
icculus@23
  1651
    SDL20_memcpy(retval->data, data, datasize);
icculus@23
  1652
    SDL20_memcpy(retval->mask, mask, datasize);
icculus@23
  1653
icculus@23
  1654
    return retval;
icculus@23
  1655
icculus@23
  1656
outofmem:
icculus@31
  1657
    SDL20_OutOfMemory();
icculus@23
  1658
icculus@23
  1659
failed:
icculus@23
  1660
    SDL_FreeCursor(retval);
icculus@23
  1661
    return NULL;
icculus@23
  1662
}
icculus@23
  1663
icculus@35
  1664
DECLSPEC void SDLCALL
icculus@23
  1665
SDL_SetCursor(SDL12_Cursor *cursor)
icculus@23
  1666
{
icculus@56
  1667
    CurrentCursor12 = cursor;
icculus@23
  1668
    SDL20_SetCursor(cursor ? cursor->wm_cursor : NULL);
icculus@23
  1669
}
icculus@23
  1670
icculus@35
  1671
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1672
SDL_GetCursor(void)
icculus@23
  1673
{
icculus@56
  1674
    return CurrentCursor12;
icculus@23
  1675
}
icculus@23
  1676
icculus@9
  1677
static void
icculus@59
  1678
GetEnvironmentWindowPosition(int *x, int *y)
icculus@9
  1679
{
icculus@16
  1680
    int display = VideoDisplayIndex;
icculus@58
  1681
    const char *window = SDL20_getenv("SDL_VIDEO_WINDOW_POS");
icculus@58
  1682
    const char *center = SDL20_getenv("SDL_VIDEO_CENTERED");
icculus@9
  1683
    if (window) {
icculus@58
  1684
        if (SDL20_strcmp(window, "center") == 0) {
icculus@58
  1685
            center = window;
icculus@58
  1686
        } else if (SDL20_sscanf(window, "%d,%d", x, y) == 2) {
icculus@9
  1687
            return;
icculus@9
  1688
        }
icculus@9
  1689
    }
icculus@58
  1690
icculus@9
  1691
    if (center) {
icculus@9
  1692
        *x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1693
        *y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1694
    }
icculus@9
  1695
}
icculus@9
  1696
icculus@9
  1697
static void
icculus@49
  1698
SetupScreenSaver(const int flags12)
icculus@9
  1699
{
icculus@9
  1700
    const char *env;
icculus@9
  1701
    SDL_bool allow_screensaver;
icculus@9
  1702
icculus@9
  1703
    /* Allow environment override of screensaver disable */
icculus@58
  1704
    env = SDL20_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
icculus@9
  1705
    if (env) {
icculus@34
  1706
        allow_screensaver = SDL20_atoi(env) ? SDL_TRUE : SDL_FALSE;
icculus@19
  1707
    } else if (flags12 & SDL12_FULLSCREEN) {
icculus@9
  1708
        allow_screensaver = SDL_FALSE;
icculus@9
  1709
    } else {
icculus@9
  1710
        allow_screensaver = SDL_TRUE;
icculus@9
  1711
    }
icculus@9
  1712
    if (allow_screensaver) {
icculus@17
  1713
        SDL20_EnableScreenSaver();
icculus@9
  1714
    } else {
icculus@17
  1715
        SDL20_DisableScreenSaver();
icculus@9
  1716
    }
icculus@9
  1717
}
icculus@9
  1718
icculus@9
  1719
icculus@59
  1720
static SDL12_Surface *
icculus@59
  1721
EndVidModeCreate(void)
icculus@59
  1722
{
icculus@59
  1723
    if (VideoTexture20) {
icculus@59
  1724
        SDL20_DestroyTexture(VideoTexture20);
icculus@59
  1725
        VideoTexture20 = NULL;
icculus@59
  1726
    }
icculus@59
  1727
    if (VideoRenderer20) {
icculus@59
  1728
        SDL20_DestroyRenderer(VideoRenderer20);
icculus@59
  1729
        VideoRenderer20 = NULL;
icculus@59
  1730
    }
icculus@59
  1731
    if (VideoGLContext20) {
icculus@59
  1732
        SDL20_GL_MakeCurrent(NULL, NULL);
icculus@59
  1733
        SDL20_GL_DeleteContext(VideoGLContext20);
icculus@59
  1734
        VideoGLContext20 = NULL;
icculus@59
  1735
    }
icculus@59
  1736
    if (VideoWindow20) {
icculus@59
  1737
        SDL20_DestroyWindow(VideoWindow20);
icculus@59
  1738
        VideoWindow20 = NULL;
icculus@59
  1739
    }
icculus@59
  1740
    if (VideoSurface12) {
icculus@59
  1741
        SDL20_free(VideoSurface12->pixels);
icculus@59
  1742
        VideoSurface12->pixels = NULL;
icculus@59
  1743
        SDL_FreeSurface(VideoSurface12);
icculus@59
  1744
        VideoSurface12 = NULL;
icculus@59
  1745
    }
icculus@59
  1746
    if (VideoConvertSurface20) {
icculus@59
  1747
        SDL20_FreeSurface(VideoConvertSurface20);
icculus@59
  1748
        VideoConvertSurface20 = NULL;
icculus@59
  1749
    }
icculus@59
  1750
    return NULL;
icculus@59
  1751
}
icculus@59
  1752
icculus@59
  1753
icculus@59
  1754
static SDL12_Surface *
icculus@59
  1755
CreateSurface12WithFormat(const int w, const int h, const Uint32 fmt)
icculus@59
  1756
{
icculus@59
  1757
    Uint32 rmask, gmask, bmask, amask;
icculus@59
  1758
    int bpp;
icculus@59
  1759
    if (!SDL20_PixelFormatEnumToMasks(fmt, &bpp, &rmask, &gmask, &bmask, &amask)) {
icculus@59
  1760
        return NULL;
icculus@59
  1761
    }
icculus@59
  1762
    return SDL_CreateRGBSurface(0, w, h, bpp, rmask, gmask, bmask, amask);
icculus@59
  1763
}
icculus@59
  1764
icculus@59
  1765
static SDL_Surface *
icculus@59
  1766
CreateNullPixelSurface20(const int width, const int height, const Uint32 fmt)
icculus@59
  1767
{
icculus@59
  1768
    SDL_Surface *surface20 = SDL20_CreateRGBSurfaceWithFormat(0, 0, 0, SDL_BITSPERPIXEL(fmt), fmt);
icculus@59
  1769
    if (surface20) {
icculus@59
  1770
        surface20->flags |= SDL_PREALLOC;
icculus@59
  1771
        surface20->pixels = NULL;
icculus@59
  1772
        surface20->w = width;
icculus@59
  1773
        surface20->h = height;
icculus@59
  1774
        surface20->pitch = 0;
icculus@59
  1775
        SDL20_SetClipRect(surface20, NULL);
icculus@59
  1776
    }
icculus@59
  1777
    return surface20;
icculus@59
  1778
}
icculus@59
  1779
icculus@59
  1780
icculus@35
  1781
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1782
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
icculus@9
  1783
{
icculus@59
  1784
    FIXME("currently ignores SDL_WINDOWID, which we could use with SDL_CreateWindowFrom ...?");
icculus@59
  1785
    SDL_DisplayMode dmode;
icculus@59
  1786
    Uint32 fullscreen_flags20 = 0;
icculus@59
  1787
    Uint32 appfmt;
icculus@59
  1788
icculus@59
  1789
    /* SDL_SetVideoMode() implicitly inits if necessary. */
icculus@59
  1790
    if (SDL20_WasInit(SDL_INIT_VIDEO) == 0) {
icculus@59
  1791
        if (SDL20_Init(SDL_INIT_VIDEO) < 0) {
icculus@59
  1792
            return NULL;
icculus@59
  1793
        }
icculus@59
  1794
    }
icculus@59
  1795
icculus@59
  1796
    if ((flags12 & SDL12_OPENGLBLIT) == SDL12_OPENGLBLIT) {
icculus@59
  1797
        FIXME("No OPENGLBLIT support at the moment");
icculus@59
  1798
        SDL20_SetError("SDL_OPENGLBLIT is (currently) unsupported");
icculus@59
  1799
        return NULL;
icculus@59
  1800
    }
icculus@59
  1801
icculus@59
  1802
    FIXME("handle SDL_ANYFORMAT");
icculus@59
  1803
icculus@59
  1804
    if ((width < 0) || (height < 0)) {
icculus@59
  1805
        SDL20_SetError("Invalid width or height");
icculus@59
  1806
        return NULL;
icculus@59
  1807
    }
icculus@59
  1808
icculus@59
  1809
    FIXME("There's an environment variable to choose a display");
icculus@59
  1810
    if (SDL20_GetCurrentDisplayMode(0, &dmode) < 0) {
icculus@59
  1811
        return NULL;
icculus@59
  1812
    }
icculus@59
  1813
icculus@59
  1814
    if (width == 0) {
icculus@59
  1815
        width = dmode.w;
icculus@59
  1816
    }
icculus@59
  1817
icculus@59
  1818
    if (height == 0) {
icculus@59
  1819
        height = dmode.h;
icculus@59
  1820
    }
icculus@59
  1821
icculus@59
  1822
    if (bpp == 0) {
icculus@59
  1823
        bpp = SDL_BITSPERPIXEL(dmode.format);
icculus@59
  1824
    }
icculus@59
  1825
icculus@59
  1826
    switch (bpp) {
icculus@59
  1827
        case  8: appfmt = SDL_PIXELFORMAT_INDEX8; break;
icculus@59
  1828
        case 16: appfmt = SDL_PIXELFORMAT_RGB565; FIXME("bgr instead of rgb?"); break;
icculus@59
  1829
        case 24: appfmt = SDL_PIXELFORMAT_RGB24; FIXME("bgr instead of rgb?"); break;
icculus@59
  1830
        case 32: appfmt = SDL_PIXELFORMAT_ARGB8888; FIXME("bgr instead of rgb?"); break;
icculus@59
  1831
        default: SDL20_SetError("Unsupported bits-per-pixel"); return NULL;
icculus@59
  1832
    }
icculus@59
  1833
icculus@59
  1834
    SDL_assert((VideoSurface12 != NULL) == (VideoWindow20 != NULL));
icculus@59
  1835
icculus@59
  1836
    FIXME("don't do anything if the window's dimensions, etc haven't changed.");
icculus@59
  1837
    FIXME("we need to preserve VideoSurface12 (but not its pixels), I think...");
icculus@59
  1838
icculus@59
  1839
    if ( VideoSurface12 && ((VideoSurface12->flags & SDL12_OPENGL) != (flags12 & SDL12_OPENGL)) ) {
icculus@59
  1840
        EndVidModeCreate();  /* rebuild the window if moving to/from a GL context */
icculus@59
  1841
    } else if ( VideoSurface12 && (VideoSurface12->surface20->format->format != appfmt)) {
icculus@59
  1842
        EndVidModeCreate();  /* rebuild the window if changing pixel format */
icculus@59
  1843
    } else if (VideoGLContext20) {
icculus@59
  1844
        /* SDL 1.2 (infuriatingly!) destroys the GL context on each resize, so we will too */
icculus@59
  1845
        SDL20_GL_MakeCurrent(NULL, NULL);
icculus@59
  1846
        SDL20_GL_DeleteContext(VideoGLContext20);
icculus@59
  1847
        VideoGLContext20 = NULL;
icculus@59
  1848
    }
icculus@59
  1849
icculus@59
  1850
    if (flags12 & SDL12_FULLSCREEN) {
icculus@59
  1851
        // OpenGL tries to force the real resolution requested, but for
icculus@59
  1852
        //  software rendering, we're just going to push it off onto the
icculus@59
  1853
        //  GPU, so use FULLSCREEN_DESKTOP and logical scaling there.
icculus@59
  1854
        FIXME("OpenGL will still expect letterboxing and centering if it didn't get an exact resolution match.");
icculus@59
  1855
        if (flags12 & SDL12_OPENGL) {
icculus@59
  1856
            fullscreen_flags20 |= SDL_WINDOW_FULLSCREEN;
icculus@59
  1857
        } else {
icculus@59
  1858
            fullscreen_flags20 |= SDL_WINDOW_FULLSCREEN_DESKTOP;
icculus@59
  1859
        }
icculus@59
  1860
    }
icculus@59
  1861
icculus@59
  1862
    if (!VideoWindow20) {  /* create it */
icculus@59
  1863
        int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
icculus@59
  1864
        Uint32 flags20 = fullscreen_flags20;
icculus@59
  1865
        if (flags12 & SDL12_OPENGL) { flags20 |= SDL_WINDOW_OPENGL; }
icculus@59
  1866
        if (flags12 & SDL12_RESIZABLE) { flags20 |= SDL_WINDOW_RESIZABLE; }
icculus@59
  1867
        if (flags12 & SDL12_NOFRAME) { flags20 |= SDL_WINDOW_BORDERLESS; }
icculus@59
  1868
icculus@59
  1869
        /* most platforms didn't check these environment variables, but the major
icculus@59
  1870
           ones did (x11, windib, quartz), so we'll just offer it everywhere. */
icculus@59
  1871
        GetEnvironmentWindowPosition(&x, &y);
icculus@59
  1872
icculus@59
  1873
        VideoWindow20 = SDL20_CreateWindow(WindowTitle, x, y, width, height, flags20);
icculus@59
  1874
        if (!VideoWindow20) {
icculus@59
  1875
            return EndVidModeCreate();
icculus@59
  1876
        }
icculus@59
  1877
    } else {  /* resize it */
icculus@59
  1878
        SDL20_SetWindowSize(VideoWindow20, width, height);
icculus@59
  1879
        SDL20_SetWindowFullscreen(VideoWindow20, fullscreen_flags20);
icculus@59
  1880
        SDL20_SetWindowBordered(VideoWindow20, (flags12 & SDL12_NOFRAME) ? SDL_FALSE : SDL_TRUE);
icculus@59
  1881
        SDL20_SetWindowResizable(VideoWindow20, (flags12 & SDL12_RESIZABLE) ? SDL_TRUE : SDL_FALSE);
icculus@59
  1882
    }
icculus@59
  1883
icculus@59
  1884
    if (VideoSurface12) {
icculus@59
  1885
        SDL20_free(VideoSurface12->pixels);
icculus@59
  1886
    } else {
icculus@59
  1887
        VideoSurface12 = CreateSurface12WithFormat(0, 0, appfmt);
icculus@59
  1888
        if (!VideoSurface12) {
icculus@59
  1889
            return EndVidModeCreate();
icculus@59
  1890
        }
icculus@59
  1891
    }
icculus@59
  1892
icculus@59
  1893
    VideoSurface12->surface20->flags |= SDL_PREALLOC;
icculus@59
  1894
    VideoSurface12->flags |= SDL12_PREALLOC;
icculus@59
  1895
    VideoSurface12->pixels = VideoSurface12->surface20->pixels = NULL;
icculus@59
  1896
    VideoSurface12->w = VideoSurface12->surface20->w = width;
icculus@59
  1897
    VideoSurface12->h = VideoSurface12->surface20->h = height;
icculus@59
  1898
    VideoSurface12->pitch = VideoSurface12->surface20->pitch = width * SDL_BYTESPERPIXEL(appfmt);
icculus@59
  1899
    SDL_SetClipRect(VideoSurface12, NULL);
icculus@59
  1900
icculus@59
  1901
    if (flags12 & SDL12_OPENGL) {
icculus@59
  1902
        SDL_assert(!VideoTexture20);  /* either a new window or we destroyed all this */
icculus@59
  1903
        SDL_assert(!VideoRenderer20);
icculus@59
  1904
        VideoGLContext20 = SDL20_GL_CreateContext(VideoWindow20);
icculus@59
  1905
        if (!VideoGLContext20) {
icculus@59
  1906
            return EndVidModeCreate();
icculus@59
  1907
        }
icculus@59
  1908
icculus@59
  1909
        VideoSurface12->flags |= SDL12_OPENGL;
icculus@59
  1910
    } else {
icculus@59
  1911
        /* always use a renderer for non-OpenGL windows. */
icculus@59
  1912
        SDL_RendererInfo rinfo;
icculus@59
  1913
        SDL_assert(!VideoGLContext20);  /* either a new window or we destroyed all this */
icculus@59
  1914
        if (!VideoRenderer20) {
icculus@68
  1915
            VideoRenderer20 = SDL20_CreateRenderer(VideoWindow20, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
icculus@68
  1916
        }
icculus@68
  1917
        if (!VideoRenderer20) {
icculus@68
  1918
            VideoRenderer20 = SDL20_CreateRenderer(VideoWindow20, -1, SDL_RENDERER_PRESENTVSYNC);
icculus@68
  1919
        }
icculus@68
  1920
        if (!VideoRenderer20) {
icculus@59
  1921
            VideoRenderer20 = SDL20_CreateRenderer(VideoWindow20, -1, 0);
icculus@68
  1922
        }
icculus@68
  1923
        if (!VideoRenderer20) {
icculus@68
  1924
            return EndVidModeCreate();
icculus@59
  1925
        }
icculus@59
  1926
icculus@59
  1927
        SDL20_RenderSetLogicalSize(VideoRenderer20, width, height);
icculus@59
  1928
        SDL20_SetRenderDrawColor(VideoRenderer20, 0, 0, 0, 255);
icculus@59
  1929
        SDL20_RenderClear(VideoRenderer20);
icculus@59
  1930
        SDL20_RenderPresent(VideoRenderer20);
icculus@59
  1931
        SDL20_SetRenderDrawColor(VideoRenderer20, 255, 255, 255, 255);
icculus@59
  1932
icculus@59
  1933
        if (SDL20_GetRendererInfo(VideoRenderer20, &rinfo) < 0) {
icculus@59
  1934
            return EndVidModeCreate();
icculus@59
  1935
        }
icculus@59
  1936
icculus@59
  1937
        if (VideoTexture20) {
icculus@59
  1938
            SDL20_DestroyTexture(VideoTexture20);
icculus@59
  1939
        }
icculus@59
  1940
icculus@59
  1941
        if (VideoConvertSurface20) {
icculus@59
  1942
            SDL20_FreeSurface(VideoConvertSurface20);
icculus@59
  1943
            VideoConvertSurface20 = NULL;
icculus@59
  1944
        }
icculus@59
  1945
icculus@59
  1946
        VideoTexture20 = SDL20_CreateTexture(VideoRenderer20, rinfo.texture_formats[0], SDL_TEXTUREACCESS_STREAMING, width, height);
icculus@59
  1947
        if (!VideoTexture20) {
icculus@59
  1948
            return EndVidModeCreate();
icculus@59
  1949
        }
icculus@59
  1950
icculus@59
  1951
        if (rinfo.texture_formats[0] != appfmt) {
icculus@59
  1952
            /* need to convert between app's format and texture format */
icculus@59
  1953
            VideoConvertSurface20 = CreateNullPixelSurface20(width, height, rinfo.texture_formats[0]);
icculus@59
  1954
            if (!VideoConvertSurface20) {
icculus@59
  1955
                return EndVidModeCreate();
icculus@59
  1956
            }
icculus@59
  1957
        }
icculus@59
  1958
icculus@59
  1959
        VideoSurface12->flags &= ~SDL12_OPENGL;
icculus@71
  1960
        VideoSurface12->flags |= SDL12_DOUBLEBUF;
icculus@59
  1961
        VideoSurface12->surface20->pixels = SDL20_malloc(height * VideoSurface12->pitch);
icculus@59
  1962
        VideoSurface12->pixels = VideoSurface12->surface20->pixels;
icculus@59
  1963
        if (!VideoSurface12->pixels) {
icculus@59
  1964
            SDL20_OutOfMemory();
icculus@59
  1965
            return EndVidModeCreate();
icculus@59
  1966
        }
icculus@69
  1967
icculus@69
  1968
        /* fill in the same default palette that SDL 1.2 does... */
icculus@69
  1969
        if (VideoSurface12->format->BitsPerPixel == 8) {
icculus@69
  1970
            int i;
icculus@69
  1971
            SDL_Color *color = VideoSurface12->format->palette->colors;
icculus@69
  1972
            for (i = 0; i < 256; i++, color++) {
icculus@72
  1973
                { const int x = i & 0xe0; color->r = x | x >> 3 | x >> 6; }
icculus@72
  1974
                { const int x = (i << 3) & 0xe0; color->g = x | x >> 3 | x >> 6; }
icculus@72
  1975
                { const int x = (i & 0x3) | ((i & 0x3) << 2); color->b = x | x << 4; }
icculus@69
  1976
                color->a = 255;
icculus@69
  1977
            }
icculus@69
  1978
        }
icculus@59
  1979
    }
icculus@59
  1980
icculus@59
  1981
    FIXME("setup screen saver");
icculus@59
  1982
icculus@59
  1983
    return VideoSurface12;
icculus@9
  1984
}
icculus@9
  1985
icculus@35
  1986
DECLSPEC SDL12_Surface * SDLCALL
icculus@9
  1987
SDL_GetVideoSurface(void)
icculus@9
  1988
{
icculus@56
  1989
    return VideoSurface12;
icculus@9
  1990
}
icculus@9
  1991
icculus@35
  1992
DECLSPEC int SDLCALL
icculus@73
  1993
SDL_UpperBlit(SDL12_Surface *src, SDL12_Rect *srcrect12, SDL12_Surface *dst, SDL12_Rect *dstrect12)
icculus@64
  1994
{
icculus@73
  1995
    SDL_Rect srcrect20, dstrect20;
icculus@73
  1996
    const int retval = SDL20_UpperBlit(src->surface20,
icculus@73
  1997
                                       srcrect12 ? Rect12to20(srcrect12, &srcrect20) : NULL,
icculus@73
  1998
                                       dst->surface20,
icculus@73
  1999
                                       dstrect12 ? Rect12to20(dstrect12, &dstrect20) : NULL);
icculus@73
  2000
icculus@73
  2001
    if (srcrect12) {
icculus@73
  2002
        Rect20to12(&srcrect20, srcrect12);
icculus@73
  2003
    }
icculus@73
  2004
icculus@73
  2005
    if (srcrect12) {
icculus@73
  2006
        Rect20to12(&dstrect20, dstrect12);
icculus@73
  2007
    }
icculus@73
  2008
icculus@73
  2009
    return retval;
icculus@64
  2010
}
icculus@64
  2011
icculus@64
  2012
DECLSPEC int SDLCALL
icculus@73
  2013
SDL_LowerBlit(SDL12_Surface *src, SDL12_Rect *srcrect12, SDL12_Surface *dst, SDL12_Rect *dstrect12)
icculus@64
  2014
{
icculus@73
  2015
    SDL_Rect srcrect20, dstrect20;
icculus@73
  2016
    const int retval = SDL20_LowerBlit(src->surface20,
icculus@73
  2017
                                       srcrect12 ? Rect12to20(srcrect12, &srcrect20) : NULL,
icculus@73
  2018
                                       dst->surface20,
icculus@73
  2019
                                       dstrect12 ? Rect12to20(dstrect12, &dstrect20) : NULL);
icculus@73
  2020
icculus@73
  2021
    if (srcrect12) {
icculus@73
  2022
        Rect20to12(&srcrect20, srcrect12);
icculus@73
  2023
    }
icculus@73
  2024
icculus@73
  2025
    if (srcrect12) {
icculus@73
  2026
        Rect20to12(&dstrect20, dstrect12);
icculus@73
  2027
    }
icculus@73
  2028
icculus@73
  2029
    return retval;
icculus@64
  2030
}
icculus@64
  2031
icculus@64
  2032
DECLSPEC int SDLCALL
icculus@35
  2033
SDL_SetAlpha(SDL12_Surface * surface, Uint32 flag, Uint8 value)
icculus@9
  2034
{
icculus@54
  2035
    FIXME("write me");
icculus@54
  2036
    return SDL20_Unsupported();
icculus@9
  2037
}
icculus@9
  2038
icculus@35
  2039
DECLSPEC SDL12_Surface * SDLCALL
icculus@65
  2040
SDL_ConvertSurface(SDL12_Surface *src12, const SDL12_PixelFormat *format12, Uint32 flags12)
icculus@65
  2041
{
icculus@65
  2042
    Uint32 flags20 = 0;
icculus@65
  2043
    SDL_PixelFormat format20;
icculus@65
  2044
    SDL_Palette palette20;
icculus@65
  2045
    SDL_Surface *surface20;
icculus@65
  2046
    SDL12_Surface *retval = NULL;
icculus@65
  2047
icculus@65
  2048
    if (flags12 & SDL12_PREALLOC) flags20 |= SDL_PREALLOC;
icculus@65
  2049
    if (flags12 & SDL12_RLEACCEL) flags20 |= SDL_RLEACCEL;
icculus@65
  2050
icculus@65
  2051
    surface20 = SDL20_ConvertSurface(src12->surface20, PixelFormat12to20(&format20, &palette20, format12), flags20);
icculus@65
  2052
    if (surface20) {
icculus@65
  2053
        retval = Surface20to12(surface20);
icculus@65
  2054
        if (!retval) {
icculus@65
  2055
            SDL20_FreeSurface(surface20);
icculus@65
  2056
        }
icculus@65
  2057
    }
icculus@65
  2058
    return retval;
icculus@65
  2059
}
icculus@65
  2060
icculus@65
  2061
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  2062
SDL_DisplayFormat(SDL12_Surface *surface12)
icculus@9
  2063
{
icculus@65
  2064
    const Uint32 flags = surface12->flags & (SDL12_SRCCOLORKEY|SDL12_SRCALPHA|SDL12_RLEACCELOK);
icculus@65
  2065
    return SDL_ConvertSurface(surface12, VideoSurface12->format, flags);
icculus@9
  2066
}
icculus@9
  2067
icculus@35
  2068
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  2069
SDL_DisplayFormatAlpha(SDL12_Surface *surface)
icculus@9
  2070
{
icculus@54
  2071
    FIXME("write me");
icculus@54
  2072
    SDL20_Unsupported();
icculus@54
  2073
    return NULL;
icculus@9
  2074
}
icculus@9
  2075
icculus@70
  2076
static void
icculus@70
  2077
PresentScreen(void)
icculus@70
  2078
{
icculus@70
  2079
    void *pixels = NULL;
icculus@70
  2080
    int pitch = 0;
icculus@70
  2081
icculus@70
  2082
    SDL_assert(VideoSurface12 != NULL);
icculus@70
  2083
icculus@70
  2084
    if (SDL20_LockTexture(VideoTexture20, NULL, &pixels, &pitch) < 0) {
icculus@70
  2085
        return;  /* oh well */
icculus@70
  2086
    }
icculus@70
  2087
icculus@70
  2088
    FIXME("Maybe lock texture always, until present, if no conversion needed?");
icculus@70
  2089
    VideoConvertSurface20->pixels = pixels;
icculus@70
  2090
    VideoConvertSurface20->pitch = pitch;
icculus@71
  2091
    SDL20_UpperBlit(VideoSurface12->surface20, NULL, VideoConvertSurface20, NULL);
icculus@70
  2092
    VideoConvertSurface20->pixels = NULL;
icculus@70
  2093
    VideoConvertSurface20->pitch = 0;
icculus@70
  2094
icculus@70
  2095
    SDL20_UnlockTexture(VideoTexture20);
icculus@70
  2096
    SDL20_RenderCopy(VideoRenderer20, VideoTexture20, NULL, NULL);
icculus@70
  2097
    SDL20_RenderPresent(VideoRenderer20);
icculus@70
  2098
}
icculus@70
  2099
icculus@35
  2100
DECLSPEC void SDLCALL
icculus@73
  2101
SDL_UpdateRects(SDL12_Surface *surface12, int numrects, SDL12_Rect *rects12)
icculus@9
  2102
{
icculus@70
  2103
    /* strangely, SDL 1.2 doesn't check if surface12 is NULL before touching it */
icculus@70
  2104
    /* (UpdateRect, singular, does...) */
icculus@70
  2105
    if (surface12->flags & SDL12_OPENGL) {
icculus@70
  2106
        SDL20_SetError("Use SDL_GL_SwapBuffers() on OpenGL surfaces");
icculus@70
  2107
        return;
icculus@70
  2108
    }
icculus@70
  2109
icculus@71
  2110
    /* everything else is marked SDL12_DOUBLEBUF and is a no-op here. */
icculus@9
  2111
}
icculus@9
  2112
icculus@35
  2113
DECLSPEC void SDLCALL
icculus@35
  2114
SDL_UpdateRect(SDL12_Surface *screen12, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
icculus@31
  2115
{
icculus@35
  2116
    if (screen12) {
icculus@73
  2117
        SDL12_Rect rect12;
icculus@73
  2118
        rect12.x = (Sint16) x;
icculus@73
  2119
        rect12.y = (Sint16) y;
icculus@73
  2120
        rect12.w = (Uint16) (w ? w : screen12->w);
icculus@73
  2121
        rect12.h = (Uint16) (h ? h : screen12->h);
icculus@73
  2122
        SDL_UpdateRects(screen12, 1, &rect12);
icculus@31
  2123
    }
icculus@31
  2124
}
icculus@31
  2125
icculus@35
  2126
DECLSPEC int SDLCALL
icculus@70
  2127
SDL_Flip(SDL12_Surface *surface12)
icculus@31
  2128
{
icculus@70
  2129
    if (surface12->flags & SDL12_OPENGL) {
icculus@70
  2130
        return SDL20_SetError("Use SDL_GL_SwapBuffers() on OpenGL surfaces");
icculus@70
  2131
    }
icculus@70
  2132
icculus@70
  2133
    if (surface12 == VideoSurface12) {
icculus@70
  2134
        PresentScreen();
icculus@70
  2135
    }
icculus@70
  2136
icculus@31
  2137
    return 0;
icculus@31
  2138
}
icculus@31
  2139
icculus@35
  2140
DECLSPEC void SDLCALL
icculus@9
  2141
SDL_WM_SetCaption(const char *title, const char *icon)
icculus@9
  2142
{
icculus@16
  2143
    if (WindowTitle) {
icculus@31
  2144
        SDL20_free(WindowTitle);
icculus@9
  2145
    }
icculus@16
  2146
    if (WindowIconTitle) {
icculus@31
  2147
        SDL20_free(WindowIconTitle);
icculus@9
  2148
    }
icculus@16
  2149
    WindowTitle = title ? SDL_strdup(title) : NULL;
icculus@16
  2150
    WindowIconTitle = icon ? SDL_strdup(icon) : NULL;
icculus@59
  2151
    if (VideoWindow20) {
icculus@59
  2152
        SDL20_SetWindowTitle(VideoWindow20, WindowTitle);
icculus@59
  2153
    }
icculus@9
  2154
}
icculus@9
  2155
icculus@35
  2156
DECLSPEC void SDLCALL
icculus@9
  2157
SDL_WM_GetCaption(const char **title, const char **icon)
icculus@9
  2158
{
icculus@9
  2159
    if (title) {
icculus@16
  2160
        *title = WindowTitle;
icculus@9
  2161
    }
icculus@9
  2162
    if (icon) {
icculus@16
  2163
        *icon = WindowIconTitle;
icculus@9
  2164
    }
icculus@9
  2165
}
icculus@9
  2166
icculus@35
  2167
DECLSPEC void SDLCALL
icculus@34
  2168
SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
icculus@9
  2169
{
icculus@54
  2170
    FIXME("write me");
icculus@54
  2171
    SDL20_Unsupported();
icculus@9
  2172
}
icculus@9
  2173
icculus@35
  2174
DECLSPEC int SDLCALL
icculus@9
  2175
SDL_WM_IconifyWindow(void)
icculus@9
  2176
{
icculus@34
  2177
    SDL20_MinimizeWindow(VideoWindow20);
icculus@9
  2178
    return 0;
icculus@9
  2179
}
icculus@9
  2180
icculus@35
  2181
DECLSPEC int SDLCALL
icculus@51
  2182
SDL_WM_ToggleFullScreen(SDL12_Surface *surface)
icculus@9
  2183
{
icculus@54
  2184
    FIXME("write me");
icculus@54
  2185
    return SDL20_Unsupported();
icculus@9
  2186
}
icculus@9
  2187
icculus@31
  2188
typedef enum
icculus@9
  2189
{
icculus@31
  2190
    SDL12_GRAB_QUERY = -1,
icculus@31
  2191
    SDL12_GRAB_OFF = 0,
icculus@31
  2192
    SDL12_GRAB_ON = 1
icculus@31
  2193
} SDL12_GrabMode;
icculus@31
  2194
icculus@35
  2195
DECLSPEC SDL12_GrabMode SDLCALL
icculus@31
  2196
SDL_WM_GrabInput(SDL12_GrabMode mode)
icculus@31
  2197
{
icculus@31
  2198
    if (mode != SDL12_GRAB_QUERY) {
icculus@34
  2199
        SDL20_SetWindowGrab(VideoWindow20, (mode == SDL12_GRAB_ON));
icculus@9
  2200
    }
icculus@34
  2201
    return SDL20_GetWindowGrab(VideoWindow20) ? SDL12_GRAB_ON : SDL12_GRAB_OFF;
icculus@9
  2202
}
icculus@9
  2203
icculus@35
  2204
DECLSPEC void SDLCALL
icculus@9
  2205
SDL_WarpMouse(Uint16 x, Uint16 y)
icculus@9
  2206
{
icculus@34
  2207
    SDL20_WarpMouseInWindow(VideoWindow20, x, y);
icculus@9
  2208
}
icculus@9
  2209
icculus@35
  2210
DECLSPEC Uint8 SDLCALL
icculus@9
  2211
SDL_GetAppState(void)
icculus@9
  2212
{
icculus@34
  2213
    Uint8 state12 = 0;
icculus@34
  2214
    Uint32 flags20 = 0;
icculus@34
  2215
icculus@34
  2216
    flags20 = SDL20_GetWindowFlags(VideoWindow20);
icculus@34
  2217
    if ((flags20 & SDL_WINDOW_SHOWN) && !(flags20 & SDL_WINDOW_MINIMIZED)) {
icculus@34
  2218
        state12 |= SDL12_APPACTIVE;
icculus@9
  2219
    }
icculus@34
  2220
    if (flags20 & SDL_WINDOW_INPUT_FOCUS) {
icculus@34
  2221
        state12 |= SDL12_APPINPUTFOCUS;
icculus@9
  2222
    }
icculus@34
  2223
    if (flags20 & SDL_WINDOW_MOUSE_FOCUS) {
icculus@34
  2224
        state12 |= SDL12_APPMOUSEFOCUS;
icculus@9
  2225
    }
icculus@34
  2226
    return state12;
icculus@9
  2227
}
icculus@9
  2228
icculus@35
  2229
DECLSPEC int SDLCALL
icculus@74
  2230
SDL_SetColorKey(SDL12_Surface *surface12, Uint32 flag12, Uint32 key)
icculus@74
  2231
{
icculus@74
  2232
    const SDL_bool addkey = (flag12 & SDL12_SRCCOLORKEY) ? SDL_TRUE : SDL_FALSE;
icculus@74
  2233
    const int retval = SDL20_SetColorKey(surface12->surface20, addkey, key);
icculus@74
  2234
    if (SDL20_GetColorKey(surface12->surface20, &surface12->format->colorkey) < 0) {
icculus@74
  2235
        surface12->format->colorkey = 0;
icculus@74
  2236
    }
icculus@74
  2237
    return retval;
icculus@74
  2238
}
icculus@74
  2239
icculus@74
  2240
DECLSPEC int SDLCALL
icculus@35
  2241
SDL_SetPalette(SDL12_Surface *surface12, int flags, const SDL_Color *colors,
icculus@9
  2242
               int firstcolor, int ncolors)
icculus@9
  2243
{
icculus@54
  2244
    FIXME("write me");
icculus@54
  2245
    return SDL20_Unsupported();
icculus@9
  2246
}
icculus@9
  2247
icculus@35
  2248
DECLSPEC int SDLCALL
icculus@35
  2249
SDL_SetColors(SDL12_Surface *surface12, const SDL_Color * colors, int firstcolor,
icculus@9
  2250
              int ncolors)
icculus@9
  2251
{
icculus@54
  2252
    FIXME("write me");
icculus@54
  2253
    return SDL20_Unsupported();
icculus@9
  2254
}
icculus@9
  2255
icculus@35
  2256
DECLSPEC int SDLCALL
icculus@9
  2257
SDL_GetWMInfo(SDL_SysWMinfo * info)
icculus@9
  2258
{
icculus@54
  2259
    FIXME("write me");
icculus@54
  2260
    //return SDL20_GetWindowWMInfo(VideoWindow20, info);
icculus@54
  2261
    return SDL20_Unsupported();
icculus@9
  2262
}
icculus@9
  2263
icculus@57
  2264
DECLSPEC SDL12_Overlay * SDLCALL
icculus@35
  2265
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL12_Surface *display)
icculus@9
  2266
{
icculus@54
  2267
    FIXME("write me");
icculus@54
  2268
    SDL20_Unsupported();
icculus@54
  2269
    return NULL;
icculus@9
  2270
}
icculus@9
  2271
icculus@35
  2272
DECLSPEC int SDLCALL
icculus@57
  2273
SDL_LockYUVOverlay(SDL12_Overlay * overlay)
icculus@9
  2274
{
icculus@54
  2275
    FIXME("write me");
icculus@54
  2276
    return SDL20_Unsupported();
icculus@9
  2277
}
icculus@9
  2278
icculus@35
  2279
DECLSPEC void SDLCALL
icculus@57
  2280
SDL_UnlockYUVOverlay(SDL12_Overlay * overlay)
icculus@9
  2281
{
icculus@54
  2282
    FIXME("write me");
icculus@9
  2283
}
icculus@9
  2284
icculus@35
  2285
DECLSPEC int SDLCALL
icculus@73
  2286
SDL_DisplayYUVOverlay(SDL12_Overlay * overlay, SDL12_Rect * dstrect12)
icculus@9
  2287
{
icculus@54
  2288
    FIXME("write me");
icculus@54
  2289
    return SDL20_Unsupported();
icculus@9
  2290
}
icculus@9
  2291
icculus@35
  2292
DECLSPEC void SDLCALL
icculus@57
  2293
SDL_FreeYUVOverlay(SDL12_Overlay * overlay)
icculus@9
  2294
{
icculus@54
  2295
    FIXME("write me");
icculus@9
  2296
}
icculus@9
  2297
icculus@35
  2298
DECLSPEC int SDLCALL
icculus@30
  2299
SDL_GL_SetAttribute(SDL12_GLattr attr, int value)
icculus@30
  2300
{
icculus@30
  2301
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2302
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2303
icculus@30
  2304
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2305
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2306
    {
icculus@30
  2307
        SwapInterval = value;
icculus@54
  2308
        FIXME("Actually set swap interval somewhere");
icculus@30
  2309
        return 0;
icculus@30
  2310
    }
icculus@30
  2311
icculus@30
  2312
    return SDL20_GL_SetAttribute((SDL_GLattr) attr, value);
icculus@30
  2313
}
icculus@30
  2314
icculus@35
  2315
DECLSPEC int SDLCALL
icculus@30
  2316
SDL_GL_GetAttribute(SDL12_GLattr attr, int* value)
icculus@30
  2317
{
icculus@30
  2318
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2319
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2320
icculus@30
  2321
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2322
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2323
    {
icculus@30
  2324
        *value = SDL20_GL_GetSwapInterval();
icculus@30
  2325
        return 0;
icculus@30
  2326
    }
icculus@30
  2327
icculus@30
  2328
    return SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
icculus@30
  2329
}
icculus@30
  2330
icculus@30
  2331
icculus@35
  2332
DECLSPEC void SDLCALL
icculus@9
  2333
SDL_GL_SwapBuffers(void)
icculus@9
  2334
{
icculus@34
  2335
    if (VideoWindow20)
icculus@34
  2336
        SDL20_GL_SwapWindow(VideoWindow20);
icculus@9
  2337
}
icculus@9
  2338
icculus@35
  2339
DECLSPEC int SDLCALL
icculus@9
  2340
SDL_SetGamma(float red, float green, float blue)
icculus@9
  2341
{
icculus@9
  2342
    Uint16 red_ramp[256];
icculus@9
  2343
    Uint16 green_ramp[256];
icculus@9
  2344
    Uint16 blue_ramp[256];
icculus@9
  2345
icculus@34
  2346
    SDL20_CalculateGammaRamp(red, red_ramp);
icculus@9
  2347
    if (green == red) {
icculus@34
  2348
        SDL20_memcpy(green_ramp, red_ramp, sizeof(red_ramp));
icculus@9
  2349
    } else {
icculus@34
  2350
        SDL20_CalculateGammaRamp(green, green_ramp);
icculus@9
  2351
    }
icculus@9
  2352
    if (blue == red) {
icculus@34
  2353
        SDL20_memcpy(blue_ramp, red_ramp, sizeof(red_ramp));
icculus@34
  2354
    } else if (blue == green) {
icculus@34
  2355
        SDL20_memcpy(blue_ramp, green_ramp, sizeof(green_ramp));
icculus@9
  2356
    } else {
icculus@34
  2357
        SDL20_CalculateGammaRamp(blue, blue_ramp);
icculus@9
  2358
    }
icculus@34
  2359
    return SDL20_SetWindowGammaRamp(VideoWindow20, red_ramp, green_ramp, blue_ramp);
icculus@9
  2360
}
icculus@9
  2361
icculus@35
  2362
DECLSPEC int SDLCALL
icculus@34
  2363
SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
icculus@9
  2364
{
icculus@34
  2365
    return SDL20_SetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2366
}
icculus@9
  2367
icculus@35
  2368
DECLSPEC int SDLCALL
icculus@34
  2369
SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
icculus@9
  2370
{
icculus@34
  2371
    return SDL20_GetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2372
}
icculus@9
  2373
icculus@35
  2374
DECLSPEC int SDLCALL
icculus@9
  2375
SDL_EnableKeyRepeat(int delay, int interval)
icculus@9
  2376
{
icculus@9
  2377
    return 0;
icculus@9
  2378
}
icculus@9
  2379
icculus@35
  2380
DECLSPEC void SDLCALL
icculus@9
  2381
SDL_GetKeyRepeat(int *delay, int *interval)
icculus@9
  2382
{
icculus@9
  2383
    if (delay) {
icculus@34
  2384
        *delay = SDL12_DEFAULT_REPEAT_DELAY;
icculus@9
  2385
    }
icculus@9
  2386
    if (interval) {
icculus@34
  2387
        *interval = SDL12_DEFAULT_REPEAT_INTERVAL;
icculus@9
  2388
    }
icculus@9
  2389
}
icculus@9
  2390
icculus@35
  2391
DECLSPEC int SDLCALL
icculus@9
  2392
SDL_EnableUNICODE(int enable)
icculus@9
  2393
{
icculus@54
  2394
    FIXME("write me");
icculus@54
  2395
    return SDL20_Unsupported();
icculus@9
  2396
}
icculus@9
  2397
icculus@9
  2398
static Uint32
icculus@34
  2399
SetTimerOld_Callback(Uint32 interval, void* param)
icculus@9
  2400
{
icculus@31
  2401
    return ((SDL12_TimerCallback)param)(interval);
icculus@9
  2402
}
icculus@9
  2403
icculus@35
  2404
DECLSPEC int SDLCALL
icculus@31
  2405
SDL_SetTimer(Uint32 interval, SDL12_TimerCallback callback)
icculus@9
  2406
{
icculus@9
  2407
    static SDL_TimerID compat_timer;
icculus@9
  2408
icculus@9
  2409
    if (compat_timer) {
icculus@10
  2410
        SDL20_RemoveTimer(compat_timer);
icculus@9
  2411
        compat_timer = 0;
icculus@9
  2412
    }
icculus@9
  2413
icculus@9
  2414
    if (interval && callback) {
icculus@34
  2415
        compat_timer = SDL20_AddTimer(interval, SetTimerOld_Callback, callback);
icculus@9
  2416
        if (!compat_timer) {
icculus@9
  2417
            return -1;
icculus@9
  2418
        }
icculus@9
  2419
    }
icculus@9
  2420
    return 0;
icculus@9
  2421
}
icculus@9
  2422
icculus@35
  2423
DECLSPEC int SDLCALL
icculus@9
  2424
SDL_putenv(const char *_var)
icculus@9
  2425
{
icculus@9
  2426
    char *ptr = NULL;
icculus@34
  2427
    char *var = SDL20_strdup(_var);
icculus@9
  2428
    if (var == NULL) {
icculus@9
  2429
        return -1;  /* we don't set errno. */
icculus@9
  2430
    }
icculus@9
  2431
icculus@34
  2432
    ptr = SDL20_strchr(var, '=');
icculus@9
  2433
    if (ptr == NULL) {
icculus@31
  2434
        SDL20_free(var);
icculus@9
  2435
        return -1;
icculus@9
  2436
    }
icculus@9
  2437
icculus@9
  2438
    *ptr = '\0';  /* split the string into name and value. */
icculus@34
  2439
    SDL20_setenv(var, ptr + 1, 1);
icculus@31
  2440
    SDL20_free(var);
icculus@9
  2441
    return 0;
icculus@9
  2442
}
icculus@9
  2443
icculus@9
  2444
icculus@9
  2445
icculus@9
  2446
/* CD-ROM support is gone from SDL 2.0, so just have stubs that fail. */
icculus@9
  2447
icculus@9
  2448
typedef void *SDL12_CD;  /* close enough.  :) */
icculus@9
  2449
typedef int SDL12_CDstatus;  /* close enough.  :) */
icculus@9
  2450
icculus@35
  2451
DECLSPEC int SDLCALL
icculus@9
  2452
SDL_CDNumDrives(void)
icculus@9
  2453
{
icculus@54
  2454
    FIXME("should return -1 without SDL_INIT_CDROM");
icculus@54
  2455
    return 0;
icculus@9
  2456
}
icculus@9
  2457
icculus@35
  2458
DECLSPEC const char *SDLCALL SDL_CDName(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2459
DECLSPEC SDL12_CD *SDLCALL SDL_CDOpen(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2460
DECLSPEC SDL12_CDstatus SDLCALL SDL_CDStatus(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2461
DECLSPEC int SDLCALL SDL_CDPlayTracks(SDL12_CD *cdrom, int start_track, int start_frame, int ntracks, int nframes) { return SDL20_Unsupported(); }
icculus@35
  2462
DECLSPEC int SDLCALL SDL_CDPlay(SDL12_CD *cdrom, int start, int length) { return SDL20_Unsupported(); }
icculus@35
  2463
DECLSPEC int SDLCALL SDL_CDPause(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2464
DECLSPEC int SDLCALL SDL_CDResume(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2465
DECLSPEC int SDLCALL SDL_CDStop(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2466
DECLSPEC int SDLCALL SDL_CDEject(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2467
DECLSPEC void SDLCALL SDL_CDClose(SDL12_CD *cdrom) {}
icculus@9
  2468
icculus@26
  2469
icculus@26
  2470
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
icculus@35
  2471
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2472
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
icculus@26
  2473
{
icculus@26
  2474
    return SDL20_CreateThread(fn, NULL, data, pfnBeginThread, pfnEndThread);
icculus@26
  2475
}
icculus@26
  2476
#else
icculus@35
  2477
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2478
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
icculus@26
  2479
{
icculus@26
  2480
    return SDL20_CreateThread(fn, NULL, data);
icculus@26
  2481
}
icculus@26
  2482
#endif
icculus@26
  2483
icculus@52
  2484
DECLSPEC int SDLCALL
icculus@52
  2485
SDL_mutexP(SDL_mutex *mutex)
icculus@52
  2486
{
icculus@52
  2487
    return SDL20_LockMutex(mutex);
icculus@52
  2488
}
icculus@52
  2489
icculus@52
  2490
DECLSPEC int SDLCALL
icculus@52
  2491
SDL_mutexV(SDL_mutex *mutex)
icculus@52
  2492
{
icculus@52
  2493
    return SDL20_UnlockMutex(mutex);
icculus@52
  2494
}
icculus@52
  2495
icculus@53
  2496
DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread *thread)
icculus@53
  2497
{
icculus@54
  2498
    FIXME("Removed from 2.0; do nothing. We can't even report failure.");
icculus@53
  2499
    fprintf(stderr,
icculus@53
  2500
        "WARNING: this app used SDL_KillThread(), an unforgivable curse.\n"
icculus@53
  2501
        "This program should be fixed. No thread was actually harmed.\n");
icculus@53
  2502
}
icculus@9
  2503
icculus@10
  2504
/* This changed from an opaque pointer to an int in 2.0. */
icculus@10
  2505
typedef struct _SDL12_TimerID *SDL12_TimerID;
icculus@10
  2506
SDL_COMPILE_TIME_ASSERT(timer, sizeof(SDL12_TimerID) >= sizeof(SDL_TimerID));
icculus@10
  2507
icculus@31
  2508
icculus@35
  2509
DECLSPEC SDL12_TimerID SDLCALL
icculus@31
  2510
SDL_AddTimer(Uint32 interval, SDL12_NewTimerCallback callback, void *param)
icculus@10
  2511
{
icculus@10
  2512
    return (SDL12_TimerID) ((size_t) SDL20_AddTimer(interval, callback, param));
icculus@10
  2513
}
icculus@10
  2514
icculus@35
  2515
DECLSPEC SDL_bool SDLCALL
icculus@10
  2516
SDL_RemoveTimer(SDL12_TimerID id)
icculus@10
  2517
{
icculus@10
  2518
    return SDL20_RemoveTimer((SDL_TimerID) ((size_t)id));
icculus@10
  2519
}
icculus@10
  2520
icculus@10
  2521
icculus@10
  2522
typedef struct SDL12_RWops {
icculus@31
  2523
    int (SDLCALL *seek)(struct SDL12_RWops *context, int offset, int whence);
icculus@31
  2524
    int (SDLCALL *read)(struct SDL12_RWops *context, void *ptr, int size, int maxnum);
icculus@31
  2525
    int (SDLCALL *write)(struct SDL12_RWops *context, const void *ptr, int size, int num);
icculus@31
  2526
    int (SDLCALL *close)(struct SDL12_RWops *context);
icculus@10
  2527
    Uint32 type;
icculus@10
  2528
    void *padding[8];
icculus@10
  2529
    SDL_RWops *rwops20;
icculus@10
  2530
} SDL12_RWops;
icculus@10
  2531
icculus@10
  2532
icculus@35
  2533
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2534
SDL_AllocRW(void)
icculus@10
  2535
{
icculus@31
  2536
    SDL12_RWops *rwops = (SDL12_RWops *) SDL20_malloc(sizeof (SDL12_RWops));
icculus@10
  2537
    if (!rwops)
icculus@10
  2538
        SDL20_OutOfMemory();
icculus@10
  2539
    return rwops;
icculus@10
  2540
}
icculus@10
  2541
icculus@35
  2542
DECLSPEC void SDLCALL
icculus@10
  2543
SDL_FreeRW(SDL12_RWops *rwops12)
icculus@10
  2544
{
icculus@31
  2545
    SDL20_free(rwops12);
icculus@10
  2546
}
icculus@10
  2547
icculus@10
  2548
static int SDLCALL
icculus@31
  2549
RWops20to12_seek(struct SDL12_RWops *rwops12, int offset, int whence)
icculus@10
  2550
{
icculus@10
  2551
    return rwops12->rwops20->seek(rwops12->rwops20, offset, whence);
icculus@10
  2552
}
icculus@10
  2553
icculus@10
  2554
static int SDLCALL
icculus@31
  2555
RWops20to12_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
icculus@10
  2556
{
icculus@10
  2557
    return rwops12->rwops20->read(rwops12->rwops20, ptr, size, maxnum);
icculus@10
  2558
}
icculus@10
  2559
icculus@10
  2560
static int SDLCALL
icculus@31
  2561
RWops20to12_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
icculus@10
  2562
{
icculus@10
  2563
    return rwops12->rwops20->write(rwops12->rwops20, ptr, size, num);
icculus@10
  2564
}
icculus@10
  2565
icculus@10
  2566
static int SDLCALL
icculus@31
  2567
RWops20to12_close(struct SDL12_RWops *rwops12)
icculus@10
  2568
{
icculus@10
  2569
    int rc = 0;
icculus@10
  2570
    if (rwops12)
icculus@10
  2571
    {
icculus@10
  2572
        rc = rwops12->rwops20->close(rwops12->rwops20);
icculus@10
  2573
        if (rc == 0)
icculus@10
  2574
            SDL_FreeRW(rwops12);
icculus@10
  2575
    }
icculus@10
  2576
    return rc;
icculus@10
  2577
}
icculus@10
  2578
icculus@10
  2579
static SDL12_RWops *
icculus@31
  2580
RWops20to12(SDL_RWops *rwops20)
icculus@10
  2581
{
icculus@31
  2582
    SDL12_RWops *rwops12;
icculus@31
  2583
icculus@10
  2584
    if (!rwops20)
icculus@10
  2585
        return NULL;
icculus@31
  2586
icculus@31
  2587
    rwops12 = SDL_AllocRW();
icculus@31
  2588
    if (!rwops12)
icculus@31
  2589
        return NULL;
icculus@31
  2590
icculus@34
  2591
    SDL20_zerop(rwops12);
icculus@10
  2592
    rwops12->type = rwops20->type;
icculus@10
  2593
    rwops12->rwops20 = rwops20;
icculus@31
  2594
    rwops12->seek = RWops20to12_seek;
icculus@31
  2595
    rwops12->read = RWops20to12_read;
icculus@31
  2596
    rwops12->write = RWops20to12_write;
icculus@31
  2597
    rwops12->close = RWops20to12_close;
icculus@31
  2598
icculus@12
  2599
    return rwops12;
icculus@10
  2600
}
icculus@10
  2601
icculus@35
  2602
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2603
SDL_RWFromFile(const char *file, const char *mode)
icculus@10
  2604
{
icculus@31
  2605
    return RWops20to12(SDL20_RWFromFile(file, mode));
icculus@10
  2606
}
icculus@10
  2607
icculus@35
  2608
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2609
SDL_RWFromFP(FILE *io, int autoclose)
icculus@10
  2610
{
icculus@31
  2611
    return RWops20to12(SDL20_RWFromFP(io, autoclose));
icculus@10
  2612
}
icculus@10
  2613
icculus@35
  2614
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2615
SDL_RWFromMem(void *mem, int size)
icculus@10
  2616
{
icculus@31
  2617
    return RWops20to12(SDL20_RWFromMem(mem, size));
icculus@10
  2618
}
icculus@10
  2619
icculus@35
  2620
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2621
SDL_RWFromConstMem(const void *mem, int size)
icculus@10
  2622
{
icculus@31
  2623
    return RWops20to12(SDL20_RWFromConstMem(mem, size));
icculus@10
  2624
}
icculus@10
  2625
icculus@10
  2626
#define READ_AND_BYTESWAP(endian, bits) \
icculus@35
  2627
    DECLSPEC Uint##bits SDLCALL SDL_Read##endian##bits(SDL12_RWops *rwops12) { \
icculus@10
  2628
        Uint##bits val; rwops12->read(rwops12, &val, sizeof (val), 1); \
icculus@10
  2629
        return SDL_Swap##endian##bits(val); \
icculus@10
  2630
    }
icculus@10
  2631
icculus@10
  2632
READ_AND_BYTESWAP(LE,16)
icculus@10
  2633
READ_AND_BYTESWAP(BE,16)
icculus@10
  2634
READ_AND_BYTESWAP(LE,32)
icculus@10
  2635
READ_AND_BYTESWAP(BE,32)
icculus@10
  2636
READ_AND_BYTESWAP(LE,64)
icculus@10
  2637
READ_AND_BYTESWAP(BE,64)
icculus@10
  2638
#undef READ_AND_BYTESWAP
icculus@10
  2639
icculus@10
  2640
#define BYTESWAP_AND_WRITE(endian, bits) \
icculus@35
  2641
    DECLSPEC int SDLCALL SDL_Write##endian##bits(SDL12_RWops *rwops12, Uint##bits val) { \
icculus@10
  2642
        val = SDL_Swap##endian##bits(val); \
icculus@10
  2643
        return rwops12->write(rwops12, &val, sizeof (val), 1); \
icculus@10
  2644
    }
icculus@10
  2645
BYTESWAP_AND_WRITE(LE,16)
icculus@10
  2646
BYTESWAP_AND_WRITE(BE,16)
icculus@10
  2647
BYTESWAP_AND_WRITE(LE,32)
icculus@10
  2648
BYTESWAP_AND_WRITE(BE,32)
icculus@10
  2649
BYTESWAP_AND_WRITE(LE,64)
icculus@10
  2650
BYTESWAP_AND_WRITE(BE,64)
icculus@10
  2651
#undef BYTESWAP_AND_WRITE
icculus@10
  2652
icculus@12
  2653
icculus@12
  2654
static Sint64 SDLCALL
icculus@31
  2655
RWops12to20_size(struct SDL_RWops *rwops20)
icculus@11
  2656
{
icculus@12
  2657
    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
icculus@61
  2658
    int size = (int) ((size_t) rwops20->hidden.unknown.data2);
icculus@12
  2659
    int pos;
icculus@12
  2660
icculus@12
  2661
    if (size != -1)
icculus@12
  2662
        return size;
icculus@12
  2663
icculus@12
  2664
    pos = rwops12->seek(rwops12, 0, SEEK_CUR);
icculus@12
  2665
    if (pos == -1)
icculus@12
  2666
        return -1;
icculus@12
  2667
icculus@31
  2668
    size = (Sint64) rwops12->seek(rwops12, 0, SEEK_END);
icculus@12
  2669
    if (size == -1)
icculus@12
  2670
        return -1;
icculus@12
  2671
icculus@61
  2672
    rwops12->seek(rwops12, pos, SEEK_SET);  FIXME("...and if this fails?");
icculus@61
  2673
    rwops20->hidden.unknown.data2 = (void *) ((size_t) size);
icculus@12
  2674
    return size;
icculus@12
  2675
}
icculus@12
  2676
icculus@35
  2677
static Sint64 SDLCALL
icculus@31
  2678
RWops12to20_seek(struct SDL_RWops *rwops20, Sint64 offset, int whence)
icculus@12
  2679
{
icculus@54
  2680
    FIXME("fail if (offset) is too big");
icculus@12
  2681
    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
icculus@12
  2682
    return (Sint64) rwops12->seek(rwops12, (int) offset, whence);
icculus@12
  2683
}
icculus@12
  2684
icculus@12
  2685
static size_t SDLCALL
icculus@31
  2686
RWops12to20_read(struct SDL_RWops *rwops20, void *ptr, size_t size, size_t maxnum)
icculus@12
  2687
{
icculus@54
  2688
    FIXME("fail if (size) or (maxnum) is too big");
icculus@12
  2689
    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
icculus@12
  2690
    return (size_t) rwops12->read(rwops12, ptr, (int) size, (int) maxnum);
icculus@12
  2691
}
icculus@12
  2692
icculus@12
  2693
static size_t SDLCALL
icculus@31
  2694
RWops12to20_write(struct SDL_RWops *rwops20, const void *ptr, size_t size, size_t num)
icculus@12
  2695
{
icculus@54
  2696
    FIXME("fail if (size) or (maxnum) is too big");
icculus@12
  2697
    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
icculus@12
  2698
    return (size_t) rwops12->write(rwops12, ptr, (int) size, (int) num);
icculus@12
  2699
}
icculus@12
  2700
icculus@12
  2701
static int SDLCALL