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