src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 Jul 2006 07:17:11 +0000
branchSDL-1.3
changeset 1724 6c63fc2bd986
parent 1722 5daa04d862f1
child 1731 875c3cf1a12c
permissions -rw-r--r--
Proof of concept done - Win32 GDI implementation mostly complete.
slouken@1667
     1
/*
slouken@1667
     2
    SDL - Simple DirectMedia Layer
slouken@1667
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1667
     4
slouken@1667
     5
    This library is free software; you can redistribute it and/or
slouken@1667
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1667
     7
    License as published by the Free Software Foundation; either
slouken@1667
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1667
     9
slouken@1667
    10
    This library is distributed in the hope that it will be useful,
slouken@1667
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1667
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1667
    13
    Lesser General Public License for more details.
slouken@1667
    14
slouken@1667
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1667
    16
    License along with this library; if not, write to the Free Software
slouken@1667
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1667
    18
slouken@1667
    19
    Sam Lantinga
slouken@1667
    20
    slouken@libsdl.org
slouken@1667
    21
*/
slouken@1667
    22
#include "SDL_config.h"
slouken@1667
    23
slouken@1667
    24
/* This file contains functions for backwards compatibility with SDL 1.2 */
slouken@1667
    25
slouken@1667
    26
#include "SDL.h"
slouken@1712
    27
#include "SDL_syswm.h"
slouken@1667
    28
slouken@1720
    29
#include "video/SDL_sysvideo.h"
slouken@1678
    30
#include "video/SDL_pixels_c.h"
slouken@1667
    31
slouken@1667
    32
slouken@1678
    33
static SDL_WindowID SDL_VideoWindow;
slouken@1678
    34
static SDL_TextureID SDL_VideoTexture;
slouken@1678
    35
static SDL_Surface *SDL_VideoSurface;
slouken@1678
    36
static SDL_Surface *SDL_ShadowSurface;
slouken@1678
    37
static SDL_Surface *SDL_PublicSurface;
slouken@1667
    38
static char *wm_title;
slouken@1667
    39
slouken@1667
    40
char *
slouken@1668
    41
SDL_AudioDriverName(char *namebuf, int maxlen)
slouken@1667
    42
{
slouken@1668
    43
    const char *name = SDL_GetCurrentAudioDriver();
slouken@1667
    44
    if (name) {
slouken@1668
    45
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    46
        return namebuf;
slouken@1667
    47
    }
slouken@1667
    48
    return NULL;
slouken@1667
    49
}
slouken@1667
    50
slouken@1667
    51
char *
slouken@1668
    52
SDL_VideoDriverName(char *namebuf, int maxlen)
slouken@1667
    53
{
slouken@1668
    54
    const char *name = SDL_GetCurrentVideoDriver();
slouken@1667
    55
    if (name) {
slouken@1668
    56
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    57
        return namebuf;
slouken@1667
    58
    }
slouken@1667
    59
    return NULL;
slouken@1667
    60
}
slouken@1667
    61
slouken@1670
    62
const SDL_VideoInfo *
slouken@1670
    63
SDL_GetVideoInfo(void)
slouken@1670
    64
{
slouken@1670
    65
    static SDL_VideoInfo info;
slouken@1670
    66
slouken@1670
    67
    /* Memory leak, compatibility code, who cares? */
slouken@1670
    68
    if (!info.vfmt && SDL_GetDesktopDisplayMode()) {
slouken@1670
    69
        int bpp;
slouken@1670
    70
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
    71
slouken@1670
    72
        SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
slouken@1670
    73
                                   &Rmask, &Gmask, &Bmask, &Amask);
slouken@1670
    74
        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
    75
    }
slouken@1670
    76
    return &info;
slouken@1670
    77
}
slouken@1670
    78
slouken@1667
    79
int
slouken@1668
    80
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
slouken@1667
    81
{
slouken@1667
    82
    int i, actual_bpp = 0;
slouken@1667
    83
slouken@1668
    84
    if (!SDL_GetVideoDevice()) {
slouken@1667
    85
        return 0;
slouken@1667
    86
    }
slouken@1667
    87
slouken@1667
    88
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1668
    89
        return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
slouken@1667
    90
    }
slouken@1667
    91
slouken@1668
    92
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
    93
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
    94
        if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
slouken@1667
    95
            if (!mode->format) {
slouken@1667
    96
                return bpp;
slouken@1667
    97
            }
slouken@1720
    98
            if (SDL_BITSPERPIXEL(mode->format) >= (Uint32) bpp) {
slouken@1668
    99
                actual_bpp = SDL_BITSPERPIXEL(mode->format);
slouken@1667
   100
            }
slouken@1667
   101
        }
slouken@1667
   102
    }
slouken@1667
   103
    return actual_bpp;
slouken@1667
   104
}
slouken@1667
   105
slouken@1667
   106
SDL_Rect **
slouken@1668
   107
SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
slouken@1667
   108
{
slouken@1667
   109
    int i, nmodes;
slouken@1667
   110
    SDL_Rect **modes;
slouken@1667
   111
slouken@1668
   112
    if (!SDL_GetVideoDevice()) {
slouken@1667
   113
        return NULL;
slouken@1667
   114
    }
slouken@1667
   115
slouken@1667
   116
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1667
   117
        return (SDL_Rect **) (-1);
slouken@1667
   118
    }
slouken@1667
   119
slouken@1667
   120
    /* Memory leak, but this is a compatibility function, who cares? */
slouken@1667
   121
    nmodes = 0;
slouken@1720
   122
    modes = NULL;
slouken@1668
   123
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
   124
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
   125
        if (!mode->w || !mode->h) {
slouken@1667
   126
            return (SDL_Rect **) (-1);
slouken@1667
   127
        }
slouken@1668
   128
        if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
slouken@1667
   129
            continue;
slouken@1667
   130
        }
slouken@1667
   131
        if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
slouken@1667
   132
            && modes[nmodes - 1]->h == mode->h) {
slouken@1667
   133
            continue;
slouken@1667
   134
        }
slouken@1667
   135
slouken@1668
   136
        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
slouken@1667
   137
        if (!modes) {
slouken@1667
   138
            return NULL;
slouken@1667
   139
        }
slouken@1668
   140
        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1667
   141
        if (!modes[nmodes]) {
slouken@1667
   142
            return NULL;
slouken@1667
   143
        }
slouken@1667
   144
        modes[nmodes]->x = 0;
slouken@1667
   145
        modes[nmodes]->y = 0;
slouken@1667
   146
        modes[nmodes]->w = mode->w;
slouken@1667
   147
        modes[nmodes]->h = mode->h;
slouken@1667
   148
        ++nmodes;
slouken@1667
   149
    }
slouken@1667
   150
    if (modes) {
slouken@1667
   151
        modes[nmodes] = NULL;
slouken@1667
   152
    }
slouken@1667
   153
    return modes;
slouken@1667
   154
}
slouken@1667
   155
slouken@1722
   156
static SDL_EventFilter orig_eventfilter;
slouken@1722
   157
static void *orig_eventfilterparam;
slouken@1669
   158
slouken@1669
   159
static int
slouken@1722
   160
