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