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