src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 24 Jun 2006 16:51:01 +0000
branchSDL-1.3
changeset 1706 1577404809f0
parent 1684 c4aa1a2f48f1
child 1707 57ce47f033a5
permissions -rw-r--r--
Added a multi-window version of testsprite using the new API.
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@1706
   510
        /* Pick some reasonable defaults if the app and driver don't care */
slouken@1706
   511
        if (!closest->format) {
slouken@1706
   512
            closest->format = SDL_PixelFormat_RGB888;
slouken@1706
   513
        }
slouken@1706
   514
        if (!closest->w) {
slouken@1706
   515
            closest->w = 640;
slouken@1706
   516
        }
slouken@1706
   517
        if (!closest->h) {
slouken@1706
   518
            closest->h = 480;
slouken@1706
   519
        }
slouken@1662
   520
        return closest;
slouken@1662
   521
    }
slouken@1662
   522
    return NULL;
slouken@1662
   523
}
slouken@0
   524
slouken@1662
   525
int
slouken@1668
   526
SDL_SetDisplayMode(const SDL_DisplayMode * mode)
slouken@1662
   527
{
slouken@1667
   528
    SDL_VideoDisplay *display;
slouken@1662
   529
    SDL_DisplayMode display_mode;
slouken@1683
   530
    int i, ncolors;
slouken@229
   531
slouken@1662
   532
    if (!_this) {
slouken@1668
   533
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   534
        return -1;
slouken@1662
   535
    }
slouken@0
   536
slouken@1662
   537
    /* Make sure there's an actual display mode to set */
slouken@1662
   538
    if (!mode) {
slouken@1668
   539
        SDL_SetError("No mode passed to SDL_SetDisplayMode");
slouken@1662
   540
        return -1;
slouken@1662
   541
    }
slouken@1667
   542
    display = &SDL_CurrentDisplay;
slouken@1662
   543
    display_mode = *mode;
slouken@0
   544
slouken@1662
   545
    /* Default to the current mode */
slouken@1662
   546
    if (!display_mode.format) {
slouken@1667
   547
        display_mode.format = display->current_mode.format;
slouken@1662
   548
    }
slouken@1662
   549
    if (!display_mode.w) {
slouken@1667
   550
        display_mode.w = display->current_mode.w;
slouken@1662
   551
    }
slouken@1662
   552
    if (!display_mode.h) {
slouken@1667
   553
        display_mode.h = display->current_mode.h;
slouken@1662
   554
    }
slouken@1662
   555
    if (!display_mode.refresh_rate) {
slouken@1667
   556
        display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@1662
   557
    }
slouken@0
   558
slouken@1662
   559
    /* Get a good video mode, the closest one possible */
slouken@1668
   560
    if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
slouken@1668
   561
        SDL_SetError("No video mode large enough for %dx%d",
slouken@1668
   562
                     display_mode.w, display_mode.h);
slouken@1662
   563
        return -1;
slouken@1662
   564
    }
slouken@0
   565
slouken@1662
   566
    /* See if there's anything left to do */
slouken@1662
   567
    if (SDL_memcmp
slouken@1668
   568
        (&display_mode, SDL_GetCurrentDisplayMode(),
slouken@1668
   569
         sizeof(display_mode)) == 0) {
slouken@1662
   570
        return 0;
slouken@1662
   571
    }
slouken@0
   572
slouken@1683
   573
    /* Set up a palette, if necessary */
slouken@1681
   574
    if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
slouken@1683
   575
        ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
slouken@1683
   576
    } else {
slouken@1683
   577
        ncolors = 0;
slouken@1683
   578
    }
slouken@1683
   579
    if ((!ncolors && display->palette) || (ncolors && !display->palette)
slouken@1684
   580
        || (ncolors && ncolors != display->palette->ncolors)) {
slouken@1683
   581
        if (display->palette) {
slouken@1683
   582
            SDL_FreePalette(display->palette);
slouken@1683
   583
            display->palette = NULL;
slouken@1681
   584
        }
slouken@1683
   585
        if (ncolors) {
slouken@1683
   586
            display->palette = SDL_AllocPalette(ncolors);
slouken@1683
   587
            if (!display->palette) {
slouken@1683
   588
                return -1;
slouken@1683
   589
            }
slouken@1706
   590
            SDL_DitherColors(display->palette->colors,
slouken@1706
   591
                             SDL_BITSPERPIXEL(display_mode.format));
slouken@1681
   592
        }
slouken@1681
   593
    }
slouken@1681
   594
slouken@1668
   595
    return _this->SetDisplayMode(_this, &display_mode);
slouken@1662
   596
}
slouken@0
   597
slouken@1681
   598
int
slouken@1681
   599
SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1681
   600
{
slouken@1681
   601
    SDL_Palette *palette;
slouken@1683
   602
    int status = 0;
slouken@1681
   603
slouken@1681
   604
    if (!_this) {
slouken@1681
   605
        SDL_SetError("Video subsystem has not been initialized");
slouken@1681
   606
        return -1;
slouken@1681
   607
    }
slouken@1683
   608
    if (!SDL_CurrentDisplay.palette) {
slouken@1681
   609
        SDL_SetError("Display mode does not have a palette");
slouken@1681
   610
        return -1;
slouken@1681
   611
    }
slouken@1681
   612
slouken@1683
   613
    status =
slouken@1683
   614
        SDL_SetPaletteColors(SDL_CurrentDisplay.palette, colors, firstcolor,
slouken@1683
   615
                             ncolors);
slouken@1681
   616
slouken@1681
   617
    if (_this->SetDisplayPalette) {
slouken@1683
   618
        if (_this->SetDisplayPalette(_this, palette) < 0) {
slouken@1683
   619
            status = -1;
slouken@1683
   620
        }
slouken@1681
   621
    }
slouken@1683
   622
    return status;
slouken@1681
   623
}
slouken@1681
   624
slouken@1681
   625
int
slouken@1681
   626
SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
slouken@1681
   627
{
slouken@1681
   628
    SDL_Palette *palette;
slouken@1681
   629
slouken@1681
   630
    if (!_this) {
slouken@1681
   631
        SDL_SetError("Video subsystem has not been initialized");
slouken@1681
   632
        return -1;
slouken@1681
   633
    }
slouken@1681
   634
slouken@1683
   635
    palette = SDL_CurrentDisplay.palette;
slouken@1681
   636
    if (!palette->ncolors) {
slouken@1681
   637
        SDL_SetError("Display mode does not have a palette");
slouken@1681
   638
        return -1;
slouken@1681
   639
    }
slouken@1681
   640
slouken@1681
   641
    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
slouken@1681
   642
        SDL_SetError("Palette indices are out of range");
slouken@1681
   643
        return -1;
slouken@1681
   644
    }
slouken@1681
   645
slouken@1681
   646
    SDL_memcpy(colors, &palette->colors[firstcolor],
slouken@1681
   647
               ncolors * sizeof(*colors));
slouken@1681
   648
    return 0;
slouken@1681
   649
}
slouken@1681
   650
slouken@1662
   651
SDL_WindowID
slouken@1668
   652
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@1662
   653
{
slouken@1662
   654
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@1662
   655
                                  SDL_WINDOW_BORDERLESS |
slouken@1662
   656
                                  SDL_WINDOW_SHOWN |
slouken@1662
   657
                                  SDL_WINDOW_OPENGL |
slouken@1662
   658
                                  SDL_WINDOW_RESIZABLE |
slouken@1662
   659
                                  SDL_WINDOW_MAXIMIZED |
slouken@1662
   660
                                  SDL_WINDOW_MINIMIZED |
slouken@1662
   661
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@1662
   662
    SDL_Window window;
slouken@1662
   663
    int num_windows;
slouken@1662
   664
    SDL_Window *windows;
slouken@0
   665
slouken@1662
   666
    if (!_this) {
slouken@1668
   667
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   668
        return 0;
slouken@1662
   669
    }
slouken@0
   670
slouken@1668
   671
    SDL_zero(window);
slouken@1670
   672
    window.id = _this->next_object_id++;
slouken@1668
   673
    window.title = title ? SDL_strdup(title) : NULL;
slouken@1662
   674
    window.x = x;
slouken@1662
   675
    window.y = y;
slouken@1662
   676
    window.w = w;
slouken@1662
   677
    window.h = h;
slouken@1662
   678
    window.flags = (flags & allowed_flags);
slouken@1706
   679
    window.display = _this->current_display;
slouken@910
   680
slouken@1668
   681
    if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
slouken@1667
   682
        if (window.title) {
slouken@1668
   683
            SDL_free(window.title);
slouken@1667
   684
        }
slouken@1662
   685
        return 0;
slouken@1662
   686
    }