SDL_CompatEventFilter(void *userdata, SDL_Event * event)
slouken@1669
   161
{
slouken@1669
   162
    SDL_Event fake;
slouken@1669
   163
slouken@1669
   164
    switch (event->type) {
slouken@1669
   165
    case SDL_WINDOWEVENT:
slouken@1669
   166
        switch (event->window.event) {
slouken@1724
   167
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1724
   168
            if (!SDL_HasEvent(SDL_VIDEOEXPOSEMASK)) {
slouken@1724
   169
                fake.type = SDL_VIDEOEXPOSE;
slouken@1724
   170
                SDL_PushEvent(&fake);
slouken@1724
   171
            }
slouken@1724
   172
            break;
slouken@1669
   173
        case SDL_WINDOWEVENT_RESIZED:
slouken@1669
   174
            fake.type = SDL_VIDEORESIZE;
slouken@1669
   175
            fake.resize.w = event->window.data1;
slouken@1669
   176
            fake.resize.h = event->window.data2;
slouken@1669
   177
            SDL_PushEvent(&fake);
slouken@1669
   178
            break;
slouken@1669
   179
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1669
   180
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   181
            fake.active.gain = 0;
slouken@1669
   182
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   183
            SDL_PushEvent(&fake);
slouken@1669
   184
            break;
slouken@1669
   185
        case SDL_WINDOWEVENT_RESTORED:
slouken@1669
   186
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   187
            fake.active.gain = 1;
slouken@1669
   188
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   189
            SDL_PushEvent(&fake);
slouken@1669
   190
            break;
slouken@1669
   191
        case SDL_WINDOWEVENT_ENTER:
slouken@1669
   192
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   193
            fake.active.gain = 1;
slouken@1669
   194
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   195
            SDL_PushEvent(&fake);
slouken@1669
   196
            break;
slouken@1669
   197
        case SDL_WINDOWEVENT_LEAVE:
slouken@1669
   198
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   199
            fake.active.gain = 0;
slouken@1669
   200
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   201
            SDL_PushEvent(&fake);
slouken@1669
   202
            break;
slouken@1669
   203
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1669
   204
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   205
            fake.active.gain = 1;
slouken@1669
   206
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   207
            SDL_PushEvent(&fake);
slouken@1669
   208
            break;
slouken@1669
   209
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1669
   210
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   211
            fake.active.gain = 1;
slouken@1669
   212
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   213
            SDL_PushEvent(&fake);
slouken@1669
   214
            break;
slouken@1724
   215
        case SDL_WINDOWEVENT_CLOSE:
slouken@1724
   216
            fake.type = SDL_QUIT;
slouken@1724
   217
            SDL_PushEvent(&fake);
slouken@1724
   218
            break;
slouken@1669
   219
        }
slouken@1686
   220
    case SDL_KEYDOWN:
slouken@1686
   221
    case SDL_KEYUP:
slouken@1686
   222
        {
slouken@1686
   223
            Uint32 unicode = 0;
slouken@1686
   224
            if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
slouken@1687
   225
                unicode = event->key.keysym.sym;
slouken@1691
   226
                if (unicode >= 'a' && unicode <= 'z') {
slouken@1687
   227
                    int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
slouken@1687
   228
                    int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
slouken@1687
   229
                    if ((shifted ^ capslock) != 0) {
slouken@1687
   230
                        unicode = SDL_toupper(unicode);
slouken@1687
   231
                    }
slouken@1686
   232
                }
slouken@1686
   233
            }
slouken@1686
   234
            if (unicode) {
slouken@1686
   235
                event->key.keysym.unicode = unicode;
slouken@1686
   236
            }
slouken@1686
   237
            break;
slouken@1686
   238
        }
slouken@1724
   239
    case SDL_MOUSEWHEEL:
slouken@1724
   240
        {
slouken@1724
   241
            Uint8 button;
slouken@1724
   242
            int selected;
slouken@1724
   243
            int x, y;
slouken@1724
   244
slouken@1724
   245
            selected = SDL_SelectMouse(event->wheel.which);
slouken@1724
   246
            SDL_GetMouseState(&x, &y);
slouken@1724
   247
            SDL_SelectMouse(selected);
slouken@1724
   248
slouken@1724
   249
            if (event->wheel.motion > 0) {
slouken@1724
   250
                button = SDL_BUTTON_WHEELUP;
slouken@1724
   251
            } else {
slouken@1724
   252
                button = SDL_BUTTON_WHEELDOWN;
slouken@1724
   253
            }
slouken@1724
   254
slouken@1724
   255
            fake.button.which = event->wheel.windowID;
slouken@1724
   256
            fake.button.button = button;
slouken@1724
   257
            fake.button.x = x;
slouken@1724
   258
            fake.button.y = y;
slouken@1724
   259
            fake.button.windowID = event->wheel.windowID;
slouken@1724
   260
slouken@1724
   261
            fake.type = SDL_MOUSEBUTTONDOWN;
slouken@1724
   262
            fake.button.state = SDL_PRESSED;
slouken@1724
   263
            SDL_PushEvent(&fake);
slouken@1724
   264
slouken@1724
   265
            fake.type = SDL_MOUSEBUTTONUP;
slouken@1724
   266
            fake.button.state = SDL_RELEASED;
slouken@1724
   267
            SDL_PushEvent(&fake);
slouken@1724
   268
            break;
slouken@1724
   269
        }
slouken@1724
   270
slouken@1669
   271
    }
slouken@1683
   272
    if (orig_eventfilter) {
slouken@1722
   273
        return orig_eventfilter(orig_eventfilterparam, event);
slouken@1683
   274
    } else {
slouken@1683
   275
        return 1;
slouken@1683
   276
    }
slouken@1683
   277
}
slouken@1683
   278
slouken@1683
   279
static int
slouken@1683
   280
SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1683
   281
{
slouken@1683
   282
    if (userdata == SDL_ShadowSurface) {
slouken@1683
   283
        /* If the shadow palette changed, make the changes visible */
slouken@1683
   284
        if (!SDL_VideoSurface->format->palette) {
slouken@1683
   285
            SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
slouken@1683
   286
        }
slouken@1683
   287
    }
slouken@1683
   288
    if (userdata == SDL_VideoSurface) {
slouken@1683
   289
        return SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors);
slouken@1683
   290
    }
slouken@1720
   291
    return 0;
slouken@1669
   292
}
slouken@1669
   293
slouken@1667
   294
SDL_Surface *
slouken@1668
   295
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@1667
   296
{
slouken@1722
   297
    SDL_EventFilter filter;
slouken@1722
   298
    void *filterparam;
slouken@1669
   299
    const SDL_DisplayMode *desktop_mode;
slouken@1667
   300
    SDL_DisplayMode mode;
slouken@1667
   301
    Uint32 window_flags;
slouken@1667
   302
    Uint32 desktop_format;
slouken@1667
   303
    Uint32 desired_format;
slouken@1682
   304
    Uint32 surface_flags;
slouken@1667
   305
slouken@1668
   306
    if (!SDL_GetVideoDevice()) {
slouken@1668
   307
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1667
   308
            return NULL;
slouken@1667
   309
        }
slouken@1667
   310
    }
slouken@1667
   311
slouken@1667
   312
    /* Destroy existing window */
slouken@1678
   313
    SDL_PublicSurface = NULL;
slouken@1678
   314
    if (SDL_ShadowSurface) {
slouken@1678
   315
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1678
   316
        SDL_ShadowSurface = NULL;
slouken@1678
   317
    }
slouken@1678
   318
    if (SDL_VideoSurface) {
slouken@1683
   319
        SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
slouken@1683
   320
                            SDL_VideoPaletteChanged, NULL);
slouken@1683
   321
        SDL_FreeSurface(SDL_VideoSurface);
slouken@1678
   322
        SDL_VideoSurface = NULL;
slouken@1678
   323
    }
slouken@1678
   324
    SDL_DestroyWindow(SDL_VideoWindow);
slouken@1667
   325
slouken@1669
   326
    /* Set up the event filter */
slouken@1722
   327
    filter = SDL_GetEventFilter(&filterparam);
slouken@1669
   328
    if (filter != SDL_CompatEventFilter) {
slouken@1669
   329
        orig_eventfilter = filter;
slouken@1722
   330
        orig_eventfilterparam = filterparam;
slouken@1669
   331
    }
slouken@1722
   332
    SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
slouken@1669
   333
slouken@1667
   334
    /* Create a new window */
slouken@1667
   335
    window_flags = SDL_WINDOW_SHOWN;
slouken@1667
   336
    if (flags & SDL_FULLSCREEN) {
slouken@1667
   337
        window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1667
   338
    }
slouken@1667
   339
    if (flags & SDL_OPENGL) {
slouken@1667
   340
        window_flags |= SDL_WINDOW_OPENGL;
slouken@1667
   341
    }
slouken@1667
   342
    if (flags & SDL_RESIZABLE) {
slouken@1667
   343
        window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1667
   344
    }
slouken@1667
   345
    if (flags & SDL_NOFRAME) {
slouken@1667
   346
        window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1667
   347
    }
slouken@1678
   348
    SDL_VideoWindow =
slouken@1724
   349
        SDL_CreateWindow(wm_title, SDL_WINDOWPOS_UNDEFINED,
slouken@1724
   350
                         SDL_WINDOWPOS_UNDEFINED, width, height,
slouken@1724
   351
                         window_flags);
