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