src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Jun 2006 04:37:45 +0000
branchSDL-1.3
changeset 1677 5e4c5e095925
parent 1676 e136f3ffdc1b
child 1678 90bf530ced8e
permissions -rw-r--r--
Added new style dummy renderer
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@1676
    30
#include "SDL_renderer_sw.h"
slouken@1361
    31
#include "../events/SDL_sysevents.h"
slouken@1361
    32
#include "../events/SDL_events_c.h"
slouken@0
    33
slouken@0
    34
/* Available video drivers */
slouken@0
    35
static VideoBootStrap *bootstrap[] = {
slouken@1361
    36
#if SDL_VIDEO_DRIVER_QUARTZ
slouken@1662
    37
    &QZ_bootstrap,
icculus@1133
    38
#endif
slouken@1361
    39
#if SDL_VIDEO_DRIVER_X11
slouken@1662
    40
    &X11_bootstrap,
slouken@0
    41
#endif
slouken@1361
    42
#if SDL_VIDEO_DRIVER_DGA
slouken@1662
    43
    &DGA_bootstrap,
slouken@0
    44
#endif
slouken@1361
    45
#if SDL_VIDEO_DRIVER_NANOX
slouken@1662
    46
    &NX_bootstrap,
slouken@30
    47
#endif
slouken@1361
    48
#if SDL_VIDEO_DRIVER_IPOD
slouken@1662
    49
    &iPod_bootstrap,
icculus@1140
    50
#endif
slouken@1361
    51
#if SDL_VIDEO_DRIVER_QTOPIA
slouken@1662
    52
    &Qtopia_bootstrap,
slouken@567
    53
#endif
slouken@1361
    54
#if SDL_VIDEO_DRIVER_WSCONS
slouken@1662
    55
    &WSCONS_bootstrap,
icculus@1187
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_FBCON
slouken@1662
    58
    &FBCON_bootstrap,
slouken@0
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@1662
    61
    &DirectFB_bootstrap,
slouken@167
    62
#endif
slouken@1361
    63
#if SDL_VIDEO_DRIVER_PS2GS
slouken@1662
    64
    &PS2GS_bootstrap,
slouken@0
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_GGI
slouken@1662
    67
    &GGI_bootstrap,
slouken@0
    68
#endif
slouken@1361
    69
#if SDL_VIDEO_DRIVER_VGL
slouken@1662
    70
    &VGL_bootstrap,
slouken@75
    71
#endif
slouken@1361
    72
#if SDL_VIDEO_DRIVER_SVGALIB
slouken@1662
    73
    &SVGALIB_bootstrap,
slouken@0
    74
#endif
slouken@1361
    75
#if SDL_VIDEO_DRIVER_GAPI
slouken@1662
    76
    &GAPI_bootstrap,
slouken@1361
    77
#endif
slouken@1361
    78
#if SDL_VIDEO_DRIVER_WINDIB
slouken@1662
    79
    &WINDIB_bootstrap,
slouken@1361
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_DDRAW
slouken@1662
    82
    &DIRECTX_bootstrap,
slouken@1361
    83
#endif
slouken@1361
    84
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@1662
    85
    &BWINDOW_bootstrap,
slouken@1361
    86
#endif
slouken@1361
    87
#if SDL_VIDEO_DRIVER_TOOLBOX
slouken@1662
    88
    &TOOLBOX_bootstrap,
slouken@1361
    89
#endif
slouken@1361
    90
#if SDL_VIDEO_DRIVER_DRAWSPROCKET
slouken@1662
    91
    &DSp_bootstrap,
slouken@1361
    92
#endif
slouken@1361
    93
#if SDL_VIDEO_DRIVER_CYBERGRAPHICS
slouken@1662
    94
    &CGX_bootstrap,
slouken@1361
    95
#endif
slouken@1361
    96
#if SDL_VIDEO_DRIVER_PHOTON
slouken@1662
    97
    &ph_bootstrap,
slouken@1361
    98
#endif
slouken@1361
    99
#if SDL_VIDEO_DRIVER_EPOC
slouken@1662
   100
    &EPOC_bootstrap,
slouken@1361
   101
#endif
slouken@1361
   102
#if SDL_VIDEO_DRIVER_XBIOS
slouken@1662
   103
    &XBIOS_bootstrap,
slouken@1361
   104
#endif
slouken@1361
   105
#if SDL_VIDEO_DRIVER_GEM
slouken@1662
   106
    &GEM_bootstrap,
slouken@1361
   107
#endif
slouken@1361
   108
#if SDL_VIDEO_DRIVER_PICOGUI
slouken@1662
   109
    &PG_bootstrap,
slouken@1361
   110
#endif
slouken@1361
   111
#if SDL_VIDEO_DRIVER_DC
slouken@1662
   112
    &DC_bootstrap,
slouken@1361
   113
#endif
slouken@1361
   114
#if SDL_VIDEO_DRIVER_RISCOS
slouken@1662
   115
    &RISCOS_bootstrap,
slouken@1361
   116
#endif
slouken@1361
   117
#if SDL_VIDEO_DRIVER_OS2FS
slouken@1662
   118
    &OS2FSLib_bootstrap,
slouken@1361
   119
#endif
slouken@1361
   120
#if SDL_VIDEO_DRIVER_AALIB
slouken@1662
   121
    &AALIB_bootstrap,
slouken@0
   122
#endif
slouken@1361
   123
#if SDL_VIDEO_DRIVER_DUMMY
slouken@1662
   124
    &DUMMY_bootstrap,
slouken@610
   125
#endif
slouken@1658
   126
#if SDL_VIDEO_DRIVER_GLSDL
slouken@1662
   127
    &glSDL_bootstrap,
slouken@1658
   128
#endif
slouken@1662
   129
    NULL
slouken@0
   130
};
slouken@173
   131
slouken@1662
   132
static SDL_VideoDevice *_this = NULL;
slouken@0
   133
slouken@0
   134
/* Various local functions */
slouken@1668
   135
int SDL_VideoInit(const char *driver_name, Uint32 flags);
slouken@1668
   136
void SDL_VideoQuit(void);
slouken@0
   137
slouken@1662
   138
static int
slouken@1668
   139
cmpmodes(const void *A, const void *B)
slouken@1662
   140
{
slouken@1662
   141
    SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
slouken@1662
   142
    SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   143
slouken@1662
   144
    if (a.w != b.w) {
slouken@1662
   145
        return b.w - a.w;
slouken@1662
   146
    }
slouken@1662
   147
    if (a.h != b.h) {
slouken@1662
   148
        return b.h - a.h;
slouken@1662
   149
    }
slouken@1668
   150
    if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
slouken@1668
   151
        return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
slouken@1662
   152
    }
slouken@1662
   153
    if (a.refresh_rate != b.refresh_rate) {
slouken@1662
   154
        return b.refresh_rate - a.refresh_rate;
slouken@1662
   155
    }
slouken@1662
   156
    return 0;
slouken@1660
   157
}
slouken@1660
   158
slouken@1662
   159
int
slouken@1668
   160
SDL_GetNumVideoDrivers(void)
slouken@1660
   161
{
slouken@1668
   162
    return SDL_arraysize(bootstrap) - 1;
slouken@1662
   163
}
slouken@1662
   164
slouken@1662
   165
const char *
slouken@1668
   166
SDL_GetVideoDriver(int index)
slouken@1662
   167
{
slouken@1668
   168
    if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
slouken@1662
   169
        return bootstrap[index]->name;
slouken@1662
   170
    }
slouken@1662
   171
    return NULL;
slouken@1660
   172
}
slouken@0
   173
slouken@0
   174
/*
slouken@0
   175
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   176
 */
slouken@1662
   177
int
slouken@1668
   178
SDL_VideoInit(const char *driver_name, Uint32 flags)
slouken@0
   179
{
slouken@1662
   180
    SDL_VideoDevice *video;
slouken@1662
   181
    int index;
slouken@1662
   182
    int i;
slouken@1662
   183
    int bpp;
slouken@1662
   184
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   185
slouken@1662
   186
    /* Toggle the event thread flags, based on OS requirements */
slouken@0
   187
#if defined(MUST_THREAD_EVENTS)
slouken@1662
   188
    flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   189
#elif defined(CANT_THREAD_EVENTS)
slouken@1662
   190
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@1668
   191
        SDL_SetError("OS doesn't support threaded events");
slouken@1662
   192
        return -1;
slouken@1662
   193
    }
slouken@0
   194
#endif
slouken@0
   195
slouken@1662
   196
    /* Check to make sure we don't overwrite '_this' */
slouken@1662
   197
    if (_this != NULL) {
slouken@1668
   198
        SDL_VideoQuit();
slouken@1662
   199
    }
slouken@0
   200
slouken@1662
   201
    /* Select the proper video driver */
slouken@1662
   202
    index = 0;
slouken@1662
   203
    video = NULL;
slouken@1662
   204
    if (driver_name != NULL) {
slouken@1662
   205
        for (i = 0; bootstrap[i]; ++i) {
slouken@1668
   206
            if (SDL_strncmp(bootstrap[i]->name, driver_name,
slouken@1668
   207
                            SDL_strlen(bootstrap[i]->name)) == 0) {
slouken@1668
   208
                if (bootstrap[i]->available()) {
slouken@1668
   209
                    video = bootstrap[i]->create(index);
slouken@1662
   210
                }
slouken@1662
   211
                break;
slouken@1662
   212
            }
slouken@1662
   213
        }
slouken@1662
   214
    } else {
slouken@1662
   215
        for (i = 0; bootstrap[i]; ++i) {
slouken@1668
   216
            if (bootstrap[i]->available()) {
slouken@1668
   217
                video = bootstrap[i]->create(index);
slouken@1662
   218
                if (video != NULL) {
slouken@1662
   219
                    break;
slouken@1662
   220
                }
slouken@1662
   221
            }
slouken@1662
   222
        }
slouken@1662
   223
    }
