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