src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 02 Jan 2009 17:39:48 +0000
changeset 2967 e4a469d6ddab
parent 2934 1fcc36adc73d
child 2984 0b160c970b7e
permissions -rw-r--r--
Implemented SDL_SetWindowIcon(), with translucent icon support under X11.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1918
    30
#include "SDL_renderer_gl.h"
hfutrell@2745
    31
#include "SDL_renderer_gles.h"
slouken@1895
    32
#include "SDL_renderer_sw.h"
slouken@1361
    33
#include "../events/SDL_sysevents.h"
slouken@1361
    34
#include "../events/SDL_events_c.h"
slouken@0
    35
hfutrell@2745
    36
#if SDL_VIDEO_OPENGL_ES
hfutrell@2745
    37
#include "SDL_opengles.h"
slouken@2753
    38
#endif /* SDL_VIDEO_OPENGL_ES */
hfutrell@2745
    39
slouken@1926
    40
#if SDL_VIDEO_OPENGL
slouken@1926
    41
#include "SDL_opengl.h"
slouken@1926
    42
slouken@1926
    43
/* On Windows, windows.h defines CreateWindow */
slouken@1926
    44
#ifdef CreateWindow
slouken@1926
    45
#undef CreateWindow
slouken@1926
    46
#endif
slouken@2753
    47
#endif /* SDL_VIDEO_OPENGL */
slouken@1926
    48
slouken@0
    49
/* Available video drivers */
slouken@0
    50
static VideoBootStrap *bootstrap[] = {
slouken@1931
    51
#if SDL_VIDEO_DRIVER_COCOA
slouken@2753
    52
    &COCOA_bootstrap,
icculus@1133
    53
#endif
slouken@1361
    54
#if SDL_VIDEO_DRIVER_X11
slouken@2753
    55
    &X11_bootstrap,
slouken@0
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_NANOX
slouken@2753
    58
    &NX_bootstrap,
slouken@30
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_IPOD
slouken@2753
    61
    &iPod_bootstrap,
icculus@1140
    62
#endif
slouken@1361
    63
#if SDL_VIDEO_DRIVER_WSCONS
slouken@2753
    64
    &WSCONS_bootstrap,
icculus@1187
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_FBCON
slouken@2753
    67
    &FBCON_bootstrap,
slouken@0
    68
#endif
slouken@1361
    69
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@2753
    70
    &DirectFB_bootstrap,
slouken@167
    71
#endif
slouken@1361
    72
#if SDL_VIDEO_DRIVER_PS2GS
slouken@2753
    73
    &PS2GS_bootstrap,
slouken@0
    74
#endif
slouken@1361
    75
#if SDL_VIDEO_DRIVER_VGL
slouken@2753
    76
    &VGL_bootstrap,
slouken@75
    77
#endif
slouken@1361
    78
#if SDL_VIDEO_DRIVER_SVGALIB
slouken@2753
    79
    &SVGALIB_bootstrap,
slouken@0
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_GAPI
slouken@2753
    82
    &GAPI_bootstrap,
slouken@1361
    83
#endif
slouken@1895
    84
#if SDL_VIDEO_DRIVER_WIN32
slouken@2753
    85
    &WIN32_bootstrap,
slouken@1361
    86
#endif
slouken@1361
    87
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@2753
    88
    &BWINDOW_bootstrap,
slouken@1361
    89
#endif
slouken@1361
    90
#if SDL_VIDEO_DRIVER_PHOTON
slouken@2753
    91
    &ph_bootstrap,
slouken@1361
    92
#endif
slouken@1361
    93
#if SDL_VIDEO_DRIVER_EPOC
slouken@2753
    94
    &EPOC_bootstrap,
slouken@1361
    95
#endif
slouken@1361
    96
#if SDL_VIDEO_DRIVER_XBIOS
slouken@2753
    97
    &XBIOS_bootstrap,
slouken@1361
    98
#endif
slouken@1361
    99
#if SDL_VIDEO_DRIVER_GEM
slouken@2753
   100
    &GEM_bootstrap,
slouken@1361
   101
#endif
slouken@1361
   102
#if SDL_VIDEO_DRIVER_DC
slouken@2753
   103
    &DC_bootstrap,
slouken@1361
   104
#endif
slouken@1361
   105
#if SDL_VIDEO_DRIVER_RISCOS
slouken@2753
   106
    &RISCOS_bootstrap,
slouken@1361
   107
#endif
slouken@1361
   108
#if SDL_VIDEO_DRIVER_OS2FS
slouken@2753
   109
    &OS2FSLib_bootstrap,
slouken@1361
   110
#endif
hfutrell@2743
   111
#if SDL_VIDEO_DRIVER_NDS
slouken@2753
   112
    &NDS_bootstrap,
slouken@2735
   113
#endif
hfutrell@2745
   114
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2753
   115
    &UIKIT_bootstrap,
hfutrell@2745
   116
#endif
slouken@1361
   117
#if SDL_VIDEO_DRIVER_DUMMY
slouken@2753
   118
    &DUMMY_bootstrap,
slouken@610
   119
#endif
slouken@2753
   120
    NULL
slouken@0
   121
};
slouken@173
   122
slouken@1895
   123
static SDL_VideoDevice *_this = NULL;
slouken@0
   124
slouken@0
   125
/* Various local functions */
slouken@2876
   126
static void SDL_UpdateWindowGrab(SDL_Window * window);
slouken@0
   127
slouken@1895
   128
static int
slouken@1895
   129
cmpmodes(const void *A, const void *B)
slouken@1895
   130
{
slouken@2753
   131
    SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
slouken@2753
   132
    SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   133
slouken@2753
   134
    if (a.w != b.w) {
slouken@2753
   135
        return b.w - a.w;
slouken@2753
   136
    }
slouken@2753
   137
    if (a.h != b.h) {
slouken@2753
   138
        return b.h - a.h;
slouken@2753
   139
    }
slouken@2753
   140
    if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
slouken@2753
   141
        return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
slouken@2753
   142
    }
slouken@2753
   143
    if (a.refresh_rate != b.refresh_rate) {
slouken@2753
   144
        return b.refresh_rate - a.refresh_rate;
slouken@2753
   145
    }
slouken@2753
   146
    return 0;
slouken@1895
   147
}
slouken@1895
   148
slouken@1912
   149
static void
slouken@1912
   150
SDL_UninitializedVideo()
slouken@1912
   151
{
slouken@2753
   152
    SDL_SetError("Video subsystem has not been initialized");
slouken@1912
   153
}
slouken@1912
   154
slouken@1895
   155
int
slouken@1895
   156
SDL_GetNumVideoDrivers(void)
slouken@1895
   157
{
slouken@2753
   158
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   159
}
slouken@1895
   160
slouken@2753
   161
const char *
slouken@1895
   162
SDL_GetVideoDriver(int index)
slouken@1895
   163
{
slouken@2753
   164
    if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
slouken@2753
   165
        return bootstrap[index]->name;
slouken@2753
   166
    }
slouken@2753
   167
    return NULL;
slouken@1895
   168
}
slouken@0
   169
slouken@0
   170
/*
slouken@0
   171
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   172
 */
slouken@1895
   173
int
slouken@1895
   174
SDL_VideoInit(const char *driver_name, Uint32 flags)
slouken@0
   175
{
slouken@2753
   176
    SDL_VideoDevice *video;
slouken@2753
   177
    int index;
slouken@2753
   178
    int i;
slouken@0
   179
slouken@2753
   180
    /* Toggle the event thread flags, based on OS requirements */
slouken@0
   181
#if defined(MUST_THREAD_EVENTS)
slouken@2753
   182
    flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   183
#elif defined(CANT_THREAD_EVENTS)
slouken@2753
   184
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@2753
   185
        SDL_SetError("OS doesn't support threaded events");
slouken@2753
   186
        return -1;
slouken@2753
   187
    }
slouken@0
   188
#endif
slouken@0
   189
slouken@2753
   190
    /* Start the event loop */
slouken@2753
   191
    if (SDL_StartEventLoop(flags) < 0) {
slouken@2753
   192
        return -1;
slouken@2753
   193
    }
slouken@2753
   194
    /* Check to make sure we don't overwrite '_this' */
slouken@2753
   195
    if (_this != NULL) {
slouken@2753
   196
        SDL_VideoQuit();
slouken@2753
   197
    }
slouken@2753
   198
    /* Select the proper video driver */
slouken@2753
   199
    index = 0;
slouken@2753
   200
    video = NULL;
slouken@2753
   201
    if (driver_name == NULL) {
slouken@2753
   202
        driver_name = SDL_getenv("SDL_VIDEODRIVER");
slouken@2753
   203
    }
slouken@2753
   204
    if (driver_name != NULL) {
slouken@2753
   205
        for (i = 0; bootstrap[i]; ++i) {
slouken@2753
   206
            if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
slouken@2753
   207
                if (bootstrap[i]->available()) {
slouken@2753
   208
                    video = bootstrap[i]->create(index);
slouken@2753
   209
                }
slouken@2753
   210
                break;
slouken@2753
   211
            }
slouken@2753
   212
        }
slouken@2753
   213
    } else {
slouken@2753
   214
        for (i = 0; bootstrap[i]; ++i) {
slouken@2753
   215
            if (bootstrap[i]->available()) {
slouken@2753
   216
                video = bootstrap[i]->create(index);
slouken@2753
   217
                if (video != NULL) {
slouken@2753
   218
                    break;
slouken@2753
   219
                }
slouken@2753
   220
            }
slouken@2753
   221
        }
slouken@2753
   222
    }
slouken@2753
   223
    if (video == NULL) {
slouken@2753
   224
        if (driver_name) {
slouken@2753
   225
            SDL_SetError("%s not available", driver_name);
slouken@2753
   226
        } else {
slouken@2753
   227
            SDL_SetError("No available video device");
slouken@2753
   228
        }
slouken@2753
   229
        return -1;
slouken@2753
   230
    }
slouken@2753
   231
    _this = video;
slouken@2753
   232
    _this->name = bootstrap[i]->name;
slouken@2753
   233
    _this->next_object_id = 1;
slouken@0
   234
slouken@0
   235
slouken@2753
   236
    /* Set some very sane GL defaults */
slouken@2753
   237
    _this->gl_config.driver_loaded = 0;
slouken@2753
   238
    _this->gl_config.dll_handle = NULL;
slouken@2753
   239
    _this->gl_config.red_size = 3;
slouken@2753
   240
    _this->gl_config.green_size = 3;
slouken@2753
   241
    _this->gl_config.blue_size = 2;
slouken@2753
   242
    _this->gl_config.alpha_size = 0;
slouken@2753
   243
    _this->gl_config.buffer_size = 0;
slouken@2753
   244
    _this->gl_config.depth_size = 16;
slouken@2753
   245
    _this->gl_config.stencil_size = 0;
slouken@2753
   246
    _this->gl_config.double_buffer = 1;
slouken@2753
   247
    _this->gl_config.accum_red_size = 0;
slouken@2753
   248
    _this->gl_config.accum_green_size = 0;
slouken@2753
   249
    _this->gl_config.accum_blue_size = 0;
slouken@2753
   250
    _this->gl_config.accum_alpha_size = 0;
slouken@2753
   251
    _this->gl_config.stereo = 0;
slouken@2753
   252
    _this->gl_config.multisamplebuffers = 0;
slouken@2753
   253
    _this->gl_config.multisamplesamples = 0;
slouken@2753
   254
    _this->gl_config.retained_backing = 1;
slouken@2753
   255
    _this->gl_config.accelerated = -1;  /* not known, don't set */
slouken@0
   256
slouken@2753
   257
    /* Initialize the video subsystem */
slouken@2753
   258
    if (_this->VideoInit(_this) < 0) {
slouken@2753
   259
        SDL_VideoQuit();
slouken@2753
   260
        return -1;
slouken@2753
   261
    }
slouken@2753
   262
    /* Make sure some displays were added */
slouken@2753
   263
    if (_this->num_displays == 0) {
slouken@2753
   264
        SDL_SetError("The video driver did not add any displays");
slouken@2753
   265
        SDL_VideoQuit();
slouken@2753
   266
        return (-1);
slouken@2753
   267
    }
slouken@2753
   268
    /* The software renderer is always available */
slouken@2753
   269
    for (i = 0; i < _this->num_displays; ++i) {
hfutrell@2744
   270
#if SDL_VIDEO_RENDER_OGL
slouken@2753
   271
        SDL_AddRenderDriver(i, &GL_RenderDriver);
hfutrell@2744
   272
#endif
hfutrell@2745
   273
hfutrell@2745
   274
#if SDL_VIDEO_RENDER_OGL_ES
slouken@2753
   275
        SDL_AddRenderDriver(i, &GL_ES_RenderDriver);
hfutrell@2745
   276
#endif
slouken@2753
   277
        if (_this->displays[i].num_render_drivers > 0) {
slouken@2753
   278
            SDL_AddRenderDriver(i, &SW_RenderDriver);
slouken@2753
   279
        }
slouken@2753
   280
    }
slouken@0
   281
slouken@2753
   282
    /* We're ready to go! */
slouken@2753
   283
    return 0;
slouken@0
   284
}
slouken@0
   285
slouken@2753
   286
const char *
slouken@1895
   287
SDL_GetCurrentVideoDriver()
slouken@0
   288
{
slouken@2753
   289
    if (!_this) {
slouken@2753
   290
        SDL_UninitializedVideo();
slouken@2753
   291
        return NULL;
slouken@2753
   292
    }
slouken@2753
   293
    return _this->name;
slouken@0
   294
}
slouken@0
   295
slouken@1895
   296
SDL_VideoDevice *
slouken@1895
   297
SDL_GetVideoDevice()
slouken@0
   298
{
slouken@2753
   299
    return _this;
slouken@0
   300
}
slouken@0
   301
slouken@1895
   302
int
slouken@1895
   303
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   304
{
slouken@2753
   305
    SDL_VideoDisplay display;
slouken@0
   306
slouken@2753
   307
    SDL_zero(display);
slouken@2753
   308
    if (desktop_mode) {
slouken@2753
   309
        display.desktop_mode = *desktop_mode;
slouken@2753
   310
    }
slouken@2753
   311
    display.current_mode = display.desktop_mode;
slouken@1895
   312
slouken@2753
   313
    return SDL_AddVideoDisplay(&display);
slouken@0
   314
}
slouken@0
   315
slouken@1895
   316
