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