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