int
slouken@1895
   317
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@0
   318
{
slouken@2753
   319
    SDL_VideoDisplay *displays;
slouken@2753
   320
    int index = -1;
slouken@0
   321
slouken@2753
   322
    displays =
slouken@2753
   323
        SDL_realloc(_this->displays,
slouken@2753
   324
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@2753
   325
    if (displays) {
slouken@2753
   326
        index = _this->num_displays++;
slouken@2753
   327
        displays[index] = *display;
slouken@2753
   328
        displays[index].device = _this;
slouken@2753
   329
        _this->displays = displays;
slouken@2753
   330
    } else {
slouken@2753
   331
        SDL_OutOfMemory();
slouken@2753
   332
    }
slouken@2753
   333
    return index;
slouken@0
   334
}
slouken@0
   335
slouken@1895
   336
int
slouken@1895
   337
SDL_GetNumVideoDisplays(void)
slouken@0
   338
{
slouken@2753
   339
    if (!_this) {
slouken@2753
   340
        SDL_UninitializedVideo();
slouken@2753
   341
        return 0;
slouken@2753
   342
    }
slouken@2753
   343
    return _this->num_displays;
slouken@0
   344
}
slouken@0
   345
slouken@1895
   346
int
slouken@1895
   347
SDL_SelectVideoDisplay(int index)
slouken@0
   348
{
slouken@2753
   349
    if (!_this) {
slouken@2753
   350
        SDL_UninitializedVideo();
slouken@2753
   351
        return (-1);
slouken@2753
   352
    }
slouken@2753
   353
    if (index < 0 || index >= _this->num_displays) {
slouken@2753
   354
        SDL_SetError("index must be in the range 0 - %d",
slouken@2753
   355
                     _this->num_displays - 1);
slouken@2753
   356
        return -1;
slouken@2753
   357
    }
slouken@2753
   358
    _this->current_display = index;
slouken@2753
   359
    return 0;
slouken@1963
   360
}
slouken@1963
   361
slouken@1963
   362
int
slouken@1963
   363
SDL_GetCurrentVideoDisplay(void)
slouken@1963
   364
{
slouken@2753
   365
    if (!_this) {
slouken@2753
   366
        SDL_UninitializedVideo();
slouken@2753
   367
        return (-1);
slouken@2753
   368
    }
slouken@2753
   369
    return _this->current_display;
slouken@0
   370
}
slouken@0
   371
slouken@1895
   372
SDL_bool
slouken@1895
   373
SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
slouken@0
   374
{
slouken@2753
   375
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@2753
   376
    SDL_DisplayMode *modes;
slouken@2753
   377
    int i, nmodes;
slouken@0
   378
slouken@2753
   379
    /* Make sure we don't already have the mode in the list */
slouken@2753
   380
    modes = display->display_modes;
slouken@2753
   381
    nmodes = display->num_display_modes;
slouken@2753
   382
    for (i = nmodes; i--;) {
slouken@2753
   383
        if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
slouken@2753
   384
            return SDL_FALSE;
slouken@2753
   385
        }
slouken@2753
   386
    }
slouken@1895
   387
slouken@2753
   388
    /* Go ahead and add the new mode */
slouken@2753
   389
    if (nmodes == display->max_display_modes) {
slouken@2753
   390
        modes =
slouken@2753
   391
            SDL_realloc(modes,
slouken@2753
   392
                        (display->max_display_modes + 32) * sizeof(*modes));
slouken@2753
   393
        if (!modes) {
slouken@2753
   394
            return SDL_FALSE;
slouken@2753
   395
        }
slouken@2753
   396
        display->display_modes = modes;
slouken@2753
   397
        display->max_display_modes += 32;
slouken@2753
   398
    }
slouken@2753
   399
    modes[nmodes] = *mode;
slouken@2753
   400
    display->num_display_modes++;
slouken@1895
   401
slouken@2753
   402
    return SDL_TRUE;
slouken@0
   403
}
slouken@0
   404
slouken@1895
   405
int
slouken@1895
   406
SDL_GetNumDisplayModes()
slouken@0
   407
{
slouken@2753
   408
    if (_this) {
slouken@2753
   409
        SDL_VideoDisplay *display = &SDL_CurrentDisplay;
slouken@2753
   410
        if (!display->num_display_modes && _this->GetDisplayModes) {
slouken@2753
   411
            _this->GetDisplayModes(_this);
slouken@2753
   412
            SDL_qsort(display->display_modes, display->num_display_modes,
slouken@2753
   413
                      sizeof(SDL_DisplayMode), cmpmodes);
slouken@2753
   414
        }
slouken@2753
   415
        return display->num_display_modes;
slouken@2753
   416
    }
slouken@2753
   417
    return 0;
slouken@0
   418
}
slouken@0
   419
slouken@1967
   420
int
slouken@1967
   421
SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
slouken@0
   422
{
slouken@2753
   423
    if (index < 0 || index >= SDL_GetNumDisplayModes()) {
slouken@2753
   424
        SDL_SetError("index must be in the range of 0 - %d",
slouken@2753
   425
                     SDL_GetNumDisplayModes() - 1);
slouken@2753
   426
        return -1;
slouken@2753
   427
    }
slouken@2753
   428
    if (mode) {
slouken@2753
   429
        *mode = SDL_CurrentDisplay.display_modes[index];
slouken@2753
   430
    }
slouken@2753
   431
    return 0;
slouken@0
   432
}
slouken@0
   433
slouken@1967
   434
int
slouken@1967
   435
SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
slouken@0
   436
{
slouken@2753
   437
    if (!_this) {
slouken@2753
   438
        SDL_UninitializedVideo();
slouken@2753
   439
        return -1;
slouken@2753
   440
    }
slouken@2753
   441
    if (mode) {
slouken@2753
   442
        *mode = SDL_CurrentDisplay.desktop_mode;
slouken@2753
   443
    }
slouken@2753
   444
    return 0;
slouken@0
   445
}
slouken@0
   446
slouken@1967
   447
int
slouken@1967
   448
SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
slouken@0
   449
{
slouken@2753
   450
    if (!_this) {
slouken@2753
   451
        SDL_UninitializedVideo();
slouken@2753
   452
        return -1;
slouken@2753
   453
    }
slouken@2753
   454
    if (mode) {
slouken@2753
   455
        *mode = SDL_CurrentDisplay.current_mode;
slouken@2753
   456
    }
slouken@2753
   457
    return 0;
slouken@0
   458
}
slouken@0
   459
slouken@1895
   460
SDL_DisplayMode *
slouken@1895
   461
SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
slouken@2753
   462
                          SDL_DisplayMode * closest)
slouken@0
   463
{
slouken@2753
   464
    Uint32 target_format;
slouken@2753
   465
    int target_refresh_rate;
slouken@2753
   466
    int i;
slouken@2753
   467
    SDL_DisplayMode *current, *match;
slouken@0
   468
slouken@2753
   469
    if (!_this || !mode || !closest) {
slouken@2753
   470
        return NULL;
slouken@2753
   471
    }
slouken@2753
   472
    /* Default to the desktop format */
slouken@2753
   473
    if (mode->format) {
slouken@2753
   474
        target_format = mode->format;
slouken@2753
   475
    } else {
slouken@2753
   476
        target_format = SDL_CurrentDisplay.desktop_mode.format;
slouken@2753
   477
    }
slouken@0
   478
slouken@2753
   479
    /* Default to the desktop refresh rate */
slouken@2753
   480
    if (mode->refresh_rate) {
slouken@2753
   481
        target_refresh_rate = mode->refresh_rate;
slouken@2753
   482
    } else {
slouken@2753
   483
        target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
slouken@2753
   484
    }
slouken@0
   485
slouken@2753
   486
    match = NULL;
slouken@2753
   487
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@2753
   488
        current = &SDL_CurrentDisplay.display_modes[i];
slouken@0
   489
slouken@2789
   490
        if (current->w && (current->w < mode->w)) {
slouken@2753
   491
            /* Out of sorted modes large enough here */
slouken@2753
   492
            break;
slouken@2753
   493
        }
slouken@2789
   494
        if (current->h && (current->h < mode->h)) {
slouken@2789
   495
            if (current->w && (current->w == mode->w)) {
slouken@2789
   496
                /* Out of sorted modes large enough here */
slouken@2789
   497
                break;
slouken@2789
   498
            }
slouken@2789
   499
            /* Wider, but not tall enough, due to a different
slouken@2789
   500
               aspect ratio. This mode must be skipped, but closer
slouken@2789
   501
               modes may still follow. */
slouken@2789
   502
            continue;
slouken@2789
   503
        }
slouken@2753
   504
        if (!match || current->w < match->w || current->h < match->h) {
slouken@2753
   505
            match = current;
slouken@2753
   506
            continue;
slouken@2753
   507
        }
slouken@2753
   508
        if (current->format != match->format) {
slouken@2753
   509
            /* Sorted highest depth to lowest */
slouken@2753
   510
            if (current->format == target_format ||
slouken@2753
   511
                (SDL_BITSPERPIXEL(current->format) >=
slouken@2753
   512
                 SDL_BITSPERPIXEL(target_format)
slouken@2753
   513
                 && SDL_PIXELTYPE(current->format) ==
slouken@2753
   514
                 SDL_PIXELTYPE(target_format))) {
slouken@2753
   515
                match = current;
slouken@2753
   516
            }
slouken@2753
   517
            continue;
slouken@2753
   518
        }
slouken@2753
   519
        if (current->refresh_rate != match->refresh_rate) {
slouken@2753
   520
            /* Sorted highest refresh to lowest */
slouken@2753
   521
            if (current->refresh_rate >= target_refresh_rate) {
slouken@2753
   522
                match = current;
slouken@2753
   523
            }
slouken@2753
   524
        }
slouken@2753
   525
    }
slouken@2753
   526
    if (match) {
slouken@2753
   527
        if (match->format) {
slouken@2753
   528
            closest->format = match->format;
slouken@2753
   529
        } else {
slouken@2753
   530
            closest->format = mode->format;
slouken@2753
   531
        }
slouken@2753
   532
        if (match->w && match->h) {
slouken@2753
   533
            closest->w = match->w;
slouken@2753
   534
            closest->h = match->h;
slouken@2753
   535
        } else {
slouken@2753
   536
            closest->w = mode->w;
slouken@2753
   537
            closest->h = mode->h;
slouken@2753
   538
        }
slouken@2753
   539
        if (match->refresh_rate) {
slouken@2753
   540
            closest->refresh_rate = match->refresh_rate;
slouken@2753
   541
        } else {
slouken@2753
   542
            closest->refresh_rate = mode->refresh_rate;
slouken@2753
   543
        }
slouken@2753
   544
        closest->driverdata = match->driverdata;
slouken@1895
   545
slouken@2753
   546
        /*
slouken@2753
   547
         * Pick some reasonable defaults if the app and driver don't
slouken@2753
   548
         * care
slouken@2753
   549
         */
slouken@2753
   550
        if (!closest->format) {
slouken@2753
   551
            closest->format = SDL_PIXELFORMAT_RGB888;
slouken@2753
   552
        }
slouken@2753
   553
        if (!closest->w) {
slouken@2753
   554
            closest->w = 640;
slouken@2753
   555
        }
slouken@2753
   556
        if (!closest->h) {
slouken@2753
   557
            closest->h = 480;
slouken@2753
   558
        }
slouken@2753
   559
        return closest;
slouken@2753
   560
    }
slouken@2753
   561
    return NULL;
slouken@0
   562
}
slouken@0
   563
slouken@1895
   564
int
slouken@1895
   565
SDL_SetDisplayMode(const SDL_DisplayMode * mode)
slouken@0
   566
{
slouken@2753
   567
    SDL_VideoDisplay *display;
slouken@2753
   568
    SDL_DisplayMode display_mode;
slouken@2753
   569
    SDL_DisplayMode current_mode;
slouken@2753
   570
    int i, ncolors;
slouken@0
   571
slouken@2753
   572
    if (!_this) {
slouken@2753
   573
        SDL_UninitializedVideo();
slouken@2753
   574
        return -1;
slouken@2753
   575
    }
slouken@2753
   576
    display = &SDL_CurrentDisplay;
slouken@2753
   577
    if (!mode) {
slouken@2753
   578
        mode = &display->desktop_mode;
slouken@2753
   579
    }
slouken@2753
   580
    display_mode = *mode;
slouken@0
   581
slouken@2753
   582
    /* Default to the current mode */
slouken@2753
   583
    if (!display_mode.format) {
slouken@2753
   584
        display_mode.format = display->current_mode.format;
slouken@2753
   585
    }
slouken@2753
   586
    if (!display_mode.w) {
slouken@2753
   587
        display_mode.w = display->current_mode.w;
slouken@2753
   588
    }
slouken@2753
   589
    if (!display_mode.h) {
slouken@2753
   590
        display_mode.h = display->current_mode.h;
slouken@2753
   591
    }
slouken@2753
   592
    if (!display_mode.refresh_rate) {
slouken@2753
   593
        display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@2753
   594
    }
slouken@2753
   595
    /* Get a good video mode, the closest one possible */
slouken@2753
   596
    if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
slouken@2753
   597
        SDL_SetError("No video mode large enough for %dx%d",
slouken@2753
   598
                     display_mode.w, display_mode.h);
slouken@2753
   599
        return -1;
slouken@2753
   600
    }
slouken@2753
   601
    /* See if there's anything left to do */
slouken@2753
   602
    SDL_GetCurrentDisplayMode(&current_mode);
slouken@2753
   603
    if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
slouken@2753
   604
        return 0;
slouken@2753
   605
    }
slouken@2753
   606
    /* Actually change the display mode */
slouken@2753
   607
    if (_this->SetDisplayMode(_this, &display_mode) < 0) {
slouken@2753
   608
        return -1;
slouken@2753
   609
    }
slouken@2753
   610
    display->current_mode = display_mode;
slouken@1895
   611
slouken@2753
   612
    /* Set up a palette, if necessary */
slouken@2753
   613
    if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
slouken@2753
   614
        ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
slouken@2753
   615
    } else {
slouken@2753
   616
        ncolors = 0;
slouken@2753
   617
    }
slouken@2753
   618
    if ((!ncolors && display->palette) || (ncolors && !display->palette)
slouken@2753
   619
        || (ncolors && ncolors != display->palette->ncolors)) {
slouken@2753
   620
        if (display->palette) {
slouken@2753
   621
            SDL_FreePalette(display->palette);
slouken@2753
   622
            display->palette = NULL;
slouken@2753
   623
        }
slouken@2753
   624
        if (ncolors) {
slouken@2753
   625
            display->palette = SDL_AllocPalette(ncolors);
slouken@2753
   626
            if (!display->palette) {
slouken@2753
   627
                return -1;
slouken@2753
   628
            }
slouken@2753
   629
            SDL_DitherColors(display->palette->colors,
slouken@2753
   630
                             SDL_BITSPERPIXEL(display_mode.format));
slouken@2753
   631
        }
slouken@2753
   632
    }
slouken@2753
   633
    /* Move any fullscreen windows into position */
slouken@2753
   634
    for (i = 0; i < display->num_windows; ++i) {
slouken@2753
   635
        SDL_Window *window = &display->windows[i];
slouken@2753
   636
        if (FULLSCREEN_VISIBLE(window)) {
slouken@2875
   637
            SDL_SetWindowPosition(window->id, window->x, window->y);
slouken@2753
   638
        }
slouken@2753
   639
    }
slouken@1895
   640
slouken@2753
   641
    return 0;
slouken@0
   642
}
slouken@0
   643
slouken@1895
   644
int
slouken@1895
   645
SDL_SetFullscreenDisplayMode(const SDL_DisplayMode * mode)
slouken@0
   646
{
slouken@2753
   647
    SDL_VideoDisplay *display;
slouken@2753
   648
    SDL_DisplayMode fullscreen_mode;
slouken@2753
   649
    int i;
slouken@0
   650
slouken@2753
   651
    if (!_this) {
slouken@2753
   652
        SDL_UninitializedVideo();
slouken@2753
   653
        return -1;
slouken@2753
   654
    }
slouken@2753
   655
    display = &SDL_CurrentDisplay;
slouken@2753
   656
    if (!mode) {
slouken@2753
   657
        mode = &display->desktop_mode;
slouken@2753
   658
    }
slouken@2869
   659
    if (!SDL_GetClosestDisplayMode(mode, &fullscreen_mode)) {
slouken@2869
   660
        SDL_SetError("Couldn't find display mode match");
slouken@2869
   661
        return -1;
slouken@2869
   662
    }
slouken@2753
   663
    if (SDL_memcmp
slouken@2753
   664
        (&fullscreen_mode, &display->fullscreen_mode,
slouken@2753
   665
         sizeof(fullscreen_mode)) == 0) {
slouken@2753
   666
        /* Nothing to do... */
slouken@2753
   667
        return 0;
slouken@2753
   668
    }
slouken@2753
   669
    display->fullscreen_mode = fullscreen_mode;
slouken@0
   670
slouken@2753
   671
    /* Actually set the mode if we have a fullscreen window visible */
slouken@2753
   672
    for (i = 0; i < display->num_windows; ++i) {
slouken@2753
   673
        SDL_Window *window = &display->windows[i];
slouken@2753
   674
        if (FULLSCREEN_VISIBLE(window)) {
slouken@2753
   675
            if (SDL_SetDisplayMode(&display->fullscreen_mode) < 0) {
slouken@2753
   676
                return -1;
slouken@2753
   677
            }
slouken@2753
   678
        }
slouken@2753
   679
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@2753
   680
            SDL_OnWindowResized(window);
slouken@2753
   681
        }
slouken@2753
   682
    }
