src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 16 Jun 2006 06:00:31 +0000
branchSDL-1.3
changeset 1682 7ae8018b2e5d
parent 1681 80a5e6a4e1e2
child 1683 396a35389351
permissions -rw-r--r--
Default palette entries to white, instead of black.
More palettized video mode support...
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* The high-level video driver subsystem */
slouken@0
    25
slouken@0
    26
#include "SDL.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_blit.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@1676
    30
#include "SDL_renderer_sw.h"
slouken@1361
    31
#include "../events/SDL_sysevents.h"
slouken@1361
    32
#include "../events/SDL_events_c.h"
slouken@0
    33
slouken@0
    34
/* Available video drivers */
slouken@0
    35
static VideoBootStrap *bootstrap[] = {
slouken@1361
    36
#if SDL_VIDEO_DRIVER_QUARTZ
slouken@1662
    37
    &QZ_bootstrap,
icculus@1133
    38
#endif
slouken@1361
    39
#if SDL_VIDEO_DRIVER_X11
slouken@1662
    40
    &X11_bootstrap,
slouken@0
    41
#endif
slouken@1361
    42
#if SDL_VIDEO_DRIVER_DGA
slouken@1662
    43
    &DGA_bootstrap,
slouken@0
    44
#endif
slouken@1361
    45
#if SDL_VIDEO_DRIVER_NANOX
slouken@1662
    46
    &NX_bootstrap,
slouken@30
    47
#endif
slouken@1361
    48
#if SDL_VIDEO_DRIVER_IPOD
slouken@1662
    49
    &iPod_bootstrap,
icculus@1140
    50
#endif
slouken@1361
    51
#if SDL_VIDEO_DRIVER_QTOPIA
slouken@1662
    52
    &Qtopia_bootstrap,
slouken@567
    53
#endif
slouken@1361
    54
#if SDL_VIDEO_DRIVER_WSCONS
slouken@1662
    55
    &WSCONS_bootstrap,
icculus@1187
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_FBCON
slouken@1662
    58
    &FBCON_bootstrap,
slouken@0
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@1662
    61
    &DirectFB_bootstrap,
slouken@167
    62
#endif
slouken@1361
    63
#if SDL_VIDEO_DRIVER_PS2GS
slouken@1662
    64
    &PS2GS_bootstrap,
slouken@0
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_GGI
slouken@1662
    67
    &GGI_bootstrap,
slouken@0
    68
#endif
slouken@1361
    69
#if SDL_VIDEO_DRIVER_VGL
slouken@1662
    70
    &VGL_bootstrap,
slouken@75
    71
#endif
slouken@1361
    72
#if SDL_VIDEO_DRIVER_SVGALIB
slouken@1662
    73
    &SVGALIB_bootstrap,
slouken@0
    74
#endif
slouken@1361
    75
#if SDL_VIDEO_DRIVER_GAPI
slouken@1662
    76
    &GAPI_bootstrap,
slouken@1361
    77
#endif
slouken@1361
    78
#if SDL_VIDEO_DRIVER_WINDIB
slouken@1662
    79
    &WINDIB_bootstrap,
slouken@1361
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_DDRAW
slouken@1662
    82
    &DIRECTX_bootstrap,
slouken@1361
    83
#endif
slouken@1361
    84
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@1662
    85
    &BWINDOW_bootstrap,
slouken@1361
    86
#endif
slouken@1361
    87
#if SDL_VIDEO_DRIVER_TOOLBOX
slouken@1662
    88
    &TOOLBOX_bootstrap,
slouken@1361
    89
#endif
slouken@1361
    90
#if SDL_VIDEO_DRIVER_DRAWSPROCKET
slouken@1662
    91
    &DSp_bootstrap,
slouken@1361
    92
#endif
slouken@1361
    93
#if SDL_VIDEO_DRIVER_CYBERGRAPHICS
slouken@1662
    94
    &CGX_bootstrap,
slouken@1361
    95
#endif
slouken@1361
    96
#if SDL_VIDEO_DRIVER_PHOTON
slouken@1662
    97
    &ph_bootstrap,
slouken@1361
    98
#endif
slouken@1361
    99
#if SDL_VIDEO_DRIVER_EPOC
slouken@1662
   100
    &EPOC_bootstrap,
slouken@1361
   101
#endif
slouken@1361
   102
#if SDL_VIDEO_DRIVER_XBIOS
slouken@1662
   103
    &XBIOS_bootstrap,
slouken@1361
   104
#endif
slouken@1361
   105
#if SDL_VIDEO_DRIVER_GEM
slouken@1662
   106
    &GEM_bootstrap,
slouken@1361
   107
#endif
slouken@1361
   108
#if SDL_VIDEO_DRIVER_PICOGUI
slouken@1662
   109
    &PG_bootstrap,
slouken@1361
   110
#endif
slouken@1361
   111
#if SDL_VIDEO_DRIVER_DC
slouken@1662
   112
    &DC_bootstrap,
slouken@1361
   113
#endif
slouken@1361
   114
#if SDL_VIDEO_DRIVER_RISCOS
slouken@1662
   115
    &RISCOS_bootstrap,
slouken@1361
   116
#endif
slouken@1361
   117
#if SDL_VIDEO_DRIVER_OS2FS
slouken@1662
   118
    &OS2FSLib_bootstrap,
slouken@1361
   119
#endif
slouken@1361
   120
#if SDL_VIDEO_DRIVER_AALIB
slouken@1662
   121
    &AALIB_bootstrap,
slouken@0
   122
#endif
slouken@1361
   123
#if SDL_VIDEO_DRIVER_DUMMY
slouken@1662
   124
    &DUMMY_bootstrap,
slouken@610
   125
#endif
slouken@1658
   126
#if SDL_VIDEO_DRIVER_GLSDL
slouken@1662
   127
    &glSDL_bootstrap,
slouken@1658
   128
#endif
slouken@1662
   129
    NULL
slouken@0
   130
};
slouken@173
   131
slouken@1662
   132
static SDL_VideoDevice *_this = NULL;
slouken@0
   133
slouken@0
   134
/* Various local functions */
slouken@1668
   135
int SDL_VideoInit(const char *driver_name, Uint32 flags);
slouken@1668
   136
void SDL_VideoQuit(void);
slouken@0
   137
slouken@1662
   138
static int
slouken@1668
   139
cmpmodes(const void *A, const void *B)
slouken@1662
   140
{
slouken@1662
   141
    SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
slouken@1662
   142
    SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   143
slouken@1662
   144
    if (a.w != b.w) {
slouken@1662
   145
        return b.w - a.w;
slouken@1662
   146
    }
slouken@1662
   147
    if (a.h != b.h) {
slouken@1662
   148
        return b.h - a.h;
slouken@1662
   149
    }
slouken@1668
   150
    if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
slouken@1668
   151
        return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
slouken@1662
   152
    }
slouken@1662
   153
    if (a.refresh_rate != b.refresh_rate) {
slouken@1662
   154
        return b.refresh_rate - a.refresh_rate;
slouken@1662
   155
    }
slouken@1662
   156
    return 0;
slouken@1660
   157
}
slouken@1660
   158
slouken@1662
   159
int
slouken@1668
   160
SDL_GetNumVideoDrivers(void)
slouken@1660
   161
{
slouken@1668
   162
    return SDL_arraysize(bootstrap) - 1;
slouken@1662
   163
}
slouken@1662
   164
slouken@1662
   165
const char *
slouken@1668
   166
SDL_GetVideoDriver(int index)
slouken@1662
   167
{
slouken@1668
   168
    if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
slouken@1662
   169
        return bootstrap[index]->name;
slouken@1662
   170
    }
slouken@1662
   171
    return NULL;
slouken@1660
   172
}
slouken@0
   173
slouken@0
   174
/*
slouken@0
   175
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   176
 */
slouken@1662
   177
int
slouken@1668
   178
SDL_VideoInit(const char *driver_name, Uint32 flags)
slouken@0
   179
{
slouken@1662
   180
    SDL_VideoDevice *video;
slouken@1662
   181
    int index;
slouken@1662
   182
    int i;
slouken@1662
   183
    int bpp;
slouken@1662
   184
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   185
slouken@1662
   186
    /* Toggle the event thread flags, based on OS requirements */
slouken@0
   187
#if defined(MUST_THREAD_EVENTS)
slouken@1662
   188
    flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   189
#elif defined(CANT_THREAD_EVENTS)
slouken@1662
   190
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@1668
   191
        SDL_SetError("OS doesn't support threaded events");
slouken@1662
   192
        return -1;
slouken@1662
   193
    }
slouken@0
   194
#endif
slouken@0
   195
slouken@1662
   196
    /* Check to make sure we don't overwrite '_this' */
slouken@1662
   197
    if (_this != NULL) {
slouken@1668
   198
        SDL_VideoQuit();
slouken@1662
   199
    }
slouken@0
   200
slouken@1662
   201
    /* Select the proper video driver */
slouken@1662
   202
    index = 0;
slouken@1662
   203
    video = NULL;
slouken@1662
   204
    if (driver_name != NULL) {
slouken@1662
   205
        for (i = 0; bootstrap[i]; ++i) {
slouken@1668
   206
            if (SDL_strncmp(bootstrap[i]->name, driver_name,
slouken@1668
   207
                            SDL_strlen(bootstrap[i]->name)) == 0) {
slouken@1668
   208
                if (bootstrap[i]->available()) {
slouken@1668
   209
                    video = bootstrap[i]->create(index);
slouken@1662
   210
                }
slouken@1662
   211
                break;
slouken@1662
   212
            }
slouken@1662
   213
        }
slouken@1662
   214
    } else {
slouken@1662
   215
        for (i = 0; bootstrap[i]; ++i) {
slouken@1668
   216
            if (bootstrap[i]->available()) {
slouken@1668
   217
                video = bootstrap[i]->create(index);
slouken@1662
   218
                if (video != NULL) {
slouken@1662
   219
                    break;
slouken@1662
   220
                }
slouken@1662
   221
            }
slouken@1662
   222
        }
slouken@1662
   223
    }
slouken@1662
   224
    if (video == NULL) {
slouken@1662
   225
        if (driver_name) {
slouken@1668
   226
            SDL_SetError("%s not available", driver_name);
slouken@1662
   227
        } else {
slouken@1668
   228
            SDL_SetError("No available video device");
slouken@1662
   229
        }
slouken@1662
   230
        return -1;
slouken@1662
   231
    }
slouken@1662
   232
    _this = video;