slouken@1662
   224
    if (video == NULL) {
slouken@1662
   225
        if (driver_name) {
slouken@1668
   226
            SDL_SetError("%s not available", driver_name);
slouken@1662
   227
        } else {
slouken@1668
   228
            SDL_SetError("No available video device");
slouken@1662
   229
        }
slouken@1662
   230
        return -1;
slouken@1662
   231
    }
slouken@1662
   232
    _this = video;
slouken@1662
   233
    _this->name = bootstrap[i]->name;
slouken@1670
   234
    _this->next_object_id = 1;
slouken@0
   235
slouken@1669
   236
slouken@1662
   237
    /* Set some very sane GL defaults */
slouken@1662
   238
    _this->gl_config.driver_loaded = 0;
slouken@1662
   239
    _this->gl_config.dll_handle = NULL;
slouken@1662
   240
    _this->gl_config.red_size = 3;
slouken@1662
   241
    _this->gl_config.green_size = 3;
slouken@1662
   242
    _this->gl_config.blue_size = 2;
slouken@1662
   243
    _this->gl_config.alpha_size = 0;
slouken@1662
   244
    _this->gl_config.buffer_size = 0;
slouken@1662
   245
    _this->gl_config.depth_size = 16;
slouken@1662
   246
    _this->gl_config.stencil_size = 0;
slouken@1662
   247
    _this->gl_config.double_buffer = 1;
slouken@1662
   248
    _this->gl_config.accum_red_size = 0;
slouken@1662
   249
    _this->gl_config.accum_green_size = 0;
slouken@1662
   250
    _this->gl_config.accum_blue_size = 0;
slouken@1662
   251
    _this->gl_config.accum_alpha_size = 0;
slouken@1662
   252
    _this->gl_config.stereo = 0;
slouken@1662
   253
    _this->gl_config.multisamplebuffers = 0;
slouken@1662
   254
    _this->gl_config.multisamplesamples = 0;
slouken@1662
   255
    _this->gl_config.accelerated = -1;  /* not known, don't set */
slouken@1662
   256
    _this->gl_config.swap_control = -1; /* not known, don't set */
slouken@0
   257
slouken@1662
   258
    /* Initialize the video subsystem */
slouken@1668
   259
    if (_this->VideoInit(_this) < 0) {
slouken@1668
   260
        SDL_VideoQuit();
slouken@1662
   261
        return -1;
slouken@1662
   262
    }
slouken@0
   263
slouken@1662
   264
    /* Make sure some displays were added */
slouken@1662
   265
    if (_this->num_displays == 0) {
slouken@1668
   266
        SDL_SetError("The video driver did not add any displays");
slouken@1668
   267
        SDL_VideoQuit();
slouken@1662
   268
        return (-1);
slouken@1662
   269
    }
slouken@0
   270
slouken@1662
   271
    /* Sort the video modes */
slouken@1662
   272
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1668
   273
        SDL_qsort(_this->displays[i].display_modes,
slouken@1668
   274
                  _this->displays[i].num_display_modes,
slouken@1668
   275
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@1662
   276
    }
slouken@0
   277
slouken@1676
   278
    /* The software renderer is always available */
slouken@1676
   279
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1676
   280
        if (_this->displays[i].num_render_drivers > 0) {
slouken@1676
   281
            SDL_AddRenderDriver(i, &SDL_SW_RenderDriver);
slouken@1676
   282
        }
slouken@1676
   283
    }
slouken@1676
   284
slouken@1662
   285
    /* Start the event loop */
slouken@1668
   286
    if (SDL_StartEventLoop(flags) < 0) {
slouken@1668
   287
        SDL_VideoQuit();
slouken@1662
   288
        return -1;
slouken@1662
   289
    }
slouken@0
   290
slouken@1662
   291
    /* We're ready to go! */
slouken@1662
   292
    return 0;
slouken@0
   293
}
slouken@0
   294
slouken@1662
   295
const char *
slouken@1668
   296
SDL_GetCurrentVideoDriver()
slouken@0
   297
{
slouken@1662
   298
    if (!_this) {
slouken@1662
   299
        return NULL;
slouken@1662
   300
    }
slouken@1662
   301
    return _this->name;
slouken@0
   302
}
slouken@0
   303
slouken@1662
   304
SDL_VideoDevice *
slouken@1668
   305
SDL_GetVideoDevice()
slouken@0
   306
{
slouken@1662
   307
    return _this;
slouken@0
   308
}
slouken@0
   309
slouken@1677
   310
int
slouken@1668
   311
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   312
{
slouken@1662
   313
    SDL_VideoDisplay display;
slouken@0
   314
slouken@1668
   315
    SDL_zero(display);
slouken@1662
   316
    if (desktop_mode) {
slouken@1662
   317
        display.desktop_mode = *desktop_mode;
slouken@1662
   318
    }
slouken@1662
   319
    display.current_mode = display.desktop_mode;
slouken@1662
   320
slouken@1677
   321
    return SDL_AddVideoDisplay(&display);
slouken@0
   322
}
slouken@0
   323
slouken@1677
   324
int
slouken@1669
   325
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@1662
   326
{
slouken@1662
   327
    SDL_VideoDisplay *displays;
slouken@1677
   328
    int index = -1;
slouken@0
   329
slouken@1662
   330
    displays =
slouken@1668
   331
        SDL_realloc(_this->displays,
slouken@1668
   332
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@1662
   333
    if (displays) {
slouken@1677
   334
        index = _this->num_displays++;
slouken@1677
   335
        displays[index] = *display;
slouken@1662
   336
        _this->displays = displays;
slouken@1677
   337
    } else {
slouken@1677
   338
        SDL_OutOfMemory();
slouken@1662
   339
    }
slouken@1677
   340
    return index;
slouken@0
   341
}
slouken@0
   342
slouken@1662
   343
int
slouken@1668
   344
SDL_GetNumVideoDisplays(void)
slouken@0
   345
{
slouken@1662
   346
    if (!_this) {
slouken@1662
   347
        return 0;
slouken@1662
   348
    }
slouken@1662
   349
    return _this->num_displays;
slouken@0
   350
}
slouken@0
   351
slouken@1662
   352
int
slouken@1668
   353
SDL_SelectVideoDisplay(int index)
slouken@0
   354
{
slouken@1662
   355
    if (!_this) {
slouken@1668
   356
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   357
        return (-1);
slouken@1662
   358
    }
slouken@1662
   359
    if (index >= 0) {
slouken@1662
   360
        if (index >= _this->num_displays) {
slouken@1668
   361
            SDL_SetError("index must be in the range 0 - %d",
slouken@1668
   362
                         _this->num_displays - 1);
slouken@1662
   363
            return -1;
slouken@1662
   364
        }
slouken@1662
   365
        _this->current_display = index;
slouken@1662
   366
    }
slouken@1662
   367
    return _this->current_display;
slouken@0
   368
}
slouken@0
   369
slouken@1662
   370
void
slouken@1670
   371
SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
slouken@0
   372
{
slouken@1670
   373
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@1662
   374
    SDL_DisplayMode *modes;
slouken@1662
   375
    int i, nmodes;
slouken@0
   376
slouken@1662
   377
    /* Make sure we don't already have the mode in the list */
slouken@1670
   378
    modes = display->display_modes;
slouken@1670
   379
    nmodes = display->num_display_modes;
slouken@1662
   380
    for (i = 0; i < nmodes; ++i) {
slouken@1668
   381
        if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
slouken@1662
   382
            return;
slouken@1662
   383
        }
slouken@1662
   384
    }
slouken@0
   385
slouken@1662
   386
    /* Go ahead and add the new mode */
slouken@1668
   387
    modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
slouken@1662
   388
    if (modes) {
slouken@1670
   389
        display->display_modes = modes;
slouken@1662
   390
        modes[nmodes] = *mode;
slouken@1670
   391
        display->num_display_modes++;
slouken@1662
   392
    }
slouken@0
   393
}
slouken@0
   394
slouken@1662
   395
int
slouken@1668
   396
SDL_GetNumDisplayModes()
slouken@1662
   397
{
slouken@1662
   398
    if (_this) {
slouken@1662
   399
        return SDL_CurrentDisplay.num_display_modes;
slouken@1662
   400
    }
slouken@1662
   401
    return 0;
slouken@1662
   402
}
slouken@910
   403
slouken@1662
   404
const SDL_DisplayMode *
slouken@1668
   405
SDL_GetDisplayMode(int index)
slouken@0
   406
{
slouken@1668
   407
    if (index < 0 || index >= SDL_GetNumDisplayModes()) {
slouken@1668
   408
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1670
   409
                     SDL_GetNumDisplayModes() - 1);
slouken@1662
   410
        return NULL;
slouken@1662
   411
    }
slouken@1662
   412
    return &SDL_CurrentDisplay.display_modes[index];
slouken@1662
   413
}
slouken@0
   414
slouken@1662
   415
const SDL_DisplayMode *
slouken@1668
   416
SDL_GetDesktopDisplayMode(void)
slouken@1662
   417
{
slouken@1662
   418
    if (_this) {
slouken@1662
   419
        return &SDL_CurrentDisplay.desktop_mode;
slouken@1662
   420
    }
slouken@1662
   421
    return NULL;
slouken@1662
   422
}
slouken@0
   423
slouken@1662
   424
const SDL_DisplayMode *
slouken@1668
   425
