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