slouken@1662
   233
    _this->name = bootstrap[i]->name;
slouken@1670
   234
    _this->next_object_id = 1;
slouken@0
   235
slouken@1669
   236
slouken@1662
   237
    /* Set some very sane GL defaults */
slouken@1662
   238
    _this->gl_config.driver_loaded = 0;
slouken@1662
   239
    _this->gl_config.dll_handle = NULL;
slouken@1662
   240
    _this->gl_config.red_size = 3;
slouken@1662
   241
    _this->gl_config.green_size = 3;
slouken@1662
   242
    _this->gl_config.blue_size = 2;
slouken@1662
   243
    _this->gl_config.alpha_size = 0;
slouken@1662
   244
    _this->gl_config.buffer_size = 0;
slouken@1662
   245
    _this->gl_config.depth_size = 16;
slouken@1662
   246
    _this->gl_config.stencil_size = 0;
slouken@1662
   247
    _this->gl_config.double_buffer = 1;
slouken@1662
   248
    _this->gl_config.accum_red_size = 0;
slouken@1662
   249
    _this->gl_config.accum_green_size = 0;
slouken@1662
   250
    _this->gl_config.accum_blue_size = 0;
slouken@1662
   251
    _this->gl_config.accum_alpha_size = 0;
slouken@1662
   252
    _this->gl_config.stereo = 0;
slouken@1662
   253
    _this->gl_config.multisamplebuffers = 0;
slouken@1662
   254
    _this->gl_config.multisamplesamples = 0;
slouken@1662
   255
    _this->gl_config.accelerated = -1;  /* not known, don't set */
slouken@1662
   256
    _this->gl_config.swap_control = -1; /* not known, don't set */
slouken@0
   257
slouken@1662
   258
    /* Initialize the video subsystem */
slouken@1668
   259
    if (_this->VideoInit(_this) < 0) {
slouken@1668
   260
        SDL_VideoQuit();
slouken@1662
   261
        return -1;
slouken@1662
   262
    }
slouken@0
   263
slouken@1662
   264
    /* Make sure some displays were added */
slouken@1662
   265
    if (_this->num_displays == 0) {
slouken@1668
   266
        SDL_SetError("The video driver did not add any displays");
slouken@1668
   267
        SDL_VideoQuit();
slouken@1662
   268
        return (-1);
slouken@1662
   269
    }
slouken@0
   270
slouken@1662
   271
    /* Sort the video modes */
slouken@1662
   272
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1668
   273
        SDL_qsort(_this->displays[i].display_modes,
slouken@1668
   274
                  _this->displays[i].num_display_modes,
slouken@1668
   275
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@1662
   276
    }
slouken@0
   277
slouken@1676
   278
    /* The software renderer is always available */
slouken@1676
   279
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1676
   280
        if (_this->displays[i].num_render_drivers > 0) {
slouken@1676
   281
            SDL_AddRenderDriver(i, &SDL_SW_RenderDriver);
slouken@1676
   282
        }
slouken@1676
   283
    }
slouken@1676
   284
slouken@1662
   285
    /* Start the event loop */
slouken@1668
   286
    if (SDL_StartEventLoop(flags) < 0) {
slouken@1668
   287
        SDL_VideoQuit();
slouken@1662
   288
        return -1;
slouken@1662
   289
    }
slouken@0
   290
slouken@1662
   291
    /* We're ready to go! */
slouken@1662
   292
    return 0;
slouken@0
   293
}
slouken@0
   294
slouken@1662
   295
const char *
slouken@1668
   296
SDL_GetCurrentVideoDriver()
slouken@0
   297
{
slouken@1662
   298
    if (!_this) {
slouken@1662
   299
        return NULL;
slouken@1662
   300
    }
slouken@1662
   301
    return _this->name;
slouken@0
   302
}
slouken@0
   303
slouken@1662
   304
SDL_VideoDevice *
slouken@1668
   305
SDL_GetVideoDevice()
slouken@0
   306
{
slouken@1662
   307
    return _this;
slouken@0
   308
}
slouken@0
   309
slouken@1677
   310
int
slouken@1668
   311
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   312
{
slouken@1662
   313
    SDL_VideoDisplay display;
slouken@0
   314
slouken@1668
   315
    SDL_zero(display);
slouken@1662
   316
    if (desktop_mode) {
slouken@1662
   317
        display.desktop_mode = *desktop_mode;
slouken@1662
   318
    }
slouken@1662
   319
    display.current_mode = display.desktop_mode;
slouken@1662
   320
slouken@1677
   321
    return SDL_AddVideoDisplay(&display);
slouken@0
   322
}
slouken@0
   323
slouken@1677
   324
int
slouken@1669
   325
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@1662
   326
{
slouken@1662
   327
    SDL_VideoDisplay *displays;
slouken@1677
   328
    int index = -1;
slouken@0
   329
slouken@1662
   330
    displays =
slouken@1668
   331
        SDL_realloc(_this->displays,
slouken@1668
   332
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@1662
   333
    if (displays) {
slouken@1677
   334
        index = _this->num_displays++;
slouken@1677
   335
        displays[index] = *display;
slouken@1682
   336
        displays[index].device = _this;
slouken@1662
   337
        _this->displays = displays;
slouken@1677
   338
    } else {
slouken@1677
   339
        SDL_OutOfMemory();
slouken@1662
   340
    }
slouken@1677
   341
    return index;
slouken@0
   342
}
slouken@0
   343
slouken@1662
   344
int
slouken@1668
   345
SDL_GetNumVideoDisplays(void)
slouken@0
   346
{
slouken@1662
   347
    if (!_this) {
slouken@1662
   348
        return 0;
slouken@1662
   349
    }
slouken@1662
   350
    return _this->num_displays;
slouken@0
   351
}
slouken@0
   352
slouken@1662
   353
int
slouken@1668
   354
SDL_SelectVideoDisplay(int index)
slouken@0
   355
{
slouken@1662
   356
    if (!_this) {
slouken@1668
   357
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   358
        return (-1);
slouken@1662
   359
    }
slouken@1662
   360
    if (index >= 0) {
slouken@1662
   361
        if (index >= _this->num_displays) {
slouken@1668
   362
            SDL_SetError("index must be in the range 0 - %d",
slouken@1668
   363
                         _this->num_displays - 1);
slouken@1662
   364
            return -1;
slouken@1662
   365
        }
slouken@1662
   366
        _this->current_display = index;
slouken@1662
   367
    }
slouken@1662
   368
    return _this->current_display;
slouken@0
   369
}
slouken@0
   370
slouken@1662
   371
void
slouken@1670
   372
SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
slouken@0
   373
{
slouken@1670
   374
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@1662
   375
    SDL_DisplayMode *modes;
slouken@1662
   376
    int i, nmodes;
slouken@0
   377
slouken@1662
   378
    /* Make sure we don't already have the mode in the list */
slouken@1670
   379
    modes = display->display_modes;
slouken@1670
   380
    nmodes = display->num_display_modes;
slouken@1662
   381
    for (i = 0; i < nmodes; ++i) {
slouken@1668
   382
        if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
slouken@1662
   383
            return;
slouken@1662
   384
        }
slouken@1662
   385
    }
slouken@0
   386
slouken@1662
   387
    /* Go ahead and add the new mode */
slouken@1668
   388
    modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
slouken@1662
   389
    if (modes) {
slouken@1670
   390
        display->display_modes = modes;
slouken@1662
   391
        modes[nmodes] = *mode;
slouken@1670
   392
        display->num_display_modes++;
slouken@1662
   393
    }
slouken@0
   394
}
slouken@0
   395
slouken@1662
   396
int
slouken@1668
   397
SDL_GetNumDisplayModes()
slouken@1662
   398
{
slouken@1662
   399
    if (_this) {
slouken@1662
   400
        return SDL_CurrentDisplay.num_display_modes;
slouken@1662
   401
    }
slouken@1662
   402
    return 0;
slouken@1662
   403
}
slouken@910
   404
slouken@1662
   405
const SDL_DisplayMode *
slouken@1668
   406
SDL_GetDisplayMode(int index)
slouken@0
   407
{
slouken@1668
   408
    if (index < 0 || index >= SDL_GetNumDisplayModes()) {
slouken@1668
   409
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1670
   410
                     SDL_GetNumDisplayModes() - 1);
slouken@1662
   411
        return NULL;
slouken@1662
   412
    }
slouken@1662
   413
    return &SDL_CurrentDisplay.display_modes[index];
slouken@1662
   414
}
slouken@0
   415
slouken@1662
   416
const SDL_DisplayMode *
slouken@1668
   417
SDL_GetDesktopDisplayMode(void)
slouken@1662
   418
{
slouken@1662
   419
    if (_this) {
slouken@1662
   420
        return &SDL_CurrentDisplay.desktop_mode;
slouken@1662
   421
    }
slouken@1662
   422
    return NULL;
slouken@1662
   423
}
slouken@0
   424
slouken@1662
   425
const SDL_DisplayMode *
slouken@1668
   426
SDL_GetCurrentDisplayMode(void)
slouken@1662
   427
{
slouken@1662
   428
    if (_this) {
slouken@1662
   429
        return &SDL_CurrentDisplay.current_mode;
slouken@1662
   430
    }
slouken@1662
   431
    return NULL;
slouken@1662
   432
}
slouken@0
   433
slouken@1662
   434
SDL_DisplayMode *
slouken@1668
   435
SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
slouken@1668
   436
                          SDL_DisplayMode * closest)