slouken@2753
   683
    return 0;
slouken@0
   684
}
slouken@0
   685
slouken@1967
   686
int
slouken@1967
   687
SDL_GetFullscreenDisplayMode(SDL_DisplayMode * mode)
slouken@0
   688
{
slouken@2753
   689
    if (!_this) {
slouken@2753
   690
        SDL_UninitializedVideo();
slouken@2753
   691
        return -1;
slouken@2753
   692
    }
slouken@2753
   693
    if (mode) {
slouken@2753
   694
        *mode = SDL_CurrentDisplay.fullscreen_mode;
slouken@2753
   695
    }
slouken@2753
   696
    return 0;
slouken@0
   697
}
slouken@0
   698
slouken@1895
   699
int
slouken@1895
   700
SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
slouken@0
   701
{
slouken@2753
   702
    SDL_Palette *palette;
slouken@2753
   703
    int status = 0;
slouken@0
   704
slouken@2753
   705
    if (!_this) {
slouken@2753
   706
        SDL_UninitializedVideo();
slouken@2753
   707
        return -1;
slouken@2753
   708
    }
slouken@2753
   709
    palette = SDL_CurrentDisplay.palette;
slouken@2753
   710
    if (!palette) {
slouken@2753
   711
        SDL_SetError("Display mode does not have a palette");
slouken@2753
   712
        return -1;
slouken@2753
   713
    }
slouken@2753
   714
    status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
slouken@0
   715
slouken@2753
   716
    if (_this->SetDisplayPalette) {
slouken@2753
   717
        if (_this->SetDisplayPalette(_this, palette) < 0) {
slouken@2753
   718
            status = -1;
slouken@2753
   719
        }
slouken@2753
   720
    }
slouken@2753
   721
    return status;
slouken@0
   722
}
slouken@0
   723
slouken@1895
   724
int
slouken@1895
   725
SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
slouken@0
   726
{
slouken@2753
   727
    SDL_Palette *palette;
slouken@1895
   728
slouken@2753
   729
    if (!_this) {
slouken@2753
   730
        SDL_UninitializedVideo();
slouken@2753
   731
        return -1;
slouken@2753
   732
    }
slouken@2753
   733
    palette = SDL_CurrentDisplay.palette;
slouken@2753
   734
    if (!palette->ncolors) {
slouken@2753
   735
        SDL_SetError("Display mode does not have a palette");
slouken@2753
   736
        return -1;
slouken@2753
   737
    }
slouken@2753
   738
    if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
slouken@2753
   739
        SDL_SetError("Palette indices are out of range");
slouken@2753
   740
        return -1;
slouken@2753
   741
    }
slouken@2753
   742
    SDL_memcpy(colors, &palette->colors[firstcolor],
slouken@2753
   743
               ncolors * sizeof(*colors));
slouken@2753
   744
    return 0;
slouken@0
   745
}
slouken@0
   746
slouken@1895
   747
SDL_WindowID
slouken@1895
   748
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
   749
{
slouken@2753
   750
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@2753
   751
                                  SDL_WINDOW_OPENGL |
slouken@2753
   752
                                  SDL_WINDOW_BORDERLESS |
slouken@2875
   753
                                  SDL_WINDOW_RESIZABLE |
slouken@2875
   754
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@2753
   755
    SDL_VideoDisplay *display;
slouken@2753
   756
    SDL_Window window;
slouken@2753
   757
    int num_windows;
slouken@2753
   758
    SDL_Window *windows;
slouken@0
   759
slouken@2753
   760
    if (!_this) {
slouken@2753
   761
        SDL_UninitializedVideo();
slouken@2753
   762
        return 0;
slouken@2753
   763
    }
slouken@2753
   764
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
   765
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
   766
        return 0;
slouken@2753
   767
    }
slouken@2753
   768
    SDL_zero(window);
slouken@2753
   769
    window.id = _this->next_object_id++;
slouken@2753
   770
    window.x = x;
slouken@2753
   771
    window.y = y;
slouken@2753
   772
    window.w = w;
slouken@2753
   773
    window.h = h;
slouken@2753
   774
    window.flags = (flags & allowed_flags);
slouken@2753
   775
    window.display = _this->current_display;
slouken@1912
   776
slouken@2753
   777
    if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
slouken@2753
   778
        return 0;
slouken@2753
   779
    }
slouken@2753
   780
    display = &SDL_CurrentDisplay;
slouken@2753
   781
    num_windows = display->num_windows;
slouken@2753
   782
    windows =
slouken@2753
   783
        SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
slouken@2753
   784
    if (!windows) {
slouken@2753
   785
        if (_this->DestroyWindow) {
slouken@2753
   786
            _this->DestroyWindow(_this, &window);
slouken@2753
   787
        }
slouken@2753
   788
        return 0;
slouken@2753
   789
    }
slouken@2753
   790
    windows[num_windows] = window;
slouken@2753
   791
    display->windows = windows;
slouken@2753
   792
    display->num_windows++;
slouken@0
   793
slouken@2753
   794
    if (title) {
slouken@2753
   795
        SDL_SetWindowTitle(window.id, title);
slouken@2753
   796
    }
slouken@2753
   797
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
   798
        SDL_MaximizeWindow(window.id);
slouken@2753
   799
    }
slouken@2753
   800
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
   801
        SDL_MinimizeWindow(window.id);
slouken@2753
   802
    }
slouken@2753
   803
    if (flags & SDL_WINDOW_SHOWN) {
slouken@2753
   804
        SDL_ShowWindow(window.id);
slouken@2753
   805
    }
slouken@2875
   806
    SDL_UpdateWindowGrab(&window);
slouken@2875
   807
slouken@2753
   808
    return window.id;
slouken@1895
   809
}
slouken@0
   810
slouken@1895
   811
SDL_WindowID
slouken@1895
   812
SDL_CreateWindowFrom(const void *data)
slouken@1895
   813
{
slouken@2753
   814
    SDL_VideoDisplay *display;
slouken@2753
   815
    SDL_Window window;
slouken@2753
   816
    int num_windows;
slouken@2753
   817
    SDL_Window *windows;
slouken@0
   818
slouken@2753
   819
    if (!_this) {
slouken@2753
   820
        SDL_UninitializedVideo();
slouken@2753
   821
        return (0);
slouken@2753
   822
    }
slouken@2753
   823
    SDL_zero(window);
slouken@2753
   824
    window.id = _this->next_object_id++;
slouken@2753
   825
    window.display = _this->current_display;
slouken@1895
   826
slouken@2753
   827
    if (!_this->CreateWindowFrom ||
slouken@2753
   828
        _this->CreateWindowFrom(_this, &window, data) < 0) {
slouken@2753
   829
        return 0;
slouken@2753
   830
    }
slouken@2753
   831
    display = &SDL_CurrentDisplay;
slouken@2753
   832
    num_windows = display->num_windows;
slouken@2753
   833
    windows =
slouken@2753
   834
        SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
slouken@2753
   835
    if (!windows) {
slouken@2753
   836
        if (_this->DestroyWindow) {
slouken@2753
   837
            _this->DestroyWindow(_this, &window);
slouken@2753
   838
        }
slouken@2753
   839
        if (window.title) {
slouken@2753
   840
            SDL_free(window.title);
slouken@2753
   841
        }
slouken@2753
   842
        return 0;
slouken@2753
   843
    }
slouken@2753
   844
    windows[num_windows] = window;
slouken@2753
   845
    display->windows = windows;
slouken@2753
   846
    display->num_windows++;
slouken@1895
   847
slouken@2753
   848
    return window.id;
slouken@1895
   849
}
slouken@1895
   850
slouken@1924
   851
int
slouken@1928
   852
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
   853
{
slouken@2753
   854
    char *title = window->title;
slouken@1956
   855
slouken@2753
   856
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
   857
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
   858
        return -1;
slouken@2753
   859
    }
slouken@2753
   860
    if (_this->DestroyWindow) {
slouken@2753
   861
        _this->DestroyWindow(_this, window);
slouken@2753
   862
    }
slouken@2753
   863
    window->title = NULL;
slouken@2753
   864
    window->flags =
slouken@2753
   865
        (flags &
slouken@2753
   866
         ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
slouken@2753
   867
           SDL_WINDOW_INPUT_GRABBED));
slouken@1956
   868
slouken@2753
   869
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@2753
   870
        return -1;
slouken@2753
   871
    }
slouken@2753
   872
    if (title) {
slouken@2753
   873
        SDL_SetWindowTitle(window->id, title);
slouken@2753
   874
        SDL_free(title);
slouken@2753
   875
    }
slouken@2753
   876
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
   877
        SDL_MaximizeWindow(window->id);
slouken@2753
   878
    }
slouken@2753
   879
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
   880
        SDL_MinimizeWindow(window->id);
slouken@2753
   881
    }
slouken@2753
   882
    if (flags & SDL_WINDOW_SHOWN) {
slouken@2753
   883
        SDL_ShowWindow(window->id);
slouken@2753
   884
    }
slouken@2875
   885
    SDL_UpdateWindowGrab(window);
slouken@2875
   886
slouken@2753
   887
    return 0;
slouken@1924
   888
}
slouken@1924
   889
slouken@2753
   890
SDL_Window *
slouken@1895
   891
SDL_GetWindowFromID(SDL_WindowID windowID)
slouken@1895
   892
{
slouken@2753
   893
    int i, j;
slouken@1895
   894
slouken@2753
   895
    if (!_this) {
slouken@2753
   896
        SDL_UninitializedVideo();
slouken@2753
   897
        return NULL;
slouken@2753
   898
    }
slouken@2753
   899
    for (i = 0; i < _this->num_displays; ++i) {
slouken@2753
   900
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
   901
        for (j = 0; j < display->num_windows; ++j) {
slouken@2753
   902
            SDL_Window *window = &display->windows[j];
slouken@2753
   903
            if (window->id == windowID) {
slouken@2753
   904
                return window;
slouken@2753
   905
            }
slouken@2753
   906
        }
slouken@2753
   907
    }
slouken@2753
   908
    return NULL;
slouken@1895
   909
}
slouken@1895
   910
slouken@1895
   911
SDL_VideoDisplay *
slouken@1895
   912
SDL_GetDisplayFromWindow(SDL_Window * window)
slouken@1895
   913
{
slouken@2753
   914
    if (!_this) {
slouken@2753
   915
        SDL_UninitializedVideo();
slouken@2753
   916
        return NULL;
slouken@2753
   917
    }
slouken@2753
   918
    if (!window) {
slouken@2753
   919
        return NULL;
slouken@2753
   920
    }
slouken@2753
   921
    return &_this->displays[window->display];
slouken@1895
   922
}
slouken@1895
   923
slouken@1895
   924
Uint32
slouken@1895
   925
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1895
   926
{
slouken@2753
   927
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
   928
slouken@2753
   929
    if (!window) {
slouken@2753
   930
        return 0;
slouken@2753
   931
    }
slouken@2753
   932
    return window->flags;
slouken@1895
   933
}
slouken@1895
   934
slouken@1895
   935
void
slouken@1895
   936
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1895
   937
{
slouken@2753
   938
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
   939
slouken@2753
   940
    if (!window || title == window->title) {
slouken@2753
   941
        return;
slouken@2753
   942
    }
slouken@2753
   943
    if (window->title) {
slouken@2753
   944
        SDL_free(window->title);
slouken@2753
   945
    }
slouken@2753
   946
    if (title) {
slouken@2753
   947
        window->title = SDL_strdup(title);
slouken@2753
   948
    } else {
slouken@2753
   949
        window->title = NULL;
slouken@2753
   950
    }
slouken@1956
   951
slouken@2753
   952
    if (_this->SetWindowTitle) {
slouken@2753
   953
        _this->SetWindowTitle(_this, window);
slouken@2753
   954
    }
slouken@1895
   955
}
slouken@1895
   956
slouken@2753
   957
const char *
slouken@1895
   958
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1895
   959
{
slouken@2753
   960
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
   961
slouken@2753
   962
    if (!window) {
slouken@2753
   963
        return NULL;
slouken@2753
   964
    }
slouken@2753
   965
    return window->title;
slouken@1895
   966
}
slouken@1895
   967
slouken@1895
   968
void
slouken@2967
   969
SDL_SetWindowIcon(SDL_WindowID windowID, SDL_Surface * icon)
slouken@2967
   970
{
slouken@2967
   971
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2967
   972
slouken@2967
   973
    if (!window) {
slouken@2967
   974
        return;
slouken@2967
   975
    }
slouken@2967
   976
    if (_this->SetWindowIcon) {
slouken@2967
   977
        _this->SetWindowIcon(_this, window, icon);
slouken@2967
   978
    }
slouken@2967
   979
}
slouken@2967
   980
slouken@2967
   981
void
slouken@1895
   982
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1895
   983
{
slouken@2753
   984
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
   985
slouken@2753
   986
    if (!window) {
slouken@2753
   987
        return;
slouken@2753
   988
    }
slouken@2753
   989
    window->userdata = userdata;
slouken@1895
   990
}
slouken@1895
   991
slouken@2753
   992
void *
slouken@1895
   993
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1895
   994
{
slouken@2753
   995
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
   996
slouken@2753
   997
    if (!window) {
slouken@2753
   998
        return NULL;
slouken@2753
   999
    }
slouken@2753
  1000
    return window->userdata;
slouken@1895
  1001
}
slouken@1895
  1002
slouken@1895
  1003
void
slouken@1895
  1004
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1895
  1005
{
slouken@2753
  1006
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2753
  1007
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1008
slouken@2753
  1009
    if (!window) {
slouken@2753
  1010
        return;
slouken@2753
  1011
    }
slouken@2875
  1012
    if (x != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1013
        window->x = x;
slouken@2753
  1014
    }
slouken@2875
  1015
    if (y != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1016
        window->y = y;
slouken@2753
  1017
    }
slouken@2753
  1018
    if (_this->SetWindowPosition) {
slouken@2753
  1019
        _this->SetWindowPosition(_this, window);
slouken@2753
  1020
    }
slouken@2875
  1021
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
  1022
}
slouken@1895
  1023
slouken@1895
  1024
void
slouken@1895
  1025
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1895
  1026
{
slouken@2753
  1027
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1028
slouken@2753
  1029
    if (!window) {
slouken@2753
  1030
        return;
slouken@2753
  1031
    }
slouken@2753
  1032
    if (x) {
slouken@2753
  1033
        *x = window->x;
slouken@2753
  1034
    }
slouken@2753
  1035
    if (y) {
slouken@2753
  1036
        *y = window->y;
slouken@2753
  1037
    }
slouken@1895
  1038
}
slouken@1895
  1039
slouken@1895
  1040
void
slouken@1895
  1041
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1895
  1042
{
slouken@2753
  1043
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1044
slouken@2753
  1045
    if (!window) {
slouken@2753
  1046
        return;
slouken@2753
  1047
    }
slouken@2753
  1048
    window->w = w;
slouken@2753
  1049
    window->h = h;
slouken@1895
  1050
slouken@2753
  1051
    if (_this->SetWindowSize) {
slouken@2753
  1052
        _this->SetWindowSize(_this, window);
slouken@2753
  1053
    }
slouken@2849
  1054
    SDL_OnWindowResized(window);
slouken@1895
  1055
}
slouken@1895
  1056
