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