slouken@910
   687
slouken@1662
   688
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   689
    windows =
slouken@1668
   690
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   691
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   692
    if (!windows) {
slouken@1662
   693
        if (_this->DestroyWindow) {
slouken@1668
   694
            _this->DestroyWindow(_this, &window);
slouken@1662
   695
        }
slouken@1667
   696
        if (window.title) {
slouken@1668
   697
            SDL_free(window.title);
slouken@1667
   698
        }
slouken@1662
   699
        return 0;
slouken@1662
   700
    }
slouken@1662
   701
    windows[num_windows] = window;
slouken@1662
   702
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   703
    SDL_CurrentDisplay.num_windows++;
slouken@910
   704
slouken@1662
   705
    return window.id;
slouken@1662
   706
}
icculus@650
   707
slouken@1662
   708
SDL_WindowID
slouken@1681
   709
SDL_CreateWindowFrom(const void *data)
slouken@1662
   710
{
slouken@1662
   711
    SDL_Window window;
slouken@1662
   712
    int num_windows;
slouken@1662
   713
    SDL_Window *windows;
slouken@0
   714
slouken@1662
   715
    if (!_this) {
slouken@1668
   716
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   717
        return (0);
slouken@1662
   718
    }
slouken@0
   719
slouken@1668
   720
    SDL_zero(window);
slouken@1670
   721
    window.id = _this->next_object_id++;
slouken@1706
   722
    window.display = _this->current_display;
slouken@1662
   723
slouken@1662
   724
    if (!_this->CreateWindowFrom ||
slouken@1668
   725
        _this->CreateWindowFrom(_this, &window, data) < 0) {
slouken@1662
   726
        return 0;
slouken@1662
   727
    }
slouken@1662
   728
slouken@1662
   729
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   730
    windows =
slouken@1668
   731
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   732
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   733
    if (!windows) {
slouken@1662
   734
        if (_this->DestroyWindow) {
slouken@1668
   735
            _this->DestroyWindow(_this, &window);
slouken@1662
   736
        }
slouken@1667
   737
        if (window.title) {
slouken@1668
   738
            SDL_free(window.title);
slouken@1667
   739
        }
slouken@1662
   740
        return 0;
slouken@1662
   741
    }
slouken@1662
   742
    windows[num_windows] = window;
slouken@1662
   743
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   744
    SDL_CurrentDisplay.num_windows++;
slouken@1662
   745
slouken@1662
   746
    return window.id;
slouken@1662
   747
}
slouken@1662
   748
slouken@1706
   749
SDL_Window *
slouken@1668
   750
SDL_GetWindowFromID(SDL_WindowID windowID)
slouken@1662
   751
{
slouken@1662
   752
    int i, j;
slouken@1662
   753
slouken@1662
   754
    if (!_this) {
slouken@1662
   755
        return NULL;
slouken@1662
   756
    }
slouken@1662
   757
slouken@1662
   758
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   759
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   760
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   761
            SDL_Window *window = &display->windows[j];
slouken@1662
   762
            if (window->id == windowID) {
slouken@1662
   763
                return window;
slouken@1662
   764
            }
slouken@1662
   765
        }
slouken@1662
   766
    }
slouken@1662
   767
    return NULL;
slouken@1662
   768
}
slouken@1662
   769
slouken@1706
   770
SDL_VideoDisplay *
slouken@1706
   771
SDL_GetDisplayFromWindow(SDL_Window * window)
slouken@1706
   772
{
slouken@1706
   773
    if (!_this) {
slouken@1706
   774
        return NULL;
slouken@1706
   775
    }
slouken@1706
   776
    return &_this->displays[window->display];
slouken@1706
   777
}
slouken@1706
   778
slouken@1662
   779
Uint32
slouken@1668
   780
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1662
   781
{
slouken@1668
   782
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   783
slouken@1662
   784
    if (!window) {
slouken@1662
   785
        return 0;
slouken@1662
   786
    }
slouken@1662
   787
    return window->flags;
slouken@1662
   788
}
slouken@1662
   789
slouken@1662
   790
void
slouken@1668
   791
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1662
   792
{
slouken@1668
   793
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   794
slouken@1662
   795
    if (!window) {
slouken@1662
   796
        return;
slouken@1662
   797
    }
slouken@1662
   798
    if (window->title) {
slouken@1668
   799
        SDL_free(window->title);
slouken@1662
   800
    }
slouken@1668
   801
    window->title = SDL_strdup(title);
slouken@1662
   802
slouken@1662
   803
    if (_this->SetWindowTitle) {
slouken@1668
   804
        _this->SetWindowTitle(_this, window);
slouken@1662
   805
    }
slouken@1662
   806
}
slouken@1662
   807
slouken@1662
   808
const char *
slouken@1668
   809
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1662
   810
{
slouken@1668
   811
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   812
slouken@1662
   813
    if (!window) {
slouken@1662
   814
        return NULL;
slouken@1662
   815
    }
slouken@1662
   816
    return window->title;
slouken@1662
   817
}
slouken@1662
   818
slouken@1662
   819
void
slouken@1668
   820
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1662
   821
{
slouken@1668
   822
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   823
slouken@1662
   824
    if (!window) {
slouken@1662
   825
        return;
slouken@1662
   826
    }
slouken@1662
   827
    window->userdata = userdata;
slouken@1662
   828
}
slouken@1662
   829
slouken@1662
   830
void *
slouken@1668
   831
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1662
   832
{
slouken@1668
   833
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   834
slouken@1662
   835
    if (!window) {
slouken@1662
   836
        return NULL;
slouken@1662
   837
    }
slouken@1662
   838
    return window->userdata;
slouken@1662
   839
}
slouken@1662
   840
slouken@1662
   841
void
slouken@1668
   842
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1662
   843
{
slouken@1668
   844
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   845
slouken@1662
   846
    if (!window) {
slouken@1662
   847
        return;
slouken@1662
   848
    }
slouken@1662
   849
slouken@1662
   850
    window->x = x;
slouken@1662
   851
    window->y = y;
slouken@1662
   852
slouken@1662
   853
    if (_this->SetWindowPosition) {
slouken@1668
   854
        _this->SetWindowPosition(_this, window);
slouken@1662
   855
    }
slouken@1662
   856
}
slouken@1662
   857
slouken@1662
   858
void
slouken@1668
   859
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1662
   860
{
slouken@1668
   861
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   862
slouken@1662
   863
    if (!window) {
slouken@1662
   864
        return;
slouken@1662
   865
    }
slouken@1662
   866
    if (x) {
slouken@1662
   867
        *x = window->x;
slouken@1662
   868
    }
slouken@1662
   869
    if (y) {
slouken@1662
   870
        *y = window->y;
slouken@1662
   871
    }
slouken@1662
   872
}
slouken@1662
   873
slouken@1662
   874
void
slouken@1668
   875
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1662
   876
{
slouken@1668
   877
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   878
slouken@1662
   879
    if (!window) {
slouken@1662
   880
        return;
slouken@1662
   881
    }
slouken@1662
   882
slouken@1662
   883
    window->w = w;
slouken@1662
   884
    window->h = h;
slouken@1662
   885
slouken@1662
   886
    if (_this->SetWindowSize) {
slouken@1668
   887
        _this->SetWindowSize(_this, window);
slouken@1662
   888
    }
slouken@1662
   889
}
slouken@1662
   890
slouken@1662
   891