slouken@1895
  1057
void
slouken@1895
  1058
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1895
  1059
{
slouken@2753
  1060
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1061
slouken@2860
  1062
    if (window) {
slouken@2849
  1063
        if (w) {
slouken@2849
  1064
            *w = window->w;
slouken@2849
  1065
        }
slouken@2849
  1066
        if (h) {
slouken@2849
  1067
            *h = window->h;
slouken@2849
  1068
        }
slouken@2849
  1069
    } else {
slouken@2849
  1070
        if (w) {
slouken@2849
  1071
            *w = 0;
slouken@2849
  1072
        }
slouken@2849
  1073
        if (h) {
slouken@2849
  1074
            *h = 0;
slouken@2849
  1075
        }
slouken@2753
  1076
    }
slouken@1895
  1077
}
slouken@1895
  1078
slouken@1895
  1079
void
slouken@1895
  1080
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1895
  1081
{
slouken@2753
  1082
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1083
slouken@2753
  1084
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1085
        return;
slouken@2753
  1086
    }
slouken@1895
  1087
slouken@2753
  1088
    if (_this->ShowWindow) {
slouken@2753
  1089
        _this->ShowWindow(_this, window);
slouken@2753
  1090
    }
slouken@2875
  1091
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1092
}
slouken@1895
  1093
slouken@1895
  1094
void
slouken@1895
  1095
SDL_HideWindow(SDL_WindowID windowID)
slouken@1895
  1096
{
slouken@2753
  1097
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1098
slouken@2753
  1099
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1100
        return;
slouken@2753
  1101
    }
slouken@1895
  1102
slouken@2753
  1103
    if (_this->HideWindow) {
slouken@2753
  1104
        _this->HideWindow(_this, window);
slouken@2753
  1105
    }
slouken@2875
  1106
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1107
}
slouken@1895
  1108
slouken@1895
  1109
void
slouken@1895
  1110
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1895
  1111
{
slouken@2753
  1112
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1113
slouken@2753
  1114
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1115
        return;
slouken@2753
  1116
    }
slouken@2753
  1117
    if (_this->RaiseWindow) {
slouken@2753
  1118
        _this->RaiseWindow(_this, window);
slouken@2753
  1119
    }
slouken@1895
  1120
}
slouken@1895
  1121
slouken@1895
  1122
void
slouken@1895
  1123
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1895
  1124
{
slouken@2753
  1125
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1126
slouken@2753
  1127
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@2753
  1128
        return;
slouken@2753
  1129
    }
slouken@1895
  1130
slouken@2753
  1131
    if (_this->MaximizeWindow) {
slouken@2753
  1132
        _this->MaximizeWindow(_this, window);
slouken@2753
  1133
    }
slouken@2875
  1134
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1135
}
slouken@1895
  1136
slouken@1895
  1137
void
slouken@1895
  1138
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1895
  1139
{
slouken@2753
  1140
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1141
slouken@2753
  1142
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@2753
  1143
        return;
slouken@2753
  1144
    }
slouken@1895
  1145
slouken@2753
  1146
    if (_this->MinimizeWindow) {
slouken@2753
  1147
        _this->MinimizeWindow(_this, window);
slouken@2753
  1148
    }
slouken@2875
  1149
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1150
}
slouken@1895
  1151
slouken@1895
  1152
void
slouken@1895
  1153
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1895
  1154
{
slouken@2753
  1155
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1156
slouken@2753
  1157
    if (!window
slouken@2934
  1158
        || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1159
        return;
slouken@2753
  1160
    }
slouken@1895
  1161
slouken@2753
  1162
    if (_this->RestoreWindow) {
slouken@2753
  1163
        _this->RestoreWindow(_this, window);
slouken@2753
  1164
    }
slouken@2875
  1165
    SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1166
}
slouken@1895
  1167
slouken@1895
  1168
int
slouken@1895
  1169
SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
slouken@1895
  1170
{
slouken@2753
  1171
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1172
slouken@2753
  1173
    if (!window) {
slouken@2753
  1174
        return -1;
slouken@2753
  1175
    }
slouken@2753
  1176
    if (fullscreen) {
slouken@2753
  1177
        fullscreen = SDL_WINDOW_FULLSCREEN;
slouken@2753
  1178
    }
slouken@2753
  1179
    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
slouken@2753
  1180
        return 0;
slouken@2753
  1181
    }
slouken@2753
  1182
    if (fullscreen) {
slouken@2753
  1183
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
  1184
slouken@2753
  1185
        if (FULLSCREEN_VISIBLE(window)) {
slouken@2753
  1186
            SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1187
slouken@2753
  1188
            /* Hide any other fullscreen windows */
slouken@2753
  1189
            int i;
slouken@2753
  1190
            for (i = 0; i < display->num_windows; ++i) {
slouken@2753
  1191
                SDL_Window *other = &display->windows[i];
slouken@2753
  1192
                if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
slouken@2753
  1193
                    SDL_MinimizeWindow(other->id);
slouken@2753
  1194
                }
slouken@2753
  1195
            }
slouken@1895
  1196
slouken@2753
  1197
            SDL_SetDisplayMode(&display->fullscreen_mode);
slouken@2753
  1198
        }
slouken@2753
  1199
    } else {
slouken@2753
  1200
        window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@1895
  1201
slouken@2753
  1202
        if (FULLSCREEN_VISIBLE(window)) {
slouken@2753
  1203
            SDL_SetDisplayMode(NULL);
slouken@2753
  1204
        }
slouken@2753
  1205
    }
slouken@2753
  1206
    return 0;
slouken@1895
  1207
}
slouken@1895
  1208
slouken@1895
  1209
void
slouken@1895
  1210
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1895
  1211
{
slouken@2753
  1212
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1213
slouken@2753
  1214
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@2753
  1215
        return;
slouken@2753
  1216
    }
slouken@2753
  1217
    if (mode) {
slouken@2753
  1218
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1219
    } else {
slouken@2753
  1220
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1221
    }
slouken@2875
  1222
    SDL_UpdateWindowGrab(window);
slouken@2875
  1223
}
slouken@1895
  1224
slouken@2875
  1225
static void
slouken@2876
  1226
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1227
{
slouken@2753
  1228
    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
slouken@2753
  1229
        _this->SetWindowGrab(_this, window);
slouken@2753
  1230
    }
slouken@1895
  1231
}
slouken@1895
  1232
slouken@1895
  1233
int
slouken@1895
  1234
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1895
  1235
{
slouken@2753
  1236
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1237
slouken@2753
  1238
    if (!window) {
slouken@2753
  1239
        return 0;
slouken@2753
  1240
    }
slouken@2753
  1241
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1242
}
slouken@1895
  1243
slouken@1895
  1244
void
slouken@1895
  1245
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1246
{
slouken@2875
  1247
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@2875
  1248
        SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@2875
  1249
    }
slouken@1895
  1250
}
slouken@1895
  1251
slouken@1895
  1252
void
slouken@1895
  1253
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1254
{
slouken@2875
  1255
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@2875
  1256
        SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@2875
  1257
    }
slouken@1895
  1258
}
slouken@1895
  1259
slouken@1895
  1260
void
slouken@1970
  1261
SDL_OnWindowResized(SDL_Window * window)
slouken@1970
  1262
{
slouken@2753
  1263
    SDL_Renderer *renderer = window->renderer;
slouken@1970
  1264
slouken@2753
  1265
    if (renderer && renderer->DisplayModeChanged) {
slouken@2753
  1266
        renderer->DisplayModeChanged(renderer);
slouken@2753
  1267
    }
slouken@1970
  1268
}
slouken@1970
  1269
slouken@1970
  1270
void
slouken@1895
  1271
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1272
{
slouken@2753
  1273
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1274
slouken@2753
  1275
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@2753
  1276
        SDL_SetDisplayMode(&display->fullscreen_mode);
slouken@2753
  1277
    }
slouken@2753
  1278
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@2753
  1279
        _this->SetDisplayGammaRamp(_this, display->gamma);
slouken@2753
  1280
    }
slouken@2876
  1281
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1282
        && _this->SetWindowGrab) {
slouken@2753
  1283
        _this->SetWindowGrab(_this, window);
slouken@2753
  1284
    }
slouken@1895
  1285
}
slouken@1895
  1286
slouken@1895
  1287
void
slouken@1895
  1288
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1289
{
slouken@2753
  1290
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
  1291
slouken@2753
  1292
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@2753
  1293
        SDL_MinimizeWindow(window->id);
slouken@2753
  1294
        SDL_SetDisplayMode(NULL);
slouken@2753
  1295
    }
slouken@2753
  1296
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@2753
  1297
        _this->SetDisplayGammaRamp(_this, display->saved_gamma);
slouken@2753
  1298
    }
slouken@2876
  1299
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1300
        && _this->SetWindowGrab) {
slouken@2753
  1301
        _this->SetWindowGrab(_this, window);
slouken@2753
  1302
    }
slouken@1895
  1303
}
slouken@1895
  1304
slouken@1895
  1305
SDL_WindowID
slouken@1895
  1306
SDL_GetFocusWindow(void)
slouken@1895
  1307
{
slouken@2753
  1308
    SDL_VideoDisplay *display;
slouken@2753
  1309
    int i;
slouken@1895
  1310
slouken@2753
  1311
    if (!_this) {
slouken@2753
  1312
        return 0;
slouken@2753
  1313
    }
slouken@2753
  1314
    display = &SDL_CurrentDisplay;
slouken@2753
  1315
    for (i = 0; i < display->num_windows; ++i) {
slouken@2753
  1316
        SDL_Window *window = &display->windows[i];
slouken@1895
  1317
slouken@2753
  1318
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@2753
  1319
            return window->id;
slouken@2753
  1320
        }
slouken@2753
  1321
    }
slouken@2753
  1322
    return 0;
slouken@1895
  1323
}
slouken@1895
  1324
slouken@1895
  1325
void
slouken@1895
  1326
SDL_DestroyWindow(SDL_WindowID windowID)
slouken@1895
  1327
{
slouken@2753
  1328
    int i, j;
slouken@1895
  1329
slouken@2753
  1330
    if (!_this) {
slouken@2753
  1331
        return;
slouken@2753
  1332
    }
slouken@2753
  1333
    /* Restore video mode, etc. */
slouken@2753
  1334
    SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@1895
  1335
slouken@2753
  1336
    for (i = 0; i < _this->num_displays; ++i) {
slouken@2753
  1337
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  1338
        for (j = 0; j < display->num_windows; ++j) {
slouken@2753
  1339
            SDL_Window *window = &display->windows[j];
slouken@2753
  1340
            if (window->id != windowID) {
slouken@2753
  1341
                continue;
slouken@2753
  1342
            }
slouken@2753
  1343
            if (window->title) {
slouken@2753
  1344
                SDL_free(window->title);
slouken@2753
  1345
                window->title = NULL;
slouken@2753
  1346
            }
slouken@2753
  1347
            if (window->renderer) {
slouken@2753
  1348
                SDL_DestroyRenderer(window->id);
slouken@2753
  1349
                window->renderer = NULL;
slouken@2753
  1350
            }
slouken@2753
  1351
            if (_this->DestroyWindow) {
slouken@2753
  1352
                _this->DestroyWindow(_this, window);
slouken@2753
  1353
            }
slouken@2753
  1354
            if (j != display->num_windows - 1) {
slouken@2753
  1355
                SDL_memcpy(&display->windows[i],
slouken@2753
  1356
                           &display->windows[i + 1],
slouken@2753
  1357
                           (display->num_windows - i - 1) * sizeof(*window));
slouken@2753
  1358
            }
slouken@2753
  1359
            --display->num_windows;
slouken@2753
  1360
            return;
slouken@2753
  1361
        }
slouken@2753
  1362
    }
slouken@1895
  1363
}
slouken@1895
  1364
slouken@1895
  1365
void
slouken@1895
  1366
SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
slouken@1895
  1367
{
slouken@2753
  1368
    SDL_VideoDisplay *display;
slouken@2753
  1369
    SDL_RenderDriver *render_drivers;
slouken@1895
  1370
slouken@2753
  1371
    if (displayIndex >= _this->num_displays) {
slouken@2753
  1372
        return;
slouken@2753
  1373
    }
slouken@2753
  1374
    display = &_this->displays[displayIndex];
slouken@2119
  1375
slouken@2753
  1376
    render_drivers =
slouken@2753
  1377
        SDL_realloc(display->render_drivers,
slouken@2753
  1378
                    (display->num_render_drivers +
slouken@2753
  1379
                     1) * sizeof(*render_drivers));
slouken@2753
  1380
    if (render_drivers) {
slouken@2753
  1381
        render_drivers[display->num_render_drivers] = *driver;
slouken@2753
  1382
        display->render_drivers = render_drivers;
slouken@2753
  1383
        display->num_render_drivers++;
slouken@2753
  1384
    }
slouken@1895
  1385
}
slouken@1895
  1386
slouken@1895
  1387
int
slouken@1969
  1388
SDL_GetNumRenderDrivers(void)
slouken@1895
  1389
{
slouken@2753
  1390
    if (_this) {
slouken@2753
  1391
        return SDL_CurrentDisplay.num_render_drivers;
slouken@2753
  1392
    }
slouken@2753
  1393
    return 0;
slouken@1895
  1394
}
slouken@1895
  1395
slouken@1895
  1396
int
slouken@1969
  1397
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@1895
  1398
{
slouken@2753
  1399
    if (!_this) {
slouken@2753
  1400
        SDL_UninitializedVideo();
slouken@2753
  1401
        return -1;
slouken@2753
  1402
    }
slouken@2753
  1403
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
slouken@2753
  1404
        SDL_SetError("index must be in the range of 0 - %d",
slouken@2753
  1405
                     SDL_GetNumRenderDrivers() - 1);
slouken@2753
  1406
        return -1;
slouken@2753
  1407
    }
slouken@2753
  1408
    *info = SDL_CurrentDisplay.render_drivers[index].info;
slouken@2753
  1409
    return 0;
slouken@1895
  1410
}
slouken@1895
  1411
slouken@1895
  1412
int
slouken@1895
  1413
SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
slouken@1895
  1414
{
slouken@2753
  1415
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  1416
slouken@2753
  1417
    if (!window) {
slouken@2753
  1418
        return 0;
slouken@2753
  1419
    }
slouken@2753
  1420
    if (index < 0) {
slouken@2753
  1421
        const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
slouken@2753
  1422
        int n = SDL_GetNumRenderDrivers();
slouken@2753
  1423
        for (index = 0; index < n; ++index) {
slouken@2753
  1424
            SDL_RenderDriver *driver =
slouken@2753
  1425
                &SDL_CurrentDisplay.render_drivers[index];
slouken@1895
  1426
slouken@2753
  1427
            if (override) {
slouken@2753
  1428
                if (SDL_strcasecmp(override, driver->info.name) == 0) {
slouken@2753
  1429
                    break;
slouken@2753
  1430
                }
slouken@2753
  1431
            } else {
slouken@2753
  1432
                if ((driver->info.flags & flags) == flags) {
slouken@2753
  1433
                    break;
slouken@2753
  1434
                }
slouken@2753
  1435
            }
slouken@2753
  1436
        }
slouken@2753
  1437
        if (index == n) {
slouken@2753
  1438
            SDL_SetError("Couldn't find matching render driver");
slouken@2753
  1439
            return -1;
slouken@2753
  1440
        }
slouken@2753
  1441
    }
slouken@2753
  1442
    if (index >= SDL_GetNumRenderDrivers()) {
slouken@2753
  1443
        SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@2753
  1444
                     SDL_GetNumRenderDrivers() - 1);
slouken@2753
  1445
        return -1;
slouken@2753
  1446
    }