slouken@1678
   352
    if (!SDL_VideoWindow) {
slouken@1667
   353
        return NULL;
slouken@1667
   354
    }
slouken@1667
   355
slouken@1682
   356
    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1716
   357
    surface_flags = 0;
slouken@1682
   358
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1682
   359
        surface_flags |= SDL_FULLSCREEN;
slouken@1682
   360
    }
slouken@1682
   361
    if (window_flags & SDL_WINDOW_OPENGL) {
slouken@1682
   362
        surface_flags |= SDL_OPENGL;
slouken@1682
   363
    }
slouken@1682
   364
    if (window_flags & SDL_WINDOW_RESIZABLE) {
slouken@1682
   365
        surface_flags |= SDL_RESIZABLE;
slouken@1682
   366
    }
slouken@1682
   367
    if (window_flags & SDL_WINDOW_BORDERLESS) {
slouken@1682
   368
        surface_flags |= SDL_NOFRAME;
slouken@1682
   369
    }
slouken@1682
   370
slouken@1667
   371
    /* Set up the desired display mode */
slouken@1669
   372
    desktop_mode = SDL_GetDesktopDisplayMode();
slouken@1669
   373
    desktop_format = desktop_mode->format;
slouken@1669
   374
    if (desktop_format && ((flags & SDL_ANYFORMAT)
slouken@1669
   375
                           || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
slouken@1667
   376
        desired_format = desktop_format;
slouken@1667
   377
    } else {
slouken@1667
   378
        switch (bpp) {
slouken@1669
   379
        case 0:
slouken@1669
   380
            if (desktop_format) {
slouken@1669
   381
                desired_format = desktop_format;
slouken@1669
   382
            } else {
slouken@1669
   383
                desired_format = SDL_PixelFormat_RGB888;
slouken@1669
   384
            }
slouken@1669
   385
            break;
slouken@1667
   386
        case 8:
slouken@1667
   387
            desired_format = SDL_PixelFormat_Index8;
slouken@1667
   388
            break;
slouken@1667
   389
        case 15:
slouken@1667
   390
            desired_format = SDL_PixelFormat_RGB555;
slouken@1667
   391
            break;
slouken@1667
   392
        case 16:
slouken@1667
   393
            desired_format = SDL_PixelFormat_RGB565;
slouken@1667
   394
            break;
slouken@1667
   395
        case 24:
slouken@1667
   396
            desired_format = SDL_PixelFormat_RGB24;
slouken@1667
   397
            break;
slouken@1667
   398
        case 32:
slouken@1667
   399
            desired_format = SDL_PixelFormat_RGB888;
slouken@1667
   400
            break;
slouken@1667
   401
        default:
slouken@1668
   402
            SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
slouken@1667
   403
            return NULL;
slouken@1667
   404
        }
slouken@1667
   405
    }
slouken@1667
   406
    mode.format = desired_format;
slouken@1667
   407
    mode.w = width;
slouken@1667
   408
    mode.h = height;
slouken@1667
   409
    mode.refresh_rate = 0;
slouken@1667
   410
slouken@1667
   411
    /* Set the desired display mode */
slouken@1667
   412
    if (flags & SDL_FULLSCREEN) {
slouken@1668
   413
        if (!SDL_GetClosestDisplayMode(&mode, &mode)) {
slouken@1667
   414
            return NULL;
slouken@1667
   415
        }
slouken@1667
   416
    } else {
slouken@1669
   417
        if (desktop_format) {
slouken@1669
   418
            mode.format = desktop_format;
slouken@1669
   419
        }
slouken@1669
   420
        if (desktop_mode->w && desktop_mode->h) {
slouken@1669
   421
            mode.w = desktop_mode->w;
slouken@1669
   422
            mode.h = desktop_mode->h;
slouken@1669
   423
        }
slouken@1669
   424
        mode.refresh_rate = desktop_mode->refresh_rate;
slouken@1667
   425
    }
slouken@1668
   426
    if (SDL_SetDisplayMode(&mode) < 0) {
slouken@1667
   427
        return NULL;
slouken@1667
   428
    }
slouken@1667
   429
slouken@1682
   430
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
slouken@1682
   431
    if (flags & SDL_OPENGL) {
slouken@1682
   432
        SDL_VideoSurface =
slouken@1682
   433
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
slouken@1682
   434
        if (!SDL_VideoSurface) {
slouken@1682
   435
            return NULL;
slouken@1682
   436
        }
slouken@1682
   437
        SDL_VideoSurface->flags |= surface_flags;
slouken@1682
   438
        SDL_PublicSurface = SDL_VideoSurface;
slouken@1682
   439
        return SDL_PublicSurface;
slouken@1682
   440
    }
slouken@1682
   441
slouken@1678
   442
    /* Create a renderer for the window */
slouken@1678
   443
    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
slouken@1670
   444
        return NULL;
slouken@1670
   445
    }
slouken@1670
   446
slouken@1678
   447
    /* Create a texture for the screen surface */
slouken@1678
   448
    SDL_VideoTexture =
slouken@1678
   449
        SDL_CreateTexture(desired_format, SDL_TextureAccess_Local, width,
slouken@1678
   450
                          height);
slouken@1678
   451
    if (!SDL_VideoTexture) {
slouken@1678
   452
        SDL_VideoTexture =
slouken@1678
   453
            SDL_CreateTexture(0, SDL_TextureAccess_Local, width, height);
slouken@1678
   454
    }
slouken@1678
   455
    if (!SDL_VideoTexture) {
slouken@1670
   456
        return NULL;
slouken@1670
   457
    }
slouken@1670
   458
slouken@1678
   459
    /* Create the screen surface */
slouken@1678
   460
    SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture);
slouken@1678
   461
    if (!SDL_VideoSurface) {
slouken@1678
   462
        return NULL;
slouken@1678
   463
    }
slouken@1682
   464
    SDL_VideoSurface->flags |= surface_flags;
slouken@1670
   465
slouken@1678
   466
    /* Set a default screen palette */
slouken@1678
   467
    if (SDL_VideoSurface->format->palette) {
slouken@1678
   468
        SDL_VideoSurface->flags |= SDL_HWPALETTE;
slouken@1678
   469
        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
slouken@1678
   470
                         SDL_VideoSurface->format->BitsPerPixel);
slouken@1683
   471
        SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
slouken@1683
   472
                            SDL_VideoPaletteChanged, NULL);
slouken@1683
   473
        SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
slouken@1683
   474
                             SDL_VideoSurface->format->palette->colors, 0,
slouken@1683
   475
                             SDL_VideoSurface->format->palette->ncolors);
slouken@1670
   476
    }
slouken@1670
   477
slouken@1670
   478
    /* Create a shadow surface if necessary */
slouken@1678
   479
    if (((bpp != SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   480
         && !(flags & SDL_ANYFORMAT))
slouken@1678
   481
        || ((SDL_VideoSurface->flags & SDL_HWSURFACE)
slouken@1678
   482
            && !(flags & SDL_HWSURFACE))) {
slouken@1678
   483
        if ((bpp == SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   484
            || (flags & SDL_ANYFORMAT)) {
slouken@1678
   485
            SDL_ShadowSurface =
slouken@1678
   486
                SDL_CreateRGBSurface(0, width, height,
slouken@1678
   487
                                     SDL_VideoSurface->format->BitsPerPixel,
slouken@1678
   488
                                     SDL_VideoSurface->format->Rmask,
slouken@1678
   489
                                     SDL_VideoSurface->format->Gmask,
slouken@1678
   490
                                     SDL_VideoSurface->format->Bmask,
slouken@1678
   491
                                     SDL_VideoSurface->format->Amask);
slouken@1678
   492
        } else {
slouken@1678
   493
            SDL_ShadowSurface =
slouken@1678
   494
                SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
slouken@1678
   495
        }
slouken@1678
   496
        if (!SDL_ShadowSurface) {
slouken@1670
   497
            return NULL;
slouken@1670
   498
        }
slouken@1682
   499
        SDL_ShadowSurface->flags |= surface_flags;
slouken@1670
   500
slouken@1678
   501
        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
slouken@1678
   502
        if (SDL_ShadowSurface->format->palette) {
slouken@1678
   503
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
slouken@1683
   504
            if (SDL_VideoSurface->format->palette) {
slouken@1683
   505
                SDL_SetSurfacePalette(SDL_ShadowSurface,
slouken@1683
   506
                                      SDL_VideoSurface->format->palette);
slouken@1683
   507
            } else {
slouken@1683
   508
                SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
slouken@1683
   509
                                 SDL_ShadowSurface->format->BitsPerPixel);
slouken@1683
   510
            }
slouken@1670
   511
        }
slouken@1670
   512
    }
slouken@1678
   513
    SDL_PublicSurface =
slouken@1678
   514
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
slouken@1670
   515
slouken@1670
   516
    /* Clear the surface for display */
slouken@1678
   517
    SDL_FillRect(SDL_PublicSurface, NULL, 0);
slouken@1670
   518
slouken@1678
   519
    /* We're finally done! */
slouken@1678
   520
    return SDL_PublicSurface;
slouken@1670
   521
}
slouken@1670
   522