void
slouken@1668
   892
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1662
   893
{
slouken@1668
   894
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   895
slouken@1662
   896
    if (!window) {
slouken@1662
   897
        return;
slouken@1662
   898
    }
slouken@1662
   899
    if (w) {
slouken@1662
   900
        *w = window->w;
slouken@1662
   901
    }
slouken@1662
   902
    if (h) {
slouken@1662
   903
        *h = window->h;
slouken@1662
   904
    }
slouken@1662
   905
}
slouken@1662
   906
slouken@1662
   907
void
slouken@1668
   908
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1662
   909
{
slouken@1668
   910
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   911
slouken@1662
   912
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   913
        return;
slouken@1662
   914
    }
slouken@1662
   915
slouken@1662
   916
    window->flags |= SDL_WINDOW_SHOWN;
slouken@1662
   917
slouken@1662
   918
    if (_this->ShowWindow) {
slouken@1668
   919
        _this->ShowWindow(_this, window);
slouken@1662
   920
    }
slouken@1662
   921
}
slouken@1662
   922
slouken@1662
   923
void
slouken@1668
   924
SDL_HideWindow(SDL_WindowID windowID)
slouken@1662
   925
{
slouken@1668
   926
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   927
slouken@1662
   928
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   929
        return;
slouken@1662
   930
    }
slouken@1662
   931
slouken@1662
   932
    window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1662
   933
slouken@1662
   934
    if (_this->HideWindow) {
slouken@1668
   935
        _this->HideWindow(_this, window);
slouken@1662
   936
    }
slouken@1662
   937
}
slouken@1662
   938
slouken@1662
   939
void
slouken@1668
   940
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1662
   941
{
slouken@1668
   942
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   943
slouken@1662
   944
    if (!window) {
slouken@1662
   945
        return;
slouken@1662
   946
    }
slouken@1662
   947
slouken@1662
   948
    if (_this->RaiseWindow) {
slouken@1668
   949
        _this->RaiseWindow(_this, window);
slouken@1662
   950
    }
slouken@1662
   951
}
slouken@1662
   952
slouken@1662
   953
void
slouken@1668
   954
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1662
   955
{
slouken@1668
   956
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   957
slouken@1662
   958
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@1662
   959
        return;
slouken@1662
   960
    }
slouken@1662
   961
slouken@1662
   962
    window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1662
   963
slouken@1662
   964
    if (_this->MaximizeWindow) {
slouken@1668
   965
        _this->MaximizeWindow(_this, window);
slouken@1662
   966
    }
slouken@1662
   967
}
slouken@1662
   968
slouken@1662
   969
void
slouken@1668
   970
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1662
   971
{
slouken@1668
   972
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   973
slouken@1662
   974
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@1662
   975
        return;
slouken@1662
   976
    }
slouken@1662
   977
slouken@1662
   978
    window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1662
   979
slouken@1662
   980
    if (_this->MinimizeWindow) {
slouken@1668
   981
        _this->MinimizeWindow(_this, window);
slouken@1662
   982
    }
slouken@1662
   983
}
slouken@1662
   984
slouken@1662
   985
void
slouken@1668
   986
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1662
   987
{
slouken@1668
   988
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   989
slouken@1662
   990
    if (!window
slouken@1662
   991
        || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@1662
   992
        return;
slouken@1662
   993
    }
slouken@1662
   994
slouken@1662
   995
    window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
slouken@1662
   996
slouken@1662
   997
    if (_this->RestoreWindow) {
slouken@1668
   998
        _this->RestoreWindow(_this, window);
slouken@1662
   999
    }
slouken@1662
  1000
}
slouken@1662
  1001
slouken@1662
  1002
void
slouken@1668
  1003
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1662
  1004
{
slouken@1668
  1005
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1006
slouken@1662
  1007
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@1662
  1008
        return;
slouken@1662
  1009
    }
slouken@1662
  1010
slouken@1662
  1011
    if (mode) {
slouken@1662
  1012
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1662
  1013
    } else {
slouken@1662
  1014
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
  1015
    }
slouken@1662
  1016
slouken@1662
  1017
    if (_this->SetWindowGrab) {
slouken@1668
  1018
        _this->SetWindowGrab(_this, window);
slouken@1662
  1019
    }
slouken@1662
  1020
}
slouken@1662
  1021
slouken@1662
  1022
int
slouken@1668
  1023
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1662
  1024
{
slouken@1668
  1025
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1026
slouken@1662
  1027
    if (!window) {
slouken@1662
  1028
        return 0;
slouken@1662
  1029
    }
slouken@1662
  1030
slouken@1662
  1031
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1662
  1032
}
slouken@1662
  1033
slouken@1662
  1034
void
slouken@1668
  1035
SDL_DestroyWindow(SDL_WindowID windowID)
slouken@1662
  1036
{
slouken@1662
  1037
    int i, j;
slouken@1662
  1038
slouken@1662
  1039
    if (!_this) {
slouken@1662
  1040
        return;
slouken@1662
  1041
    }
slouken@1662
  1042
slouken@1662
  1043
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
  1044
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1045
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
  1046
            SDL_Window *window = &display->windows[j];
slouken@1662
  1047
            if (window->id != windowID) {
slouken@1662
  1048
                continue;
slouken@1662
  1049
            }
slouken@1662
  1050
            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1662
  1051
                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1668
  1052
                _this->SetWindowGrab(_this, window);
slouken@1662
  1053
            }
slouken@1670
  1054
            if (window->renderer) {
slouken@1670
  1055
                SDL_DestroyRenderer(window->id);
slouken@1662
  1056
            }
slouken@1662
  1057
            if (_this->DestroyWindow) {
slouken@1668
  1058
                _this->DestroyWindow(_this, window);
slouken@1662
  1059
            }
slouken@1662
  1060
            if (window->title) {
slouken@1668
  1061
                SDL_free(window->title);
slouken@1662
  1062
            }
slouken@1662
  1063
            if (window->gamma) {
slouken@1668
  1064
                SDL_free(window->gamma);
slouken@1662
  1065
            }
slouken@1662
  1066
            if (j != display->num_windows - 1) {
slouken@1668
  1067
                SDL_memcpy(&display->windows[i],
slouken@1668
  1068
                           &display->windows[i + 1],
slouken@1668
  1069
                           (display->num_windows - i - 1) * sizeof(*window));
slouken@1662
  1070
            }
slouken@1662
  1071
            --display->num_windows;
slouken@1662
  1072
            return;
slouken@1662
  1073
        }
slouken@1662
  1074
    }
slouken@1662
  1075
}
slouken@1662
  1076
slouken@1670
  1077
void
slouken@1670
  1078
SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
slouken@1670
  1079
{
slouken@1670
  1080
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@1670
  1081
    SDL_RenderDriver *render_drivers;
slouken@1670
  1082
slouken@1670
  1083
    render_drivers =
slouken@1670
  1084
        SDL_realloc(display->render_drivers,
slouken@1670
  1085
                    (display->num_render_drivers +
slouken@1670
  1086
                     1) * sizeof(*render_drivers));
slouken@1670
  1087
    if (render_drivers) {
slouken@1670
  1088
        render_drivers[display->num_render_drivers] = *driver;
slouken@1670
  1089
        display->render_drivers = render_drivers;
slouken@1670
  1090
        display->num_render_drivers++;
slouken@1670
  1091
    }
slouken@1670
  1092
}
slouken@1670
  1093
slouken@1670
  1094
int
slouken@1670
  1095
SDL_GetNumRenderers(void)
slouken@1670
  1096
{
slouken@1670
  1097
    if (_this) {
slouken@1670
  1098
        return SDL_CurrentDisplay.num_render_drivers;
slouken@1670
  1099
    }
slouken@1670
  1100
    return 0;
slouken@1670
  1101
}
slouken@1670
  1102
slouken@1670
  1103
int
slouken@1670
  1104
SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
slouken@1670
  1105
{
slouken@1670
  1106
    if (index < 0 || index >= SDL_GetNumRenderers()) {
slouken@1670
  1107
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1670
  1108
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1109
        return -1;
slouken@1670
  1110
    }
slouken@1670
  1111
    *info = SDL_CurrentDisplay.render_drivers[index].info;
slouken@1670
  1112
    return 0;
slouken@1670
  1113
}
slouken@1670
  1114
