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