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