slouken@1670
   523
SDL_Surface *
slouken@1668
   524
SDL_GetVideoSurface(void)
slouken@1667
   525
{
slouken@1678
   526
    return SDL_PublicSurface;
slouken@1667
   527
}
slouken@1667
   528
slouken@1670
   529
SDL_Surface *
slouken@1670
   530
SDL_DisplayFormat(SDL_Surface * surface)
slouken@1670
   531
{
slouken@1670
   532
    Uint32 flags;
slouken@1670
   533
slouken@1670
   534
    if (!SDL_PublicSurface) {
slouken@1670
   535
        SDL_SetError("No video mode has been set");
slouken@1678
   536
        return NULL;
slouken@1670
   537
    }
slouken@1678
   538
slouken@1670
   539
    /* Set the flags appropriate for copying to display surface */
slouken@1678
   540
    flags = SDL_SWSURFACE;
slouken@1670
   541
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1670
   542
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1670
   543
    flags |= SDL_RLEACCELOK;
slouken@1670
   544
#else
slouken@1670
   545
    flags |=
slouken@1670
   546
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   547
#endif
slouken@1678
   548
    return SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags);
slouken@1670
   549
}
slouken@1670
   550
slouken@1670
   551
SDL_Surface *
slouken@1670
   552
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@1670
   553
{
slouken@1670
   554
    SDL_PixelFormat *vf;
slouken@1670
   555
    SDL_PixelFormat *format;
slouken@1670
   556
    SDL_Surface *converted;
slouken@1670
   557
    Uint32 flags;
slouken@1670
   558
    /* default to ARGB8888 */
slouken@1670
   559
    Uint32 amask = 0xff000000;
slouken@1670
   560
    Uint32 rmask = 0x00ff0000;
slouken@1670
   561
    Uint32 gmask = 0x0000ff00;
slouken@1670
   562
    Uint32 bmask = 0x000000ff;
slouken@1670
   563
slouken@1670
   564
    if (!SDL_PublicSurface) {
slouken@1670
   565
        SDL_SetError("No video mode has been set");
slouken@1678
   566
        return NULL;
slouken@1670
   567
    }
slouken@1670
   568
    vf = SDL_PublicSurface->format;
slouken@1670
   569
slouken@1670
   570
    switch (vf->BytesPerPixel) {
slouken@1670
   571
    case 2:
slouken@1670
   572
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1670
   573
           For anything else (like ARGB4444) it doesn't matter
slouken@1670
   574
           since we have no special code for it anyway */
slouken@1670
   575
        if ((vf->Rmask == 0x1f) &&
slouken@1670
   576
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1670
   577
            rmask = 0xff;
slouken@1670
   578
            bmask = 0xff0000;
slouken@1670
   579
        }
slouken@1670
   580
        break;
slouken@1670
   581
slouken@1670
   582
    case 3:
slouken@1670
   583
    case 4:
slouken@1670
   584
        /* Keep the video format, as long as the high 8 bits are
slouken@1670
   585
           unused or alpha */
slouken@1670
   586
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1670
   587
            rmask = 0xff;
slouken@1670
   588
            bmask = 0xff0000;
slouken@1670
   589
        }
slouken@1670
   590
        break;
slouken@1670
   591
slouken@1670
   592
    default:
slouken@1670
   593
        /* We have no other optimised formats right now. When/if a new
slouken@1670
   594
           optimised alpha format is written, add the converter here */
slouken@1670
   595
        break;
slouken@1670
   596
    }
slouken@1670
   597
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@1670
   598
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1670
   599
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   600
    converted = SDL_ConvertSurface(surface, format, flags);
slouken@1670
   601
    SDL_FreeFormat(format);
slouken@1678
   602
    return converted;
slouken@1678
   603
}
slouken@1678
   604
slouken@1678
   605
int
slouken@1678
   606
SDL_Flip(SDL_Surface * screen)
slouken@1678
   607
{
slouken@1678
   608
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1678
   609
    return 0;
slouken@1670
   610
}
slouken@1670
   611
slouken@1670
   612
void
slouken@1670
   613
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@1670
   614
{
slouken@1670
   615
    if (screen) {
slouken@1670
   616
        SDL_Rect rect;
slouken@1670
   617
slouken@1670
   618
        /* Perform some checking */
slouken@1670
   619
        if (w == 0)
slouken@1670
   620
            w = screen->w;
slouken@1670
   621
        if (h == 0)
slouken@1670
   622
            h = screen->h;
slouken@1670
   623
        if ((int) (x + w) > screen->w)
slouken@1670
   624
            return;
slouken@1670
   625
        if ((int) (y + h) > screen->h)
slouken@1670
   626
            return;
slouken@1670
   627
slouken@1670
   628
        /* Fill the rectangle */
slouken@1670
   629
        rect.x = (Sint16) x;
slouken@1670
   630
        rect.y = (Sint16) y;
slouken@1670
   631
        rect.w = (Uint16) w;
slouken@1670
   632
        rect.h = (Uint16) h;
slouken@1670
   633
        SDL_UpdateRects(screen, 1, &rect);
slouken@1670
   634
    }
slouken@1670
   635
}
slouken@1670
   636
void
slouken@1670
   637
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@1670
   638
{
slouken@1670
   639
    int i;
slouken@1670
   640
slouken@1678
   641
    if (screen == SDL_ShadowSurface) {
slouken@1678
   642
        for (i = 0; i < numrects; ++i) {
slouken@1678
   643
            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
slouken@1678
   644
                          &rects[i]);
slouken@1670
   645
        }
slouken@1670
   646
slouken@1670
   647
        /* Fall through to video surface update */
slouken@1678
   648
        screen = SDL_VideoSurface;
slouken@1670
   649
    }
slouken@1678
   650
    if (screen == SDL_VideoSurface) {
slouken@1678
   651
        for (i = 0; i < numrects; ++i) {
slouken@1678
   652
            SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i],
slouken@1678
   653
                           SDL_TextureBlendMode_None,
slouken@1678
   654
                           SDL_TextureScaleMode_None);
slouken@1670
   655
        }
slouken@1678
   656
        SDL_RenderPresent();
slouken@1670
   657
    }
slouken@1670
   658
}
slouken@1670
   659
slouken@1667
   660
void
slouken@1668
   661
SDL_WM_SetCaption(const char *title, const char *icon)
slouken@1667
   662
{
slouken@1667
   663
    if (wm_title) {
slouken@1668
   664
        SDL_free(wm_title);
slouken@1667
   665
    } else {
slouken@1668
   666
        wm_title = SDL_strdup(title);
slouken@1667
   667
    }
slouken@1678
   668
    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
slouken@1667
   669
}
slouken@1667
   670
slouken@1667
   671
void
slouken@1668
   672
SDL_WM_GetCaption(char **title, char **icon)
slouken@1667
   673
{
slouken@1667
   674
    if (title) {
slouken@1667
   675
        *title = wm_title;
slouken@1667
   676
    }
slouken@1667
   677
    if (icon) {
slouken@1667
   678
        *icon = "";
slouken@1667
   679
    }
slouken@1667
   680
}
slouken@1667
   681
slouken@1667
   682
void
slouken@1668
   683
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@1667
   684
{
slouken@1667
   685
    /* FIXME */
slouken@1667
   686
}
slouken@1667
   687