slouken@1670
  1115
int
slouken@1670
  1116
SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
slouken@1662
  1117
{
slouken@1668
  1118
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1119
slouken@1662
  1120
    if (!window) {
slouken@1670
  1121
        return 0;
slouken@1670
  1122
    }
slouken@1670
  1123
slouken@1670
  1124
    if (index < 0) {
slouken@1670
  1125
        int n = SDL_GetNumRenderers();
slouken@1670
  1126
        for (index = 0; index < n; ++index) {
slouken@1680
  1127
            SDL_RenderDriver *driver =
slouken@1680
  1128
                &SDL_CurrentDisplay.render_drivers[index];
slouken@1680
  1129
slouken@1680
  1130
            /* Skip minimal drivers in automatic scans */
slouken@1680
  1131
            if (!(flags & SDL_Renderer_Minimal)
slouken@1680
  1132
                && (driver->info.flags & SDL_Renderer_Minimal)) {
slouken@1680
  1133
                continue;
slouken@1680
  1134
            }
slouken@1680
  1135
            if ((driver->info.flags & flags) == flags) {
slouken@1670
  1136
                break;
slouken@1670
  1137
            }
slouken@1670
  1138
        }
slouken@1670
  1139
        if (index == n) {
slouken@1670
  1140
            SDL_SetError("Couldn't find matching render driver");
slouken@1670
  1141
            return -1;
slouken@1670
  1142
        }
slouken@1670
  1143
    }
slouken@1670
  1144
slouken@1670
  1145
    if (index >= SDL_GetNumRenderers()) {
slouken@1670
  1146
        SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@1670
  1147
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1148
        return -1;
slouken@1670
  1149
    }
slouken@1670
  1150
slouken@1670
  1151
    /* Free any existing renderer */
slouken@1670
  1152
    SDL_DestroyRenderer(windowID);
slouken@1670
  1153
slouken@1670
  1154
    /* Create a new renderer instance */
slouken@1670
  1155
    window->renderer =
slouken@1670
  1156
        SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
slouken@1670
  1157
                                                                flags);
slouken@1670
  1158
    if (!window->renderer) {
slouken@1670
  1159
        return -1;
slouken@1670
  1160
    }
slouken@1670
  1161
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1162
slouken@1670
  1163
    return 0;
slouken@1670
  1164
}
slouken@1670
  1165
slouken@1670
  1166
int
slouken@1670
  1167
SDL_SelectRenderer(SDL_WindowID windowID)
slouken@1670
  1168
{
slouken@1670
  1169
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1170
slouken@1670
  1171
    if (!window || !window->renderer) {
slouken@1670
  1172
        return -1;
slouken@1670
  1173
    }
slouken@1670
  1174
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1175
    return 0;
slouken@1670
  1176
}
slouken@1670
  1177
slouken@1670
  1178
SDL_TextureID
slouken@1670
  1179
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1670
  1180
{
slouken@1680
  1181
    int hash;
slouken@1670
  1182
    SDL_Renderer *renderer;
slouken@1670
  1183
    SDL_Texture *texture;
slouken@1670
  1184
slouken@1670
  1185
    if (!_this) {
slouken@1670
  1186
        return 0;
slouken@1670
  1187
    }
slouken@1670
  1188
slouken@1670
  1189
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1190
    if (!renderer || !renderer->CreateTexture) {
slouken@1670
  1191
        return 0;
slouken@1670
  1192
    }
slouken@1670
  1193
slouken@1670
  1194
    texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
slouken@1670
  1195
    if (!texture) {
slouken@1670
  1196
        SDL_OutOfMemory();
slouken@1670
  1197
        return 0;
slouken@1670
  1198
    }
slouken@1670
  1199
slouken@1670
  1200
    SDL_zerop(texture);
slouken@1670
  1201
    texture->id = _this->next_object_id++;
slouken@1670
  1202
    texture->format = format;
slouken@1670
  1203
    texture->access = access;
slouken@1670
  1204
    texture->w = w;
slouken@1670
  1205
    texture->h = h;
slouken@1670
  1206
    texture->renderer = renderer;
slouken@1670
  1207
slouken@1676
  1208
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1670
  1209
        SDL_free(texture);
slouken@1670
  1210
        return 0;
slouken@1670
  1211
    }
slouken@1680
  1212
slouken@1680
  1213
    hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1680
  1214
    texture->next = SDL_CurrentDisplay.textures[hash];
slouken@1680
  1215
    SDL_CurrentDisplay.textures[hash] = texture;
slouken@1680
  1216
slouken@1680
  1217
    return texture->id;
slouken@1670
  1218
}
slouken@1670
  1219
slouken@1670
  1220
SDL_TextureID
slouken@1670
  1221
SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
slouken@1670
  1222
{
slouken@1670
  1223
    SDL_TextureID textureID;
slouken@1670
  1224
    Uint32 surface_flags = surface->flags;
slouken@1670
  1225
    SDL_PixelFormat *fmt = surface->format;
slouken@1670
  1226
    Uint32 colorkey;
slouken@1670
  1227
    Uint8 alpha;
slouken@1670
  1228
    SDL_Rect bounds;
slouken@1670
  1229
    SDL_Surface dst;
slouken@1670
  1230
    int bpp;
slouken@1670
  1231
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
  1232
slouken@1670
  1233
    if (!surface) {
slouken@1670
  1234
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@1670
  1235
        return 0;
slouken@1670
  1236
    }
slouken@1670
  1237
slouken@1670
  1238
    if (format) {
slouken@1670
  1239
        if (!SDL_PixelFormatEnumToMasks
slouken@1670
  1240
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1670
  1241
            SDL_SetError("Unknown pixel format");
slouken@1670
  1242
            return 0;
slouken@1670
  1243
        }
slouken@1670
  1244
    } else {
slouken@1706
  1245
        if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
slouken@1706
  1246
            bpp = fmt->BitsPerPixel;
slouken@1706
  1247
            Rmask = fmt->Rmask;
slouken@1706
  1248
            Gmask = fmt->Gmask;
slouken@1706
  1249
            Bmask = fmt->Bmask;
slouken@1706
  1250
            Amask = fmt->Amask;
slouken@1706
  1251
        } else {
slouken@1706
  1252
            /* Need a format with alpha */
slouken@1706
  1253
            bpp = 32;
slouken@1706
  1254
            Rmask = 0x00FF0000;
slouken@1706
  1255
            Gmask = 0x0000FF00;
slouken@1706
  1256
            Bmask = 0x000000FF;
slouken@1706
  1257
            Amask = 0xFF000000;
slouken@1706
  1258
        }
slouken@1670
  1259
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1260
        if (!format) {
slouken@1670
  1261
            SDL_SetError("Unknown pixel format");
slouken@1670
  1262
            return 0;
slouken@1670
  1263
        }
slouken@1670
  1264
    }
slouken@1670
  1265
slouken@1670
  1266
    textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
slouken@1670
  1267
    if (!textureID) {
slouken@1670
  1268
        return 0;
slouken@1670
  1269
    }
slouken@1670
  1270
slouken@1670
  1271
    /* Set up a destination surface for the texture update */
slouken@1670
  1272
    SDL_zero(dst);
slouken@1670
  1273
    dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1274
    if (!dst.format) {
slouken@1670
  1275
        SDL_DestroyTexture(textureID);
slouken@1670
  1276
        return 0;
slouken@1670
  1277
    }
slouken@1670
  1278
    dst.w = surface->w;
slouken@1670
  1279
    dst.h = surface->h;
slouken@1670
  1280
    if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
slouken@1670
  1281
        dst.flags |= SDL_PREALLOC;
slouken@1670
  1282
    } else {
slouken@1670
  1283
        dst.pitch = SDL_CalculatePitch(&dst);
slouken@1670
  1284
        dst.pixels = SDL_malloc(dst.h * dst.pitch);
slouken@1670
  1285
        if (!dst.pixels) {
slouken@1670
  1286
            SDL_DestroyTexture(textureID);
slouken@1670
  1287
            SDL_FreeFormat(dst.format);
slouken@1670
  1288
            SDL_OutOfMemory();
slouken@1670
  1289
            return 0;
slouken@1670
  1290
        }
slouken@1670
  1291
    }
