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