slouken@2753
  1447
    /* Free any existing renderer */
slouken@2753
  1448
    SDL_DestroyRenderer(windowID);
slouken@1895
  1449
slouken@2753
  1450
    /* Create a new renderer instance */
slouken@2753
  1451
    window->renderer = SDL_CurrentDisplay.render_drivers[index]
slouken@2753
  1452
        .CreateRenderer(window, flags);
slouken@2753
  1453
    SDL_SelectRenderer(window->id);
slouken@1895
  1454
slouken@2753
  1455
    return 0;
slouken@1895
  1456
}
slouken@1895
  1457
slouken@1895
  1458
int
slouken@1895
  1459
SDL_SelectRenderer(SDL_WindowID windowID)
slouken@1895
  1460
{
slouken@2753
  1461
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2753
  1462
    SDL_Renderer *renderer;
slouken@1895
  1463
slouken@2753
  1464
    if (!window || !window->renderer) {
slouken@2753
  1465
        return -1;
slouken@2753
  1466
    }
slouken@2753
  1467
    renderer = window->renderer;
slouken@2753
  1468
    if (renderer && renderer->ActivateRenderer) {
slouken@2753
  1469
        if (renderer->ActivateRenderer(renderer) < 0) {
slouken@2753
  1470
            return -1;
slouken@2753
  1471
        }
slouken@2753
  1472
    }
slouken@2753
  1473
    SDL_CurrentDisplay.current_renderer = renderer;
slouken@2753
  1474
    return 0;
slouken@1895
  1475
}
slouken@1895
  1476
slouken@1969
  1477
int
slouken@1969
  1478
SDL_GetRendererInfo(SDL_RendererInfo * info)
slouken@1969
  1479
{
slouken@2753
  1480
    if (!_this) {
slouken@2753
  1481
        SDL_UninitializedVideo();
slouken@2753
  1482
        return -1;
slouken@2753
  1483
    }
slouken@2753
  1484
    if (!SDL_CurrentDisplay.current_renderer) {
slouken@2753
  1485
        SDL_SetError("There is no current renderer");
slouken@2753
  1486
        return -1;
slouken@2753
  1487
    }
slouken@2753
  1488
    *info = SDL_CurrentDisplay.current_renderer->info;
slouken@2753
  1489
    return 0;
slouken@1969
  1490
}
slouken@1969
  1491
slouken@1895
  1492
SDL_TextureID
slouken@1895
  1493
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1895
  1494
{
slouken@2753
  1495
    int hash;
slouken@2753
  1496
    SDL_Renderer *renderer;
slouken@2753
  1497
    SDL_Texture *texture;
slouken@1895
  1498
slouken@2753
  1499
    if (!_this) {
slouken@2753
  1500
        SDL_UninitializedVideo();
slouken@2753
  1501
        return 0;
slouken@2753
  1502
    }
slouken@2753
  1503
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2810
  1504
    if (!renderer) {
slouken@2810
  1505
        return 0;
slouken@2810
  1506
    }
slouken@2810
  1507
    if (!renderer->CreateTexture) {
slouken@2810
  1508
        SDL_Unsupported();
slouken@2753
  1509
        return 0;
slouken@2753
  1510
    }
slouken@2753
  1511
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@2753
  1512
    if (!texture) {
slouken@2753
  1513
        SDL_OutOfMemory();
slouken@2753
  1514
        return 0;
slouken@2753
  1515
    }
slouken@2753
  1516
    texture->id = _this->next_object_id++;
slouken@2753
  1517
    texture->format = format;
slouken@2753
  1518
    texture->access = access;
slouken@2753
  1519
    texture->w = w;
slouken@2753
  1520
    texture->h = h;
slouken@2753
  1521
    texture->r = 255;
slouken@2753
  1522
    texture->g = 255;
slouken@2753
  1523
    texture->b = 255;
slouken@2753
  1524
    texture->a = 255;
slouken@2753
  1525
    texture->renderer = renderer;
slouken@1895
  1526
slouken@2753
  1527
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@2753
  1528
        if (renderer->DestroyTexture) {
slouken@2753
  1529
            renderer->DestroyTexture(renderer, texture);
slouken@2753
  1530
        }
slouken@2753
  1531
        SDL_free(texture);
slouken@2753
  1532
        return 0;
slouken@2753
  1533
    }
slouken@2753
  1534
    hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@2753
  1535
    texture->next = SDL_CurrentDisplay.textures[hash];
slouken@2753
  1536
    SDL_CurrentDisplay.textures[hash] = texture;
slouken@1895
  1537
slouken@2753
  1538
    return texture->id;
slouken@1895
  1539
}
slouken@1895
  1540
slouken@1895
  1541
SDL_TextureID
slouken@2222
  1542
SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
slouken@1895
  1543
{
slouken@2753
  1544
    SDL_TextureID textureID;
slouken@2753
  1545
    SDL_PixelFormat *fmt;
slouken@2753
  1546
    int bpp;
slouken@2753
  1547
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
  1548
slouken@2753
  1549
    if (!surface) {
slouken@2753
  1550
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@2753
  1551
        return 0;
slouken@2753
  1552
    }
slouken@2753
  1553
    fmt = surface->format;
slouken@1895
  1554
slouken@2753
  1555
    if (format) {
slouken@2753
  1556
        if (!SDL_PixelFormatEnumToMasks
slouken@2753
  1557
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@2753
  1558
            SDL_SetError("Unknown pixel format");
slouken@2753
  1559
            return 0;
slouken@2753
  1560
        }
slouken@2753
  1561
    } else {
slouken@2753
  1562
        if (surface->format->Amask
slouken@2753
  1563
            || !(surface->map->info.flags &
slouken@2753
  1564
                 (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
slouken@2753
  1565
            bpp = fmt->BitsPerPixel;
slouken@2753
  1566
            Rmask = fmt->Rmask;
slouken@2753
  1567
            Gmask = fmt->Gmask;
slouken@2753
  1568
            Bmask = fmt->Bmask;
slouken@2753
  1569
            Amask = fmt->Amask;
slouken@2753
  1570
        } else {
slouken@2753
  1571
            /* Need a format with alpha */
slouken@2753
  1572
            bpp = 32;
slouken@2753
  1573
            Rmask = 0x00FF0000;
slouken@2753
  1574
            Gmask = 0x0000FF00;
slouken@2753
  1575
            Bmask = 0x000000FF;
slouken@2753
  1576
            Amask = 0xFF000000;
slouken@2753
  1577
        }
slouken@2753
  1578
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2753
  1579
        if (!format) {
slouken@2753
  1580
            SDL_SetError("Unknown pixel format");
slouken@2753
  1581
            return 0;
slouken@2753
  1582
        }
slouken@2753
  1583
    }
slouken@1895
  1584
slouken@2753
  1585
    textureID =
slouken@2753
  1586
        SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
slouken@2753
  1587
                          surface->h);
slouken@2753
  1588
    if (!textureID) {
slouken@2753
  1589
        return 0;
slouken@2753
  1590
    }
slouken@2753
  1591
    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
slouken@2753
  1592
        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
slouken@2753
  1593
        if (SDL_MUSTLOCK(surface)) {
slouken@2753
  1594
            if (SDL_LockSurface(surface) < 0) {
slouken@2753
  1595
                SDL_DestroyTexture(textureID);
slouken@2753
  1596
                return 0;
slouken@2753
  1597
            }
slouken@2753
  1598
            SDL_UpdateTexture(textureID, NULL, surface->pixels,
slouken@2753
  1599
                              surface->pitch);
slouken@2753
  1600
            SDL_UnlockSurface(surface);
slouken@2753
  1601
        } else {
slouken@2753
  1602
            SDL_UpdateTexture(textureID, NULL, surface->pixels,
slouken@2753
  1603
                              surface->pitch);
slouken@2753
  1604
        }
slouken@2753
  1605
    } else {
slouken@2967
  1606
        SDL_PixelFormat dst_fmt;
slouken@2753
  1607
        SDL_Surface *dst = NULL;
slouken@1895
  1608
slouken@2753
  1609
        /* Set up a destination surface for the texture update */
slouken@2967
  1610
        SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
slouken@2967
  1611
        if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@2967
  1612
            dst_fmt.palette =
slouken@2967
  1613
                SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
slouken@2967
  1614
            if (dst_fmt.palette) {
slouken@2967
  1615
                /*
slouken@2967
  1616
                 * FIXME: Should we try to copy
slouken@2967
  1617
                 * fmt->palette?
slouken@2967
  1618
                 */
slouken@2967
  1619
                SDL_DitherColors(dst_fmt.palette->colors,
slouken@2967
  1620
                                 SDL_BITSPERPIXEL(format));
slouken@2753
  1621
            }
slouken@2967
  1622
        }
slouken@2967
  1623
        dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
slouken@2967
  1624
        if (dst) {
slouken@2967
  1625
            SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
slouken@2967
  1626
            SDL_FreeSurface(dst);
slouken@2967
  1627
        }
slouken@2967
  1628
        if (dst_fmt.palette) {
slouken@2967
  1629
            SDL_FreePalette(dst_fmt.palette);
slouken@2753
  1630
        }
slouken@2753
  1631
        if (!dst) {
slouken@2753
  1632
            SDL_DestroyTexture(textureID);
slouken@2753
  1633
            return 0;
slouken@2753
  1634
        }
slouken@2753
  1635
    }
slouken@2222
  1636
slouken@2753
  1637
    if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
slouken@2753
  1638
        SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
slouken@2753
  1639
                              fmt->palette->ncolors);
slouken@2753
  1640
    }
slouken@2753
  1641
    return textureID;
slouken@1895
  1642
}
slouken@1895
  1643
slouken@1895
  1644
static __inline__ SDL_Texture *
slouken@1895
  1645
SDL_GetTextureFromID(SDL_TextureID textureID)
slouken@1895
  1646
{
slouken@2753
  1647
    int hash;
slouken@2753
  1648
    SDL_Texture *texture;
slouken@1895
  1649
slouken@2753
  1650
    if (!_this) {
slouken@2753
  1651
        return NULL;
slouken@2753
  1652
    }
slouken@2753
  1653
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@2753
  1654
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@2753
  1655
         texture = texture->next) {
slouken@2753
  1656
        if (texture->id == textureID) {
slouken@2753
  1657
            return texture;
slouken@2753
  1658
        }
slouken@2753
  1659
    }
slouken@2753
  1660
    return NULL;
slouken@1895
  1661
}
slouken@1895
  1662
slouken@1895
  1663
int
slouken@1895
  1664
SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
slouken@2753
  1665
                 int *w, int *h)
slouken@1895
  1666
{
slouken@2753
  1667
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1895
  1668
slouken@2753
  1669
    if (!texture) {
slouken@2753
  1670
        return -1;
slouken@2753
  1671
    }
slouken@2753
  1672
    if (format) {
slouken@2753
  1673
        *format = texture->format;
slouken@2753
  1674
    }
slouken@2753
  1675
    if (access) {
slouken@2753
  1676
        *access = texture->access;
slouken@2753
  1677
    }
slouken@2753
  1678
    if (w) {
slouken@2753
  1679
        *w = texture->w;
slouken@2753
  1680
    }
slouken@2753
  1681
    if (h) {
slouken@2753
  1682
        *h = texture->h;
slouken@2753
  1683
    }
slouken@2753
  1684
    return 0;
slouken@1895
  1685
}
slouken@1895
  1686
slouken@1895
  1687
int
slouken@1895
  1688
SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
slouken@1895
  1689
{
slouken@2753
  1690
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1691
    SDL_Renderer *renderer;
slouken@1895
  1692
slouken@2753
  1693
    if (!texture) {
slouken@2753
  1694
        return -1;
slouken@2753
  1695
    }
slouken@2753
  1696
    renderer = texture->renderer;
slouken@2753
  1697
    if (!renderer->QueryTexturePixels) {
slouken@2810
  1698
        SDL_Unsupported();
slouken@2753
  1699
        return -1;
slouken@2753
  1700
    }
slouken@2753
  1701
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1895
  1702
}
slouken@1895
  1703
slouken@1895
  1704
int
slouken@1895
  1705
SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
slouken@2753
  1706
                      int firstcolor, int ncolors)
slouken@1895
  1707
{
slouken@2753
  1708
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1709
    SDL_Renderer *renderer;
slouken@1895
  1710
slouken@2753
  1711
    if (!texture) {
slouken@2753
  1712
        return -1;
slouken@2753
  1713
    }
slouken@2753
  1714
    renderer = texture->renderer;
slouken@2753
  1715
    if (!renderer->SetTexturePalette) {
slouken@2810
  1716
        SDL_Unsupported();
slouken@2753
  1717
        return -1;
slouken@2753
  1718
    }
slouken@2753
  1719
    return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  1720
                                       ncolors);
slouken@1895
  1721
}
slouken@1895
  1722
slouken@1895
  1723
int
slouken@1895
  1724
SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
slouken@2753
  1725
                      int firstcolor, int ncolors)
slouken@1895
  1726
{
slouken@2753
  1727
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1728
    SDL_Renderer *renderer;
slouken@1895
  1729
slouken@2753
  1730
    if (!texture) {
slouken@2753
  1731
        return -1;
slouken@2753
  1732
    }
slouken@2753
  1733
    renderer = texture->renderer;
slouken@2753
  1734
    if (!renderer->GetTexturePalette) {
slouken@2810
  1735
        SDL_Unsupported();
slouken@2753
  1736
        return -1;
slouken@2753
  1737
    }
slouken@2753
  1738
    return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  1739
                                       ncolors);
slouken@1895
  1740
}
slouken@1895
  1741
slouken@1895
  1742
int
slouken@1985
  1743
SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
slouken@1985
  1744
{
slouken@2753
  1745
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1746
    SDL_Renderer *renderer;
slouken@1985
  1747
slouken@2753
  1748
    if (!texture) {
slouken@2753
  1749
        return -1;
slouken@2753
  1750
    }
slouken@2753
  1751
    renderer = texture->renderer;
slouken@2753
  1752
    if (!renderer->SetTextureColorMod) {
slouken@2810
  1753
        SDL_Unsupported();
slouken@2753
  1754
        return -1;
slouken@2753
  1755
    }
slouken@2753
  1756
    if (r < 255 || g < 255 || b < 255) {
slouken@2753
  1757
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  1758
    } else {
slouken@2753
  1759
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  1760
    }
slouken@2753
  1761
    texture->r = r;
slouken@2753
  1762
    texture->g = g;
slouken@2753
  1763
    texture->b = b;
slouken@2753
  1764
    return renderer->SetTextureColorMod(renderer, texture);
slouken@1985
  1765
}
slouken@1985
  1766
slouken@1985
  1767
int
slouken@1985
  1768
SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
slouken@2753
  1769
                       Uint8 * b)
slouken@1985
  1770
{
slouken@2753
  1771
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1772
    SDL_Renderer *renderer;
slouken@1985
  1773
slouken@2753
  1774
    if (!texture) {
slouken@2753
  1775
        return -1;
slouken@2753
  1776
    }
slouken@2753
  1777
    renderer = texture->renderer;
slouken@2753
  1778
    if (r) {
slouken@2753
  1779
        *r = texture->r;
slouken@2753
  1780
    }
slouken@2753
  1781
    if (g) {
slouken@2753
  1782
        *g = texture->g;
slouken@2753
  1783
    }
slouken@2753
  1784
    if (b) {
slouken@2753
  1785
        *b = texture->b;
slouken@2753
  1786
    }
slouken@2753
  1787
    return 0;
slouken@1985
  1788
}
slouken@1985
  1789
slouken@1985
  1790
int
slouken@1985
  1791
SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
slouken@1985
  1792
{
slouken@2753
  1793
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1794
    SDL_Renderer *renderer;
slouken@1985
  1795
slouken@2753
  1796
    if (!texture) {
slouken@2753
  1797
        return -1;
slouken@2753
  1798
    }
slouken@2753
  1799
    renderer = texture->renderer;
slouken@2753
  1800
    if (!renderer->SetTextureAlphaMod) {
slouken@2810
  1801
        SDL_Unsupported();
slouken@2753
  1802
        return -1;
slouken@2753
  1803
    }
slouken@2753
  1804
    if (alpha < 255) {
slouken@2753
  1805
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  1806
    } else {
slouken@2753
  1807
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  1808
    }
slouken@2753
  1809
    texture->a = alpha;
slouken@2753
  1810
    return renderer->SetTextureAlphaMod(renderer, texture);
slouken@1985
  1811
}
slouken@1985
  1812
slouken@1985
  1813
int
slouken@1985
  1814
SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
slouken@1985
  1815
{
slouken@2753
  1816
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1817
slouken@2753
  1818
    if (!texture) {
slouken@2753
  1819
        return -1;
slouken@2753
  1820
    }
slouken@2753
  1821
    if (alpha) {
slouken@2753
  1822
        *alpha = texture->a;
slouken@2753
  1823
    }
slouken@2753
  1824
    return 0;
slouken@1985
  1825
}
slouken@1985
  1826
slouken@1985
  1827
int
slouken@1985
  1828
SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
slouken@1985
  1829
{
slouken@2753
  1830
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1831
    SDL_Renderer *renderer;
slouken@1985
  1832
slouken@2753
  1833
    if (!texture) {
slouken@2753
  1834
        return -1;
slouken@2753
  1835
    }
slouken@2753
  1836
    renderer = texture->renderer;
slouken@2753
  1837
    if (!renderer->SetTextureBlendMode) {
slouken@2810
  1838
        SDL_Unsupported();
slouken@2753
  1839
        return -1;
slouken@2753
  1840
    }
slouken@2753
  1841
    texture->blendMode = blendMode;
slouken@2753
  1842
    return renderer->SetTextureBlendMode(renderer, texture);
slouken@1985
  1843
}
slouken@1985
  1844
slouken@1985
  1845
int
slouken@1985
  1846
SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
slouken@1985
  1847
{
slouken@2753
  1848
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1849
slouken@2753
  1850
    if (!texture) {
slouken@2753
  1851
        return -1;
slouken@2753
  1852
    }
slouken@2753
  1853
    if (blendMode) {
slouken@2753
  1854
        *blendMode = texture->blendMode;
slouken@2753
  1855
    }
slouken@2753
  1856
    return 0;
slouken@1985
  1857
}
slouken@1985
  1858
slouken@1985
  1859
int
slouken@1985
  1860
SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
slouken@1985
  1861
{
slouken@2753
  1862
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1863
    SDL_Renderer *renderer;
slouken@1985
  1864
slouken@2753
  1865
    if (!texture) {
slouken@2753
  1866
        return -1;
slouken@2753
  1867
    }
slouken@2753
  1868
    renderer = texture->renderer;
slouken@2753
  1869
    if (!renderer->SetTextureScaleMode) {
slouken@2810
  1870
        SDL_Unsupported();
slouken@2753
  1871
        return -1;
slouken@2753
  1872
    }
slouken@2753
  1873
    texture->scaleMode = scaleMode;
slouken@2753
  1874
    return renderer->SetTextureScaleMode(renderer, texture);
slouken@1985
  1875
}
slouken@1985
  1876
slouken@1985
  1877
int
slouken@1985
  1878
SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
slouken@1985
  1879
{
slouken@2753
  1880
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1985
  1881
slouken@2753
  1882
    if (!texture) {
slouken@2753
  1883
        return -1;
slouken@2753
  1884
    }
slouken@2753
  1885
    if (scaleMode) {
slouken@2753
  1886
        *scaleMode = texture->scaleMode;
slouken@2753
  1887
    }
slouken@2753
  1888
    return 0;
slouken@1985
  1889
}
slouken@1985
  1890
slouken@1985
  1891
int
slouken@1895
  1892
SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
slouken@2753
  1893
                  const void *pixels, int pitch)
slouken@1895
  1894
{
slouken@2753
  1895
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1896
    SDL_Renderer *renderer;
slouken@2753
  1897
    SDL_Rect full_rect;
slouken@1895
  1898
slouken@2753
  1899
    if (!texture) {
slouken@2753
  1900
        return -1;
slouken@2753
  1901
    }
slouken@2753
  1902
    renderer = texture->renderer;
slouken@2753
  1903
    if (!renderer->UpdateTexture) {
slouken@2810
  1904
        SDL_Unsupported();
slouken@2753
  1905
        return -1;
slouken@2753
  1906
    }
slouken@2753
  1907
    if (!rect) {
slouken@2753
  1908
        full_rect.x = 0;
slouken@2753
  1909
        full_rect.y = 0;
slouken@2753
  1910
        full_rect.w = texture->w;
slouken@2753
  1911
        full_rect.h = texture->h;
slouken@2753
  1912
        rect = &full_rect;
slouken@2753
  1913
    }
slouken@2753
  1914
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1895
  1915
}
slouken@1895
  1916
slouken@1895
  1917
int
slouken@1895
  1918
SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
slouken@2753
  1919
                void **pixels, int *pitch)
slouken@1895
  1920
{
slouken@2753
  1921
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1922
    SDL_Renderer *renderer;
slouken@2753
  1923
    SDL_Rect full_rect;
slouken@1895
  1924
slouken@2753
  1925
    if (!texture) {
slouken@2753
  1926
        return -1;
slouken@2753
  1927
    }
slouken@2753
  1928
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  1929
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@2753
  1930
        return -1;
slouken@2753
  1931
    }
slouken@2753
  1932
    renderer = texture->renderer;
slouken@2753
  1933
    if (!renderer->LockTexture) {
slouken@2810
  1934
        SDL_Unsupported();
slouken@2753
  1935
        return -1;
slouken@2753
  1936
    }
slouken@2753
  1937
    if (!rect) {
slouken@2753
  1938
        full_rect.x = 0;
slouken@2753
  1939
        full_rect.y = 0;
slouken@2753
  1940
        full_rect.w = texture->w;
slouken@2753
  1941
        full_rect.h = texture->h;
slouken@2753
  1942
        rect = &full_rect;
slouken@2753
  1943
    }
slouken@2753
  1944
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@2753
  1945
                                 pitch);
slouken@1895
  1946
}
slouken@1895
  1947
slouken@1895
  1948
void
slouken@1895
  1949
SDL_UnlockTexture(SDL_TextureID textureID)
slouken@1895
  1950
{
slouken@2753
  1951
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1952
    SDL_Renderer *renderer;
slouken@1895
  1953
slouken@2753
  1954
    if (!texture) {
slouken@2753
  1955
        return;
slouken@2753
  1956
    }
slouken@2753
  1957
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  1958
        return;
slouken@2753
  1959
    }
slouken@2753
  1960
    renderer = texture->renderer;
slouken@2753
  1961
    if (!renderer->UnlockTexture) {
slouken@2753
  1962
        return;
slouken@2753
  1963
    }
slouken@2753
  1964
    renderer->UnlockTexture(renderer, texture);
slouken@1895
  1965
}
slouken@1895
  1966
slouken@1895
  1967
void
slouken@1895
  1968
SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
slouken@2753
  1969
                 const SDL_Rect * rects)
slouken@1895
  1970
{
slouken@2753
  1971
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  1972
    SDL_Renderer *renderer;
slouken@1895
  1973
slouken@2753
  1974
    if (!texture) {
slouken@2753
  1975
        return;
slouken@2753
  1976
    }
slouken@2753
  1977
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  1978
        return;
slouken@2753
  1979
    }
slouken@2753
  1980
    renderer = texture->renderer;
slouken@2753
  1981
    if (!renderer->DirtyTexture) {
slouken@2753
  1982
        return;
slouken@2753
  1983
    }
slouken@2753
  1984
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1895
  1985
}
slouken@1895
  1986
slouken@1895
  1987
int
slouken@2884
  1988
SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2884
  1989
{
slouken@2884
  1990
    SDL_Renderer *renderer;
slouken@2884
  1991
slouken@2884
  1992
    if (!_this) {
slouken@2884
  1993
        SDL_UninitializedVideo();
slouken@2884
  1994
        return -1;
slouken@2884
  1995
    }
slouken@2884
  1996
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2884
  1997
    if (!renderer) {
slouken@2884
  1998
        return -1;
slouken@2884
  1999
    }
slouken@2884
  2000
    renderer->r = r;
slouken@2884
  2001
    renderer->g = g;
slouken@2884
  2002
    renderer->b = b;
slouken@2884
  2003
    renderer->a = a;
slouken@2927
  2004
    if (renderer->SetDrawColor) {
slouken@2927
  2005
        return renderer->SetDrawColor(renderer);
slouken@2927
  2006
    } else {
slouken@2927
  2007
        return 0;
slouken@2927
  2008
    }
slouken@2884
  2009
}
slouken@2884
  2010
slouken@2884
  2011
int
slouken@2885
  2012
SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@2884
  2013
{
slouken@2884
  2014
    SDL_Renderer *renderer;
slouken@2884
  2015
slouken@2884
  2016
    if (!_this) {
slouken@2884
  2017
        SDL_UninitializedVideo();
slouken@2884
  2018
        return -1;
slouken@2884
  2019
    }
slouken@2884
  2020
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2884
  2021
    if (!renderer) {
slouken@2884
  2022
        return -1;
slouken@2884
  2023
    }
slouken@2884
  2024
    if (r) {
slouken@2884
  2025
        *r = renderer->r;
slouken@2884
  2026
    }
slouken@2884
  2027
    if (g) {
slouken@2884
  2028
        *g = renderer->g;
slouken@2884
  2029
    }
slouken@2884
  2030
    if (b) {
slouken@2884
  2031
        *b = renderer->b;
slouken@2884
  2032
    }
slouken@2884
  2033
    if (a) {
slouken@2884
  2034
        *a = renderer->a;
slouken@2884
  2035
    }
slouken@2928
  2036
    return 0;
slouken@2884
  2037
}
slouken@2884
  2038
slouken@2884
  2039
int
slouken@2884
  2040
SDL_SetRenderDrawBlendMode(int blendMode)
slouken@2884
  2041
{
slouken@2884
  2042
    SDL_Renderer *renderer;
slouken@2884
  2043
slouken@2884
  2044
    if (!_this) {
slouken@2884
  2045
        SDL_UninitializedVideo();
slouken@2884
  2046
        return -1;
slouken@2884
  2047
    }
slouken@2884
  2048
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2884
  2049
    if (!renderer) {
slouken@2884
  2050
        return -1;
slouken@2884
  2051
    }
slouken@2927
  2052
    renderer->blendMode = blendMode;
slouken@2927
  2053
    if (renderer->SetDrawBlendMode) {
slouken@2927
  2054
        return renderer->SetDrawBlendMode(renderer);
slouken@2927
  2055
    } else {
slouken@2927
  2056
        return 0;
slouken@2884
  2057
    }
slouken@2884
  2058
}
slouken@2884
  2059
slouken@2884
  2060
int
slouken@2884
  2061
SDL_GetRenderDrawBlendMode(int *blendMode)
slouken@2884
  2062
{
slouken@2884
  2063
    SDL_Renderer *renderer;
slouken@2884
  2064
slouken@2884
  2065
    if (!_this) {
slouken@2884
  2066
        SDL_UninitializedVideo();
slouken@2884
  2067
        return -1;
slouken@2884
  2068
    }
slouken@2884
  2069
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2884
  2070
    if (!renderer) {
slouken@2884
  2071
        return -1;
slouken@2884
  2072
    }
slouken@2884
  2073
    *blendMode = renderer->blendMode;
slouken@2884
  2074
    return 0;
slouken@2884
  2075
}
slouken@2884
  2076
slouken@2901
  2077
int
slouken@2901
  2078
SDL_RenderPoint(int x, int y)
slouken@2901
  2079
{
slouken@2901
  2080
    SDL_Renderer *renderer;
slouken@2901
  2081
    SDL_Window *window;
slouken@2901
  2082
slouken@2901
  2083
    if (!_this) {
slouken@2901
  2084
        SDL_UninitializedVideo();
slouken@2901
  2085
        return -1;
slouken@2901
  2086
    }
slouken@2901
  2087
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2901
  2088
    if (!renderer) {
slouken@2901
  2089
        return -1;
slouken@2901
  2090
    }
slouken@2901
  2091
    if (!renderer->RenderPoint) {
slouken@2901
  2092
        SDL_Unsupported();
slouken@2901
  2093
        return -1;
slouken@2901
  2094
    }
slouken@2901
  2095
    window = SDL_GetWindowFromID(renderer->window);
slouken@2901
  2096
    if (x < 0 || y < 0 || x >= window->w || y >= window->h) {
slouken@2901
  2097
        return 0;
slouken@2901
  2098
    }
slouken@2901
  2099
    return renderer->RenderPoint(renderer, x, y);
slouken@2901
  2100
}
slouken@2901
  2101
slouken@2901
  2102
int
slouken@2901
  2103
SDL_RenderLine(int x1, int y1, int x2, int y2)
slouken@2901
  2104
{
slouken@2901
  2105
    SDL_Renderer *renderer;
slouken@2901
  2106
    SDL_Window *window;
slouken@2901
  2107
    SDL_Rect real_rect;
slouken@2901
  2108
slouken@2901
  2109
    if (x1 == x2 && y1 == y2) {
slouken@2901
  2110
        return SDL_RenderPoint(x1, y1);
slouken@2901
  2111
    }
slouken@2901
  2112
slouken@2901
  2113
    if (!_this) {
slouken@2901
  2114
        SDL_UninitializedVideo();
slouken@2901
  2115
        return -1;
slouken@2901
  2116
    }
slouken@2901
  2117
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2901
  2118
    if (!renderer) {
slouken@2901
  2119
        return -1;
slouken@2901
  2120
    }
slouken@2901
  2121
    if (!renderer->RenderLine) {
slouken@2901
  2122
        SDL_Unsupported();
slouken@2901
  2123
        return -1;
slouken@2901
  2124
    }
slouken@2901
  2125
    window = SDL_GetWindowFromID(renderer->window);
slouken@2909
  2126
slouken@2901
  2127
    real_rect.x = 0;
slouken@2901
  2128
    real_rect.y = 0;
slouken@2901
  2129
    real_rect.w = window->w;
slouken@2901
  2130
    real_rect.h = window->h;
slouken@2910
  2131
    if (!SDL_IntersectRectAndLine(&real_rect, &x1, &y1, &x2, &y2)) {
slouken@2909
  2132
        return (0);
slouken@2901
  2133
    }
slouken@2901
  2134
    return renderer->RenderLine(renderer, x1, y1, x2, y2);
slouken@2901
  2135
}
slouken@2884
  2136
slouken@2884
  2137
int
slouken@2884
  2138
SDL_RenderFill(const SDL_Rect * rect)
slouken@1895
  2139
{
slouken@2753
  2140
    SDL_Renderer *renderer;
slouken@2753
  2141
    SDL_Window *window;
slouken@2753
  2142
    SDL_Rect real_rect;
slouken@1895
  2143
slouken@2753
  2144
    if (!_this) {
slouken@2753
  2145
        SDL_UninitializedVideo();
slouken@2753
  2146
        return -1;
slouken@2753
  2147
    }
slouken@2753
  2148
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2810
  2149
    if (!renderer) {
slouken@2810
  2150
        return -1;
slouken@2810
  2151
    }
slouken@2814
  2152
    if (!renderer->RenderFill) {
slouken@2810
  2153
        SDL_Unsupported();
slouken@2753
  2154
        return -1;
slouken@2753
  2155
    }
slouken@2753
  2156
    window = SDL_GetWindowFromID(renderer->window);
slouken@2908
  2157
slouken@2753
  2158
    real_rect.x = 0;
slouken@2753
  2159
    real_rect.y = 0;
slouken@2753
  2160
    real_rect.w = window->w;
slouken@2753
  2161
    real_rect.h = window->h;
slouken@2753
  2162
    if (rect) {
slouken@2753
  2163
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@2753
  2164
            return 0;
slouken@2753
  2165
        }
slouken@2753
  2166
    }
