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