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