slouken@2884
  2167
    return renderer->RenderFill(renderer, &real_rect);
slouken@2884
  2168
}
slouken@2884
  2169
slouken@2884
  2170
int
slouken@1895
  2171
SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
slouken@2753
  2172
               const SDL_Rect * dstrect)
slouken@1895
  2173
{
slouken@2753
  2174
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@2753
  2175
    SDL_Renderer *renderer;
slouken@2753
  2176
    SDL_Window *window;
slouken@2753
  2177
    SDL_Rect real_srcrect;
slouken@2753
  2178
    SDL_Rect real_dstrect;
slouken@1895
  2179
slouken@2753
  2180
    if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
slouken@2753
  2181
        return -1;
slouken@2753
  2182
    }
slouken@2753
  2183
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2810
  2184
    if (!renderer) {
slouken@2810
  2185
        return -1;
slouken@2810
  2186
    }
slouken@2810
  2187
    if (!renderer->RenderCopy) {
slouken@2810
  2188
        SDL_Unsupported();
slouken@2753
  2189
        return -1;
slouken@2753
  2190
    }
slouken@2753
  2191
    window = SDL_GetWindowFromID(renderer->window);
slouken@2908
  2192
slouken@2908
  2193
    real_srcrect.x = 0;
slouken@2908
  2194
    real_srcrect.y = 0;
slouken@2908
  2195
    real_srcrect.w = texture->w;
slouken@2908
  2196
    real_srcrect.h = texture->h;
slouken@2753
  2197
    if (srcrect) {
slouken@2908
  2198
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@2908
  2199
            return 0;
slouken@2908
  2200
        }
slouken@2753
  2201
    }
slouken@2908
  2202
slouken@2908
  2203
    real_dstrect.x = 0;
slouken@2908
  2204
    real_dstrect.y = 0;
slouken@2908
  2205
    real_dstrect.w = window->w;
slouken@2908
  2206
    real_dstrect.h = window->h;
slouken@2753
  2207
    if (dstrect) {
slouken@2908
  2208
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@2908
  2209
            return 0;
slouken@2908
  2210
        }
slouken@2912
  2211
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@2912
  2212
        if (dstrect->w != real_dstrect.w) {
slouken@2912
  2213
            int deltax = (real_dstrect.x - dstrect->x);
slouken@2912
  2214
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@2912
  2215
            real_srcrect.x += (deltax * dstrect->w) / real_srcrect.w;
slouken@2912
  2216
            real_srcrect.w += (deltaw * dstrect->w) / real_srcrect.w;
slouken@2912
  2217
        }
slouken@2912
  2218
        if (dstrect->h != real_dstrect.h) {
slouken@2912
  2219
            int deltay = (real_dstrect.y - dstrect->y);
slouken@2912
  2220
            int deltah = (real_dstrect.h - dstrect->h);
slouken@2912
  2221
            real_srcrect.y += (deltay * dstrect->h) / real_srcrect.h;
slouken@2912
  2222
            real_srcrect.h += (deltah * dstrect->h) / real_srcrect.h;
slouken@2912
  2223
        }
slouken@2753
  2224
    }
slouken@1895
  2225
slouken@2753
  2226
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@2753
  2227
                                &real_dstrect);
slouken@1895
  2228
}
slouken@1895
  2229
slouken@1895
  2230
void
slouken@1895
  2231
SDL_RenderPresent(void)
slouken@1895
  2232
{
slouken@2753
  2233
    SDL_Renderer *renderer;
slouken@1895
  2234
slouken@2753
  2235
    if (!_this) {
slouken@2753
  2236
        SDL_UninitializedVideo();
slouken@2753
  2237
        return;
slouken@2753
  2238
    }
slouken@2753
  2239
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@2753
  2240
    if (!renderer || !renderer->RenderPresent) {
slouken@2753
  2241
        return;
slouken@2753
  2242
    }
slouken@2753
  2243
    renderer->RenderPresent(renderer);
slouken@1895
  2244
}
slouken@1895
  2245
slouken@1895
  2246
void
slouken@1895
  2247
SDL_DestroyTexture(SDL_TextureID textureID)
slouken@1895
  2248
{
slouken@2753
  2249
    int hash;
slouken@2753
  2250
    SDL_Texture *prev, *texture;
slouken@2753
  2251
    SDL_Renderer *renderer;
slouken@1895
  2252
slouken@2753
  2253
    if (!_this) {
slouken@2753
  2254
        SDL_UninitializedVideo();
slouken@2753
  2255
        return;
slouken@2753
  2256
    }
slouken@2753
  2257
    /* Look up the texture in the hash table */
slouken@2753
  2258
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@2753
  2259
    prev = NULL;
slouken@2753
  2260
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@2753
  2261
         prev = texture, texture = texture->next) {
slouken@2753
  2262
        if (texture->id == textureID) {
slouken@2753
  2263
            break;
slouken@2753
  2264
        }
slouken@2753
  2265
    }
slouken@2753
  2266
    if (!texture) {
slouken@2753
  2267
        return;
slouken@2753
  2268
    }
slouken@2753
  2269
    /* Unlink the texture from the list */
slouken@2753
  2270
    if (prev) {
slouken@2753
  2271
        prev->next = texture->next;
slouken@2753
  2272
    } else {
slouken@2753
  2273
        SDL_CurrentDisplay.textures[hash] = texture->next;
slouken@2753
  2274
    }
slouken@1895
  2275
slouken@2753
  2276
    /* Free the texture */
slouken@2753
  2277
    renderer = texture->renderer;
slouken@2753
  2278
    renderer->DestroyTexture(renderer, texture);
slouken@2753
  2279
    SDL_free(texture);
slouken@1895
  2280
}
slouken@1895
  2281
slouken@1895
  2282
void
slouken@1895
  2283
SDL_DestroyRenderer(SDL_WindowID windowID)
slouken@1895
  2284
{
slouken@2753
  2285
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2753
  2286
    SDL_Renderer *renderer;
slouken@2753
  2287
    int i;
slouken@1895
  2288
slouken@2753
  2289
    if (!window) {
slouken@2753
  2290
        return;
slouken@2753
  2291
    }
slouken@2753
  2292
    renderer = window->renderer;
slouken@2753
  2293
    if (!renderer) {
slouken@2753
  2294
        return;
slouken@2753
  2295
    }
slouken@2753
  2296
    /* Free existing textures for this renderer */
slouken@2753
  2297
    for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
slouken@2753
  2298
        SDL_Texture *texture;
slouken@2753
  2299
        SDL_Texture *prev = NULL;
slouken@2753
  2300
        SDL_Texture *next;
slouken@2753
  2301
        for (texture = SDL_CurrentDisplay.textures[i]; texture;
slouken@2753
  2302
             texture = next) {
slouken@2753
  2303
            next = texture->next;
slouken@2753
  2304
            if (texture->renderer == renderer) {
slouken@2753
  2305
                if (prev) {
slouken@2753
  2306
                    prev->next = next;
slouken@2753
  2307
                } else {
slouken@2753
  2308
                    SDL_CurrentDisplay.textures[i] = next;
slouken@2753
  2309
                }
slouken@2753
  2310
                renderer->DestroyTexture(renderer, texture);
slouken@2753
  2311
                SDL_free(texture);
slouken@2753
  2312
            } else {
slouken@2753
  2313
                prev = texture;
slouken@2753
  2314
            }
slouken@2753
  2315
        }
slouken@2753
  2316
    }
slouken@1895
  2317
slouken@2753
  2318
    /* Free the renderer instance */
slouken@2753
  2319
    renderer->DestroyRenderer(renderer);
slouken@1895
  2320
slouken@2753
  2321
    /* Clear references */
slouken@2753
  2322
    window->renderer = NULL;
slouken@2753
  2323
    if (SDL_CurrentDisplay.current_renderer == renderer) {
slouken@2753
  2324
        SDL_CurrentDisplay.current_renderer = NULL;
slouken@2753
  2325
    }
slouken@1895
  2326
}
slouken@1895
  2327
slouken@1895
  2328
void
slouken@1895
  2329
SDL_VideoQuit(void)
slouken@1895
  2330
{
slouken@2753
  2331
    int i, j;
slouken@1895
  2332
slouken@2753
  2333
    if (!_this) {
slouken@2753
  2334
        return;
slouken@2753
  2335
    }
slouken@2753
  2336
    /* Halt event processing before doing anything else */
slouken@2753
  2337
    SDL_StopEventLoop();
slouken@1895
  2338
slouken@2753
  2339
    /* Clean up the system video */
slouken@2753
  2340
    for (i = _this->num_displays; i--;) {
slouken@2753
  2341
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  2342
        for (j = display->num_windows; j--;) {
slouken@2753
  2343
            SDL_DestroyWindow(display->windows[i].id);
slouken@2753
  2344
        }
slouken@2753
  2345
        if (display->windows) {
slouken@2753
  2346
            SDL_free(display->windows);
slouken@2753
  2347
            display->windows = NULL;
slouken@2753
  2348
        }
slouken@2753
  2349
        display->num_windows = 0;
slouken@2753
  2350
        if (display->render_drivers) {
slouken@2753
  2351
            SDL_free(display->render_drivers);
slouken@2753
  2352
            display->render_drivers = NULL;
slouken@2753
  2353
        }
slouken@2753
  2354
        display->num_render_drivers = 0;
slouken@2753
  2355
    }
slouken@2753
  2356
    _this->VideoQuit(_this);
slouken@1895
  2357
slouken@2753
  2358
    for (i = _this->num_displays; i--;) {
slouken@2753
  2359
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  2360
        for (j = display->num_display_modes; j--;) {
slouken@2753
  2361
            if (display->display_modes[j].driverdata) {
slouken@2753
  2362
                SDL_free(display->display_modes[j].driverdata);
slouken@2753
  2363
                display->display_modes[j].driverdata = NULL;
slouken@2753
  2364
            }
slouken@2753
  2365
        }
slouken@2753
  2366
        if (display->display_modes) {
slouken@2753
  2367
            SDL_free(display->display_modes);
slouken@2753
  2368
            display->display_modes = NULL;
slouken@2753
  2369
        }
slouken@2753
  2370
        if (display->desktop_mode.driverdata) {
slouken@2753
  2371
            SDL_free(display->desktop_mode.driverdata);
slouken@2753
  2372
            display->desktop_mode.driverdata = NULL;
slouken@2753
  2373
        }
slouken@2753
  2374
        if (display->palette) {
slouken@2753
  2375
            SDL_FreePalette(display->palette);
slouken@2753
  2376
            display->palette = NULL;
slouken@2753
  2377
        }
slouken@2753
  2378
        if (display->gamma) {
slouken@2753
  2379
            SDL_free(display->gamma);
slouken@2753
  2380
            display->gamma = NULL;
slouken@2753
  2381
        }
slouken@2753
  2382
        if (display->driverdata) {
slouken@2753
  2383
            SDL_free(display->driverdata);
slouken@2753
  2384
            display->driverdata = NULL;
slouken@2753
  2385
        }
slouken@2753
  2386
    }
slouken@2753
  2387
    if (_this->displays) {
slouken@2753
  2388
        SDL_free(_this->displays);
slouken@2753
  2389
        _this->displays = NULL;
slouken@2753
  2390
    }
slouken@2753
  2391
    _this->free(_this);
slouken@2753
  2392
    _this = NULL;
slouken@0
  2393
}
slouken@0
  2394
slouken@1895
  2395
int
slouken@1895
  2396
SDL_GL_LoadLibrary(const char *path)
slouken@0
  2397
{
slouken@2753
  2398
    int retval;
slouken@0
  2399
slouken@2753
  2400
    if (!_this) {
slouken@2753
  2401
        SDL_UninitializedVideo();
slouken@2753
  2402
        return -1;
slouken@2753
  2403
    }
slouken@2753
  2404
    if (_this->GL_LoadLibrary) {
slouken@2753
  2405
        retval = _this->GL_LoadLibrary(_this, path);
slouken@2753
  2406
    } else {
slouken@2753
  2407
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  2408
        retval = -1;
slouken@2753
  2409
    }
slouken@2753
  2410
    return (retval);
slouken@0
  2411
}
slouken@0
  2412
slouken@2753
  2413
void *
slouken@1895
  2414
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  2415
{
slouken@2753
  2416
    void *func;
slouken@0
  2417
slouken@2753
  2418
    if (!_this) {
slouken@2753
  2419
        SDL_UninitializedVideo();
slouken@2753
  2420
        return NULL;
slouken@2753
  2421
    }
slouken@2753
  2422
    func = NULL;
slouken@2753
  2423
    if (_this->GL_GetProcAddress) {
slouken@2753
  2424
        if (_this->gl_config.driver_loaded) {
slouken@2753
  2425
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  2426
        } else {
slouken@2753
  2427
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  2428
        }
slouken@2753
  2429
    } else {
slouken@2753
  2430
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  2431
    }
slouken@2753
  2432
    return func;
slouken@0
  2433
}
slouken@0
  2434
slouken@1926
  2435
SDL_bool
slouken@1926
  2436
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  2437
{
hfutrell@2745
  2438
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  2439
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  2440
    const char *extensions;
slouken@2753
  2441
    const char *start;
slouken@2753
  2442
    const char *where, *terminator;
slouken@1926
  2443
slouken@2753
  2444
    /* Extension names should not have spaces. */
slouken@2753
  2445
    where = SDL_strchr(extension, ' ');
slouken@2753
  2446
    if (where || *extension == '\0') {
slouken@2753
  2447
        return SDL_FALSE;
slouken@2753
  2448
    }
slouken@2753
  2449
    /* See if there's an environment variable override */
slouken@2753
  2450
    start = SDL_getenv(extension);
slouken@2753
  2451
    if (start && *start == '0') {
slouken@2753
  2452
        return SDL_FALSE;
slouken@2753
  2453
    }
slouken@2753
  2454
    /* Lookup the available extensions */
slouken@2753
  2455
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@2753
  2456
    if (glGetStringFunc) {
slouken@2753
  2457
        extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  2458
    } else {
slouken@2753
  2459
        extensions = NULL;
slouken@2753
  2460
    }
slouken@2753
  2461
    if (!extensions) {
slouken@2753
  2462
        return SDL_FALSE;
slouken@2753
  2463
    }
slouken@2753
  2464
    /*
slouken@2753
  2465
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  2466
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  2467
     */
slouken@1926
  2468
slouken@2753
  2469
    start = extensions;
slouken@1926
  2470
slouken@2753
  2471
    for (;;) {
slouken@2753
  2472
        where = SDL_strstr(start, extension);
slouken@2753
  2473
        if (!where)
slouken@2753
  2474
            break;
slouken@1926
  2475
slouken@2753
  2476
        terminator = where + SDL_strlen(extension);
slouken@2753
  2477
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2478
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2479
                return SDL_TRUE;
slouken@1926
  2480
slouken@2753
  2481
        start = terminator;
slouken@2753
  2482
    }
slouken@2753
  2483
    return SDL_FALSE;
slouken@1926
  2484
#else
slouken@2753
  2485
    return SDL_FALSE;
slouken@1926
  2486
#endif
slouken@1926
  2487
}
slouken@1926
  2488
