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