slouken@1670
  1292
slouken@1670
  1293
    /* Copy the palette if any */
slouken@1706
  1294
    if (fmt->palette) {
slouken@1670
  1295
        SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
slouken@1670
  1296
                              fmt->palette->ncolors);
slouken@1706
  1297
        SDL_SetSurfacePalette(&dst, fmt->palette);
slouken@1670
  1298
    }
slouken@1670
  1299
slouken@1670
  1300
    /* Make the texture transparent if the surface has colorkey */
slouken@1670
  1301
    if (surface_flags & SDL_SRCCOLORKEY) {
slouken@1670
  1302
        int row;
slouken@1670
  1303
        int length = dst.w * dst.format->BytesPerPixel;
slouken@1670
  1304
        Uint8 *p = (Uint8 *) dst.pixels;
slouken@1670
  1305
        for (row = 0; row < dst.h; ++row) {
slouken@1670
  1306
            SDL_memset(p, 0, length);
slouken@1670
  1307
            p += dst.pitch;
slouken@1670
  1308
        }
slouken@1670
  1309
    }
slouken@1670
  1310
slouken@1670
  1311
    /* Copy over the alpha channel */
slouken@1670
  1312
    if (surface_flags & SDL_SRCALPHA) {
slouken@1670
  1313
        if (fmt->Amask) {
slouken@1670
  1314
            surface->flags &= ~SDL_SRCALPHA;
slouken@1670
  1315
        } else {
slouken@1670
  1316
            /* FIXME: Need to make sure the texture has an alpha channel
slouken@1670
  1317
             *        and copy 'alpha' into the texture alpha channel.
slouken@1670
  1318
             */
slouken@1670
  1319
            alpha = surface->format->alpha;
slouken@1670
  1320
            SDL_SetAlpha(surface, 0, 0);
slouken@1670
  1321
        }
slouken@1670
  1322
    }
slouken@1670
  1323
slouken@1670
  1324
    /* Copy over the image data */
slouken@1670
  1325
    bounds.x = 0;
slouken@1670
  1326
    bounds.y = 0;
slouken@1670
  1327
    bounds.w = surface->w;
slouken@1670
  1328
    bounds.h = surface->h;
slouken@1670
  1329
    SDL_LowerBlit(surface, &bounds, &dst, &bounds);
slouken@1670
  1330
slouken@1670
  1331
    /* Clean up the original surface */
slouken@1670
  1332
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1670
  1333
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1670
  1334
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1670
  1335
    }
slouken@1670
  1336
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1670
  1337
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
  1338
        if (fmt->Amask) {
slouken@1670
  1339
            surface->flags |= SDL_SRCALPHA;
slouken@1670
  1340
        } else {
slouken@1670
  1341
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1670
  1342
        }
slouken@1670
  1343
    }
slouken@1670
  1344
slouken@1670
  1345
    /* Update the texture */
slouken@1670
  1346
    if (dst.flags & SDL_PREALLOC) {
slouken@1670
  1347
        SDL_UnlockTexture(textureID);
slouken@1670
  1348
    } else {
slouken@1670
  1349
        SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
slouken@1670
  1350
        SDL_free(dst.pixels);
slouken@1670
  1351
    }
slouken@1670
  1352
    SDL_FreeFormat(dst.format);
slouken@1670
  1353
slouken@1670
  1354
    return textureID;
slouken@1670
  1355
}
slouken@1670
  1356
slouken@1670
  1357
static __inline__ SDL_Texture *
slouken@1670
  1358
SDL_GetTextureFromID(SDL_TextureID textureID)
slouken@1670
  1359
{
slouken@1670
  1360
    int hash;
slouken@1670
  1361
    SDL_Texture *texture;
slouken@1670
  1362
slouken@1670
  1363
    if (!_this) {
slouken@1662
  1364
        return NULL;
slouken@1662
  1365
    }
slouken@1662
  1366
slouken@1670
  1367
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1368
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1369
         texture = texture->next) {
slouken@1670
  1370
        if (texture->id == textureID) {
slouken@1670
  1371
            return texture;
slouken@1670
  1372
        }
slouken@1670
  1373
    }
slouken@1670
  1374
    return NULL;
slouken@1670
  1375
}
slouken@1670
  1376
slouken@1670
  1377
int
slouken@1670
  1378
SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
slouken@1670
  1379
                 int *w, int *h)
slouken@1670
  1380
{
slouken@1670
  1381
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1382
slouken@1670
  1383
    if (!texture) {
slouken@1670
  1384
        return -1;
slouken@1662
  1385
    }
slouken@1662
  1386
slouken@1670
  1387
    if (format) {
slouken@1670
  1388
        *format = texture->format;
slouken@1670
  1389
    }
slouken@1670
  1390
    if (access) {
slouken@1670
  1391
        *access = texture->access;
slouken@1670
  1392
    }
slouken@1670
  1393
    if (w) {
slouken@1670
  1394
        *w = texture->w;
slouken@1670
  1395
    }
slouken@1670
  1396
    if (h) {
slouken@1670
  1397
        *h = texture->h;
slouken@1670
  1398
    }
slouken@1670
  1399
    return 0;
slouken@1670
  1400
}
slouken@1669
  1401
slouken@1670
  1402
int
slouken@1678
  1403
SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
slouken@1678
  1404
{
slouken@1678
  1405
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1678
  1406
    SDL_Renderer *renderer;
slouken@1678
  1407
slouken@1678
  1408
    if (!texture) {
slouken@1678
  1409
        return -1;
slouken@1678
  1410
    }
slouken@1678
  1411
slouken@1678
  1412
    renderer = texture->renderer;
slouken@1678
  1413
    if (!renderer->QueryTexturePixels) {
slouken@1678
  1414
        return -1;
slouken@1678
  1415
    }
slouken@1678
  1416
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1678
  1417
}
slouken@1678
  1418
slouken@1678
  1419
int
slouken@1681
  1420
SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
slouken@1680
  1421
                      int firstcolor, int ncolors)
slouken@1680
  1422
{
slouken@1680
  1423
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1680
  1424
    SDL_Renderer *renderer;
slouken@1680
  1425
slouken@1680
  1426
    if (!texture) {
slouken@1680
  1427
        return -1;
slouken@1680
  1428
    }
slouken@1680
  1429
slouken@1680
  1430
    renderer = texture->renderer;
slouken@1680
  1431
    if (!renderer->SetTexturePalette) {
slouken@1680
  1432
        return -1;
slouken@1680
  1433
    }
slouken@1680
  1434
    return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
slouken@1680
  1435
                                       ncolors);
slouken@1680
  1436
}
slouken@1680
  1437
slouken@1680
  1438
int
slouken@1681
  1439
SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
slouken@1681
  1440
                      int firstcolor, int ncolors)
slouken@1681
  1441
{
slouken@1681
  1442
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1681
  1443
    SDL_Renderer *renderer;
slouken@1681
  1444
slouken@1681
  1445
    if (!texture) {
slouken@1681
  1446
        return -1;
slouken@1681
  1447
    }
slouken@1681
  1448
slouken@1681
  1449
    renderer = texture->renderer;
slouken@1681
  1450
    if (!renderer->GetTexturePalette) {
slouken@1681
  1451
        return -1;
slouken@1681
  1452
    }
slouken@1681
  1453
    return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
slouken@1681
  1454
                                       ncolors);
slouken@1681
  1455
}
slouken@1681
  1456
slouken@1681
  1457
int
slouken@1681
  1458
SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
slouken@1670
  1459
                  const void *pixels, int pitch)
