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