slouken@1662
   437
{
slouken@1662
   438
    Uint32 target_format;
slouken@1662
   439
    int target_refresh_rate;
slouken@1662
   440
    int i;
slouken@1662
   441
    SDL_DisplayMode *current, *match;
slouken@0
   442
slouken@1662
   443
    if (!_this || !mode || !closest) {
slouken@1662
   444
        return NULL;
slouken@1662
   445
    }
slouken@0
   446
slouken@1662
   447
    /* Default to the desktop format */
slouken@1662
   448
    if (mode->format) {
slouken@1662
   449
        target_format = mode->format;
slouken@1662
   450
    } else {
slouken@1662
   451
        target_format = SDL_CurrentDisplay.desktop_mode.format;
slouken@1662
   452
    }
slouken@0
   453
slouken@1662
   454
    /* Default to the desktop refresh rate */
slouken@1662
   455
    if (mode->refresh_rate) {
slouken@1662
   456
        target_refresh_rate = mode->refresh_rate;
slouken@1662
   457
    } else {
slouken@1662
   458
        target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
slouken@1662
   459
    }
slouken@14
   460
slouken@1662
   461
    match = NULL;
slouken@1662
   462
    for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
slouken@1662
   463
        current = &SDL_CurrentDisplay.display_modes[i];
slouken@0
   464
slouken@1662
   465
        if ((current->w && current->h) &&
slouken@1662
   466
            (current->w < mode->w || current->h < mode->h)) {
slouken@1662
   467
            /* Out of sorted modes large enough here */
slouken@1662
   468
            break;
slouken@1662
   469
        }
slouken@1662
   470
        if (!match || current->w < match->w || current->h < match->h) {
slouken@1662
   471
            match = current;
slouken@1662
   472
            continue;
slouken@1662
   473
        }
slouken@1662
   474
        if (current->format != match->format) {
slouken@1662
   475
            /* Sorted highest depth to lowest */
slouken@1662
   476
            if (current->format == target_format ||
slouken@1668
   477
                (SDL_BITSPERPIXEL(current->format) >=
slouken@1668
   478
                 SDL_BITSPERPIXEL(target_format)
slouken@1668
   479
                 && SDL_PIXELTYPE(current->format) ==
slouken@1668
   480
                 SDL_PIXELTYPE(target_format))) {
slouken@1662
   481
                match = current;
slouken@1662
   482
            }
slouken@1662
   483
            continue;
slouken@1662
   484
        }
slouken@1662
   485
        if (current->refresh_rate != match->refresh_rate) {
slouken@1662
   486
            /* Sorted highest refresh to lowest */
slouken@1662
   487
            if (current->refresh_rate >= target_refresh_rate) {
slouken@1662
   488
                match = current;
slouken@1662
   489
            }
slouken@1662
   490
        }
slouken@1662
   491
    }
slouken@1662
   492
    if (match) {
slouken@1662
   493
        if (match->format) {
slouken@1662
   494
            closest->format = match->format;
slouken@1662
   495
        } else {
slouken@1662
   496
            closest->format = mode->format;
slouken@1662
   497
        }
slouken@1662
   498
        if (match->w && match->h) {
slouken@1662
   499
            closest->w = match->w;
slouken@1662
   500
            closest->h = match->h;
slouken@1662
   501
        } else {
slouken@1662
   502
            closest->w = mode->w;
slouken@1662
   503
            closest->h = mode->h;
slouken@1662
   504
        }
slouken@1662
   505
        if (match->refresh_rate) {
slouken@1662
   506
            closest->refresh_rate = match->refresh_rate;
slouken@1662
   507
        } else {
slouken@1662
   508
            closest->refresh_rate = mode->refresh_rate;
slouken@1662
   509
        }
slouken@1662
   510
        return closest;
slouken@1662
   511
    }
slouken@1662
   512
    return NULL;
slouken@1662
   513
}
slouken@0
   514
slouken@1662
   515
int
slouken@1668
   516
SDL_SetDisplayMode(const SDL_DisplayMode * mode)
slouken@1662
   517
{
slouken@1667
   518
    SDL_VideoDisplay *display;
slouken@1662
   519
    SDL_DisplayMode display_mode;
slouken@1667
   520
    int i;
slouken@229
   521
slouken@1662
   522
    if (!_this) {
slouken@1668
   523
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   524
        return -1;
slouken@1662
   525
    }
slouken@0
   526
slouken@1662
   527
    /* Make sure there's an actual display mode to set */
slouken@1662
   528
    if (!mode) {
slouken@1668
   529
        SDL_SetError("No mode passed to SDL_SetDisplayMode");
slouken@1662
   530
        return -1;
slouken@1662
   531
    }
slouken@1667
   532
    display = &SDL_CurrentDisplay;
slouken@1662
   533
    display_mode = *mode;
slouken@0
   534
slouken@1662
   535
    /* Default to the current mode */
slouken@1662
   536
    if (!display_mode.format) {
slouken@1667
   537
        display_mode.format = display->current_mode.format;
slouken@1662
   538
    }
slouken@1662
   539
    if (!display_mode.w) {
slouken@1667
   540
        display_mode.w = display->current_mode.w;
slouken@1662
   541
    }
slouken@1662
   542
    if (!display_mode.h) {
slouken@1667
   543
        display_mode.h = display->current_mode.h;
slouken@1662
   544
    }
slouken@1662
   545
    if (!display_mode.refresh_rate) {
slouken@1667
   546
        display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@1662
   547
    }
slouken@0
   548
slouken@1662
   549
    /* Get a good video mode, the closest one possible */
slouken@1668
   550
    if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
slouken@1668
   551
        SDL_SetError("No video mode large enough for %dx%d",
slouken@1668
   552
                     display_mode.w, display_mode.h);
slouken@1662
   553
        return -1;
slouken@1662
   554
    }
slouken@0
   555
slouken@1662
   556
    /* See if there's anything left to do */
slouken@1662
   557
    if (SDL_memcmp
slouken@1668
   558
        (&display_mode, SDL_GetCurrentDisplayMode(),
slouken@1668
   559
         sizeof(display_mode)) == 0) {
slouken@1662
   560
        return 0;
slouken@1662
   561
    }
slouken@0
   562
slouken@1681
   563
    if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
slouken@1681
   564
        display->palette.ncolors =
slouken@1681
   565
            (1 << SDL_BITSPERPIXEL(display_mode.format));
slouken@1681
   566
        display->palette.colors =
slouken@1681
   567
            (SDL_Color *) SDL_realloc(display->palette.colors,
slouken@1681
   568
                                      display->palette.ncolors *
slouken@1681
   569
                                      sizeof(*display->palette.colors));
slouken@1681
   570
        if (!display->palette.colors) {
slouken@1681
   571
            SDL_OutOfMemory();
slouken@1681
   572
            return -1;
slouken@1681
   573
        }
slouken@1681
   574
        SDL_memset(display->palette.colors, 0xff,
slouken@1681
   575
                   display->palette.ncolors *
slouken@1681
   576
                   sizeof(*display->palette.colors));
slouken@1681
   577
    } else {
slouken@1681
   578
        if (display->palette.colors) {
slouken@1681
   579
            SDL_free(display->palette.colors);
slouken@1681
   580
        }
slouken@1681
   581
        display->palette.colors = NULL;
slouken@1681
   582
        display->palette.ncolors = 0;
slouken@1681
   583
    }
slouken@1681
   584
slouken@1668
   585
    return _this->SetDisplayMode(_this, &display_mode);
slouken@1662
   586
}
slouken@0
   587
slouken@1681
   588
int
slouken@1681
   589
SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1681
   590
{
slouken@1681
   591
    SDL_Palette *palette;
slouken@1681
   592
slouken@1681
   593
    if (!_this) {
slouken@1681
   594
        SDL_SetError("Video subsystem has not been initialized");
slouken@1681
   595
        return -1;
slouken@1681
   596
    }
slouken@1681
   597
slouken@1681
   598
    palette = &SDL_CurrentDisplay.palette;
slouken@1681
   599
    if (!palette->ncolors) {
slouken@1681
   600
        SDL_SetError("Display mode does not have a palette");
slouken@1681
   601
        return -1;
slouken@1681
   602
    }
slouken@1681
   603
slouken@1681
   604
    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
slouken@1681
   605
        SDL_SetError("Palette indices are out of range");
slouken@1681
   606
        return -1;
slouken@1681
   607
    }
slouken@1681
   608
slouken@1681
   609
    SDL_memcpy(&palette->colors[firstcolor], colors,
slouken@1681
   610
               ncolors * sizeof(*colors));
slouken@1681
   611
slouken@1681
   612
    if (_this->SetDisplayPalette) {
slouken@1681
   613
        return _this->SetDisplayPalette(_this, palette);
slouken@1681
   614
    } else {
slouken@1681
   615
        return 0;
slouken@1681
   616
    }
slouken@1681
   617
}
slouken@1681
   618
slouken@1681
   619
int
slouken@1681
   620
SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
slouken@1681
   621
{
slouken@1681
   622
    SDL_Palette *palette;
slouken@1681
   623
slouken@1681
   624
    if (!_this) {
slouken@1681
   625
        SDL_SetError("Video subsystem has not been initialized");
slouken@1681
   626
        return -1;
slouken@1681
   627
    }
slouken@1681
   628
slouken@1681
   629
    palette = &SDL_CurrentDisplay.palette;
slouken@1681
   630
    if (!palette->ncolors) {
slouken@1681
   631
        SDL_SetError("Display mode does not have a palette");
slouken@1681
   632
        return -1;
slouken@1681
   633
    }
slouken@1681
   634
slouken@1681
   635
    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
slouken@1681
   636
        SDL_SetError("Palette indices are out of range");
slouken@1681
   637
        return -1;
slouken@1681
   638
    }
slouken@1681
   639
slouken@1681
   640
    SDL_memcpy(colors, &palette->colors[firstcolor],
slouken@1681
   641
               ncolors * sizeof(*colors));
slouken@1681
   642
    return 0;
slouken@1681
   643
}
slouken@1681
   644
slouken@1662
   645
SDL_WindowID
slouken@1668
   646
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@1662
   647
{
slouken@1662
   648
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@1662
   649
                                  SDL_WINDOW_BORDERLESS |
slouken@1662
   650
                                  SDL_WINDOW_SHOWN |
slouken@1662
   651
                                  SDL_WINDOW_OPENGL |
slouken@1662
   652
                                  SDL_WINDOW_RESIZABLE |
slouken@1662
   653
                                  SDL_WINDOW_MAXIMIZED |
slouken@1662
   654
                                  SDL_WINDOW_MINIMIZED |
slouken@1662
   655
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@1662
   656
    SDL_Window window;
slouken@1662
   657
    int num_windows;
slouken@1662
   658
    SDL_Window *windows;
slouken@0
   659
slouken@1662
   660
    if (!_this) {
slouken@1668
   661
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   662
        return 0;
slouken@1662
   663
    }
slouken@0
   664
slouken@1668
   665
    SDL_zero(window);
slouken@1670
   666
    window.id = _this->next_object_id++;
slouken@1668
   667
    window.title = title ? SDL_strdup(title) : NULL;
slouken@1662
   668
    window.x = x;
slouken@1662
   669
    window.y = y;
slouken@1662
   670
    window.w = w;
slouken@1662
   671
    window.h = h;
slouken@1662
   672
    window.flags = (flags & allowed_flags);
slouken@1676
   673
    window.display = &SDL_CurrentDisplay;
slouken@910
   674
slouken@1668
   675
    if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
slouken@1667
   676
        if (window.title) {
slouken@1668
   677
            SDL_free(window.title);
slouken@1667
   678
        }
slouken@1662
   679
        return 0;
slouken@1662
   680
    }