slouken@1670
  1460
{
slouken@1670
  1461
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1462
    SDL_Renderer *renderer;
slouken@1684
  1463
    SDL_Rect full_rect;
slouken@1670
  1464
slouken@1670
  1465
    if (!texture) {
slouken@1670
  1466
        return -1;
slouken@1670
  1467
    }
slouken@1670
  1468
slouken@1670
  1469
    renderer = texture->renderer;
slouken@1670
  1470
    if (!renderer->UpdateTexture) {
slouken@1670
  1471
        return -1;
slouken@1670
  1472
    }
slouken@1684
  1473
slouken@1684
  1474
    if (!rect) {
slouken@1684
  1475
        full_rect.x = 0;
slouken@1684
  1476
        full_rect.y = 0;
slouken@1684
  1477
        full_rect.w = texture->w;
slouken@1684
  1478
        full_rect.h = texture->h;
slouken@1684
  1479
        rect = &full_rect;
slouken@1684
  1480
    }
slouken@1684
  1481
slouken@1676
  1482
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1670
  1483
}
slouken@1670
  1484
slouken@1670
  1485
int
slouken@1681
  1486
SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
slouken@1670
  1487
                void **pixels, int *pitch)
slouken@1670
  1488
{
slouken@1670
  1489
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1490
    SDL_Renderer *renderer;
slouken@1684
  1491
    SDL_Rect full_rect;
slouken@1670
  1492
slouken@1670
  1493
    if (!texture) {
slouken@1670
  1494
        return -1;
slouken@1670
  1495
    }
slouken@1670
  1496
slouken@1670
  1497
    renderer = texture->renderer;
slouken@1670
  1498
    if (!renderer->LockTexture) {
slouken@1670
  1499
        return -1;
slouken@1670
  1500
    }
slouken@1684
  1501
slouken@1684
  1502
    if (!rect) {
slouken@1684
  1503
        full_rect.x = 0;
slouken@1684
  1504
        full_rect.y = 0;
slouken@1684
  1505
        full_rect.w = texture->w;
slouken@1684
  1506
        full_rect.h = texture->h;
slouken@1684
  1507
        rect = &full_rect;
slouken@1684
  1508
    }
slouken@1684
  1509
slouken@1676
  1510
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@1676
  1511
                                 pitch);
slouken@1670
  1512
}
slouken@1670
  1513
slouken@1670
  1514
void
slouken@1670
  1515
SDL_UnlockTexture(SDL_TextureID textureID)
slouken@1670
  1516
{
slouken@1670
  1517
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1518
    SDL_Renderer *renderer;
slouken@1670
  1519
slouken@1670
  1520
    if (!texture) {
slouken@1670
  1521
        return;
slouken@1670
  1522
    }
slouken@1670
  1523
slouken@1670
  1524
    renderer = texture->renderer;
slouken@1670
  1525
    if (!renderer->UnlockTexture) {
slouken@1670
  1526
        return;
slouken@1670
  1527
    }
slouken@1676
  1528
    return renderer->UnlockTexture(renderer, texture);
slouken@1670
  1529
}
slouken@1670
  1530
slouken@1670
  1531
void
slouken@1681
  1532
SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
slouken@1681
  1533
                 const SDL_Rect * rects)
slouken@1670
  1534
{
slouken@1670
  1535
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1536
    SDL_Renderer *renderer;
slouken@1670
  1537
slouken@1670
  1538
    if (!texture) {
slouken@1670
  1539
        return;
slouken@1670
  1540
    }
slouken@1670
  1541
slouken@1670
  1542
    renderer = texture->renderer;
slouken@1670
  1543
    if (!renderer->DirtyTexture) {
slouken@1670
  1544
        return;
slouken@1670
  1545
    }
slouken@1676
  1546
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1670
  1547
}
slouken@1670
  1548
slouken@1670
  1549
void
slouken@1670
  1550
SDL_SelectRenderTexture(SDL_TextureID textureID)
slouken@1670
  1551
{
slouken@1670
  1552
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1553
    SDL_Renderer *renderer;
slouken@1670
  1554
slouken@1670
  1555
    if (!texture || texture->access != SDL_TextureAccess_Render) {
slouken@1670
  1556
        return;
slouken@1670
  1557
    }
slouken@1670
  1558
    renderer = texture->renderer;
slouken@1670
  1559
    if (!renderer->SelectRenderTexture) {
slouken@1670
  1560
        return;
slouken@1670
  1561
    }
slouken@1676
  1562
    renderer->SelectRenderTexture(renderer, texture);
slouken@1670
  1563
}
slouken@1670
  1564
slouken@1670
  1565
int
slouken@1681
  1566
SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
slouken@1670
  1567
{
slouken@1670
  1568
    SDL_Renderer *renderer;
slouken@1684
  1569
    SDL_Rect full_rect;
slouken@1670
  1570
slouken@1670
  1571
    if (!_this) {
slouken@1670
  1572
        return -1;
slouken@1670
  1573
    }
slouken@1670
  1574
slouken@1670
  1575
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1576
    if (!renderer || !renderer->RenderFill) {
slouken@1670
  1577
        return -1;
slouken@1670
  1578
    }
slouken@1670
  1579
slouken@1684
  1580
    if (!rect) {
slouken@1706
  1581
        SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1684
  1582
        full_rect.x = 0;
slouken@1684
  1583
        full_rect.y = 0;
slouken@1706
  1584
        full_rect.w = window->w;
slouken@1706
  1585
        full_rect.h = window->h;
slouken@1684
  1586
        rect = &full_rect;
slouken@1684
  1587
    }
slouken@1684
  1588
slouken@1676
  1589
    renderer->RenderFill(renderer, rect, color);
slouken@1670
  1590
}
slouken@1670
  1591
slouken@1670
  1592
int
slouken@1681
  1593
SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
slouken@1681
  1594
               const SDL_Rect * dstrect, int blendMode, int scaleMode)
slouken@1670
  1595
{
slouken@1670
  1596
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1597
    SDL_Renderer *renderer;
slouken@1684
  1598
    SDL_Rect full_srcrect;
slouken@1684
  1599
    SDL_Rect full_dstrect;
slouken@1670
  1600
slouken@1670
  1601
    if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
slouken@1670
  1602
        return;
slouken@1670
  1603
    }
slouken@1670
  1604
slouken@1670
  1605
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1606
    if (!renderer || !renderer->RenderCopy) {
slouken@1670
  1607
        return -1;
slouken@1670
  1608
    }
slouken@1670
  1609
slouken@1684
  1610
    if (!srcrect) {
slouken@1684
  1611
        full_srcrect.x = 0;
slouken@1684
  1612
        full_srcrect.y = 0;
slouken@1684
  1613
        full_srcrect.w = texture->w;
slouken@1684
  1614
        full_srcrect.h = texture->h;
slouken@1684
  1615
        srcrect = &full_srcrect;
slouken@1684
  1616
    }
slouken@1684
  1617
    if (!dstrect) {
slouken@1706
  1618
        SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1684
  1619
        full_dstrect.x = 0;
slouken@1684
  1620
        full_dstrect.y = 0;
slouken@1706
  1621
        full_dstrect.w = window->w;
slouken@1706
  1622
        full_dstrect.h = window->h;
slouken@1684
  1623
        dstrect = &full_dstrect;
slouken@1684
  1624
    }
slouken@1684
  1625
slouken@1676
  1626
    return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
slouken@1676
  1627
                                blendMode, scaleMode);
slouken@1670
  1628
}
slouken@1670
  1629
slouken@1670
  1630
int
slouken@1681
  1631
SDL_RenderReadPixels(const SDL_Rect * rect, void *pixels, int pitch)
slouken@1670
  1632
{
slouken@1670
  1633
    SDL_Renderer *renderer;
slouken@1684
  1634
    SDL_Rect full_rect;
slouken@1670
  1635
slouken@1670
  1636
    if (!_this) {
slouken@1670
  1637
        return -1;
slouken@1670
  1638
    }
slouken@1670
  1639
slouken@1670
  1640
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1641
    if (!renderer || !renderer->RenderReadPixels) {
slouken@1670
  1642
        return -1;
slouken@1670
  1643
    }
slouken@1670
  1644
slouken@1684
  1645
    if (!rect) {
slouken@1706
  1646
        SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1684
  1647
        full_rect.x = 0;
slouken@1684
  1648
        full_rect.y = 0;
slouken@1706
  1649
        full_rect.w = window->w;
slouken@1706
  1650
        full_rect.h = window->h;
slouken@1684
  1651
        rect = &full_rect;
slouken@1684
  1652
    }
slouken@1684
  1653
slouken@1676
  1654
    return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
slouken@1670
  1655
}
slouken@1670
  1656