slouken@1667
   688
int
slouken@1668
   689
SDL_WM_IconifyWindow(void)
slouken@1667
   690
{
slouken@1678
   691
    SDL_MinimizeWindow(SDL_VideoWindow);
slouken@1720
   692
    return 0;
slouken@1667
   693
}
slouken@1667
   694
slouken@1667
   695
int
slouken@1668
   696
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
slouken@1667
   697
{
slouken@1667
   698
    return 0;
slouken@1667
   699
}
slouken@1667
   700
slouken@1667
   701
SDL_GrabMode
slouken@1668
   702
SDL_WM_GrabInput(SDL_GrabMode mode)
slouken@1667
   703
{
slouken@1667
   704
    if (mode != SDL_GRAB_QUERY) {
slouken@1678
   705
        SDL_SetWindowGrab(SDL_VideoWindow, mode);
slouken@1667
   706
    }
slouken@1678
   707
    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
slouken@1667
   708
}
slouken@1667
   709
slouken@1680
   710
void
slouken@1680
   711
SDL_WarpMouse(Uint16 x, Uint16 y)
slouken@1680
   712
{
slouken@1680
   713
    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
slouken@1680
   714
}
slouken@1680
   715
slouken@1667
   716
Uint8
slouken@1668
   717
SDL_GetAppState(void)
slouken@1667
   718
{
slouken@1667
   719
    Uint8 state = 0;
slouken@1667
   720
    Uint32 flags = 0;
slouken@1667
   721
slouken@1678
   722
    flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1667
   723
    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
slouken@1667
   724
        state |= SDL_APPACTIVE;
slouken@1667
   725
    }
slouken@1667
   726
    if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
slouken@1667
   727
        state |= SDL_APPINPUTFOCUS;
slouken@1667
   728
    }
slouken@1667
   729
    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1667
   730
        state |= SDL_APPMOUSEFOCUS;
slouken@1667
   731
    }
slouken@1667
   732
    return state;
slouken@1667
   733
}
slouken@1667
   734
slouken@1667
   735
const SDL_version *
slouken@1668
   736
SDL_Linked_Version(void)
slouken@1667
   737
{
slouken@1667
   738
    static SDL_version version;
slouken@1668
   739
    SDL_VERSION(&version);
slouken@1667
   740
    return &version;
slouken@1667
   741
}
slouken@1667
   742
slouken@1667
   743
int
slouken@1682
   744
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
slouken@1668
   745
               int firstcolor, int ncolors)
slouken@1667
   746
{
slouken@1720
   747
    return SDL_SetColors(surface, colors, firstcolor, ncolors);
slouken@1667
   748
}
slouken@1667
   749
slouken@1667
   750
int
slouken@1683
   751
SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
slouken@1683
   752
              int ncolors)
slouken@1670
   753
{
slouken@1683
   754
    if (SDL_SetPaletteColors
slouken@1683
   755
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
slouken@1683
   756
        return 1;
slouken@1683
   757
    } else {
slouken@1683
   758
        return 0;
slouken@1670
   759
    }
slouken@1670
   760
}
slouken@1670
   761
slouken@1670
   762
int
slouken@1668
   763
SDL_GetWMInfo(SDL_SysWMinfo * info)
slouken@1667
   764
{
slouken@1678
   765
    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
slouken@1667
   766
}
slouken@1667
   767
slouken@1678
   768
#if 0
slouken@1670
   769
void
slouken@1670
   770
SDL_MoveCursor(int x, int y)
slouken@1670
   771
{
slouken@1670
   772
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   773
slouken@1670
   774
    /* Erase and update the current mouse position */
slouken@1670
   775
    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1670
   776
        /* Erase and redraw mouse cursor in new position */
slouken@1670
   777
        SDL_LockCursor();
slouken@1670
   778
        SDL_EraseCursor(SDL_VideoSurface);
slouken@1670
   779
        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@1670
   780
        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@1670
   781
        SDL_DrawCursor(SDL_VideoSurface);
slouken@1670
   782
        SDL_UnlockCursor();
slouken@1670
   783
    } else if (_this->MoveWMCursor) {
slouken@1670
   784
        _this->MoveWMCursor(_this, x, y);
slouken@1670
   785
    }
slouken@1670
   786
}
slouken@1670
   787
slouken@1670
   788
/* Keep track of the current cursor colors */
slouken@1670
   789
static int palette_changed = 1;
slouken@1670
   790
static Uint8 pixels8[2];
slouken@1670
   791
slouken@1670
   792
void
slouken@1670
   793
SDL_CursorPaletteChanged(void)
slouken@1670
   794
{
slouken@1670
   795
    palette_changed = 1;
slouken@1670
   796
}
slouken@1670
   797
slouken@1670
   798
void
slouken@1670
   799
SDL_MouseRect(SDL_Rect * area)
slouken@1670
   800
{
slouken@1670
   801
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   802
    int clip_diff;
slouken@1670
   803
slouken@1670
   804
    *area = SDL_cursor->area;
slouken@1670
   805
    if (area->x < 0) {
slouken@1670
   806
        area->w += area->x;
slouken@1670
   807
        area->x = 0;
slouken@1670
   808
    }
slouken@1670
   809
    if (area->y < 0) {
slouken@1670
   810
        area->h += area->y;
slouken@1670
   811
        area->y = 0;
slouken@1670
   812
    }
slouken@1670
   813
    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
slouken@1670
   814
    if (clip_diff > 0) {
slouken@1670
   815
        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
slouken@1670
   816
    }
slouken@1670
   817
    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
slouken@1670
   818
    if (clip_diff > 0) {
slouken@1670
   819
        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
slouken@1670
   820
    }
slouken@1670
   821
}
slouken@1670
   822
slouken@1670
   823
static void
slouken@1670
   824
SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   825
{
slouken@1670
   826
    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@1670
   827
    int i, w, h;
slouken@1670
   828
    Uint8 *data, datab;
slouken@1670
   829
    Uint8 *mask, maskb;
slouken@1670
   830
slouken@1670
   831
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   832
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   833
    switch (screen->format->BytesPerPixel) {
slouken@1670
   834
slouken@1670
   835
    case 1:
slouken@1670
   836
        {
slouken@1670
   837
            Uint8 *dst;
slouken@1670
   838
            int dstskip;
slouken@1670
   839
slouken@1670
   840
            if (palette_changed) {
slouken@1670
   841
                pixels8[0] =
slouken@1670
   842
                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   843
                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   844
                palette_changed = 0;
slouken@1670
   845
            }
slouken@1670
   846
            dst = (Uint8 *) screen->pixels +
slouken@1670
   847
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   848
                SDL_cursor->area.x;
slouken@1670
   849
            dstskip = screen->pitch - area->w;
slouken@1670
   850
slouken@1670
   851
            for (h = area->h; h; h--) {
slouken@1670
   852
                for (w = area->w / 8; w; w--) {
slouken@1670
   853
                    maskb = *mask++;
slouken@1670
   854
                    datab = *data++;
slouken@1670
   855
                    for (i = 0; i < 8; ++i) {
slouken@1670
   856
                        if (maskb & 0x80) {
slouken@1670
   857
                            *dst = pixels8[datab >> 7];
slouken@1670
   858
                        }
slouken@1670
   859
                        maskb <<= 1;
slouken@1670
   860
                        datab <<= 1;
slouken@1670
   861
                        dst++;
slouken@1670
   862
                    }
slouken@1670
   863
                }
slouken@1670
   864
                dst += dstskip;
slouken@1670
   865
            }
slouken@1670
   866
        }
slouken@1670
   867
        break;
slouken@1670
   868
slouken@1670
   869
    case 2:
slouken@1670
   870
        {
slouken@1670
   871
            Uint16 *dst;
slouken@1670
   872
            int dstskip;
slouken@1670
   873
slouken@1670
   874
            dst = (Uint16 *) screen->pixels +
slouken@1670
   875
                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
slouken@1670
   876
                SDL_cursor->area.x;
slouken@1670
   877
            dstskip = (screen->pitch / 2) - area->w;
slouken@1670
   878
slouken@1670
   879
            for (h = area->h; h; h--) {
slouken@1670
   880
                for (w = area->w / 8; w; w--) {
slouken@1670
   881
                    maskb = *mask++;
slouken@1670
   882
                    datab = *data++;
slouken@1670
   883
                    for (i = 0; i < 8; ++i) {
slouken@1670
   884
                        if (maskb & 0x80) {
slouken@1670
   885
                            *dst = (Uint16) pixels[datab >> 7];
slouken@1670
   886
                        }
slouken@1670
   887
                        maskb <<= 1;
slouken@1670
   888
                        datab <<= 1;
slouken@1670
   889
                        dst++;
slouken@1670
   890
                    }
slouken@1670
   891
                }
slouken@1670
   892
                dst += dstskip;
slouken@1670
   893
            }
slouken@1670
   894
        }
slouken@1670
   895
        break;
slouken@1670
   896
slouken@1670
   897
    case 3:
slouken@1670
   898
        {
slouken@1670
   899
            Uint8 *dst;
slouken@1670
   900
            int dstskip;
slouken@1670
   901
slouken@1670
   902
            dst = (Uint8 *) screen->pixels +
slouken@1670
   903
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   904
                SDL_cursor->area.x * 3;
slouken@1670
   905
            dstskip = screen->pitch - area->w * 3;
slouken@1670
   906
slouken@1670
   907
            for (h = area->h; h; h--) {
slouken@1670
   908
                for (w = area->w / 8; w; w--) {
slouken@1670
   909
                    maskb = *mask++;
slouken@1670
   910
                    datab = *data++;
slouken@1670
   911
                    for (i = 0; i < 8; ++i) {
slouken@1670
   912
                        if (maskb & 0x80) {
slouken@1670
   913
                            SDL_memset(dst, pixels[datab >> 7], 3);
slouken@1670
   914
                        }
slouken@1670
   915
                        maskb <<= 1;
slouken@1670
   916
                        datab <<= 1;
slouken@1670
   917
                        dst += 3;
slouken@1670
   918
                    }
slouken@1670
   919
                }
slouken@1670
   920
                dst += dstskip;
slouken@1670
   921
            }
slouken@1670
   922
        }
slouken@1670
   923
        break;
slouken@1670
   924
slouken@1670
   925
    case 4:
slouken@1670
   926
        {
slouken@1670
   927
            Uint32 *dst;
slouken@1670
   928
            int dstskip;
slouken@1670
   929
slouken@1670
   930
            dst = (Uint32 *) screen->pixels +
slouken@1670
   931
                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
slouken@1670
   932
                SDL_cursor->area.x;
slouken@1670
   933
            dstskip = (screen->pitch / 4) - area->w;
slouken@1670
   934
slouken@1670
   935
            for (h = area->h; h; h--) {
slouken@1670
   936
                for (w = area->w / 8; w; w--) {
slouken@1670
   937
                    maskb = *mask++;
slouken@1670
   938
                    datab = *data++;
slouken@1670
   939
                    for (i = 0; i < 8; ++i) {
slouken@1670
   940
                        if (maskb & 0x80) {
slouken@1670
   941
                            *dst = pixels[datab >> 7];
slouken@1670
   942
                        }
slouken@1670
   943
                        maskb <<= 1;
slouken@1670
   944
                        datab <<= 1;
slouken@1670
   945
                        dst++;
slouken@1670
   946
                    }
slouken@1670
   947
                }
slouken@1670
   948
                dst += dstskip;
slouken@1670
   949
            }
slouken@1670
   950
        }
slouken@1670
   951
        break;
slouken@1670
   952
    }
slouken@1670
   953
}
slouken@1670
   954
slouken@1670
   955
static void
slouken@1670
   956
SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   957
{
slouken@1670
   958
    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@1670
   959
    int h;
slouken@1670
   960
    int x, minx, maxx;
slouken@1670
   961
    Uint8 *data, datab = 0;
slouken@1670
   962
    Uint8 *mask, maskb = 0;
slouken@1670
   963
    Uint8 *dst;
slouken@1670
   964
    int dstbpp, dstskip;
slouken@1670
   965
slouken@1670
   966
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   967
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   968
    dstbpp = screen->format->BytesPerPixel;
slouken@1670
   969
    dst = (Uint8 *) screen->pixels +
slouken@1670
   970
        (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   971
        SDL_cursor->area.x * dstbpp;
slouken@1670
   972
    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
slouken@1670
   973
slouken@1670
   974
    minx = area->x;
slouken@1670
   975
    maxx = area->x + area->w;
slouken@1670
   976
    if (screen->format->BytesPerPixel == 1) {
slouken@1670
   977
        if (palette_changed) {
slouken@1670
   978
            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   979
            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   980
            palette_changed = 0;
slouken@1670
   981
        }
slouken@1670
   982
        for (h = area->h; h; h--) {
slouken@1670
   983
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
   984
                if ((x % 8) == 0) {
slouken@1670
   985
                    maskb = *mask++;
slouken@1670
   986
                    datab = *data++;
slouken@1670
   987
                }
slouken@1670
   988
                if ((x >= minx) && (x < maxx)) {
slouken@1670
   989
                    if (maskb & 0x80) {
slouken@1670
   990
                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
slouken@1670
   991
                    }
slouken@1670
   992
                }
slouken@1670
   993
                maskb <<= 1;
slouken@1670
   994
                datab <<= 1;
slouken@1670
   995
                dst += dstbpp;
slouken@1670
   996
            }
slouken@1670
   997
            dst += dstskip;
slouken@1670
   998
        }
slouken@1670
   999
    } else {
slouken@1670
  1000
        for (h = area->h; h; h--) {
slouken@1670
  1001
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
  1002
                if ((x % 8) == 0) {
slouken@1670
  1003
                    maskb = *mask++;
slouken@1670
  1004
                    datab = *data++;
slouken@1670
  1005
                }
slouken@1670
  1006
                if ((x >= minx) && (x < maxx)) {
slouken@1670
  1007
                    if (maskb & 0x80) {
slouken@1670
  1008
                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
slouken@1670
  1009
                    }
slouken@1670
  1010
                }
slouken@1670
  1011
                maskb <<= 1;
slouken@1670
  1012
                datab <<= 1;
slouken@1670
  1013
                dst += dstbpp;
slouken@1670
  1014
            }
slouken@1670
  1015
            dst += dstskip;
slouken@1670
  1016
        }
slouken@1670
  1017
    }
slouken@1670
  1018
}
slouken@1670
  1019
slouken@1670
  1020
/* This handles the ugly work of converting the saved cursor background from
slouken@1670
  1021
   the pixel format of the shadow surface to that of the video surface.
slouken@1670
  1022
   This is only necessary when blitting from a shadow surface of a different
slouken@1670
  1023
   pixel format than the video surface, and using a software rendered cursor.
slouken@1670
  1024
*/
slouken@1670
  1025
static void
slouken@1670
  1026
SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
slouken@1670
  1027
{
slouken@1670
  1028
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1029
    SDL_BlitInfo info;
slouken@1670
  1030
    SDL_loblit RunBlit;
slouken@1670
  1031
slouken@1670
  1032
    /* Make sure we can steal the blit mapping */
slouken@1670
  1033
    if (screen->map->dst != SDL_VideoSurface) {
slouken@1670
  1034
        return;
slouken@1670
  1035
    }
slouken@1670
  1036
slouken@1670
  1037
    /* Set up the blit information */
slouken@1670
  1038
    info.s_pixels = SDL_cursor->save[1];
slouken@1670
  1039
    info.s_width = w;
slouken@1670
  1040
    info.s_height = h;
slouken@1670
  1041
    info.s_skip = 0;
slouken@1670
  1042
    info.d_pixels = SDL_cursor->save[0];
slouken@1670
  1043
    info.d_width = w;
slouken@1670
  1044
    info.d_height = h;
slouken@1670
  1045
    info.d_skip = 0;
slouken@1670
  1046
    info.aux_data = screen->map->sw_data->aux_data;
slouken@1670
  1047
    info.src = screen->format;
slouken@1670
  1048
    info.table = screen->map->table;
slouken@1670
  1049
    info.dst = SDL_VideoSurface->format;
slouken@1670
  1050
    RunBlit = screen->map->sw_data->blit;
slouken@1670
  1051
slouken@1670
  1052
    /* Run the actual software blit */
slouken@1670
  1053
    RunBlit(&info);
slouken@1670
  1054
}
slouken@1670
  1055
