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