slouken@910
   681
slouken@1662
   682
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   683
    windows =
slouken@1668
   684
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   685
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   686
    if (!windows) {
slouken@1662
   687
        if (_this->DestroyWindow) {
slouken@1668
   688
            _this->DestroyWindow(_this, &window);
slouken@1662
   689
        }
slouken@1667
   690
        if (window.title) {
slouken@1668
   691
            SDL_free(window.title);
slouken@1667
   692
        }
slouken@1662
   693
        return 0;
slouken@1662
   694
    }
slouken@1662
   695
    windows[num_windows] = window;
slouken@1662
   696
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   697
    SDL_CurrentDisplay.num_windows++;
slouken@910
   698
slouken@1662
   699
    return window.id;
slouken@1662
   700
}
icculus@650
   701
slouken@1662
   702
SDL_WindowID
slouken@1681
   703
SDL_CreateWindowFrom(const void *data)
slouken@1662
   704
{
slouken@1662
   705
    SDL_Window window;
slouken@1662
   706
    int num_windows;
slouken@1662
   707
    SDL_Window *windows;
slouken@0
   708
slouken@1662
   709
    if (!_this) {
slouken@1668
   710
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   711
        return (0);
slouken@1662
   712
    }
slouken@0
   713
slouken@1668
   714
    SDL_zero(window);
slouken@1670
   715
    window.id = _this->next_object_id++;
slouken@1676
   716
    window.display = &SDL_CurrentDisplay;
slouken@1662
   717
slouken@1662
   718
    if (!_this->CreateWindowFrom ||
slouken@1668
   719
        _this->CreateWindowFrom(_this, &window, data) < 0) {
slouken@1662
   720
        return 0;
slouken@1662
   721
    }
slouken@1662
   722
slouken@1662
   723
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   724
    windows =
slouken@1668
   725
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   726
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   727
    if (!windows) {
slouken@1662
   728
        if (_this->DestroyWindow) {
slouken@1668
   729
            _this->DestroyWindow(_this, &window);
slouken@1662
   730
        }
slouken@1667
   731
        if (window.title) {
slouken@1668
   732
            SDL_free(window.title);
slouken@1667
   733
        }
slouken@1662
   734
        return 0;
slouken@1662
   735
    }
slouken@1662
   736
    windows[num_windows] = window;
slouken@1662
   737
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   738
    SDL_CurrentDisplay.num_windows++;
slouken@1662
   739
slouken@1662
   740
    return window.id;
slouken@1662
   741
}
slouken@1662
   742
slouken@1662
   743
static __inline__ SDL_Window *
slouken@1668
   744
SDL_GetWindowFromID(SDL_WindowID windowID)
slouken@1662
   745
{
slouken@1662
   746
    int i, j;
slouken@1662
   747
slouken@1662
   748
    if (!_this) {
slouken@1662
   749
        return NULL;
slouken@1662
   750
    }
slouken@1662
   751
slouken@1662
   752
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   753
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   754
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   755
            SDL_Window *window = &display->windows[j];
slouken@1662
   756
            if (window->id == windowID) {
slouken@1662
   757
                return window;
slouken@1662
   758
            }
slouken@1662
   759
        }
slouken@1662
   760
    }
slouken@1662
   761
    return NULL;
slouken@1662
   762
}
slouken@1662
   763
slouken@1662
   764
Uint32
slouken@1668
   765
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1662
   766
{
slouken@1668
   767
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   768
slouken@1662
   769
    if (!window) {
slouken@1662
   770
        return 0;
slouken@1662
   771
    }
slouken@1662
   772
    return window->flags;
slouken@1662
   773
}
slouken@1662
   774
slouken@1662
   775
void
slouken@1668
   776
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1662
   777
{
slouken@1668
   778
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   779
slouken@1662
   780
    if (!window) {
slouken@1662
   781
        return;
slouken@1662
   782
    }
slouken@1662
   783
    if (window->title) {
slouken@1668
   784
        SDL_free(window->title);
slouken@1662
   785
    }
slouken@1668
   786
    window->title = SDL_strdup(title);
slouken@1662
   787
slouken@1662
   788
    if (_this->SetWindowTitle) {
slouken@1668
   789
        _this->SetWindowTitle(_this, window);
slouken@1662
   790
    }
slouken@1662
   791
}
slouken@1662
   792
slouken@1662
   793
const char *
slouken@1668
   794
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1662
   795
{
slouken@1668
   796
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   797
slouken@1662
   798
    if (!window) {
slouken@1662
   799
        return NULL;
slouken@1662
   800
    }
slouken@1662
   801
    return window->title;
slouken@1662
   802
}
slouken@1662
   803
slouken@1662
   804
void
slouken@1668
   805
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1662
   806
{
slouken@1668
   807
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   808
slouken@1662
   809
    if (!window) {
slouken@1662
   810
        return;
slouken@1662
   811
    }
slouken@1662
   812
    window->userdata = userdata;
slouken@1662
   813
}
slouken@1662
   814
slouken@1662
   815
void *
slouken@1668
   816
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1662
   817
{
slouken@1668
   818
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   819
slouken@1662
   820
    if (!window) {
slouken@1662
   821
        return NULL;
slouken@1662
   822
    }
slouken@1662
   823
    return window->userdata;
slouken@1662
   824
}
slouken@1662
   825
slouken@1662
   826
void
slouken@1668
   827
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1662
   828
{
slouken@1668
   829
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   830
slouken@1662
   831
    if (!window) {
slouken@1662
   832
        return;
slouken@1662
   833
    }
slouken@1662
   834
slouken@1662
   835
    window->x = x;
slouken@1662
   836
    window->y = y;
slouken@1662
   837
slouken@1662
   838
    if (_this->SetWindowPosition) {
slouken@1668
   839
        _this->SetWindowPosition(_this, window);
slouken@1662
   840
    }
slouken@1662
   841
}
slouken@1662
   842
slouken@1662
   843
void
slouken@1668
   844
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1662
   845
{
slouken@1668
   846
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   847
slouken@1662
   848
    if (!window) {
slouken@1662
   849
        return;
slouken@1662
   850
    }
slouken@1662
   851
    if (x) {
slouken@1662
   852
        *x = window->x;
slouken@1662
   853
    }
slouken@1662
   854
    if (y) {
slouken@1662
   855
        *y = window->y;
slouken@1662
   856
    }
slouken@1662
   857
}
slouken@1662
   858
slouken@1662
   859
void
slouken@1668
   860
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1662
   861
{
slouken@1668
   862
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   863
slouken@1662
   864
    if (!window) {
slouken@1662
   865
        return;
slouken@1662
   866
    }
slouken@1662
   867
slouken@1662
   868
    window->w = w;
slouken@1662
   869
    window->h = h;
slouken@1662
   870
slouken@1662
   871
    if (_this->SetWindowSize) {
slouken@1668
   872
        _this->SetWindowSize(_this, window);
slouken@1662
   873
    }
slouken@1662
   874
}
slouken@1662
   875
slouken@1662
   876
void
slouken@1668
   877
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1662
   878
{
slouken@1668
   879
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   880
slouken@1662
   881
    if (!window) {
slouken@1662
   882
        return;
slouken@1662
   883
    }
slouken@1662
   884
    if (w) {
slouken@1662
   885
        *w = window->w;
slouken@1662
   886
    }
slouken@1662
   887
    if (h) {
slouken@1662
   888
        *h = window->h;
slouken@1662
   889
    }
slouken@1662
   890
}
slouken@1662
   891
slouken@1662
   892
void
slouken@1668
   893
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1662
   894
{
slouken@1668
   895
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   896
slouken@1662
   897
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   898
        return;
slouken@1662
   899
    }
slouken@1662
   900
slouken@1662
   901
    window->flags |= SDL_WINDOW_SHOWN;
slouken@1662
   902
slouken@1662
   903
    if (_this->ShowWindow) {
slouken@1668
   904
        _this->ShowWindow(_this, window);
slouken@1662
   905
    }
slouken@1662
   906
}
slouken@1662
   907
slouken@1662
   908
void
slouken@1668
   909
SDL_HideWindow(SDL_WindowID windowID)
slouken@1662
   910
{
slouken@1668
   911
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   912
slouken@1662
   913
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   914
        return;
slouken@1662
   915
    }
slouken@1662
   916
slouken@1662
   917
    window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1662
   918
slouken@1662
   919
    if (_this->HideWindow) {
slouken@1668
   920
        _this->HideWindow(_this, window);
slouken@1662
   921
    }
slouken@1662
   922
}
slouken@1662
   923
slouken@1662
   924
void
slouken@1668
   925
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1662
   926
{
slouken@1668
   927
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   928
slouken@1662
   929
    if (!window) {
slouken@1662
   930
        return;
slouken@1662
   931
    }
slouken@1662
   932
slouken@1662
   933
    if (_this->RaiseWindow) {
slouken@1668
   934
        _this->RaiseWindow(_this, window);
slouken@1662
   935
    }
slouken@1662
   936
}
slouken@1662
   937
slouken@1662
   938
void
slouken@1668
   939
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1662
   940
{
slouken@1668
   941
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   942
slouken@1662
   943
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@1662
   944
        return;
slouken@1662
   945
    }
slouken@1662
   946
slouken@1662
   947
    window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1662
   948
slouken@1662
   949
    if (_this->MaximizeWindow) {
slouken@1668
   950
        _this->MaximizeWindow(_this, window);
slouken@1662
   951
    }
slouken@1662
   952
}
slouken@1662
   953
slouken@1662
   954
void
slouken@1668
   955
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1662
   956
{
slouken@1668
   957
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   958
slouken@1662
   959
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@1662
   960
        return;
slouken@1662
   961
    }
slouken@1662
   962
slouken@1662
   963
    window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1662
   964
slouken@1662
   965
    if (_this->MinimizeWindow) {
slouken@1668
   966
        _this->MinimizeWindow(_this, window);
slouken@1662
   967
    }
slouken@1662
   968
}
slouken@1662
   969
slouken@1662
   970