SDL_GetCurrentDisplayMode(void)
slouken@1662
   426
{
slouken@1662
   427
    if (_this) {
slouken@1662
   428
        return &SDL_CurrentDisplay.current_mode;
slouken@1662
   429
    }
slouken@1662
   430
    return NULL;
slouken@1662
   431
}
slouken@0
   432
slouken@1662
   433
SDL_DisplayMode *
slouken@1668
   434
SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
slouken@1668
   435
                          SDL_DisplayMode * closest)
slouken@1662
   436
{
slouken@1662
   437
    Uint32 target_format;
slouken@1662
   438
    int target_refresh_rate;
slouken@1662
   439
    int i;
slouken@1662
   440
    SDL_DisplayMode *current, *match;
slouken@0
   441
slouken@1662
   442
    if (!_this || !mode || !closest) {
slouken@1662
   443
        return NULL;
slouken@1662
   444
    }
slouken@0
   445
slouken@1662
   446
    /* Default to the desktop format */
slouken@1662
   447
    if (mode->format) {
slouken@1662
   448
        target_format = mode->format;
slouken@1662
   449
    } else {
slouken@1662
   450
        target_format = SDL_CurrentDisplay.desktop_mode.format;
slouken@1662
   451
    }
slouken@0
   452
slouken@1662
   453
    /* Default to the desktop refresh rate */
slouken@1662
   454
    if (mode->refresh_rate) {
slouken@1662
   455
        target_refresh_rate = mode->refresh_rate;
slouken@1662
   456
    } else {
slouken@1662
   457
        target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
slouken@1662
   458
    }
slouken@14
   459
slouken@1662
   460
    match = NULL;
slouken@1662
   461
    for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
slouken@1662
   462
        current = &SDL_CurrentDisplay.display_modes[i];
slouken@0
   463
slouken@1662
   464
        if ((current->w && current->h) &&
slouken@1662
   465
            (current->w < mode->w || current->h < mode->h)) {
slouken@1662
   466
            /* Out of sorted modes large enough here */
slouken@1662
   467
            break;
slouken@1662
   468
        }
slouken@1662
   469
        if (!match || current->w < match->w || current->h < match->h) {
slouken@1662
   470
            match = current;
slouken@1662
   471
            continue;
slouken@1662
   472
        }
slouken@1662
   473
        if (current->format != match->format) {
slouken@1662
   474
            /* Sorted highest depth to lowest */
slouken@1662
   475
            if (current->format == target_format ||
slouken@1668
   476
                (SDL_BITSPERPIXEL(current->format) >=
slouken@1668
   477
                 SDL_BITSPERPIXEL(target_format)
slouken@1668
   478
                 && SDL_PIXELTYPE(current->format) ==
slouken@1668
   479
                 SDL_PIXELTYPE(target_format))) {
slouken@1662
   480
                match = current;
slouken@1662
   481
            }
slouken@1662
   482
            continue;
slouken@1662
   483
        }
slouken@1662
   484
        if (current->refresh_rate != match->refresh_rate) {
slouken@1662
   485
            /* Sorted highest refresh to lowest */
slouken@1662
   486
            if (current->refresh_rate >= target_refresh_rate) {
slouken@1662
   487
                match = current;
slouken@1662
   488
            }
slouken@1662
   489
        }
slouken@1662
   490
    }
slouken@1662
   491
    if (match) {
slouken@1662
   492
        if (match->format) {
slouken@1662
   493
            closest->format = match->format;
slouken@1662
   494
        } else {
slouken@1662
   495
            closest->format = mode->format;
slouken@1662
   496
        }
slouken@1662
   497
        if (match->w && match->h) {
slouken@1662
   498
            closest->w = match->w;
slouken@1662
   499
            closest->h = match->h;
slouken@1662
   500
        } else {
slouken@1662
   501
            closest->w = mode->w;
slouken@1662
   502
            closest->h = mode->h;
slouken@1662
   503
        }
slouken@1662
   504
        if (match->refresh_rate) {
slouken@1662
   505
            closest->refresh_rate = match->refresh_rate;
slouken@1662
   506
        } else {
slouken@1662
   507
            closest->refresh_rate = mode->refresh_rate;
slouken@1662
   508
        }
slouken@1662
   509
        return closest;
slouken@1662
   510
    }
slouken@1662
   511
    return NULL;
slouken@1662
   512
}
slouken@0
   513
slouken@1662
   514
int
slouken@1668
   515
SDL_SetDisplayMode(const SDL_DisplayMode * mode)
slouken@1662
   516
{
slouken@1667
   517
    SDL_VideoDisplay *display;
slouken@1662
   518
    SDL_DisplayMode display_mode;
slouken@1667
   519
    int i;
slouken@229
   520
slouken@1662
   521
    if (!_this) {
slouken@1668
   522
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   523
        return -1;
slouken@1662
   524
    }
slouken@0
   525
slouken@1662
   526
    /* Make sure there's an actual display mode to set */
slouken@1662
   527
    if (!mode) {
slouken@1668
   528
        SDL_SetError("No mode passed to SDL_SetDisplayMode");
slouken@1662
   529
        return -1;
slouken@1662
   530
    }
slouken@1667
   531
    display = &SDL_CurrentDisplay;
slouken@1662
   532
    display_mode = *mode;
slouken@0
   533
slouken@1662
   534
    /* Default to the current mode */
slouken@1662
   535
    if (!display_mode.format) {
slouken@1667
   536
        display_mode.format = display->current_mode.format;
slouken@1662
   537
    }
slouken@1662
   538
    if (!display_mode.w) {
slouken@1667
   539
        display_mode.w = display->current_mode.w;
slouken@1662
   540
    }
slouken@1662
   541
    if (!display_mode.h) {
slouken@1667
   542
        display_mode.h = display->current_mode.h;
slouken@1662
   543
    }
slouken@1662
   544
    if (!display_mode.refresh_rate) {
slouken@1667
   545
        display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@1662
   546
    }
slouken@0
   547
slouken@1662
   548
    /* Get a good video mode, the closest one possible */
slouken@1668
   549
    if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
slouken@1668
   550
        SDL_SetError("No video mode large enough for %dx%d",
slouken@1668
   551
                     display_mode.w, display_mode.h);
slouken@1662
   552
        return -1;
slouken@1662
   553
    }
slouken@0
   554
slouken@1662
   555
    /* See if there's anything left to do */
slouken@1662
   556
    if (SDL_memcmp
slouken@1668
   557
        (&display_mode, SDL_GetCurrentDisplayMode(),
slouken@1668
   558
         sizeof(display_mode)) == 0) {
slouken@1662
   559
        return 0;
slouken@1662
   560
    }
slouken@0
   561
slouken@1668
   562
    return _this->SetDisplayMode(_this, &display_mode);
slouken@1662
   563
}
slouken@0
   564
slouken@1662
   565
SDL_WindowID
slouken@1668
   566
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@1662
   567
{
slouken@1662
   568
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@1662
   569
                                  SDL_WINDOW_BORDERLESS |
slouken@1662
   570
                                  SDL_WINDOW_SHOWN |
slouken@1662
   571
                                  SDL_WINDOW_OPENGL |
slouken@1662
   572
                                  SDL_WINDOW_RESIZABLE |
slouken@1662
   573
                                  SDL_WINDOW_MAXIMIZED |
slouken@1662
   574
                                  SDL_WINDOW_MINIMIZED |
slouken@1662
   575
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@1662
   576
    SDL_Window window;
slouken@1662
   577
    int num_windows;
slouken@1662
   578
    SDL_Window *windows;
slouken@0
   579
slouken@1662
   580
    if (!_this) {
slouken@1668
   581
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   582
        return 0;
slouken@1662
   583
    }
slouken@0
   584
slouken@1668
   585
    SDL_zero(window);
slouken@1670
   586
    window.id = _this->next_object_id++;
slouken@1668
   587
    window.title = title ? SDL_strdup(title) : NULL;
slouken@1662
   588
    window.x = x;
slouken@1662
   589
    window.y = y;
slouken@1662
   590
    window.w = w;
slouken@1662
   591
    window.h = h;
slouken@1662
   592
    window.flags = (flags & allowed_flags);
slouken@1676
   593
    window.display = &SDL_CurrentDisplay;
slouken@910
   594
slouken@1668
   595
    if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
slouken@1667
   596
        if (window.title) {
slouken@1668
   597
            SDL_free(window.title);
slouken@1667
   598
        }
slouken@1662
   599
        return 0;
slouken@1662
   600
    }
slouken@910
   601
slouken@1662
   602
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   603
    windows =
slouken@1668
   604
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   605
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   606
    if (!windows) {
slouken@1662
   607
        if (_this->DestroyWindow) {
slouken@1668
   608
            _this->DestroyWindow(_this, &window);
slouken@1662
   609
        }
slouken@1667
   610
        if (window.title) {
slouken@1668
   611
            SDL_free(window.title);
slouken@1667
   612
        }
slouken@1662
   613
        return 0;
slouken@1662
   614
    }
slouken@1662
   615
    windows[num_windows] = window;
slouken@1662
   616
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   617
    SDL_CurrentDisplay.num_windows++;
slouken@910
   618
slouken@1662
   619
    return window.id;
slouken@1662
   620
}
icculus@650
   621
slouken@1662
   622
SDL_WindowID
slouken@1668
   623
