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