void
slouken@1668
   971
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1662
   972
{
slouken@1668
   973
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   974
slouken@1662
   975
    if (!window
slouken@1662
   976
        || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@1662
   977
        return;
slouken@1662
   978
    }
slouken@1662
   979
slouken@1662
   980
    window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
slouken@1662
   981
slouken@1662
   982
    if (_this->RestoreWindow) {
slouken@1668
   983
        _this->RestoreWindow(_this, window);
slouken@1662
   984
    }
slouken@1662
   985
}
slouken@1662
   986
slouken@1662
   987
void
slouken@1668
   988
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1662
   989
{
slouken@1668
   990
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   991
slouken@1662
   992
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@1662
   993
        return;
slouken@1662
   994
    }
slouken@1662
   995
slouken@1662
   996
    if (mode) {
slouken@1662
   997
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   998
    } else {
slouken@1662
   999
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
  1000
    }
slouken@1662
  1001
slouken@1662
  1002
    if (_this->SetWindowGrab) {
slouken@1668
  1003
        _this->SetWindowGrab(_this, window);
slouken@1662
  1004
    }
slouken@1662
  1005
}
slouken@1662
  1006
slouken@1662
  1007
int
slouken@1668
  1008
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1662
  1009
{
slouken@1668
  1010
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1011
slouken@1662
  1012
    if (!window) {
slouken@1662
  1013
        return 0;
slouken@1662
  1014
    }
slouken@1662
  1015
slouken@1662
  1016
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1662
  1017
}
slouken@1662
  1018
slouken@1662
  1019
void
slouken@1668
  1020
SDL_DestroyWindow(SDL_WindowID windowID)
slouken@1662
  1021
{
slouken@1662
  1022
    int i, j;
slouken@1662
  1023
slouken@1662
  1024
    if (!_this) {
slouken@1662
  1025
        return;
slouken@1662
  1026
    }
slouken@1662
  1027
slouken@1662
  1028
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
  1029
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1030
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
  1031
            SDL_Window *window = &display->windows[j];
slouken@1662
  1032
            if (window->id != windowID) {
slouken@1662
  1033
                continue;
slouken@1662
  1034
            }
slouken@1662
  1035
            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1662
  1036
                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1668
  1037
                _this->SetWindowGrab(_this, window);
slouken@1662
  1038
            }
slouken@1670
  1039
            if (window->renderer) {
slouken@1670
  1040
                SDL_DestroyRenderer(window->id);
slouken@1662
  1041
            }
slouken@1662
  1042
            if (_this->DestroyWindow) {
slouken@1668
  1043
                _this->DestroyWindow(_this, window);
slouken@1662
  1044
            }
slouken@1662
  1045
            if (window->title) {
slouken@1668
  1046
                SDL_free(window->title);
slouken@1662
  1047
            }
slouken@1662
  1048
            if (window->gamma) {
slouken@1668
  1049
                SDL_free(window->gamma);
slouken@1662
  1050
            }
slouken@1662
  1051
            if (j != display->num_windows - 1) {
slouken@1668
  1052
                SDL_memcpy(&display->windows[i],
slouken@1668
  1053
                           &display->windows[i + 1],
slouken@1668
  1054
                           (display->num_windows - i - 1) * sizeof(*window));
slouken@1662
  1055
            }
slouken@1662
  1056
            --display->num_windows;
slouken@1662
  1057
            return;
slouken@1662
  1058
        }
slouken@1662
  1059
    }
slouken@1662
  1060
}
slouken@1662
  1061
slouken@1670
  1062
void
slouken@1670
  1063
SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
slouken@1670
  1064
{
slouken@1670
  1065
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@1670
  1066
    SDL_RenderDriver *render_drivers;
slouken@1670
  1067
slouken@1670
  1068
    render_drivers =
slouken@1670
  1069
        SDL_realloc(display->render_drivers,
slouken@1670
  1070
                    (display->num_render_drivers +
slouken@1670
  1071
                     1) * sizeof(*render_drivers));
slouken@1670
  1072
    if (render_drivers) {
slouken@1670
  1073
        render_drivers[display->num_render_drivers] = *driver;
slouken@1670
  1074
        display->render_drivers = render_drivers;
slouken@1670
  1075
        display->num_render_drivers++;
slouken@1670
  1076
    }
slouken@1670
  1077
}
slouken@1670
  1078
slouken@1670
  1079
int
slouken@1670
  1080
SDL_GetNumRenderers(void)
slouken@1670
  1081
{
slouken@1670
  1082
    if (_this) {
slouken@1670
  1083
        return SDL_CurrentDisplay.num_render_drivers;
slouken@1670
  1084
    }
slouken@1670
  1085
    return 0;
slouken@1670
  1086
}
slouken@1670
  1087
slouken@1670
  1088
int
slouken@1670
  1089
SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
slouken@1670
  1090
{
slouken@1670
  1091
    if (index < 0 || index >= SDL_GetNumRenderers()) {
slouken@1670
  1092
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1670
  1093
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1094
        return -1;
slouken@1670
  1095
    }
slouken@1670
  1096
    *info = SDL_CurrentDisplay.render_drivers[index].info;
slouken@1670
  1097
    return 0;
slouken@1670
  1098
}
slouken@1670
  1099
slouken@1670
  1100
int
slouken@1670
  1101
SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
slouken@1662
  1102
{
slouken@1668
  1103
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1104
slouken@1662
  1105
    if (!window) {
slouken@1670
  1106
        return 0;
slouken@1670
  1107
    }
slouken@1670
  1108
slouken@1670
  1109
    if (index < 0) {
slouken@1670
  1110
        int n = SDL_GetNumRenderers();
slouken@1670
  1111
        for (index = 0; index < n; ++index) {
slouken@1680
  1112
            SDL_RenderDriver *driver =
slouken@1680
  1113
                &SDL_CurrentDisplay.render_drivers[index];
slouken@1680
  1114
slouken@1680
  1115
            /* Skip minimal drivers in automatic scans */
slouken@1680
  1116
            if (!(flags & SDL_Renderer_Minimal)
slouken@1680
  1117
                && (driver->info.flags & SDL_Renderer_Minimal)) {
slouken@1680
  1118
                continue;
slouken@1680
  1119
            }
slouken@1680
  1120
            if ((driver->info.flags & flags) == flags) {
slouken@1670
  1121
                break;
slouken@1670
  1122
            }
slouken@1670
  1123
        }
slouken@1670
  1124
        if (index == n) {
slouken@1670
  1125
            SDL_SetError("Couldn't find matching render driver");
slouken@1670
  1126
            return -1;
slouken@1670
  1127
        }
slouken@1670
  1128
    }
slouken@1670
  1129
slouken@1670
  1130
    if (index >= SDL_GetNumRenderers()) {
slouken@1670
  1131
        SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@1670
  1132
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1133
        return -1;
slouken@1670
  1134
    }
slouken@1670
  1135
slouken@1670
  1136
    /* Free any existing renderer */
slouken@1670
  1137
    SDL_DestroyRenderer(windowID);
slouken@1670
  1138
slouken@1670
  1139
    /* Create a new renderer instance */
slouken@1670
  1140
    window->renderer =
slouken@1670
  1141
        SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
slouken@1670
  1142
                                                                flags);
slouken@1670
  1143
    if (!window->renderer) {
slouken@1670
  1144
        return -1;
slouken@1670
  1145
    }
slouken@1670
  1146
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1147
slouken@1670
  1148
    return 0;
slouken@1670
  1149
}
slouken@1670
  1150
slouken@1670
  1151
int
slouken@1670
  1152
SDL_SelectRenderer(SDL_WindowID windowID)
slouken@1670
  1153
{
slouken@1670
  1154
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1155
slouken@1670
  1156
    if (!window || !window->renderer) {
slouken@1670
  1157
        return -1;
slouken@1670
  1158
    }
slouken@1670
  1159
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1160
    return 0;
slouken@1670
  1161
}
slouken@1670
  1162
slouken@1670
  1163
SDL_TextureID
slouken@1670
  1164
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1670
  1165
{
slouken@1680
  1166
    int hash;
slouken@1670
  1167
    SDL_Renderer *renderer;
slouken@1670
  1168
    SDL_Texture *texture;
slouken@1670
  1169
slouken@1670
  1170
    if (!_this) {
slouken@1670
  1171
        return 0;
slouken@1670
  1172
    }
slouken@1670
  1173
slouken@1670
  1174
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1175
    if (!renderer || !renderer->CreateTexture) {
slouken@1670
  1176
        return 0;
slouken@1670
  1177
    }
slouken@1670
  1178
slouken@1670
  1179
    texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
slouken@1670
  1180
    if (!texture) {
slouken@1670
  1181
        SDL_OutOfMemory();
slouken@1670
  1182
        return 0;
slouken@1670
  1183
    }
slouken@1670
  1184
slouken@1670
  1185
    SDL_zerop(texture);
slouken@1670
  1186
    texture->id = _this->next_object_id++;
slouken@1670
  1187
    texture->format = format;
slouken@1670
  1188
    texture->access = access;
slouken@1670
  1189
    texture->w = w;
slouken@1670
  1190
    texture->h = h;
slouken@1670
  1191
    texture->renderer = renderer;
slouken@1670
  1192
slouken@1676
  1193
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1670
  1194
        SDL_free(texture);
slouken@1670
  1195
        return 0;
slouken@1670
  1196
    }
slouken@1680
  1197
slouken@1680
  1198
    hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1680
  1199
    texture->next = SDL_CurrentDisplay.textures[hash];
slouken@1680
  1200
    SDL_CurrentDisplay.textures[hash] = texture;
slouken@1680
  1201
slouken@1680
  1202
    return texture->id;
slouken@1670
  1203
}
slouken@1670
  1204
slouken@1670
  1205
SDL_TextureID
slouken@1670
  1206
SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
slouken@1670
  1207
{
slouken@1670
  1208
    SDL_TextureID textureID;
slouken@1670
  1209
    Uint32 surface_flags = surface->flags;
slouken@1670
  1210
    SDL_PixelFormat *fmt = surface->format;
slouken@1670
  1211
    Uint32 colorkey;
slouken@1670
  1212
    Uint8 alpha;
slouken@1670
  1213
    SDL_Rect bounds;
slouken@1670
  1214
    SDL_Surface dst;
slouken@1670
  1215
    int bpp;
slouken@1670
  1216
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
  1217
slouken@1670
  1218
    if (!surface) {
slouken@1670
  1219
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@1670
  1220
        return 0;
slouken@1670
  1221
    }
slouken@1670
  1222
slouken@1670
  1223
    if (format) {
slouken@1670
  1224
        if (!SDL_PixelFormatEnumToMasks
slouken@1670
  1225
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1670
  1226
            SDL_SetError("Unknown pixel format");
slouken@1670
  1227
            return 0;
slouken@1670
  1228
        }
slouken@1670
  1229
    } else {
slouken@1670
  1230
        bpp = fmt->BitsPerPixel;
slouken@1670
  1231
        Rmask = fmt->Rmask;
slouken@1670
  1232
        Gmask = fmt->Gmask;
slouken@1670
  1233
        Bmask = fmt->Bmask;
slouken@1670
  1234
        Amask = fmt->Amask;
slouken@1670
  1235
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1236
        if (!format) {
slouken@1670
  1237
            SDL_SetError("Unknown pixel format");
slouken@1670
  1238
            return 0;
slouken@1670
  1239
        }
slouken@1670
  1240
    }
slouken@1670
  1241
slouken@1670
  1242
    textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
slouken@1670
  1243
    if (!textureID) {
slouken@1670
  1244
        return 0;
slouken@1670
  1245
    }
slouken@1670
  1246
slouken@1670
  1247
    /* Set up a destination surface for the texture update */
slouken@1670
  1248
    SDL_zero(dst);
slouken@1670
  1249
    dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1250
    if (!dst.format) {
slouken@1670
  1251
        SDL_DestroyTexture(textureID);
slouken@1670
  1252
        return 0;
slouken@1670
  1253
    }
slouken@1670
  1254
    dst.w = surface->w;
slouken@1670
  1255
    dst.h = surface->h;
slouken@1670
  1256
    if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
slouken@1670
  1257
        dst.flags |= SDL_PREALLOC;
slouken@1670
  1258
    } else {
slouken@1670
  1259
        dst.pitch = SDL_CalculatePitch(&dst);
slouken@1670
  1260
        dst.pixels = SDL_malloc(dst.h * dst.pitch);
slouken@1670
  1261
        if (!dst.pixels) {
slouken@1670
  1262
            SDL_DestroyTexture(textureID);
slouken@1670
  1263
            SDL_FreeFormat(dst.format);
slouken@1670
  1264
            SDL_OutOfMemory();
slouken@1670
  1265
            return 0;
slouken@1670
  1266
        }
slouken@1670
  1267
    }
slouken@1670
  1268
slouken@1670
  1269
    /* Copy the palette if any */
slouken@1670
  1270
    if (fmt->palette && dst.format->palette) {
slouken@1670
  1271
        SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
slouken@1670
  1272
                              fmt->palette->ncolors);
slouken@1670
  1273
slouken@1670
  1274
        SDL_memcpy(dst.format->palette->colors,
slouken@1670
  1275
                   fmt->palette->colors,
slouken@1670
  1276
                   fmt->palette->ncolors * sizeof(SDL_Color));
slouken@1670
  1277
        dst.format->palette->ncolors = fmt->palette->ncolors;
slouken@1670
  1278
    }
slouken@1670
  1279
slouken@1670
  1280
    /* Make the texture transparent if the surface has colorkey */
slouken@1670
  1281
    if (surface_flags & SDL_SRCCOLORKEY) {
slouken@1670
  1282
        int row;
slouken@1670
  1283
        int length = dst.w * dst.format->BytesPerPixel;
slouken@1670
  1284
        Uint8 *p = (Uint8 *) dst.pixels;
slouken@1670
  1285
        for (row = 0; row < dst.h; ++row) {
slouken@1670
  1286
            SDL_memset(p, 0, length);
slouken@1670
  1287
            p += dst.pitch;
slouken@1670
  1288
        }
slouken@1670
  1289
    }
slouken@1670
  1290
slouken@1670
  1291
    /* Copy over the alpha channel */
slouken@1670
  1292
    if (surface_flags & SDL_SRCALPHA) {
slouken@1670
  1293
        if (fmt->Amask) {
slouken@1670
  1294
            surface->flags &= ~SDL_SRCALPHA;
slouken@1670
  1295
        } else {
slouken@1670
  1296
            /* FIXME: Need to make sure the texture has an alpha channel
slouken@1670
  1297
             *        and copy 'alpha' into the texture alpha channel.
slouken@1670
  1298
             */
slouken@1670
  1299
            alpha = surface->format->alpha;
slouken@1670
  1300
            SDL_SetAlpha(surface, 0, 0);
slouken@1670
  1301
        }
slouken@1670
  1302
    }
slouken@1670
  1303
slouken@1670
  1304
    /* Copy over the image data */
slouken@1670
  1305
    bounds.x = 0;
slouken@1670
  1306
    bounds.y = 0;
slouken@1670
  1307
    bounds.w = surface->w;
slouken@1670
  1308
    bounds.h = surface->h;
slouken@1670
  1309
    SDL_LowerBlit(surface, &bounds, &dst, &bounds);
slouken@1670
  1310
slouken@1670
  1311
    /* Clean up the original surface */
slouken@1670
  1312
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1670
  1313
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1670
  1314
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1670
  1315
    }
slouken@1670
  1316
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1670
  1317
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
  1318
        if (fmt->Amask) {
slouken@1670
  1319
            surface->flags |= SDL_SRCALPHA;
slouken@1670
  1320
        } else {
slouken@1670
  1321
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1670
  1322
        }
slouken@1670
  1323
    }
slouken@1670
  1324
slouken@1670
  1325
    /* Update the texture */
slouken@1670
  1326
    if (dst.flags & SDL_PREALLOC) {
slouken@1670
  1327
        SDL_UnlockTexture(textureID);
slouken@1670
  1328
    } else {
slouken@1670
  1329
        SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
slouken@1670
  1330
        SDL_free(dst.pixels);
slouken@1670
  1331
    }
slouken@1670
  1332
    SDL_FreeFormat(dst.format);
slouken@1670
  1333
slouken@1670
  1334
    return textureID;
slouken@1670
  1335
}
slouken@1670
  1336
slouken@1670
  1337
static __inline__ SDL_Texture *
slouken@1670
  1338
SDL_GetTextureFromID(SDL_TextureID textureID)
slouken@1670
  1339
{
slouken@1670
  1340
    int hash;
slouken@1670
  1341
    SDL_Texture *texture;
slouken@1670
  1342
slouken@1670
  1343
    if (!_this) {
slouken@1662
  1344
        return NULL;
slouken@1662
  1345
    }
slouken@1662
  1346
slouken@1670
  1347
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1348
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1349
         texture = texture->next) {
slouken@1670
  1350
        if (texture->id == textureID) {
slouken@1670
  1351
            return texture;
slouken@1670
  1352
        }
slouken@1670
  1353
    }
slouken@1670
  1354
    return NULL;
slouken@1670
  1355
}
slouken@1670
  1356
slouken@1670
  1357
int
slouken@1670
  1358
SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
slouken@1670
  1359
                 int *w, int *h)
slouken@1670
  1360
{
slouken@1670
  1361
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1362
slouken@1670
  1363
    if (!texture) {
slouken@1670
  1364
        return -1;
slouken@1662
  1365
    }
slouken@1662
  1366
slouken@1670
  1367
    if (format) {
slouken@1670
  1368
        *format = texture->format;
slouken@1670
  1369
    }
slouken@1670
  1370
    if (access) {
slouken@1670
  1371
        *access = texture->access;
slouken@1670
  1372
    }
slouken@1670
  1373
    if (w) {
slouken@1670
  1374
        *w = texture->w;
slouken@1670
  1375
    }
slouken@1670
  1376
    if (h) {
slouken@1670
  1377
        *h = texture->h;
slouken@1670
  1378
    }
slouken@1670
  1379
    return 0;
slouken@1670
  1380
}
slouken@1669
  1381
slouken@1670
  1382
int
slouken@1678
  1383
SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
slouken@1678
  1384
{
slouken@1678
  1385
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1678
  1386
    SDL_Renderer *renderer;
slouken@1678
  1387
slouken@1678
  1388
    if (!texture) {
slouken@1678
  1389
        return -1;
slouken@1678
  1390
    }
slouken@1678
  1391
slouken@1678
  1392
    renderer = texture->renderer;
slouken@1678
  1393
    if (!renderer->QueryTexturePixels) {
slouken@1678
  1394
        return -1;
slouken@1678
  1395
    }
slouken@1678
  1396
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1678
  1397
}
slouken@1678
  1398
slouken@1678
  1399
int
slouken@1681
  1400
SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
slouken@1680
  1401
                      int firstcolor, int ncolors)
slouken@1680
  1402
{
slouken@1680
  1403
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1680
  1404
    SDL_Renderer *renderer;
slouken@1680
  1405
slouken@1680
  1406
    if (!texture) {
slouken@1680
  1407
        return -1;
slouken@1680
  1408
    }
slouken@1680
  1409
slouken@1680
  1410
    renderer = texture->renderer;
slouken@1680
  1411
    if (!renderer->SetTexturePalette) {
slouken@1680
  1412
        return -1;
slouken@1680
  1413
    }
slouken@1680
  1414
    return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
slouken@1680
  1415
                                       ncolors);
slouken@1680
  1416
}
slouken@1680
  1417
slouken@1680
  1418
int
slouken@1681
  1419
SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
slouken@1681
  1420
                      int firstcolor, int ncolors)
slouken@1681
  1421
{
slouken@1681
  1422
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1681
  1423
    SDL_Renderer *renderer;
slouken@1681
  1424
slouken@1681
  1425
    if (!texture) {
slouken@1681
  1426
        return -1;
slouken@1681
  1427
    }
slouken@1681
  1428
slouken@1681
  1429
    renderer = texture->renderer;
slouken@1681
  1430
    if (!renderer->GetTexturePalette) {
slouken@1681
  1431
        return -1;
slouken@1681
  1432
    }
slouken@1681
  1433
    return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
slouken@1681
  1434
                                       ncolors);
slouken@1681
  1435
}
slouken@1681
  1436
slouken@1681
  1437
int
slouken@1681
  1438
SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
slouken@1670
  1439
                  const void *pixels, int pitch)
slouken@1670
  1440
{
slouken@1670
  1441
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1442
    SDL_Renderer *renderer;
slouken@1670
  1443
slouken@1670
  1444
    if (!texture) {
slouken@1670
  1445
        return -1;
slouken@1670
  1446
    }
slouken@1670
  1447
slouken@1670
  1448
    renderer = texture->renderer;
slouken@1670
  1449
    if (!renderer->UpdateTexture) {
slouken@1670
  1450
        return -1;
slouken@1670
  1451
    }
slouken@1676
  1452
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1670
  1453
}
slouken@1670
  1454
