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