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