SDL_CreateWindowFrom(void *data)
slouken@1662
   624
{
slouken@1662
   625
    SDL_Window window;
slouken@1662
   626
    int num_windows;
slouken@1662
   627
    SDL_Window *windows;
slouken@0
   628
slouken@1662
   629
    if (!_this) {
slouken@1668
   630
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   631
        return (0);
slouken@1662
   632
    }
slouken@0
   633
slouken@1668
   634
    SDL_zero(window);
slouken@1670
   635
    window.id = _this->next_object_id++;
slouken@1676
   636
    window.display = &SDL_CurrentDisplay;
slouken@1662
   637
slouken@1662
   638
    if (!_this->CreateWindowFrom ||
slouken@1668
   639
        _this->CreateWindowFrom(_this, &window, data) < 0) {
slouken@1662
   640
        return 0;
slouken@1662
   641
    }
slouken@1662
   642
slouken@1662
   643
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   644
    windows =
slouken@1668
   645
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   646
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   647
    if (!windows) {
slouken@1662
   648
        if (_this->DestroyWindow) {
slouken@1668
   649
            _this->DestroyWindow(_this, &window);
slouken@1662
   650
        }
slouken@1667
   651
        if (window.title) {
slouken@1668
   652
            SDL_free(window.title);
slouken@1667
   653
        }
slouken@1662
   654
        return 0;
slouken@1662
   655
    }
slouken@1662
   656
    windows[num_windows] = window;
slouken@1662
   657
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   658
    SDL_CurrentDisplay.num_windows++;
slouken@1662
   659
slouken@1662
   660
    return window.id;
slouken@1662
   661
}
slouken@1662
   662
slouken@1662
   663
static __inline__ SDL_Window *
slouken@1668
   664
SDL_GetWindowFromID(SDL_WindowID windowID)
slouken@1662
   665
{
slouken@1662
   666
    int i, j;
slouken@1662
   667
slouken@1662
   668
    if (!_this) {
slouken@1662
   669
        return NULL;
slouken@1662
   670
    }
slouken@1662
   671
slouken@1662
   672
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   673
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   674
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   675
            SDL_Window *window = &display->windows[j];
slouken@1662
   676
            if (window->id == windowID) {
slouken@1662
   677
                return window;
slouken@1662
   678
            }
slouken@1662
   679
        }
slouken@1662
   680
    }
slouken@1662
   681
    return NULL;
slouken@1662
   682
}
slouken@1662
   683
slouken@1662
   684
Uint32
slouken@1668
   685
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1662
   686
{
slouken@1668
   687
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   688
slouken@1662
   689
    if (!window) {
slouken@1662
   690
        return 0;
slouken@1662
   691
    }
slouken@1662
   692
    return window->flags;
slouken@1662
   693
}
slouken@1662
   694
slouken@1662
   695
void
slouken@1668
   696
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1662
   697
{
slouken@1668
   698
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   699
slouken@1662
   700
    if (!window) {
slouken@1662
   701
        return;
slouken@1662
   702
    }
slouken@1662
   703
    if (window->title) {
slouken@1668
   704
        SDL_free(window->title);
slouken@1662
   705
    }
slouken@1668
   706
    window->title = SDL_strdup(title);
slouken@1662
   707
slouken@1662
   708
    if (_this->SetWindowTitle) {
slouken@1668
   709
        _this->SetWindowTitle(_this, window);
slouken@1662
   710
    }
slouken@1662
   711
}
slouken@1662
   712
slouken@1662
   713
const char *
slouken@1668
   714
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1662
   715
{
slouken@1668
   716
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   717
slouken@1662
   718
    if (!window) {
slouken@1662
   719
        return NULL;
slouken@1662
   720
    }
slouken@1662
   721
    return window->title;
slouken@1662
   722
}
slouken@1662
   723
slouken@1662
   724
void
slouken@1668
   725
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1662
   726
{
slouken@1668
   727
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   728
slouken@1662
   729
    if (!window) {
slouken@1662
   730
        return;
slouken@1662
   731
    }
slouken@1662
   732
    window->userdata = userdata;
slouken@1662
   733
}
slouken@1662
   734
slouken@1662
   735
void *
slouken@1668
   736
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1662
   737
{
slouken@1668
   738
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   739
slouken@1662
   740
    if (!window) {
slouken@1662
   741
        return NULL;
slouken@1662
   742
    }
slouken@1662
   743
    return window->userdata;
slouken@1662
   744
}
slouken@1662
   745
slouken@1662
   746
void
slouken@1668
   747
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1662
   748
{
slouken@1668
   749
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   750
slouken@1662
   751
    if (!window) {
slouken@1662
   752
        return;
slouken@1662
   753
    }
slouken@1662
   754
slouken@1662
   755
    window->x = x;
slouken@1662
   756
    window->y = y;
slouken@1662
   757
slouken@1662
   758
    if (_this->SetWindowPosition) {
slouken@1668
   759
        _this->SetWindowPosition(_this, window);
slouken@1662
   760
    }
slouken@1662
   761
}
slouken@1662
   762
slouken@1662
   763
void
slouken@1668
   764
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1662
   765
{
slouken@1668
   766
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   767
slouken@1662
   768
    if (!window) {
slouken@1662
   769
        return;
slouken@1662
   770
    }
slouken@1662
   771
    if (x) {
slouken@1662
   772
        *x = window->x;
slouken@1662
   773
    }
slouken@1662
   774
    if (y) {
slouken@1662
   775
        *y = window->y;
slouken@1662
   776
    }
slouken@1662
   777
}
slouken@1662
   778
slouken@1662
   779
void
slouken@1668
   780
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1662
   781
{
slouken@1668
   782
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   783
slouken@1662
   784
    if (!window) {
slouken@1662
   785
        return;
slouken@1662
   786
    }
slouken@1662
   787
slouken@1662
   788
    window->w = w;
slouken@1662
   789
    window->h = h;
slouken@1662
   790
slouken@1662
   791
    if (_this->SetWindowSize) {
slouken@1668
   792
        _this->SetWindowSize(_this, window);
slouken@1662
   793
    }
slouken@1662
   794
}
slouken@1662
   795
slouken@1662
   796
void
slouken@1668
   797
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1662
   798
{
slouken@1668
   799
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   800
slouken@1662
   801
    if (!window) {
slouken@1662
   802
        return;
slouken@1662
   803
    }
slouken@1662
   804
    if (w) {
slouken@1662
   805
        *w = window->w;
slouken@1662
   806
    }
slouken@1662
   807
    if (h) {
slouken@1662
   808
        *h = window->h;
slouken@1662
   809
    }
slouken@1662
   810
}
slouken@1662
   811
slouken@1662
   812
void
slouken@1668
   813
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1662
   814
{
slouken@1668
   815
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   816
slouken@1662
   817
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   818
        return;
slouken@1662
   819
    }
slouken@1662
   820
slouken@1662
   821
    window->flags |= SDL_WINDOW_SHOWN;
slouken@1662
   822
slouken@1662
   823
    if (_this->ShowWindow) {
slouken@1668
   824
        _this->ShowWindow(_this, window);
slouken@1662
   825
    }
slouken@1662
   826
}
slouken@1662
   827
slouken@1662
   828
void
slouken@1668
   829
SDL_HideWindow(SDL_WindowID windowID)
slouken@1662
   830
{
slouken@1668
   831
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   832
slouken@1662
   833
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   834
        return;
slouken@1662
   835
    }
slouken@1662
   836
slouken@1662
   837
    window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1662
   838
slouken@1662
   839
    if (_this->HideWindow) {
slouken@1668
   840
        _this->HideWindow(_this, window);
slouken@1662
   841
    }
slouken@1662
   842
}
slouken@1662
   843
slouken@1662
   844
void
slouken@1668
   845
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1662
   846
{
slouken@1668
   847
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   848
slouken@1662
   849
    if (!window) {
slouken@1662
   850
        return;
slouken@1662
   851
    }
slouken@1662
   852
slouken@1662
   853
    if (_this->RaiseWindow) {
slouken@1668
   854
        _this->RaiseWindow(_this, window);
slouken@1662
   855
    }
slouken@1662
   856
}
slouken@1662
   857
slouken@1662
   858
void
slouken@1668
   859
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1662
   860
{
slouken@1668
   861
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   862
slouken@1662
   863
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@1662
   864
        return;
slouken@1662
   865
    }
slouken@1662
   866
slouken@1662
   867
    window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1662
   868
slouken@1662
   869
    if (_this->MaximizeWindow) {
slouken@1668
   870
        _this->MaximizeWindow(_this, window);
slouken@1662
   871
    }
slouken@1662
   872
}
slouken@1662
   873
slouken@1662
   874
void
slouken@1668
   875
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1662
   876
{
slouken@1668
   877
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   878
slouken@1662
   879
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@1662
   880
        return;
slouken@1662
   881
    }
slouken@1662
   882
slouken@1662
   883
    window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1662
   884
slouken@1662
   885
    if (_this->MinimizeWindow) {
slouken@1668
   886
        _this->MinimizeWindow(_this, window);
slouken@1662
   887
    }
slouken@1662
   888
}
slouken@1662
   889
slouken@1662
   890
void
slouken@1668
   891
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1662
   892
{
slouken@1668
   893
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   894
slouken@1662
   895
    if (!window
slouken@1662
   896
        || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@1662
   897
        return;
slouken@1662
   898
    }
slouken@1662
   899
slouken@1662
   900
    window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
slouken@1662
   901
slouken@1662
   902
    if (_this->RestoreWindow) {
slouken@1668
   903
        _this->RestoreWindow(_this, window);
slouken@1662
   904
    }
slouken@1662
   905
}
slouken@1662
   906
slouken@1662
   907
void
slouken@1668
   908
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1662
   909
{
slouken@1668
   910
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   911
slouken@1662
   912
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@1662
   913
        return;
slouken@1662
   914
    }
slouken@1662
   915
slouken@1662
   916
    if (mode) {
slouken@1662
   917
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   918
    } else {
slouken@1662
   919
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   920
    }
slouken@1662
   921
