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