slouken@1670
  1657
int
slouken@1681
  1658
SDL_RenderWritePixels(const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1670
  1659
{
slouken@1670
  1660
    SDL_Renderer *renderer;
slouken@1684
  1661
    SDL_Rect full_rect;
slouken@1670
  1662
slouken@1670
  1663
    if (!_this) {
slouken@1670
  1664
        return -1;
slouken@1670
  1665
    }
slouken@1670
  1666
slouken@1670
  1667
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1668
    if (!renderer || !renderer->RenderWritePixels) {
slouken@1670
  1669
        return -1;
slouken@1670
  1670
    }
slouken@1670
  1671
slouken@1684
  1672
    if (!rect) {
slouken@1706
  1673
        SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1684
  1674
        full_rect.x = 0;
slouken@1684
  1675
        full_rect.y = 0;
slouken@1706
  1676
        full_rect.w = window->w;
slouken@1706
  1677
        full_rect.h = window->h;
slouken@1684
  1678
        rect = &full_rect;
slouken@1684
  1679
    }
slouken@1684
  1680
slouken@1676
  1681
    return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
slouken@1670
  1682
}
slouken@1670
  1683
slouken@1670
  1684
void
slouken@1670
  1685
SDL_RenderPresent(void)
slouken@1670
  1686
{
slouken@1670
  1687
    SDL_Renderer *renderer;
slouken@1670
  1688
slouken@1670
  1689
    if (!_this) {
slouken@1670
  1690
        return;
slouken@1670
  1691
    }
slouken@1670
  1692
slouken@1670
  1693
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1694
    if (!renderer || !renderer->RenderPresent) {
slouken@1670
  1695
        return;
slouken@1670
  1696
    }
slouken@1670
  1697
slouken@1676
  1698
    renderer->RenderPresent(renderer);
slouken@1670
  1699
}
slouken@1670
  1700
slouken@1670
  1701
void
slouken@1670
  1702
SDL_DestroyTexture(SDL_TextureID textureID)
slouken@1670
  1703
{
slouken@1670
  1704
    int hash;
slouken@1670
  1705
    SDL_Texture *prev, *texture;
slouken@1670
  1706
    SDL_Renderer *renderer;
slouken@1670
  1707
slouken@1670
  1708
    if (!_this) {
slouken@1670
  1709
        return;
slouken@1670
  1710
    }
slouken@1670
  1711
slouken@1670
  1712
    /* Look up the texture in the hash table */
slouken@1670
  1713
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1714
    prev = NULL;
slouken@1670
  1715
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1716
         prev = texture, texture = texture->next) {
slouken@1670
  1717
        if (texture->id == textureID) {
slouken@1670
  1718
            break;
slouken@1669
  1719
        }
slouken@1662
  1720
    }
slouken@1670
  1721
    if (!texture) {
slouken@1670
  1722
        return;
slouken@1662
  1723
    }
slouken@1662
  1724
slouken@1670
  1725
    /* Unlink the texture from the list */
slouken@1670
  1726
    if (prev) {
slouken@1670
  1727
        prev->next = texture->next;
slouken@1670
  1728
    } else {
slouken@1670
  1729
        SDL_CurrentDisplay.textures[hash] = texture->next;
slouken@1670
  1730
    }
slouken@1662
  1731
slouken@1670
  1732
    /* Free the texture */
slouken@1670
  1733
    renderer = texture->renderer;
slouken@1676
  1734
    renderer->DestroyTexture(renderer, texture);
slouken@1670
  1735
    SDL_free(texture);
slouken@1670
  1736
}
slouken@1669
  1737
slouken@1670
  1738
void
slouken@1670
  1739
SDL_DestroyRenderer(SDL_WindowID windowID)
slouken@1670
  1740
{
slouken@1670
  1741
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1742
    SDL_Renderer *renderer;
slouken@1670
  1743
    int i;
slouken@1662
  1744
slouken@1670
  1745
    if (!window) {
slouken@1670
  1746
        return;
slouken@1670
  1747
    }
slouken@1670
  1748
slouken@1670
  1749
    renderer = window->renderer;
slouken@1670
  1750
    if (!renderer) {
slouken@1670
  1751
        return;
slouken@1670
  1752
    }
slouken@1670
  1753
slouken@1670
  1754
    /* Free existing textures for this renderer */
slouken@1670
  1755
    for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
slouken@1670
  1756
        SDL_Texture *texture;
slouken@1670
  1757
        SDL_Texture *prev = NULL;
slouken@1670
  1758
        SDL_Texture *next;
slouken@1670
  1759
        for (texture = SDL_CurrentDisplay.textures[i]; texture;
slouken@1670
  1760
             texture = next) {
slouken@1670
  1761
            next = texture->next;
slouken@1670
  1762
            if (texture->renderer == renderer) {
slouken@1670
  1763
                if (prev) {
slouken@1670
  1764
                    prev->next = next;
slouken@1670
  1765
                } else {
slouken@1670
  1766
                    SDL_CurrentDisplay.textures[i] = next;
slouken@1670
  1767
                }
slouken@1676
  1768
                renderer->DestroyTexture(renderer, texture);
slouken@1670
  1769
                SDL_free(texture);
slouken@1662
  1770
            } else {
slouken@1670
  1771
                prev = texture;
slouken@1662
  1772
            }
slouken@1662
  1773
        }
slouken@1662
  1774
    }
slouken@1662
  1775
slouken@1670
  1776
    /* Free the renderer instance */
slouken@1670
  1777
    renderer->DestroyRenderer(renderer);
slouken@1670
  1778
slouken@1670
  1779
    /* Clear references */
slouken@1670
  1780
    window->renderer = NULL;
slouken@1670
  1781
    if (SDL_CurrentDisplay.current_renderer == renderer) {
slouken@1670
  1782
        SDL_CurrentDisplay.current_renderer = NULL;
slouken@1662
  1783
    }
slouken@0
  1784
}
slouken@0
  1785
slouken@1662
  1786
void
slouken@1668
  1787
SDL_VideoQuit(void)
slouken@0
  1788
{
slouken@1662
  1789
    int i, j;
slouken@0
  1790
slouken@1662
  1791
    if (!_this) {
slouken@1662
  1792
        return;
slouken@1662
  1793
    }
slouken@0
  1794
slouken@1662
  1795
    /* Halt event processing before doing anything else */
slouken@1668
  1796
    SDL_StopEventLoop();
slouken@0
  1797
slouken@1662
  1798
    /* Clean up the system video */
slouken@1662
  1799
    for (i = _this->num_displays; i--;) {
slouken@1662
  1800
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1801
        for (j = display->num_windows; j--;) {
slouken@1668
  1802
            SDL_DestroyWindow(display->windows[i].id);
slouken@1662
  1803
        }
slouken@1662
  1804
        if (display->windows) {
slouken@1668
  1805
            SDL_free(display->windows);
slouken@1662
  1806
            display->windows = NULL;
slouken@1662
  1807
        }
slouken@1683
  1808
        if (display->palette) {
slouken@1683
  1809
            SDL_FreePalette(display->palette);
slouken@1683
  1810
            display->palette = NULL;
slouken@1681
  1811
        }
slouken@1662
  1812
    }
slouken@1668
  1813
    _this->VideoQuit(_this);
slouken@1662
  1814
    if (_this->displays) {
slouken@1668
  1815
        SDL_free(_this->displays);
slouken@1662
  1816
    }
slouken@1668
  1817
    _this->free(_this);
slouken@1662
  1818
    _this = NULL;
slouken@0
  1819
}
slouken@0
  1820
slouken@0
  1821
/* Load the GL driver library */
slouken@1662
  1822