slouken@1662
   922
    if (_this->SetWindowGrab) {
slouken@1668
   923
        _this->SetWindowGrab(_this, window);
slouken@1662
   924
    }
slouken@1662
   925
}
slouken@1662
   926
slouken@1662
   927
int
slouken@1668
   928
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1662
   929
{
slouken@1668
   930
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
   931
slouken@1662
   932
    if (!window) {
slouken@1662
   933
        return 0;
slouken@1662
   934
    }
slouken@1662
   935
slouken@1662
   936
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1662
   937
}
slouken@1662
   938
slouken@1662
   939
void
slouken@1668
   940
SDL_DestroyWindow(SDL_WindowID windowID)
slouken@1662
   941
{
slouken@1662
   942
    int i, j;
slouken@1662
   943
slouken@1662
   944
    if (!_this) {
slouken@1662
   945
        return;
slouken@1662
   946
    }
slouken@1662
   947
slouken@1662
   948
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   949
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   950
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   951
            SDL_Window *window = &display->windows[j];
slouken@1662
   952
            if (window->id != windowID) {
slouken@1662
   953
                continue;
slouken@1662
   954
            }
slouken@1662
   955
            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1662
   956
                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1668
   957
                _this->SetWindowGrab(_this, window);
slouken@1662
   958
            }
slouken@1670
   959
            if (window->renderer) {
slouken@1670
   960
                SDL_DestroyRenderer(window->id);
slouken@1662
   961
            }
slouken@1662
   962
            if (_this->DestroyWindow) {
slouken@1668
   963
                _this->DestroyWindow(_this, window);
slouken@1662
   964
            }
slouken@1662
   965
            if (window->title) {
slouken@1668
   966
                SDL_free(window->title);
slouken@1662
   967
            }
slouken@1662
   968
            if (window->gamma) {
slouken@1668
   969
                SDL_free(window->gamma);
slouken@1662
   970
            }
slouken@1662
   971
            if (j != display->num_windows - 1) {
slouken@1668
   972
                SDL_memcpy(&display->windows[i],
slouken@1668
   973
                           &display->windows[i + 1],
slouken@1668
   974
                           (display->num_windows - i - 1) * sizeof(*window));
slouken@1662
   975
            }
slouken@1662
   976
            --display->num_windows;
slouken@1662
   977
            return;
slouken@1662
   978
        }
slouken@1662
   979
    }
slouken@1662
   980
}
slouken@1662
   981
slouken@1670
   982
void
slouken@1670
   983
SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
slouken@1670
   984
{
slouken@1670
   985
    SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@1670
   986
    SDL_RenderDriver *render_drivers;
slouken@1670
   987
slouken@1670
   988
    render_drivers =
slouken@1670
   989
        SDL_realloc(display->render_drivers,
slouken@1670
   990
                    (display->num_render_drivers +
slouken@1670
   991
                     1) * sizeof(*render_drivers));
slouken@1670
   992
    if (render_drivers) {
slouken@1670
   993
        render_drivers[display->num_render_drivers] = *driver;
slouken@1670
   994
        display->render_drivers = render_drivers;
slouken@1670
   995
        display->num_render_drivers++;
slouken@1670
   996
    }
slouken@1670
   997
}
slouken@1670
   998
slouken@1670
   999
int
slouken@1670
  1000
SDL_GetNumRenderers(void)
slouken@1670
  1001
{
slouken@1670
  1002
    if (_this) {
slouken@1670
  1003
        return SDL_CurrentDisplay.num_render_drivers;
slouken@1670
  1004
    }
slouken@1670
  1005
    return 0;
slouken@1670
  1006
}
slouken@1670
  1007
slouken@1670
  1008
int
slouken@1670
  1009
SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
slouken@1670
  1010
{
slouken@1670
  1011
    if (index < 0 || index >= SDL_GetNumRenderers()) {
slouken@1670
  1012
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1670
  1013
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1014
        return -1;
slouken@1670
  1015
    }
slouken@1670
  1016
    *info = SDL_CurrentDisplay.render_drivers[index].info;
slouken@1670
  1017
    return 0;
slouken@1670
  1018
}
slouken@1670
  1019
slouken@1670
  1020
int
slouken@1670
  1021
SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
slouken@1662
  1022
{
slouken@1668
  1023
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1024
slouken@1662
  1025
    if (!window) {
slouken@1670
  1026
        return 0;
slouken@1670
  1027
    }
slouken@1670
  1028
slouken@1670
  1029
    if (index < 0) {
slouken@1670
  1030
        int n = SDL_GetNumRenderers();
slouken@1670
  1031
        for (index = 0; index < n; ++index) {
slouken@1670
  1032
            if ((SDL_CurrentDisplay.render_drivers[index].info.
slouken@1670
  1033
                 flags & flags) == flags) {
slouken@1670
  1034
                break;
slouken@1670
  1035
            }
slouken@1670
  1036
        }
slouken@1670
  1037
        if (index == n) {
slouken@1670
  1038
            SDL_SetError("Couldn't find matching render driver");
slouken@1670
  1039
            return -1;
slouken@1670
  1040
        }
slouken@1670
  1041
    }
slouken@1670
  1042
slouken@1670
  1043
    if (index >= SDL_GetNumRenderers()) {
slouken@1670
  1044
        SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@1670
  1045
                     SDL_GetNumRenderers() - 1);
slouken@1670
  1046
        return -1;
slouken@1670
  1047
    }
slouken@1670
  1048
slouken@1670
  1049
    /* Free any existing renderer */
slouken@1670
  1050
    SDL_DestroyRenderer(windowID);
slouken@1670
  1051
slouken@1670
  1052
    /* Create a new renderer instance */
slouken@1670
  1053
    window->renderer =
slouken@1670
  1054
        SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
slouken@1670
  1055
                                                                flags);
slouken@1670
  1056
    if (!window->renderer) {
slouken@1670
  1057
        return -1;
slouken@1670
  1058
    }
slouken@1670
  1059
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1060
slouken@1670
  1061
    return 0;
slouken@1670
  1062
}
slouken@1670
  1063
slouken@1670
  1064
int
slouken@1670
  1065
SDL_SelectRenderer(SDL_WindowID windowID)
slouken@1670
  1066
{
slouken@1670
  1067
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1068
slouken@1670
  1069
    if (!window || !window->renderer) {
slouken@1670
  1070
        return -1;
slouken@1670
  1071
    }
slouken@1670
  1072
    SDL_CurrentDisplay.current_renderer = window->renderer;
slouken@1670
  1073
    return 0;
slouken@1670
  1074
}
slouken@1670
  1075
slouken@1670
  1076
SDL_TextureID
slouken@1670
  1077
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1670
  1078
{
slouken@1670
  1079
    SDL_Renderer *renderer;
slouken@1670
  1080
    SDL_Texture *texture;
slouken@1670
  1081
slouken@1670
  1082
    if (!_this) {
slouken@1670
  1083
        return 0;
slouken@1670
  1084
    }
slouken@1670
  1085
slouken@1670
  1086
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1087
    if (!renderer || !renderer->CreateTexture) {
slouken@1670
  1088
        return 0;
slouken@1670
  1089
    }
slouken@1670
  1090
slouken@1670
  1091
    texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
slouken@1670
  1092
    if (!texture) {
slouken@1670
  1093
        SDL_OutOfMemory();
slouken@1670
  1094
        return 0;
slouken@1670
  1095
    }
slouken@1670
  1096
slouken@1670
  1097
    SDL_zerop(texture);
slouken@1670
  1098
    texture->id = _this->next_object_id++;
slouken@1670
  1099
    texture->format = format;
slouken@1670
  1100
    texture->access = access;
slouken@1670
  1101
    texture->w = w;
slouken@1670
  1102
    texture->h = h;
slouken@1670
  1103
    texture->renderer = renderer;
slouken@1670
  1104
slouken@1676
  1105
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1670
  1106
        SDL_free(texture);
slouken@1670
  1107
        return 0;
slouken@1670
  1108
    }
slouken@1670
  1109
}
slouken@1670
  1110
slouken@1670
  1111
SDL_TextureID
slouken@1670
  1112
SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
slouken@1670
  1113
{
slouken@1670
  1114
    SDL_TextureID textureID;
slouken@1670
  1115
    Uint32 surface_flags = surface->flags;
slouken@1670
  1116
    SDL_PixelFormat *fmt = surface->format;
slouken@1670
  1117
    Uint32 colorkey;
slouken@1670
  1118
    Uint8 alpha;
slouken@1670
  1119
    SDL_Rect bounds;
slouken@1670
  1120
    SDL_Surface dst;
slouken@1670
  1121
    int bpp;
slouken@1670
  1122
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
  1123
slouken@1670
  1124
    if (!surface) {
slouken@1670
  1125
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@1670
  1126
        return 0;
slouken@1670
  1127
    }
slouken@1670
  1128
slouken@1670
  1129
    if (format) {
slouken@1670
  1130
        if (!SDL_PixelFormatEnumToMasks
slouken@1670
  1131
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1670
  1132
            SDL_SetError("Unknown pixel format");
slouken@1670
  1133
            return 0;
slouken@1670
  1134
        }
slouken@1670
  1135
    } else {
slouken@1670
  1136
        bpp = fmt->BitsPerPixel;
slouken@1670
  1137
        Rmask = fmt->Rmask;
slouken@1670
  1138
        Gmask = fmt->Gmask;
slouken@1670
  1139
        Bmask = fmt->Bmask;
slouken@1670
  1140
        Amask = fmt->Amask;
slouken@1670
  1141
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1142
        if (!format) {
slouken@1670
  1143
            SDL_SetError("Unknown pixel format");
slouken@1670
  1144
            return 0;
slouken@1670
  1145
        }
slouken@1670
  1146
    }
slouken@1670
  1147
slouken@1670
  1148
    textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
slouken@1670
  1149
    if (!textureID) {
slouken@1670
  1150
        return 0;
slouken@1670
  1151
    }
slouken@1670
  1152
slouken@1670
  1153
    /* Set up a destination surface for the texture update */
slouken@1670
  1154
    SDL_zero(dst);
slouken@1670
  1155
    dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
  1156
    if (!dst.format) {
slouken@1670
  1157
        SDL_DestroyTexture(textureID);
slouken@1670
  1158
        return 0;
slouken@1670
  1159
    }
slouken@1670
  1160
    dst.w = surface->w;
slouken@1670
  1161
    dst.h = surface->h;
slouken@1670
  1162
    if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
slouken@1670
  1163
        dst.flags |= SDL_PREALLOC;
slouken@1670
  1164
    } else {
slouken@1670
  1165
        dst.pitch = SDL_CalculatePitch(&dst);
slouken@1670
  1166
        dst.pixels = SDL_malloc(dst.h * dst.pitch);
slouken@1670
  1167
        if (!dst.pixels) {
slouken@1670
  1168
            SDL_DestroyTexture(textureID);
slouken@1670
  1169
            SDL_FreeFormat(dst.format);
slouken@1670
  1170
            SDL_OutOfMemory();
slouken@1670
  1171
            return 0;
slouken@1670
  1172
        }
slouken@1670
  1173
    }
