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