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