slouken@1670
  1056
void
slouken@1670
  1057
SDL_DrawCursorNoLock(SDL_Surface * screen)
slouken@1670
  1058
{
slouken@1670
  1059
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1060
    SDL_Rect area;
slouken@1670
  1061
slouken@1670
  1062
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1063
    SDL_MouseRect(&area);
slouken@1670
  1064
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1065
        return;
slouken@1670
  1066
    }
slouken@1670
  1067
slouken@1670
  1068
    /* Copy mouse background */
slouken@1670
  1069
    {
slouken@1670
  1070
        int w, h, screenbpp;
slouken@1670
  1071
        Uint8 *src, *dst;
slouken@1670
  1072
slouken@1670
  1073
        /* Set up the copy pointers */
slouken@1670
  1074
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1075
        if ((screen == SDL_VideoSurface) ||
slouken@1670
  1076
            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
slouken@1670
  1077
            dst = SDL_cursor->save[0];
slouken@1670
  1078
        } else {
slouken@1670
  1079
            dst = SDL_cursor->save[1];
slouken@1670
  1080
        }
slouken@1670
  1081
        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1082
            area.x * screenbpp;
slouken@1670
  1083
slouken@1670
  1084
        /* Perform the copy */
slouken@1670
  1085
        w = area.w * screenbpp;
slouken@1670
  1086
        h = area.h;
slouken@1670
  1087
        while (h--) {
slouken@1670
  1088
            SDL_memcpy(dst, src, w);
slouken@1670
  1089
            dst += w;
slouken@1670
  1090
            src += screen->pitch;
slouken@1670
  1091
        }
slouken@1670
  1092
    }
slouken@1670
  1093
slouken@1670
  1094
    /* Draw the mouse cursor */
slouken@1670
  1095
    area.x -= SDL_cursor->area.x;
slouken@1670
  1096
    area.y -= SDL_cursor->area.y;
slouken@1670
  1097
    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
slouken@1670
  1098
        SDL_DrawCursorFast(screen, &area);
slouken@1670
  1099
    } else {
slouken@1670
  1100
        SDL_DrawCursorSlow(screen, &area);
slouken@1670
  1101
    }
slouken@1670
  1102
}
slouken@1670
  1103
slouken@1670
  1104
void
slouken@1670
  1105
SDL_DrawCursor(SDL_Surface * screen)
slouken@1670
  1106
{
slouken@1670
  1107
    /* Lock the screen if necessary */
slouken@1670
  1108
    if (screen == NULL) {
slouken@1670
  1109
        return;
slouken@1670
  1110
    }
slouken@1670
  1111
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1112
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1113
            return;
slouken@1670
  1114
        }
slouken@1670
  1115
    }
slouken@1670
  1116
slouken@1670
  1117
    SDL_DrawCursorNoLock(screen);
slouken@1670
  1118
slouken@1670
  1119
    /* Unlock the screen and update if necessary */
slouken@1670
  1120
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1121
        SDL_UnlockSurface(screen);
slouken@1670
  1122
    }
slouken@1670
  1123
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1124
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1125
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1126
        SDL_Window *window;
slouken@1670
  1127
        SDL_Rect area;
slouken@1670
  1128
slouken@1670
  1129
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1130
        if (!window) {
slouken@1670
  1131
            return;
slouken@1670
  1132
        }
slouken@1670
  1133
slouken@1670
  1134
        SDL_MouseRect(&area);
slouken@1670
  1135
slouken@1670
  1136
        if (_this->UpdateWindowSurface) {
slouken@1670
  1137
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1138
        }
slouken@1670
  1139
    }
slouken@1670
  1140
}
slouken@1670
  1141
slouken@1670
  1142
void
slouken@1670
  1143
SDL_EraseCursorNoLock(SDL_Surface * screen)
slouken@1670
  1144
{
slouken@1670
  1145
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1146
    SDL_Window *window;
slouken@1670
  1147
    SDL_Rect area;
slouken@1670
  1148
slouken@1670
  1149
    /* Get the window associated with the surface */
slouken@1670
  1150
    window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1151
    if (!window || !window->surface) {
slouken@1670
  1152
        return;
slouken@1670
  1153
    }
slouken@1670
  1154
slouken@1670
  1155
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1156
    SDL_MouseRect(&area);
slouken@1670
  1157
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1158
        return;
slouken@1670
  1159
    }
slouken@1670
  1160
slouken@1670
  1161
    /* Copy mouse background */
slouken@1670
  1162
    {
slouken@1670
  1163
        int w, h, screenbpp;
slouken@1670
  1164
        Uint8 *src, *dst;
slouken@1670
  1165
slouken@1670
  1166
        /* Set up the copy pointers */
slouken@1670
  1167
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1168
        if ((screen->flags & SDL_SCREEN_SURFACE) ||
slouken@1670
  1169
            FORMAT_EQUAL(screen->format, window->surface->format)) {
slouken@1670
  1170
            src = SDL_cursor->save[0];
slouken@1670
  1171
        } else {
slouken@1670
  1172
            src = SDL_cursor->save[1];
slouken@1670
  1173
        }
slouken@1670
  1174
        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1175
            area.x * screenbpp;
slouken@1670
  1176
slouken@1670
  1177
        /* Perform the copy */
slouken@1670
  1178
        w = area.w * screenbpp;
slouken@1670
  1179
        h = area.h;
slouken@1670
  1180
        while (h--) {
slouken@1670
  1181
            SDL_memcpy(dst, src, w);
slouken@1670
  1182
            src += w;
slouken@1670
  1183
            dst += screen->pitch;
slouken@1670
  1184
        }
slouken@1670
  1185
slouken@1670
  1186
        /* Perform pixel conversion on cursor background */
slouken@1670
  1187
        if (src > SDL_cursor->save[1]) {
slouken@1670
  1188
            SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@1670
  1189
        }
slouken@1670
  1190
    }
slouken@1670
  1191
}
slouken@1670
  1192
slouken@1670
  1193
void
slouken@1670
  1194
SDL_EraseCursor(SDL_Surface * screen)
slouken@1670
  1195
{
slouken@1670
  1196
    /* Lock the screen if necessary */
slouken@1670
  1197
    if (screen == NULL) {
slouken@1670
  1198
        return;
slouken@1670
  1199
    }
slouken@1670
  1200
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1201
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1202
            return;
slouken@1670
  1203
        }
slouken@1670
  1204
    }
slouken@1670
  1205
slouken@1670
  1206
    SDL_EraseCursorNoLock(screen);
slouken@1670
  1207
slouken@1670
  1208
    /* Unlock the screen and update if necessary */
slouken@1670
  1209
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1210
        SDL_UnlockSurface(screen);
slouken@1670
  1211
    }
slouken@1670
  1212
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1213
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1214
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1215
        SDL_Window *window;
slouken@1670
  1216
        SDL_Rect area;
slouken@1670
  1217
slouken@1670
  1218
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1219
        if (!window) {
slouken@1670
  1220
            return;
slouken@1670
  1221
        }
slouken@1670
  1222
slouken@1670
  1223
        SDL_MouseRect(&area);
slouken@1670
  1224
slouken@1670
  1225
        if (_this->UpdateWindowSurface) {
slouken@1670
  1226
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1227
        }
slouken@1670
  1228
    }
slouken@1670
  1229
}
slouken@1670
  1230
slouken@1670
  1231
/* Reset the cursor on video mode change
slouken@1670
  1232
   FIXME:  Keep track of all cursors, and reset them all.
slouken@1670
  1233
 */
slouken@1670
  1234
void
slouken@1670
  1235
SDL_ResetCursor(void)
slouken@1670
  1236
{
slouken@1670
  1237
    int savelen;
slouken@1670
  1238
slouken@1670
  1239
    if (SDL_cursor) {
slouken@1670
  1240
        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
slouken@1670
  1241
        SDL_cursor->area.x = 0;
slouken@1670
  1242
        SDL_cursor->area.y = 0;
slouken@1670
  1243
        SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@1670
  1244
    }
slouken@1670
  1245
}
slouken@1684
  1246
#endif
slouken@1684
  1247
slouken@1684
  1248