slouken@1670
  1174
slouken@1670
  1175
    /* Copy the palette if any */
slouken@1670
  1176
    if (fmt->palette && dst.format->palette) {
slouken@1670
  1177
        SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
slouken@1670
  1178
                              fmt->palette->ncolors);
slouken@1670
  1179
slouken@1670
  1180
        SDL_memcpy(dst.format->palette->colors,
slouken@1670
  1181
                   fmt->palette->colors,
slouken@1670
  1182
                   fmt->palette->ncolors * sizeof(SDL_Color));
slouken@1670
  1183
        dst.format->palette->ncolors = fmt->palette->ncolors;
slouken@1670
  1184
    }
slouken@1670
  1185
slouken@1670
  1186
    /* Make the texture transparent if the surface has colorkey */
slouken@1670
  1187
    if (surface_flags & SDL_SRCCOLORKEY) {
slouken@1670
  1188
        int row;
slouken@1670
  1189
        int length = dst.w * dst.format->BytesPerPixel;
slouken@1670
  1190
        Uint8 *p = (Uint8 *) dst.pixels;
slouken@1670
  1191
        for (row = 0; row < dst.h; ++row) {
slouken@1670
  1192
            SDL_memset(p, 0, length);
slouken@1670
  1193
            p += dst.pitch;
slouken@1670
  1194
        }
slouken@1670
  1195
    }
slouken@1670
  1196
slouken@1670
  1197
    /* Copy over the alpha channel */
slouken@1670
  1198
    if (surface_flags & SDL_SRCALPHA) {
slouken@1670
  1199
        if (fmt->Amask) {
slouken@1670
  1200
            surface->flags &= ~SDL_SRCALPHA;
slouken@1670
  1201
        } else {
slouken@1670
  1202
            /* FIXME: Need to make sure the texture has an alpha channel
slouken@1670
  1203
             *        and copy 'alpha' into the texture alpha channel.
slouken@1670
  1204
             */
slouken@1670
  1205
            alpha = surface->format->alpha;
slouken@1670
  1206
            SDL_SetAlpha(surface, 0, 0);
slouken@1670
  1207
        }
slouken@1670
  1208
    }
slouken@1670
  1209
slouken@1670
  1210
    /* Copy over the image data */
slouken@1670
  1211
    bounds.x = 0;
slouken@1670
  1212
    bounds.y = 0;
slouken@1670
  1213
    bounds.w = surface->w;
slouken@1670
  1214
    bounds.h = surface->h;
slouken@1670
  1215
    SDL_LowerBlit(surface, &bounds, &dst, &bounds);
slouken@1670
  1216
slouken@1670
  1217
    /* Clean up the original surface */
slouken@1670
  1218
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1670
  1219
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1670
  1220
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1670
  1221
    }
slouken@1670
  1222
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1670
  1223
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
  1224
        if (fmt->Amask) {
slouken@1670
  1225
            surface->flags |= SDL_SRCALPHA;
slouken@1670
  1226
        } else {
slouken@1670
  1227
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1670
  1228
        }
slouken@1670
  1229
    }
slouken@1670
  1230
slouken@1670
  1231
    /* Update the texture */
slouken@1670
  1232
    if (dst.flags & SDL_PREALLOC) {
slouken@1670
  1233
        SDL_UnlockTexture(textureID);
slouken@1670
  1234
    } else {
slouken@1670
  1235
        SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
slouken@1670
  1236
        SDL_free(dst.pixels);
slouken@1670
  1237
    }
slouken@1670
  1238
    SDL_FreeFormat(dst.format);
slouken@1670
  1239
slouken@1670
  1240
    return textureID;
slouken@1670
  1241
}
slouken@1670
  1242
slouken@1670
  1243
static __inline__ SDL_Texture *
slouken@1670
  1244
SDL_GetTextureFromID(SDL_TextureID textureID)
slouken@1670
  1245
{
slouken@1670
  1246
    int hash;
slouken@1670
  1247
    SDL_Texture *texture;
slouken@1670
  1248
slouken@1670
  1249
    if (!_this) {
slouken@1662
  1250
        return NULL;
slouken@1662
  1251
    }
slouken@1662
  1252
slouken@1670
  1253
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1254
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1255
         texture = texture->next) {
slouken@1670
  1256
        if (texture->id == textureID) {
slouken@1670
  1257
            return texture;
slouken@1670
  1258
        }
slouken@1670
  1259
    }
slouken@1670
  1260
    return NULL;
slouken@1670
  1261
}
slouken@1670
  1262
slouken@1670
  1263
int
slouken@1670
  1264
SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
slouken@1670
  1265
                 int *w, int *h)
slouken@1670
  1266
{
slouken@1670
  1267
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1268
slouken@1670
  1269
    if (!texture) {
slouken@1670
  1270
        return -1;
slouken@1662
  1271
    }
slouken@1662
  1272
slouken@1670
  1273
    if (format) {
slouken@1670
  1274
        *format = texture->format;
slouken@1670
  1275
    }
slouken@1670
  1276
    if (access) {
slouken@1670
  1277
        *access = texture->access;
slouken@1670
  1278
    }
slouken@1670
  1279
    if (w) {
slouken@1670
  1280
        *w = texture->w;
slouken@1670
  1281
    }
slouken@1670
  1282
    if (h) {
slouken@1670
  1283
        *h = texture->h;
slouken@1670
  1284
    }
slouken@1670
  1285
    return 0;
slouken@1670
  1286
}
slouken@1669
  1287
slouken@1670
  1288
int
slouken@1670
  1289
SDL_UpdateTexture(SDL_TextureID textureID, SDL_Rect * rect,
slouken@1670
  1290
                  const void *pixels, int pitch)
slouken@1670
  1291
{
slouken@1670
  1292
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1293
    SDL_Renderer *renderer;
slouken@1670
  1294
slouken@1670
  1295
    if (!texture) {
slouken@1670
  1296
        return -1;
slouken@1670
  1297
    }
slouken@1670
  1298
slouken@1670
  1299
    renderer = texture->renderer;
slouken@1670
  1300
    if (!renderer->UpdateTexture) {
slouken@1670
  1301
        return -1;
slouken@1670
  1302
    }
slouken@1676
  1303
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1670
  1304
}
slouken@1670
  1305
slouken@1670
  1306
int
slouken@1670
  1307
SDL_LockTexture(SDL_TextureID textureID, SDL_Rect * rect, int markDirty,
slouken@1670
  1308
                void **pixels, int *pitch)
slouken@1670
  1309
{
slouken@1670
  1310
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1311
    SDL_Renderer *renderer;
slouken@1670
  1312
slouken@1670
  1313
    if (!texture) {
slouken@1670
  1314
        return -1;
slouken@1670
  1315
    }
slouken@1670
  1316
slouken@1670
  1317
    renderer = texture->renderer;
slouken@1670
  1318
    if (!renderer->LockTexture) {
slouken@1670
  1319
        return -1;
slouken@1670
  1320
    }
slouken@1676
  1321
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@1676
  1322
                                 pitch);
slouken@1670
  1323
}
slouken@1670
  1324
slouken@1670
  1325
void
slouken@1670
  1326
SDL_UnlockTexture(SDL_TextureID textureID)
slouken@1670
  1327
{
slouken@1670
  1328
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1329
    SDL_Renderer *renderer;
slouken@1670
  1330
slouken@1670
  1331
    if (!texture) {
slouken@1670
  1332
        return;
slouken@1670
  1333
    }
slouken@1670
  1334
slouken@1670
  1335
    renderer = texture->renderer;
slouken@1670
  1336
    if (!renderer->UnlockTexture) {
slouken@1670
  1337
        return;
slouken@1670
  1338
    }
slouken@1676
  1339
    return renderer->UnlockTexture(renderer, texture);
slouken@1670
  1340
}
slouken@1670
  1341
slouken@1670
  1342
void
slouken@1670
  1343
SDL_DirtyTexture(SDL_TextureID textureID, int numrects, SDL_Rect * rects)
slouken@1670
  1344
{
slouken@1670
  1345
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1346
    SDL_Renderer *renderer;
slouken@1670
  1347
slouken@1670
  1348
    if (!texture) {
slouken@1670
  1349
        return;
slouken@1670
  1350
    }
slouken@1670
  1351
slouken@1670
  1352
    renderer = texture->renderer;
slouken@1670
  1353
    if (!renderer->DirtyTexture) {
slouken@1670
  1354
        return;
slouken@1670
  1355
    }
slouken@1676
  1356
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1670
  1357
}
slouken@1670
  1358