slouken@1670
  1455
int
slouken@1681
  1456
SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
slouken@1670
  1457
                void **pixels, int *pitch)
slouken@1670
  1458
{
slouken@1670
  1459
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1460
    SDL_Renderer *renderer;
slouken@1670
  1461
slouken@1670
  1462
    if (!texture) {
slouken@1670
  1463
        return -1;
slouken@1670
  1464
    }
slouken@1670
  1465
slouken@1670
  1466
    renderer = texture->renderer;
slouken@1670
  1467
    if (!renderer->LockTexture) {
slouken@1670
  1468
        return -1;
slouken@1670
  1469
    }
slouken@1676
  1470
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@1676
  1471
                                 pitch);
slouken@1670
  1472
}
slouken@1670
  1473
slouken@1670
  1474
void
slouken@1670
  1475
SDL_UnlockTexture(SDL_TextureID textureID)
slouken@1670
  1476
{
slouken@1670
  1477
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1478
    SDL_Renderer *renderer;
slouken@1670
  1479
slouken@1670
  1480
    if (!texture) {
slouken@1670
  1481
        return;
slouken@1670
  1482
    }
slouken@1670
  1483
slouken@1670
  1484
    renderer = texture->renderer;
slouken@1670
  1485
    if (!renderer->UnlockTexture) {
slouken@1670
  1486
        return;
slouken@1670
  1487
    }
slouken@1676
  1488
    return renderer->UnlockTexture(renderer, texture);
slouken@1670
  1489
}
slouken@1670
  1490
slouken@1670
  1491
void
slouken@1681
  1492
SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
slouken@1681
  1493
                 const SDL_Rect * rects)
slouken@1670
  1494
{
slouken@1670
  1495
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1496
    SDL_Renderer *renderer;
slouken@1670
  1497
slouken@1670
  1498
    if (!texture) {
slouken@1670
  1499
        return;
slouken@1670
  1500
    }
slouken@1670
  1501
slouken@1670
  1502
    renderer = texture->renderer;
slouken@1670
  1503
    if (!renderer->DirtyTexture) {
slouken@1670
  1504
        return;
slouken@1670
  1505
    }
slouken@1676
  1506
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1670
  1507
}
slouken@1670
  1508
slouken@1670
  1509
void
slouken@1670
  1510
SDL_SelectRenderTexture(SDL_TextureID textureID)
slouken@1670
  1511
{
slouken@1670
  1512
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1513
    SDL_Renderer *renderer;
slouken@1670
  1514
slouken@1670
  1515
    if (!texture || texture->access != SDL_TextureAccess_Render) {
slouken@1670
  1516
        return;
slouken@1670
  1517
    }
slouken@1670
  1518
    renderer = texture->renderer;
slouken@1670
  1519
    if (!renderer->SelectRenderTexture) {
slouken@1670
  1520
        return;
slouken@1670
  1521
    }
slouken@1676
  1522
    renderer->SelectRenderTexture(renderer, texture);
slouken@1670
  1523
}
slouken@1670
  1524
slouken@1670
  1525
int
slouken@1681
  1526
SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
slouken@1670
  1527
{
slouken@1670
  1528
    SDL_Renderer *renderer;
slouken@1670
  1529
slouken@1670
  1530
    if (!_this) {
slouken@1670
  1531
        return -1;
slouken@1670
  1532
    }
slouken@1670
  1533
slouken@1670
  1534
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1535
    if (!renderer || !renderer->RenderFill) {
slouken@1670
  1536
        return -1;
slouken@1670
  1537
    }
slouken@1670
  1538
slouken@1676
  1539
    renderer->RenderFill(renderer, rect, color);
slouken@1670
  1540
}
slouken@1670
  1541
slouken@1670
  1542
int
slouken@1681
  1543
SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
slouken@1681
  1544
               const SDL_Rect * dstrect, int blendMode, int scaleMode)
slouken@1670
  1545
{
slouken@1670
  1546
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1547
    SDL_Renderer *renderer;
slouken@1670
  1548
slouken@1670
  1549
    if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
slouken@1670
  1550
        return;
slouken@1670
  1551
    }
slouken@1670
  1552
slouken@1670
  1553
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1554
    if (!renderer || !renderer->RenderCopy) {
slouken@1670
  1555
        return -1;
slouken@1670
  1556
    }
slouken@1670
  1557
slouken@1676
  1558
    return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
slouken@1676
  1559
                                blendMode, scaleMode);
slouken@1670
  1560
}
slouken@1670
  1561
slouken@1670
  1562
int
slouken@1681
  1563
SDL_RenderReadPixels(const SDL_Rect * rect, void *pixels, int pitch)
slouken@1670
  1564
{
slouken@1670
  1565
    SDL_Renderer *renderer;
slouken@1670
  1566
slouken@1670
  1567
    if (!_this) {
slouken@1670
  1568
        return -1;
slouken@1670
  1569
    }
slouken@1670
  1570
slouken@1670
  1571
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1572
    if (!renderer || !renderer->RenderReadPixels) {
slouken@1670
  1573
        return -1;
slouken@1670
  1574
    }
slouken@1670
  1575
slouken@1676
  1576
    return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
slouken@1670
  1577
}
slouken@1670
  1578
slouken@1670
  1579
int
slouken@1681
  1580
SDL_RenderWritePixels(const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1670
  1581
{
slouken@1670
  1582
    SDL_Renderer *renderer;
slouken@1670
  1583
slouken@1670
  1584
    if (!_this) {
slouken@1670
  1585
        return -1;
slouken@1670
  1586
    }
slouken@1670
  1587
slouken@1670
  1588
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1589
    if (!renderer || !renderer->RenderWritePixels) {
slouken@1670
  1590
        return -1;
slouken@1670
  1591
    }
slouken@1670
  1592
slouken@1676
  1593
    return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
slouken@1670
  1594
}
slouken@1670
  1595
slouken@1670
  1596
void
slouken@1670
  1597
SDL_RenderPresent(void)
slouken@1670
  1598
{
slouken@1670
  1599
    SDL_Renderer *renderer;
slouken@1670
  1600
slouken@1670
  1601
    if (!_this) {
slouken@1670
  1602
        return;
slouken@1670
  1603
    }
slouken@1670
  1604
slouken@1670
  1605
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1606
    if (!renderer || !renderer->RenderPresent) {
slouken@1670
  1607
        return;
slouken@1670
  1608
    }
slouken@1670
  1609
slouken@1676
  1610
    renderer->RenderPresent(renderer);
slouken@1670
  1611
}
slouken@1670
  1612
slouken@1670
  1613
void
slouken@1670
  1614
SDL_DestroyTexture(SDL_TextureID textureID)
slouken@1670
  1615
{
slouken@1670
  1616
    int hash;
slouken@1670
  1617
    SDL_Texture *prev, *texture;
slouken@1670
  1618
    SDL_Renderer *renderer;
slouken@1670
  1619
slouken@1670
  1620
    if (!_this) {
slouken@1670
  1621
        return;
slouken@1670
  1622
    }
slouken@1670
  1623
slouken@1670
  1624
    /* Look up the texture in the hash table */
slouken@1670
  1625
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1626
    prev = NULL;
slouken@1670
  1627
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1628
         prev = texture, texture = texture->next) {
slouken@1670
  1629
        if (texture->id == textureID) {
slouken@1670
  1630
            break;
slouken@1669
  1631
        }
slouken@1662
  1632
    }
slouken@1670
  1633
    if (!texture) {
slouken@1670
  1634
        return;
slouken@1662
  1635
    }
slouken@1662
  1636
slouken@1670
  1637
    /* Unlink the texture from the list */
slouken@1670
  1638
    if (prev) {
slouken@1670
  1639
        prev->next = texture->next;
slouken@1670
  1640
    } else {
slouken@1670
  1641
        SDL_CurrentDisplay.textures[hash] = texture->next;
slouken@1670
  1642
    }
slouken@1662
  1643
slouken@1670
  1644
    /* Free the texture */
slouken@1670
  1645
    renderer = texture->renderer;
slouken@1676
  1646
    renderer->DestroyTexture(renderer, texture);
slouken@1670
  1647
    SDL_free(texture);
slouken@1670
  1648
}
slouken@1669
  1649
slouken@1670
  1650
void
slouken@1670
  1651
SDL_DestroyRenderer(SDL_WindowID windowID)
slouken@1670
  1652
{
slouken@1670
  1653
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1654
    SDL_Renderer *renderer;
slouken@1670
  1655
    int i;
slouken@1662
  1656
slouken@1670
  1657
    if (!window) {
slouken@1670
  1658
        return;
slouken@1670
  1659
    }
slouken@1670
  1660
slouken@1670
  1661
    renderer = window->renderer;
slouken@1670
  1662
    if (!renderer) {
slouken@1670
  1663
        return;
slouken@1670
  1664
    }
slouken@1670
  1665
slouken@1670
  1666
    /* Free existing textures for this renderer */
slouken@1670
  1667
    for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
slouken@1670
  1668
        SDL_Texture *texture;
slouken@1670
  1669
        SDL_Texture *prev = NULL;
slouken@1670
  1670
        SDL_Texture *next;
slouken@1670
  1671
        for (texture = SDL_CurrentDisplay.textures[i]; texture;
slouken@1670
  1672
             texture = next) {
slouken@1670
  1673
            next = texture->next;
slouken@1670
  1674
            if (texture->renderer == renderer) {
slouken@1670
  1675
                if (prev) {
slouken@1670
  1676
                    prev->next = next;
slouken@1670
  1677
                } else {
slouken@1670
  1678
                    SDL_CurrentDisplay.textures[i] = next;
slouken@1670
  1679
                }
slouken@1676
  1680
                renderer->DestroyTexture(renderer, texture);
slouken@1670
  1681
                SDL_free(texture);
slouken@1662
  1682
            } else {
slouken@1670
  1683
                prev = texture;
slouken@1662
  1684
            }
slouken@1662
  1685
        }
slouken@1662
  1686
    }
slouken@1662
  1687
slouken@1670
  1688
    /* Free the renderer instance */
slouken@1670
  1689
    renderer->DestroyRenderer(renderer);
slouken@1670
  1690
slouken@1670
  1691
    /* Clear references */
slouken@1670
  1692
    window->renderer = NULL;