slouken@1895
  2489
int
slouken@1895
  2490
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2491
{
hfutrell@2745
  2492
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  2493
    int retval;
slouken@0
  2494
slouken@2753
  2495
    if (!_this) {
slouken@2753
  2496
        SDL_UninitializedVideo();
slouken@2753
  2497
        return -1;
slouken@2753
  2498
    }
slouken@2753
  2499
    retval = 0;
slouken@2753
  2500
    switch (attr) {
slouken@2753
  2501
    case SDL_GL_RED_SIZE:
slouken@2753
  2502
        _this->gl_config.red_size = value;
slouken@2753
  2503
        break;
slouken@2753
  2504
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2505
        _this->gl_config.green_size = value;
slouken@2753
  2506
        break;
slouken@2753
  2507
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2508
        _this->gl_config.blue_size = value;
slouken@2753
  2509
        break;
slouken@2753
  2510
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2511
        _this->gl_config.alpha_size = value;
slouken@2753
  2512
        break;
slouken@2753
  2513
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2514
        _this->gl_config.double_buffer = value;
slouken@2753
  2515
        break;
slouken@2753
  2516
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2517
        _this->gl_config.buffer_size = value;
slouken@2753
  2518
        break;
slouken@2753
  2519
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2520
        _this->gl_config.depth_size = value;
slouken@2753
  2521
        break;
slouken@2753
  2522
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2523
        _this->gl_config.stencil_size = value;
slouken@2753
  2524
        break;
slouken@2753
  2525
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2526
        _this->gl_config.accum_red_size = value;
slouken@2753
  2527
        break;
slouken@2753
  2528
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2529
        _this->gl_config.accum_green_size = value;
slouken@2753
  2530
        break;
slouken@2753
  2531
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2532
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2533
        break;
slouken@2753
  2534
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2535
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2536
        break;
slouken@2753
  2537
    case SDL_GL_STEREO:
slouken@2753
  2538
        _this->gl_config.stereo = value;
slouken@2753
  2539
        break;
slouken@2753
  2540
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2541
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2542
        break;
slouken@2753
  2543
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2544
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2545
        break;
slouken@2753
  2546
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2547
        _this->gl_config.accelerated = value;
slouken@2753
  2548
        break;
slouken@2753
  2549
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2550
        _this->gl_config.retained_backing = value;
slouken@2753
  2551
        break;
slouken@2753
  2552
    default:
slouken@2753
  2553
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2554
        retval = -1;
slouken@2753
  2555
        break;
slouken@2753
  2556
    }
slouken@2753
  2557
    return retval;
slouken@1936
  2558
#else
slouken@2753
  2559
    SDL_Unsupported();
slouken@2753
  2560
    return -1;
slouken@2753
  2561
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2562
}
slouken@0
  2563
slouken@1895
  2564
int
slouken@1936
  2565
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  2566
{
hfutrell@2745
  2567
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  2568
    void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
slouken@2753
  2569
    GLenum attrib = 0;
slouken@1912
  2570
slouken@2753
  2571
    glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
slouken@2753
  2572
    if (!glGetIntegervFunc) {
slouken@2753
  2573
        return -1;
slouken@2753
  2574
    }
slouken@2753
  2575
    switch (attr) {
slouken@2753
  2576
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2577
        *value = _this->gl_config.retained_backing;
slouken@2753
  2578
        return 0;
slouken@2753
  2579
    case SDL_GL_RED_SIZE:
slouken@2753
  2580
        attrib = GL_RED_BITS;
slouken@2753
  2581
        break;
slouken@2753
  2582
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2583
        attrib = GL_BLUE_BITS;
slouken@2753
  2584
        break;
slouken@2753
  2585
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2586
        attrib = GL_GREEN_BITS;
slouken@2753
  2587
        break;
slouken@2753
  2588
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2589
        attrib = GL_ALPHA_BITS;
slouken@2753
  2590
        break;
slouken@2753
  2591
    case SDL_GL_DOUBLEBUFFER:
hfutrell@2745
  2592
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  2593
        attrib = GL_DOUBLEBUFFER;
slouken@2753
  2594
        break;
hfutrell@2745
  2595
#else
slouken@2753
  2596
        /*
slouken@2753
  2597
         * I believe double buffering is the only option in OpenGL ES
slouken@2753
  2598
         * -- in any case, GL_DOUBLEBUFFER doesn't exist
slouken@2753
  2599
         */
slouken@2753
  2600
        *value = 1;
slouken@2753
  2601
        return 0;
hfutrell@2745
  2602
#endif
slouken@2753
  2603
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2604
        attrib = GL_DEPTH_BITS;
slouken@2753
  2605
        break;
slouken@2753
  2606
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2607
        attrib = GL_STENCIL_BITS;
slouken@2753
  2608
        break;
hfutrell@2745
  2609
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  2610
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2611
        attrib = GL_ACCUM_RED_BITS;
slouken@2753
  2612
        break;
slouken@2753
  2613
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2614
        attrib = GL_ACCUM_GREEN_BITS;
slouken@2753
  2615
        break;
slouken@2753
  2616
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2617
        attrib = GL_ACCUM_BLUE_BITS;
slouken@2753
  2618
        break;
slouken@2753
  2619
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2620
        attrib = GL_ACCUM_ALPHA_BITS;
slouken@2753
  2621
        break;
slouken@2753
  2622
    case SDL_GL_STEREO:
slouken@2753
  2623
        attrib = GL_STEREO;
slouken@2753
  2624
        break;
hfutrell@2745
  2625
#else
slouken@2753
  2626
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2627
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2628
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2629
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2630
    case SDL_GL_STEREO:
slouken@2753
  2631
        /* none of these are supported in OpenGL ES */
slouken@2753
  2632
        *value = 0;
slouken@2753
  2633
        return 0;
hfutrell@2745
  2634
#endif
slouken@2753
  2635
    case SDL_GL_MULTISAMPLEBUFFERS:
hfutrell@2745
  2636
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  2637
        attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  2638
#else
slouken@2753
  2639
        attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  2640
#endif
slouken@2753
  2641
        break;
slouken@2753
  2642
    case SDL_GL_MULTISAMPLESAMPLES:
hfutrell@2745
  2643
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  2644
        attrib = GL_SAMPLES_ARB;
hfutrell@2745
  2645
#else
slouken@2753
  2646
        attrib = GL_SAMPLES;
hfutrell@2745
  2647
#endif
slouken@2753
  2648
        break;
slouken@2753
  2649
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2650
        {
slouken@2753
  2651
            GLint bits = 0;
slouken@2753
  2652
            GLint component;
slouken@1936
  2653
slouken@2753
  2654
            /*
slouken@2753
  2655
             * there doesn't seem to be a single flag in OpenGL
slouken@2753
  2656
             * for this!
slouken@2753
  2657
             */
slouken@2753
  2658
            glGetIntegervFunc(GL_RED_BITS, &component);
slouken@2753
  2659
            bits += component;
slouken@2753
  2660
            glGetIntegervFunc(GL_GREEN_BITS, &component);
slouken@2753
  2661
            bits += component;
slouken@2753
  2662
            glGetIntegervFunc(GL_BLUE_BITS, &component);
slouken@2753
  2663
            bits += component;
slouken@2753
  2664
            glGetIntegervFunc(GL_ALPHA_BITS, &component);
slouken@2753
  2665
            bits += component;
slouken@1936
  2666
slouken@2753
  2667
            *value = bits;
slouken@2753
  2668
            return 0;
slouken@2753
  2669
        }
slouken@2753
  2670
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2671
        {
slouken@2753
  2672
            /* FIXME: How do we get this information? */
slouken@2753
  2673
            *value = (_this->gl_config.accelerated != 0);
slouken@2753
  2674
            return 0;
slouken@2753
  2675
        }
slouken@2753
  2676
    default:
slouken@2753
  2677
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2678
        return -1;
slouken@2753
  2679
    }
slouken@0
  2680
slouken@2753
  2681
    glGetIntegervFunc(attrib, (GLint *) value);
slouken@2753
  2682
    return 0;
slouken@1936
  2683
#else
slouken@2753
  2684
    SDL_Unsupported();
slouken@2753
  2685
    return -1;
slouken@2753
  2686
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2687
}
slouken@0
  2688
slouken@1912
  2689
SDL_GLContext
slouken@1912
  2690
SDL_GL_CreateContext(SDL_WindowID windowID)
slouken@1912
  2691
{
slouken@2753
  2692
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2693
slouken@2753
  2694
    if (!window) {
slouken@2753
  2695
        return NULL;
slouken@2753
  2696
    }
slouken@2753
  2697
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2698
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2699
        return NULL;
slouken@2753
  2700
    }
slouken@2753
  2701
    return _this->GL_CreateContext(_this, window);
slouken@1912
  2702
}
slouken@1912
  2703
slouken@1912
  2704
int
slouken@1912
  2705
SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
slouken@1912
  2706
{
slouken@2753
  2707
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2708
slouken@2753
  2709
    if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2710
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2711
        return -1;
slouken@2753
  2712
    }
slouken@2753
  2713
    if (!context) {
slouken@2753
  2714
        window = NULL;
slouken@2753
  2715
    }
slouken@2753
  2716
    return _this->GL_MakeCurrent(_this, window, context);
slouken@1912
  2717
}
slouken@1912
  2718
slouken@1912
  2719
int
slouken@1912
  2720
SDL_GL_SetSwapInterval(int interval)
slouken@1912
  2721
{
slouken@2753
  2722
    if (!_this) {
slouken@2753
  2723
        SDL_UninitializedVideo();
slouken@2753
  2724
        return -1;
slouken@2753
  2725
    }
slouken@2753
  2726
    if (_this->GL_SetSwapInterval) {
slouken@2753
  2727
        return _this->GL_SetSwapInterval(_this, interval);
slouken@2753
  2728
    } else {
slouken@2753
  2729
        SDL_SetError("Setting the swap interval is not supported");
slouken@2753
  2730
        return -1;
slouken@2753
  2731
    }
slouken@1912
  2732
}
slouken@1912
  2733
slouken@1912
  2734
int
slouken@1912
  2735
SDL_GL_GetSwapInterval(void)
slouken@1912
  2736
{
slouken@2753
  2737
    if (!_this) {
slouken@2753
  2738
        SDL_UninitializedVideo();
slouken@2753
  2739
        return -1;
slouken@2753
  2740
    }
slouken@2753
  2741
    if (_this->GL_GetSwapInterval) {
slouken@2753
  2742
        return _this->GL_GetSwapInterval(_this);
slouken@2753
  2743
    } else {
slouken@2753
  2744
        SDL_SetError("Getting the swap interval is not supported");
slouken@2753
  2745
        return -1;
slouken@2753
  2746
    }
slouken@1912
  2747
}
slouken@1912
  2748
slouken@1895
  2749
void
slouken@1912
  2750
SDL_GL_SwapWindow(SDL_WindowID windowID)
slouken@0
  2751
{
slouken@2753
  2752
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1912
  2753
slouken@2753
  2754
    if (!window) {
slouken@2753
  2755
        return;
slouken@2753
  2756
    }
slouken@2753
  2757
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2758
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2759
        return;
slouken@2753
  2760
    }
slouken@2753
  2761
    _this->GL_SwapWindow(_this, window);
slouken@1912
  2762
}
slouken@1912
  2763
slouken@1912
  2764
void
slouken@1912
  2765
SDL_GL_DeleteContext(SDL_GLContext context)
slouken@1912
  2766
{
slouken@2753
  2767
    if (!_this || !context) {
slouken@2753
  2768
        return;
slouken@2753
  2769
    }
slouken@2753
  2770
    _this->GL_MakeCurrent(_this, NULL, NULL);
slouken@2753
  2771
    _this->GL_DeleteContext(_this, context);
slouken@0
  2772
}
slouken@0
  2773
slouken@1895
  2774
#if 0                           // FIXME
hfutrell@2744
  2775
/*
hfutrell@2744
  2776
 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
hfutrell@2744
  2777
 * & 2 for alpha channel.
hfutrell@2744
  2778
 */
slouken@1895
  2779
static void
slouken@1895
  2780
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  2781
{
slouken@2753
  2782
    int x, y;
slouken@2753
  2783
    Uint32 colorkey;
slouken@0
  2784
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  2785
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  2786
slouken@2753
  2787
    colorkey = icon->format->colorkey;
slouken@2753
  2788
    switch (icon->format->BytesPerPixel) {
slouken@2753
  2789
    case 1:
slouken@2753
  2790
        {
slouken@2753
  2791
            Uint8 *pixels;
slouken@2753
  2792
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2793
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@2753
  2794
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2795
                    if (*pixels++ == colorkey) {
slouken@2753
  2796
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2797
                    }
slouken@2753
  2798
                }
slouken@2753
  2799
            }
slouken@2753
  2800
        }
slouken@2753
  2801
        break;
slouken@0
  2802
slouken@2753
  2803
    case 2:
slouken@2753
  2804
        {
slouken@2753
  2805
            Uint16 *pixels;
slouken@2753
  2806
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2807
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@2753
  2808
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2809
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2810
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2811
                    } else if ((flags & 2)
slouken@2753
  2812
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2813
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2814
                    }
slouken@2753
  2815
                    pixels++;
slouken@2753
  2816
                }
slouken@2753
  2817
            }
slouken@2753
  2818
        }
slouken@2753
  2819
        break;
slouken@0
  2820
slouken@2753
  2821
    case 4:
slouken@2753
  2822
        {
slouken@2753
  2823
            Uint32 *pixels;
slouken@2753
  2824
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2825
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@2753
  2826
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2827
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2828
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2829
                    } else if ((flags & 2)
slouken@2753
  2830
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2831
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2832
                    }
slouken@2753
  2833
                    pixels++;
slouken@2753
  2834
                }
slouken@2753
  2835
            }
slouken@2753
  2836
        }
slouken@2753
  2837
        break;
slouken@2753
  2838
    }
slouken@0
  2839
}
slouken@0
  2840
slouken@0
  2841
/*
slouken@0
  2842
 * Sets the window manager icon for the display window.
slouken@0
  2843
 */
slouken@1895
  2844
void
slouken@1895
  2845
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  2846
{
slouken@2753
  2847
    if (icon && _this->SetIcon) {
slouken@2753
  2848
        /* Generate a mask if necessary, and create the icon! */
slouken@2753
  2849
        if (mask == NULL) {
slouken@2753
  2850
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@2753
  2851
            int flags = 0;
slouken@2753
  2852
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@2753
  2853
            if (mask == NULL) {
slouken@2753
  2854
                return;
slouken@2753
  2855
            }
slouken@2753
  2856
            SDL_memset(mask, ~0, mask_len);
slouken@2753
  2857
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@2753
  2858
                flags |= 1;
slouken@2753
  2859
            if (icon->flags & SDL_SRCALPHA)
slouken@2753
  2860
                flags |= 2;
slouken@2753
  2861
            if (flags) {
slouken@2753
  2862
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@2753
  2863
            }
slouken@2753
  2864
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2865
            SDL_free(mask);
slouken@2753
  2866
        } else {
slouken@2753
  2867
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2868
        }
slouken@2753
  2869
    }
slouken@1895
  2870
}
slouken@1895
  2871
#endif
slouken@0
  2872
slouken@1895
  2873
SDL_bool
slouken@2753
  2874
SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
slouken@1895
  2875
{
slouken@2753
  2876
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1895
  2877
slouken@2753
  2878
    if (!window || !_this->GetWindowWMInfo) {
slouken@2753
  2879
        return SDL_FALSE;
slouken@2753
  2880
    }
slouken@2753
  2881
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  2882
}
slouken@0
  2883
slouken@1895
  2884
/* vi: set ts=4 sw=4 expandtab: */