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