slouken@1670
  1693
    if (SDL_CurrentDisplay.current_renderer == renderer) {
slouken@1670
  1694
        SDL_CurrentDisplay.current_renderer = NULL;
slouken@1662
  1695
    }
slouken@0
  1696
}
slouken@0
  1697
slouken@1662
  1698
void
slouken@1668
  1699
SDL_VideoQuit(void)
slouken@0
  1700
{
slouken@1662
  1701
    int i, j;
slouken@0
  1702
slouken@1662
  1703
    if (!_this) {
slouken@1662
  1704
        return;
slouken@1662
  1705
    }
slouken@0
  1706
slouken@1662
  1707
    /* Halt event processing before doing anything else */
slouken@1668
  1708
    SDL_StopEventLoop();
slouken@0
  1709
slouken@1662
  1710
    /* Clean up the system video */
slouken@1662
  1711
    for (i = _this->num_displays; i--;) {
slouken@1662
  1712
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1713
        for (j = display->num_windows; j--;) {
slouken@1668
  1714
            SDL_DestroyWindow(display->windows[i].id);
slouken@1662
  1715
        }
slouken@1662
  1716
        if (display->windows) {
slouken@1668
  1717
            SDL_free(display->windows);
slouken@1662
  1718
            display->windows = NULL;
slouken@1662
  1719
        }
slouken@1681
  1720
        if (display->palette.colors) {
slouken@1681
  1721
            SDL_free(display->palette.colors);
slouken@1681
  1722
            display->palette.colors = NULL;
slouken@1681
  1723
            display->palette.ncolors = 0;
slouken@1681
  1724
        }
slouken@1662
  1725
    }
slouken@1668
  1726
    _this->VideoQuit(_this);
slouken@1662
  1727
    if (_this->displays) {
slouken@1668
  1728
        SDL_free(_this->displays);
slouken@1662
  1729
    }
slouken@1668
  1730
    _this->free(_this);
slouken@1662
  1731
    _this = NULL;
slouken@0
  1732
}
slouken@0
  1733
slouken@0
  1734
/* Load the GL driver library */
slouken@1662
  1735
int
slouken@1668
  1736
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1737
{
slouken@1662
  1738
    int retval;
slouken@0
  1739
slouken@1662
  1740
    retval = -1;
slouken@1662
  1741
    if (_this == NULL) {
slouken@1668
  1742
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
  1743
    } else {
slouken@1662
  1744
        if (_this->GL_LoadLibrary) {
slouken@1668
  1745
            retval = _this->GL_LoadLibrary(_this, path);
slouken@1662
  1746
        } else {
slouken@1668
  1747
            SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1748
        }
slouken@1662
  1749
    }
slouken@1662
  1750
    return (retval);
slouken@0
  1751
}
slouken@0
  1752
slouken@1662
  1753
void *
slouken@1668
  1754
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1755
{
slouken@1662
  1756
    void *func;
slouken@0
  1757
slouken@1662
  1758
    func = NULL;
slouken@1662
  1759
    if (_this->GL_GetProcAddress) {
slouken@1662
  1760
        if (_this->gl_config.driver_loaded) {
slouken@1668
  1761
            func = _this->GL_GetProcAddress(_this, proc);
slouken@1662
  1762
        } else {
slouken@1668
  1763
            SDL_SetError("No GL driver has been loaded");
slouken@1662
  1764
        }
slouken@1662
  1765
    } else {
slouken@1668
  1766
        SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1767
    }
slouken@1662
  1768
    return func;
slouken@0
  1769
}
slouken@0
  1770
slouken@0
  1771
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1772
int
slouken@1668
  1773
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  1774
{
slouken@1662
  1775
    int retval;
slouken@0
  1776
slouken@1662
  1777
    retval = 0;
slouken@1662
  1778
    switch (attr) {
slouken@1662
  1779
    case SDL_GL_RED_SIZE:
slouken@1662
  1780
        _this->gl_config.red_size = value;
slouken@1662
  1781
        break;
slouken@1662
  1782
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1783
        _this->gl_config.green_size = value;
slouken@1662
  1784
        break;
slouken@1662
  1785
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1786
        _this->gl_config.blue_size = value;
slouken@1662
  1787
        break;
slouken@1662
  1788
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1789
        _this->gl_config.alpha_size = value;
slouken@1662
  1790
        break;
slouken@1662
  1791
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1792
        _this->gl_config.double_buffer = value;
slouken@1662
  1793
        break;
slouken@1662
  1794
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1795
        _this->gl_config.buffer_size = value;
slouken@1662
  1796
        break;
slouken@1662
  1797
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1798
        _this->gl_config.depth_size = value;
slouken@1662
  1799
        break;
slouken@1662
  1800
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1801
        _this->gl_config.stencil_size = value;
slouken@1662
  1802
        break;
slouken@1662
  1803
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1804
        _this->gl_config.accum_red_size = value;
slouken@1662
  1805
        break;
slouken@1662
  1806
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1807
        _this->gl_config.accum_green_size = value;
slouken@1662
  1808
        break;
slouken@1662
  1809
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1810
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1811
        break;
slouken@1662
  1812
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1813
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1814
        break;
slouken@1662
  1815
    case SDL_GL_STEREO:
slouken@1662
  1816
        _this->gl_config.stereo = value;
slouken@1662
  1817
        break;
slouken@1662
  1818
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1819
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1820
        break;
slouken@1662
  1821
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1822
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1823
        break;
slouken@1662
  1824
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1825
        _this->gl_config.accelerated = value;
slouken@1662
  1826
        break;
slouken@1662
  1827
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1828
        _this->gl_config.swap_control = value;
slouken@1662
  1829
        break;
slouken@1662
  1830
    default:
slouken@1668
  1831
        SDL_SetError("Unknown OpenGL attribute");
slouken@1662
  1832
        retval = -1;
slouken@1662
  1833
        break;
slouken@1662
  1834
    }
slouken@1662
  1835
    return (retval);
slouken@0
  1836
}
slouken@0
  1837
slouken@0
  1838
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1839
int
slouken@1668
  1840
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  1841
{
slouken@1662
  1842
    int retval = -1;
slouken@0
  1843
slouken@1662
  1844
    if (_this->GL_GetAttribute) {
slouken@1668
  1845
        retval = _this->GL_GetAttribute(_this, attr, value);
slouken@1662
  1846
    } else {
slouken@1662
  1847
        *value = 0;
slouken@1668
  1848
        SDL_SetError("GL_GetAttribute not supported");
slouken@1662
  1849
    }
slouken@1662
  1850
    return retval;
slouken@0
  1851
}
slouken@0
  1852
slouken@0
  1853
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1854
void
slouken@1668
  1855
SDL_GL_SwapBuffers(void)
slouken@0
  1856
{
slouken@1670
  1857
    // FIXME: Track the current window context - do we provide N contexts, and match them to M windows, or is there a one-to-one mapping?
slouken@1670
  1858
    _this->GL_SwapBuffers(_this);
slouken@0
  1859
}
slouken@0
  1860
slouken@1662
  1861
#if 0                           // FIXME
slouken@541
  1862
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1863
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1864
static void
slouken@1668
  1865
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1866
{
slouken@1662
  1867
    int x, y;
slouken@1662
  1868
    Uint32 colorkey;
slouken@0
  1869
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1870
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1871
slouken@1662
  1872
    colorkey = icon->format->colorkey;
slouken@1662
  1873
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1874
    case 1:
slouken@1662
  1875
        {
slouken@1662
  1876
            Uint8 *pixels;
slouken@1662
  1877
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1878
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1879
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1880
                    if (*pixels++ == colorkey) {
slouken@1668
  1881
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1882
                    }
slouken@1662
  1883
                }
slouken@1662
  1884
            }
slouken@1662
  1885
        }
slouken@1662
  1886
        break;
slouken@0
  1887
slouken@1662
  1888
    case 2:
slouken@1662
  1889
        {
slouken@1662
  1890
            Uint16 *pixels;
slouken@1662
  1891
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1892
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1893
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1894
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1895
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1896
                    } else if ((flags & 2)
slouken@1662
  1897
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1898
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1899
                    }
slouken@1662
  1900
                    pixels++;
slouken@1662
  1901
                }
slouken@1662
  1902
            }
slouken@1662
  1903
        }
slouken@1662
  1904
        break;
slouken@0
  1905
slouken@1662
  1906
    case 4:
slouken@1662
  1907
        {
slouken@1662
  1908
            Uint32 *pixels;
slouken@1662
  1909
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1910
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1911
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1912
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1913
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1914
                    } else if ((flags & 2)
slouken@1662
  1915
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1916
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1917
                    }
slouken@1662
  1918
                    pixels++;
slouken@1662
  1919
                }
slouken@1662
  1920
            }
slouken@1662
  1921
        }
slouken@1662
  1922
        break;
slouken@1662
  1923
    }
slouken@0
  1924
}
slouken@0
  1925
slouken@0
  1926
/*
slouken@0
  1927
 * Sets the window manager icon for the display window.
slouken@0
  1928
 */
slouken@1662
  1929
void
slouken@1668
  1930
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  1931
{
slouken@1662
  1932
    if (icon && _this->SetIcon) {
slouken@1662
  1933
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  1934
        if (mask == NULL) {
slouken@1662
  1935
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  1936
            int flags = 0;
slouken@1668
  1937
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@1662
  1938
            if (mask == NULL) {
slouken@1662
  1939
                return;
slouken@1662
  1940
            }
slouken@1668
  1941
            SDL_memset(mask, ~0, mask_len);
slouken@1662
  1942
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  1943
                flags |= 1;
slouken@1662
  1944
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  1945
                flags |= 2;
slouken@1662
  1946
            if (flags) {
slouken@1668
  1947
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@1662
  1948
            }
slouken@1668
  1949
            _this->SetIcon(_this, icon, mask);
slouken@1668
  1950
            SDL_free(mask);
slouken@1662
  1951
        } else {
slouken@1668
  1952
            _this->SetIcon(_this, icon, mask);
slouken@1662
  1953
        }
slouken@1662
  1954
    }
slouken@1662
  1955
}
slouken@1662
  1956
#endif
slouken@0
  1957
slouken@1662
  1958
SDL_bool
slouken@1668
  1959
SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  1960
{
slouken@1668
  1961
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1962
slouken@1662
  1963
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  1964
        return SDL_FALSE;
slouken@1662
  1965
    }
slouken@1668
  1966
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  1967
}
slouken@0
  1968
slouken@1662
  1969
/* vi: set ts=4 sw=4 expandtab: */