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