struct private_yuvhwdata
slouken@1684
  1249
{
slouken@1684
  1250
    Uint16 pitches[3];
slouken@1684
  1251
    Uint8 *planes[3];
slouken@1684
  1252
slouken@1684
  1253
    SDL_TextureID textureID;
slouken@1684
  1254
};
slouken@1670
  1255
slouken@1675
  1256
SDL_Overlay *
slouken@1675
  1257
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
slouken@1675
  1258
{
slouken@1675
  1259
    SDL_Overlay *overlay;
slouken@1684
  1260
    Uint32 texture_format;
slouken@1675
  1261
slouken@1684
  1262
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
slouken@1675
  1263
        SDL_SetError("YUV overlays are not supported in OpenGL mode");
slouken@1675
  1264
        return NULL;
slouken@1675
  1265
    }
slouken@1675
  1266
slouken@1684
  1267
    if (display != SDL_PublicSurface) {
slouken@1684
  1268
        SDL_SetError("YUV display is only supported on the screen surface");
slouken@1684
  1269
        return NULL;
slouken@1675
  1270
    }
slouken@1684
  1271
slouken@1684
  1272
    switch (format) {
slouken@1684
  1273
    case SDL_YV12_OVERLAY:
slouken@1684
  1274
        texture_format = SDL_PixelFormat_YV12;
slouken@1684
  1275
        break;
slouken@1684
  1276
    case SDL_IYUV_OVERLAY:
slouken@1684
  1277
        texture_format = SDL_PixelFormat_IYUV;
slouken@1684
  1278
        break;
slouken@1684
  1279
    case SDL_YUY2_OVERLAY:
slouken@1684
  1280
        texture_format = SDL_PixelFormat_YUY2;
slouken@1684
  1281
        break;
slouken@1684
  1282
    case SDL_UYVY_OVERLAY:
slouken@1684
  1283
        texture_format = SDL_PixelFormat_UYVY;
slouken@1684
  1284
        break;
slouken@1684
  1285
    case SDL_YVYU_OVERLAY:
slouken@1684
  1286
        texture_format = SDL_PixelFormat_YVYU;
slouken@1684
  1287
        break;
slouken@1684
  1288
    default:
slouken@1684
  1289
        SDL_SetError("Unknown YUV format");
slouken@1684
  1290
        return NULL;
slouken@1675
  1291
    }
slouken@1684
  1292
slouken@1684
  1293
    overlay = (SDL_Overlay *) SDL_malloc(sizeof(*overlay));
slouken@1684
  1294
    if (!overlay) {
slouken@1684
  1295
        SDL_OutOfMemory();
slouken@1684
  1296
        return NULL;
slouken@1675
  1297
    }
slouken@1684
  1298
    SDL_zerop(overlay);
slouken@1684
  1299
slouken@1684
  1300
    overlay->hwdata =
slouken@1684
  1301
        (struct private_yuvhwdata *) SDL_malloc(sizeof(*overlay->hwdata));
slouken@1684
  1302
    if (!overlay->hwdata) {
slouken@1684
  1303
        SDL_free(overlay);
slouken@1684
  1304
        SDL_OutOfMemory();
slouken@1684
  1305
        return NULL;
slouken@1684
  1306
    }
slouken@1684
  1307
slouken@1684
  1308
    overlay->format = format;
slouken@1684
  1309
    overlay->w = w;
slouken@1684
  1310
    overlay->h = h;
slouken@1684
  1311
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
slouken@1684
  1312
        overlay->planes = 3;
slouken@1684
  1313
    } else {
slouken@1684
  1314
        overlay->planes = 1;
slouken@1684
  1315
    }
slouken@1684
  1316
    overlay->pitches = overlay->hwdata->pitches;
slouken@1684
  1317
    overlay->pixels = overlay->hwdata->planes;
slouken@1684
  1318
slouken@1684
  1319
    switch (format) {
slouken@1684
  1320
    case SDL_YV12_OVERLAY:
slouken@1684
  1321
    case SDL_IYUV_OVERLAY:
slouken@1684
  1322
        overlay->pitches[0] = overlay->w;
slouken@1684
  1323
        overlay->pitches[1] = overlay->w / 2;
slouken@1684
  1324
        overlay->pitches[2] = overlay->w / 2;
slouken@1684
  1325
        break;
slouken@1684
  1326
    case SDL_YUY2_OVERLAY:
slouken@1684
  1327
    case SDL_UYVY_OVERLAY:
slouken@1684
  1328
    case SDL_YVYU_OVERLAY:
slouken@1685
  1329
        overlay->pitches[0] = overlay->w * 2;
slouken@1684
  1330
        break;
slouken@1684
  1331
    }
slouken@1684
  1332
slouken@1684
  1333
    overlay->hwdata->textureID =
slouken@1684
  1334
        SDL_CreateTexture(texture_format, SDL_TextureAccess_Local, w, h);
slouken@1684
  1335
    if (!overlay->hwdata->textureID) {
slouken@1684
  1336
        SDL_FreeYUVOverlay(overlay);
slouken@1684
  1337
        return NULL;
slouken@1684
  1338
    }
slouken@1684
  1339
slouken@1675
  1340
    return overlay;
slouken@1675
  1341
}
slouken@1675
  1342
slouken@1675
  1343
int
slouken@1675
  1344
SDL_LockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1345
{
slouken@1684
  1346
    void *pixels;
slouken@1684
  1347
    int pitch;
slouken@1684
  1348
    if (SDL_LockTexture(overlay->hwdata->textureID, NULL, 1, &pixels, &pitch)
slouken@1684
  1349
        < 0) {
slouken@1684
  1350
        return -1;
slouken@1684
  1351
    }
slouken@1684
  1352
    switch (overlay->format) {
slouken@1684
  1353
    case SDL_YV12_OVERLAY:
slouken@1684
  1354
    case SDL_IYUV_OVERLAY:
slouken@1684
  1355
        overlay->pixels[0] = (Uint8 *) pixels;
slouken@1684
  1356
        overlay->pixels[1] =
slouken@1684
  1357
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
slouken@1684
  1358
        overlay->pixels[2] =
slouken@1684
  1359
            overlay->pixels[1] + overlay->pitches[1] * overlay->h;
slouken@1684
  1360
        break;
slouken@1684
  1361
    case SDL_YUY2_OVERLAY:
slouken@1684
  1362
    case SDL_UYVY_OVERLAY:
slouken@1684
  1363
    case SDL_YVYU_OVERLAY:
slouken@1684
  1364
        overlay->pixels[0] = (Uint8 *) pixels;
slouken@1684
  1365
        break;
slouken@1684
  1366
    }
slouken@1684
  1367
    return 0;
slouken@1675
  1368
}
slouken@1675
  1369
slouken@1675
  1370
void
slouken@1675
  1371
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1372
{
slouken@1684
  1373
    SDL_UnlockTexture(overlay->hwdata->textureID);
slouken@1675
  1374
}
slouken@1675
  1375
slouken@1675
  1376
int
slouken@1675
  1377
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@1675
  1378
{
slouken@1684
  1379
    if (SDL_RenderCopy(overlay->hwdata->textureID, NULL, dstrect,
slouken@1684
  1380
                       SDL_TextureBlendMode_None,
slouken@1684
  1381
                       SDL_TextureScaleMode_Fast) < 0) {
slouken@1684
  1382
        return -1;
slouken@1675
  1383
    }
slouken@1684
  1384
    SDL_RenderPresent();
slouken@1720
  1385
    return 0;
slouken@1675
  1386
}
slouken@1675
  1387
slouken@1675
  1388
void
slouken@1675
  1389
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1390
{
slouken@1675
  1391
    if (overlay) {
slouken@1684
  1392
        if (overlay->hwdata) {
slouken@1684
  1393
            if (overlay->hwdata->textureID) {
slouken@1684
  1394
                SDL_DestroyTexture(overlay->hwdata->textureID);
slouken@1684
  1395
            }
slouken@1684
  1396
            SDL_free(overlay->hwdata);
slouken@1675
  1397
        }
slouken@1675
  1398
        SDL_free(overlay);
slouken@1675
  1399
    }
slouken@1675
  1400
}
slouken@1675
  1401
slouken@1667
  1402
/* vi: set ts=4 sw=4 expandtab: */