slouken@1670
  1359
void
slouken@1670
  1360
SDL_SelectRenderTexture(SDL_TextureID textureID)
slouken@1670
  1361
{
slouken@1670
  1362
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1363
    SDL_Renderer *renderer;
slouken@1670
  1364
slouken@1670
  1365
    if (!texture || texture->access != SDL_TextureAccess_Render) {
slouken@1670
  1366
        return;
slouken@1670
  1367
    }
slouken@1670
  1368
    renderer = texture->renderer;
slouken@1670
  1369
    if (!renderer->SelectRenderTexture) {
slouken@1670
  1370
        return;
slouken@1670
  1371
    }
slouken@1676
  1372
    renderer->SelectRenderTexture(renderer, texture);
slouken@1670
  1373
}
slouken@1670
  1374
slouken@1670
  1375
int
slouken@1670
  1376
SDL_RenderFill(SDL_Rect * rect, Uint32 color)
slouken@1670
  1377
{
slouken@1670
  1378
    SDL_Renderer *renderer;
slouken@1670
  1379
slouken@1670
  1380
    if (!_this) {
slouken@1670
  1381
        return -1;
slouken@1670
  1382
    }
slouken@1670
  1383
slouken@1670
  1384
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1385
    if (!renderer || !renderer->RenderFill) {
slouken@1670
  1386
        return -1;
slouken@1670
  1387
    }
slouken@1670
  1388
slouken@1676
  1389
    renderer->RenderFill(renderer, rect, color);
slouken@1670
  1390
}
slouken@1670
  1391
slouken@1670
  1392
int
slouken@1670
  1393
SDL_RenderCopy(SDL_TextureID textureID, SDL_Rect * srcrect,
slouken@1670
  1394
               SDL_Rect * dstrect, int blendMode, int scaleMode)
slouken@1670
  1395
{
slouken@1670
  1396
    SDL_Texture *texture = SDL_GetTextureFromID(textureID);
slouken@1670
  1397
    SDL_Renderer *renderer;
slouken@1670
  1398
slouken@1670
  1399
    if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
slouken@1670
  1400
        return;
slouken@1670
  1401
    }
slouken@1670
  1402
slouken@1670
  1403
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1404
    if (!renderer || !renderer->RenderCopy) {
slouken@1670
  1405
        return -1;
slouken@1670
  1406
    }
slouken@1670
  1407
slouken@1676
  1408
    return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
slouken@1676
  1409
                                blendMode, scaleMode);
slouken@1670
  1410
}
slouken@1670
  1411
slouken@1670
  1412
int
slouken@1670
  1413
SDL_RenderReadPixels(SDL_Rect * rect, void *pixels, int pitch)
slouken@1670
  1414
{
slouken@1670
  1415
    SDL_Renderer *renderer;
slouken@1670
  1416
slouken@1670
  1417
    if (!_this) {
slouken@1670
  1418
        return -1;
slouken@1670
  1419
    }
slouken@1670
  1420
slouken@1670
  1421
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1422
    if (!renderer || !renderer->RenderReadPixels) {
slouken@1670
  1423
        return -1;
slouken@1670
  1424
    }
slouken@1670
  1425
slouken@1676
  1426
    return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
slouken@1670
  1427
}
slouken@1670
  1428
slouken@1670
  1429
int
slouken@1670
  1430
SDL_RenderWritePixels(SDL_Rect * rect, const void *pixels, int pitch)
slouken@1670
  1431
{
slouken@1670
  1432
    SDL_Renderer *renderer;
slouken@1670
  1433
slouken@1670
  1434
    if (!_this) {
slouken@1670
  1435
        return -1;
slouken@1670
  1436
    }
slouken@1670
  1437
slouken@1670
  1438
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1439
    if (!renderer || !renderer->RenderWritePixels) {
slouken@1670
  1440
        return -1;
slouken@1670
  1441
    }
slouken@1670
  1442
slouken@1676
  1443
    return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
slouken@1670
  1444
}
slouken@1670
  1445
slouken@1670
  1446
void
slouken@1670
  1447
SDL_RenderPresent(void)
slouken@1670
  1448
{
slouken@1670
  1449
    SDL_Renderer *renderer;
slouken@1670
  1450
slouken@1670
  1451
    if (!_this) {
slouken@1670
  1452
        return;
slouken@1670
  1453
    }
slouken@1670
  1454
slouken@1670
  1455
    renderer = SDL_CurrentDisplay.current_renderer;
slouken@1670
  1456
    if (!renderer || !renderer->RenderPresent) {
slouken@1670
  1457
        return;
slouken@1670
  1458
    }
slouken@1670
  1459
slouken@1676
  1460
    renderer->RenderPresent(renderer);
slouken@1670
  1461
}
slouken@1670
  1462
slouken@1670
  1463
void
slouken@1670
  1464
SDL_DestroyTexture(SDL_TextureID textureID)
slouken@1670
  1465
{
slouken@1670
  1466
    int hash;
slouken@1670
  1467
    SDL_Texture *prev, *texture;
slouken@1670
  1468
    SDL_Renderer *renderer;
slouken@1670
  1469
slouken@1670
  1470
    if (!_this) {
slouken@1670
  1471
        return;
slouken@1670
  1472
    }
slouken@1670
  1473
slouken@1670
  1474
    /* Look up the texture in the hash table */
slouken@1670
  1475
    hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
slouken@1670
  1476
    prev = NULL;
slouken@1670
  1477
    for (texture = SDL_CurrentDisplay.textures[hash]; texture;
slouken@1670
  1478
         prev = texture, texture = texture->next) {
slouken@1670
  1479
        if (texture->id == textureID) {
slouken@1670
  1480
            break;
slouken@1669
  1481
        }
slouken@1662
  1482
    }
slouken@1670
  1483
    if (!texture) {
slouken@1670
  1484
        return;
slouken@1662
  1485
    }
slouken@1662
  1486
slouken@1670
  1487
    /* Unlink the texture from the list */
slouken@1670
  1488
    if (prev) {
slouken@1670
  1489
        prev->next = texture->next;
slouken@1670
  1490
    } else {
slouken@1670
  1491
        SDL_CurrentDisplay.textures[hash] = texture->next;
slouken@1670
  1492
    }
slouken@1662
  1493
slouken@1670
  1494
    /* Free the texture */
slouken@1670
  1495
    renderer = texture->renderer;
slouken@1676
  1496
    renderer->DestroyTexture(renderer, texture);
slouken@1670
  1497
    SDL_free(texture);
slouken@1670
  1498
}
slouken@1669
  1499
slouken@1670
  1500
void
slouken@1670
  1501
SDL_DestroyRenderer(SDL_WindowID windowID)
slouken@1670
  1502
{
slouken@1670
  1503
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1670
  1504
    SDL_Renderer *renderer;
slouken@1670
  1505
    int i;
slouken@1662
  1506
slouken@1670
  1507
    if (!window) {
slouken@1670
  1508
        return;
slouken@1670
  1509
    }
slouken@1670
  1510
slouken@1670
  1511
    renderer = window->renderer;
slouken@1670
  1512
    if (!renderer) {
slouken@1670
  1513
        return;
slouken@1670
  1514
    }
slouken@1670
  1515
slouken@1670
  1516
    /* Free existing textures for this renderer */
slouken@1670
  1517
    for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
slouken@1670
  1518
        SDL_Texture *texture;
slouken@1670
  1519
        SDL_Texture *prev = NULL;
slouken@1670
  1520
        SDL_Texture *next;
slouken@1670
  1521
        for (texture = SDL_CurrentDisplay.textures[i]; texture;
slouken@1670
  1522
             texture = next) {
slouken@1670
  1523
            next = texture->next;
slouken@1670
  1524
            if (texture->renderer == renderer) {
slouken@1670
  1525
                if (prev) {
slouken@1670
  1526
                    prev->next = next;
slouken@1670
  1527
                } else {
slouken@1670
  1528
                    SDL_CurrentDisplay.textures[i] = next;
slouken@1670
  1529
                }
slouken@1676
  1530
                renderer->DestroyTexture(renderer, texture);
slouken@1670
  1531
                SDL_free(texture);
slouken@1662
  1532
            } else {
slouken@1670
  1533
                prev = texture;
slouken@1662
  1534
            }
slouken@1662
  1535
        }
slouken@1662
  1536
    }
slouken@1662
  1537
slouken@1670
  1538
    /* Free the renderer instance */
slouken@1670
  1539
    renderer->DestroyRenderer(renderer);
slouken@1670
  1540
slouken@1670
  1541
    /* Clear references */
slouken@1670
  1542
    window->renderer = NULL;
slouken@1670
  1543
    if (SDL_CurrentDisplay.current_renderer == renderer) {
slouken@1670
  1544
        SDL_CurrentDisplay.current_renderer = NULL;
slouken@1662
  1545
    }
slouken@0
  1546
}
slouken@0
  1547
slouken@1662
  1548
void
slouken@1668
  1549
