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