src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 24 Mar 2009 10:33:12 +0000
changeset 3099 82e60908fab1
parent 3092 cad1aefa2ed9
child 3100 7dc982143c06
permissions -rw-r--r--
Date: Mon, 23 Mar 2009 09:17:24 +0200
From: "Mike Gorchak"
Subject: New QNX patches

Please apply patch qnx4.diff, which is attached. What has been done:
1)Added back OpenGL ES renderer for QNX target. Added few corrections to
OpenGL ES renderer to let it work under QNX. OpenGL ES renderer do not
support textures under QNX, so I think some additional work must be done.
2) Added GL_OES_query_matrix extension to SDL_opengles.h header file, which
required by OpenGL ES 1.1 specification.
3) Added attribute clearing at the entrance of function
SDL_GL_GetAttribure(). Added error checking into the function
SDL_GL_GetAttribure(), because some attributes can't be obtained in OpenGL
ES 1.0.
4) Porting testdyngles to OpenGL ES 1.0 (1.1 has glColor4ub() and
glColor4f() functions, but 1.0 has glColor4f() only).
5) Added error checking after obtaining attributes using
SDL_GL_GetAttribute() function to the testgl2 and testgles.
6) Small correction to testmultiaudio with printing errors.
7) Added software and accelerated OpenGL ES 1.0 support into the QNX GF
driver.

Please remove ./src/audio/nto directory - it will not be used anymore.
Please create ./src/audio/qsa directory and add content of the archive
qsa.tar.gz into this directory. I rewrote some sound code, added support for
multiple audio cards, enumeration, etc. Added initial support for capture.

As far as I can understand SDL 1.3 is not supporting audio capture right now
? Sam, Am I right ? Or audio capture must be supported through the
PlayDevice routine ?

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