SDL_VideoQuit(void)
slouken@0
  1550
{
slouken@1662
  1551
    int i, j;
slouken@0
  1552
slouken@1662
  1553
    if (!_this) {
slouken@1662
  1554
        return;
slouken@1662
  1555
    }
slouken@0
  1556
slouken@1662
  1557
    /* Halt event processing before doing anything else */
slouken@1668
  1558
    SDL_StopEventLoop();
slouken@0
  1559
slouken@1662
  1560
    /* Clean up allocated window manager items */
slouken@1668
  1561
    SDL_CursorQuit();
slouken@0
  1562
slouken@1662
  1563
    /* Clean up the system video */
slouken@1662
  1564
    for (i = _this->num_displays; i--;) {
slouken@1662
  1565
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1566
        for (j = display->num_windows; j--;) {
slouken@1668
  1567
            SDL_DestroyWindow(display->windows[i].id);
slouken@1662
  1568
        }
slouken@1662
  1569
        if (display->windows) {
slouken@1668
  1570
            SDL_free(display->windows);
slouken@1662
  1571
            display->windows = NULL;
slouken@1662
  1572
        }
slouken@1662
  1573
    }
slouken@1668
  1574
    _this->VideoQuit(_this);
slouken@1662
  1575
    if (_this->displays) {
slouken@1668
  1576
        SDL_free(_this->displays);
slouken@1662
  1577
    }
slouken@1668
  1578
    _this->free(_this);
slouken@1662
  1579
    _this = NULL;
slouken@0
  1580
}
slouken@0
  1581
slouken@0
  1582
/* Load the GL driver library */
slouken@1662
  1583
int
slouken@1668
  1584
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1585
{
slouken@1662
  1586
    int retval;
slouken@0
  1587
slouken@1662
  1588
    retval = -1;
slouken@1662
  1589
    if (_this == NULL) {
slouken@1668
  1590
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
  1591
    } else {
slouken@1662
  1592
        if (_this->GL_LoadLibrary) {
slouken@1668
  1593
            retval = _this->GL_LoadLibrary(_this, path);
slouken@1662
  1594
        } else {
slouken@1668
  1595
            SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1596
        }
slouken@1662
  1597
    }
slouken@1662
  1598
    return (retval);
slouken@0
  1599
}
slouken@0
  1600
slouken@1662
  1601
void *
slouken@1668
  1602
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1603
{
slouken@1662
  1604
    void *func;
slouken@0
  1605
slouken@1662
  1606
    func = NULL;
slouken@1662
  1607
    if (_this->GL_GetProcAddress) {
slouken@1662
  1608
        if (_this->gl_config.driver_loaded) {
slouken@1668
  1609
            func = _this->GL_GetProcAddress(_this, proc);
slouken@1662
  1610
        } else {
slouken@1668
  1611
            SDL_SetError("No GL driver has been loaded");
slouken@1662
  1612
        }
slouken@1662
  1613
    } else {
slouken@1668
  1614
        SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1615
    }
slouken@1662
  1616
    return func;
slouken@0
  1617
}
slouken@0
  1618
slouken@0
  1619
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1620
int
slouken@1668
  1621
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  1622
{
slouken@1662
  1623
    int retval;
slouken@0
  1624
slouken@1662
  1625
    retval = 0;
slouken@1662
  1626
    switch (attr) {
slouken@1662
  1627
    case SDL_GL_RED_SIZE:
slouken@1662
  1628
        _this->gl_config.red_size = value;
slouken@1662
  1629
        break;
slouken@1662
  1630
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1631
        _this->gl_config.green_size = value;
slouken@1662
  1632
        break;
slouken@1662
  1633
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1634
        _this->gl_config.blue_size = value;
slouken@1662
  1635
        break;
slouken@1662
  1636
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1637
        _this->gl_config.alpha_size = value;
slouken@1662
  1638
        break;
slouken@1662
  1639
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1640
        _this->gl_config.double_buffer = value;
slouken@1662
  1641
        break;
slouken@1662
  1642
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1643
        _this->gl_config.buffer_size = value;
slouken@1662
  1644
        break;
slouken@1662
  1645
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1646
        _this->gl_config.depth_size = value;
slouken@1662
  1647
        break;
slouken@1662
  1648
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1649
        _this->gl_config.stencil_size = value;
slouken@1662
  1650
        break;
slouken@1662
  1651
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1652
        _this->gl_config.accum_red_size = value;
slouken@1662
  1653
        break;
slouken@1662
  1654
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1655
        _this->gl_config.accum_green_size = value;
slouken@1662
  1656
        break;
slouken@1662
  1657
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1658
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1659
        break;
slouken@1662
  1660
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1661
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1662
        break;
slouken@1662
  1663
    case SDL_GL_STEREO:
slouken@1662
  1664
        _this->gl_config.stereo = value;
slouken@1662
  1665
        break;
slouken@1662
  1666
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1667
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1668
        break;
slouken@1662
  1669
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1670
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1671
        break;
slouken@1662
  1672
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1673
        _this->gl_config.accelerated = value;
slouken@1662
  1674
        break;
slouken@1662
  1675
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1676
        _this->gl_config.swap_control = value;
slouken@1662
  1677
        break;
slouken@1662
  1678
    default:
slouken@1668
  1679
        SDL_SetError("Unknown OpenGL attribute");
slouken@1662
  1680
        retval = -1;
slouken@1662
  1681
        break;
slouken@1662
  1682
    }
slouken@1662
  1683
    return (retval);
slouken@0
  1684
}
slouken@0
  1685
slouken@0
  1686
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1687
int
slouken@1668
  1688
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  1689
{
slouken@1662
  1690
    int retval = -1;
slouken@0
  1691
slouken@1662
  1692
    if (_this->GL_GetAttribute) {
slouken@1668
  1693
        retval = _this->GL_GetAttribute(_this, attr, value);
slouken@1662
  1694
    } else {
slouken@1662
  1695
        *value = 0;
slouken@1668
  1696
        SDL_SetError("GL_GetAttribute not supported");
slouken@1662
  1697
    }
slouken@1662
  1698
    return retval;
slouken@0
  1699
}
slouken@0
  1700
slouken@0
  1701
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1702
void
slouken@1668
  1703
SDL_GL_SwapBuffers(void)
slouken@0
  1704
{
slouken@1670
  1705
    // FIXME: Track the current window context - do we provide N contexts, and match them to M windows, or is there a one-to-one mapping?
slouken@1670
  1706
    _this->GL_SwapBuffers(_this);
slouken@0
  1707
}
slouken@0
  1708
slouken@1662
  1709
#if 0                           // FIXME
slouken@541
  1710
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1711
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1712
static void
slouken@1668
  1713
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1714
{
slouken@1662
  1715
    int x, y;
slouken@1662
  1716
    Uint32 colorkey;
slouken@0
  1717
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1718
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1719
slouken@1662
  1720
    colorkey = icon->format->colorkey;
slouken@1662
  1721
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1722
    case 1:
slouken@1662
  1723
        {
slouken@1662
  1724
            Uint8 *pixels;
slouken@1662
  1725
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1726
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1727
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1728
                    if (*pixels++ == colorkey) {
slouken@1668
  1729
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1730
                    }
slouken@1662
  1731
                }
slouken@1662
  1732
            }
slouken@1662
  1733
        }
slouken@1662
  1734
        break;
slouken@0
  1735
slouken@1662
  1736
    case 2:
slouken@1662
  1737
        {
slouken@1662
  1738
            Uint16 *pixels;
slouken@1662
  1739
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1740
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1741
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1742
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1743
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1744
                    } else if ((flags & 2)
slouken@1662
  1745
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1746
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1747
                    }
slouken@1662
  1748
                    pixels++;
slouken@1662
  1749
                }
slouken@1662
  1750
            }
slouken@1662
  1751
        }
slouken@1662
  1752
        break;
slouken@0
  1753
slouken@1662
  1754
    case 4:
slouken@1662
  1755
        {
slouken@1662
  1756
            Uint32 *pixels;
slouken@1662
  1757
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1758
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1759
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1760
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1761
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1762
                    } else if ((flags & 2)
slouken@1662
  1763
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1764
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1765
                    }
slouken@1662
  1766
                    pixels++;
slouken@1662
  1767
                }
slouken@1662
  1768
            }
slouken@1662
  1769
        }
slouken@1662
  1770
        break;
slouken@1662
  1771
    }
slouken@0
  1772
}
slouken@0
  1773
slouken@0
  1774
/*
slouken@0
  1775
 * Sets the window manager icon for the display window.
slouken@0
  1776
 */
slouken@1662
  1777
void
slouken@1668
  1778
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  1779
{
slouken@1662
  1780
    if (icon && _this->SetIcon) {
slouken@1662
  1781
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  1782
        if (mask == NULL) {
slouken@1662
  1783
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  1784
            int flags = 0;
slouken@1668
  1785
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@1662
  1786
            if (mask == NULL) {
slouken@1662
  1787
                return;
slouken@1662
  1788
            }
slouken@1668
  1789
            SDL_memset(mask, ~0, mask_len);
slouken@1662
  1790
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  1791
                flags |= 1;
slouken@1662
  1792
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  1793
                flags |= 2;
slouken@1662
  1794
            if (flags) {
slouken@1668
  1795
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@1662
  1796
            }
slouken@1668
  1797
            _this->SetIcon(_this, icon, mask);
slouken@1668
  1798
            SDL_free(mask);
slouken@1662
  1799
        } else {
slouken@1668
  1800
            _this->SetIcon(_this, icon, mask);
slouken@1662
  1801
        }
slouken@1662
  1802
    }
slouken@1662
  1803
}
slouken@1662
  1804
#endif
slouken@0
  1805
slouken@1662
  1806
SDL_bool
slouken@1668
  1807
SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  1808
{
slouken@1668
  1809
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1810
slouken@1662
  1811
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  1812
        return SDL_FALSE;
slouken@1662
  1813
    }
slouken@1668
  1814
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  1815
}
slouken@0
  1816
slouken@1662
  1817
/* vi: set ts=4 sw=4 expandtab: */