src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 10:13:44 +0000
changeset 3315 87ffdad11607
parent 3283 1ed5d432e468
child 3316 9cf18687b761
permissions -rw-r--r--
Fixed bug #761

Mason Wheeler 2009-07-05 09:28:33 PDT

This patch fixes two issues with SDL_CreateTextureFromSurface.

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