int
slouken@1668
  1823
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1824
{
slouken@1662
  1825
    int retval;
slouken@0
  1826
slouken@1662
  1827
    retval = -1;
slouken@1662
  1828
    if (_this == NULL) {
slouken@1668
  1829
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
  1830
    } else {
slouken@1662
  1831
        if (_this->GL_LoadLibrary) {
slouken@1668
  1832
            retval = _this->GL_LoadLibrary(_this, path);
slouken@1662
  1833
        } else {
slouken@1668
  1834
            SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1835
        }
slouken@1662
  1836
    }
slouken@1662
  1837
    return (retval);
slouken@0
  1838
}
slouken@0
  1839
slouken@1662
  1840
void *
slouken@1668
  1841
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1842
{
slouken@1662
  1843
    void *func;
slouken@0
  1844
slouken@1662
  1845
    func = NULL;
slouken@1662
  1846
    if (_this->GL_GetProcAddress) {
slouken@1662
  1847
        if (_this->gl_config.driver_loaded) {
slouken@1668
  1848
            func = _this->GL_GetProcAddress(_this, proc);
slouken@1662
  1849
        } else {
slouken@1668
  1850
            SDL_SetError("No GL driver has been loaded");
slouken@1662
  1851
        }
slouken@1662
  1852
    } else {
slouken@1668
  1853
        SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1854
    }
slouken@1662
  1855
    return func;
slouken@0
  1856
}
slouken@0
  1857
slouken@0
  1858
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1859
int
slouken@1668
  1860
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  1861
{
slouken@1662
  1862
    int retval;
slouken@0
  1863
slouken@1662
  1864
    retval = 0;
slouken@1662
  1865
    switch (attr) {
slouken@1662
  1866
    case SDL_GL_RED_SIZE:
slouken@1662
  1867
        _this->gl_config.red_size = value;
slouken@1662
  1868
        break;
slouken@1662
  1869
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1870
        _this->gl_config.green_size = value;
slouken@1662
  1871
        break;
slouken@1662
  1872
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1873
        _this->gl_config.blue_size = value;
slouken@1662
  1874
        break;
slouken@1662
  1875
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1876
        _this->gl_config.alpha_size = value;
slouken@1662
  1877
        break;
slouken@1662
  1878
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1879
        _this->gl_config.double_buffer = value;
slouken@1662
  1880
        break;
slouken@1662
  1881
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1882
        _this->gl_config.buffer_size = value;
slouken@1662
  1883
        break;
slouken@1662
  1884
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1885
        _this->gl_config.depth_size = value;
slouken@1662
  1886
        break;
slouken@1662
  1887
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1888
        _this->gl_config.stencil_size = value;
slouken@1662
  1889
        break;
slouken@1662
  1890
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1891
        _this->gl_config.accum_red_size = value;
slouken@1662
  1892
        break;
slouken@1662
  1893
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1894
        _this->gl_config.accum_green_size = value;
slouken@1662
  1895
        break;
slouken@1662
  1896
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1897
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1898
        break;
slouken@1662
  1899
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1900
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1901
        break;
slouken@1662
  1902
    case SDL_GL_STEREO:
slouken@1662
  1903
        _this->gl_config.stereo = value;
slouken@1662
  1904
        break;
slouken@1662
  1905
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1906
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1907
        break;
slouken@1662
  1908
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1909
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1910
        break;
slouken@1662
  1911
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1912
        _this->gl_config.accelerated = value;
slouken@1662
  1913
        break;
slouken@1662
  1914
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1915
        _this->gl_config.swap_control = value;
slouken@1662
  1916
        break;
slouken@1662
  1917
    default:
slouken@1668
  1918
        SDL_SetError("Unknown OpenGL attribute");
slouken@1662
  1919
        retval = -1;
slouken@1662
  1920
        break;
slouken@1662
  1921
    }
slouken@1662
  1922
    return (retval);
slouken@0
  1923
}
slouken@0
  1924
slouken@0
  1925
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1926
int
slouken@1668
  1927
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  1928
{
slouken@1662
  1929
    int retval = -1;
slouken@0
  1930
slouken@1662
  1931
    if (_this->GL_GetAttribute) {
slouken@1668
  1932
        retval = _this->GL_GetAttribute(_this, attr, value);
slouken@1662
  1933
    } else {
slouken@1662
  1934
        *value = 0;
slouken@1668
  1935
        SDL_SetError("GL_GetAttribute not supported");
slouken@1662
  1936
    }
slouken@1662
  1937
    return retval;
slouken@0
  1938
}
slouken@0
  1939
slouken@0
  1940
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1941
void
slouken@1668
  1942
SDL_GL_SwapBuffers(void)
slouken@0
  1943
{
slouken@1670
  1944
    // 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
  1945
    _this->GL_SwapBuffers(_this);
slouken@0
  1946
}
slouken@0
  1947
slouken@1662
  1948
#if 0                           // FIXME
slouken@541
  1949
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1950
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1951
static void
slouken@1668
  1952
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1953
{
slouken@1662
  1954
    int x, y;
slouken@1662
  1955
    Uint32 colorkey;
slouken@0
  1956
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1957
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1958
slouken@1662
  1959
    colorkey = icon->format->colorkey;
slouken@1662
  1960
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1961
    case 1:
slouken@1662
  1962
        {
slouken@1662
  1963
            Uint8 *pixels;
slouken@1662
  1964
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1965
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1966
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1967
                    if (*pixels++ == colorkey) {
slouken@1668
  1968
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1969
                    }
slouken@1662
  1970
                }
slouken@1662
  1971
            }
slouken@1662
  1972
        }
slouken@1662
  1973
        break;
slouken@0
  1974
slouken@1662
  1975
    case 2:
slouken@1662
  1976
        {
slouken@1662
  1977
            Uint16 *pixels;
slouken@1662
  1978
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1979
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1980
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1981
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1982
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1983
                    } else if ((flags & 2)
slouken@1662
  1984
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1985
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1986
                    }
slouken@1662
  1987
                    pixels++;
slouken@1662
  1988
                }
slouken@1662
  1989
            }
slouken@1662
  1990
        }
slouken@1662
  1991
        break;
slouken@0
  1992
slouken@1662
  1993
    case 4:
slouken@1662
  1994
        {
slouken@1662
  1995
            Uint32 *pixels;
slouken@1662
  1996
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1997
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1998
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1999
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  2000
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  2001
                    } else if ((flags & 2)
slouken@1662
  2002
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  2003
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  2004
                    }
slouken@1662
  2005
                    pixels++;
slouken@1662
  2006
                }
slouken@1662
  2007
            }
slouken@1662
  2008
        }
slouken@1662
  2009
        break;
slouken@1662
  2010
    }
slouken@0
  2011
}
slouken@0
  2012
slouken@0
  2013
/*
slouken@0
  2014
 * Sets the window manager icon for the display window.
slouken@0
  2015
 */
slouken@1662
  2016
void
slouken@1668
  2017
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  2018
{
slouken@1662
  2019
    if (icon && _this->SetIcon) {
slouken@1662
  2020
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  2021
        if (mask == NULL) {
slouken@1662
  2022
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  2023
            int flags = 0;
slouken@1668
  2024
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@1662
  2025
            if (mask == NULL) {
slouken@1662
  2026
                return;
slouken@1662
  2027
            }
slouken@1668
  2028
            SDL_memset(mask, ~0, mask_len);
slouken@1662
  2029
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  2030
                flags |= 1;
slouken@1662
  2031
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  2032
                flags |= 2;
slouken@1662
  2033
            if (flags) {
slouken@1668
  2034
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@1662
  2035
            }
slouken@1668
  2036
            _this->SetIcon(_this, icon, mask);
slouken@1668
  2037
            SDL_free(mask);
slouken@1662
  2038
        } else {
slouken@1668
  2039
            _this->SetIcon(_this, icon, mask);
slouken@1662
  2040
        }
slouken@1662
  2041
    }
slouken@1662
  2042
}
slouken@1662
  2043
#endif
slouken@0
  2044
slouken@1662
  2045
SDL_bool
slouken@1668
  2046
SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  2047
{
slouken@1668
  2048
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  2049
slouken@1662
  2050
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  2051
        return SDL_FALSE;
slouken@1662
  2052
    }
slouken@1668
  2053
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  2054
}
slouken@0
  2055
slouken@1662
  2056
/* vi: set ts=4 sw=4 expandtab: */