src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 15:22:29 -0800
changeset 4984 889e6fef8fc5
parent 4978 4a7f284a82b2
child 4987 b68a0e03db02
permissions -rw-r--r--
Support the new 16-bit pixel formats in SDL_CreateTextureFromSurface()
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@4984
  1742
                SDL_PIXELFORMAT_RGBA5551,
slouken@3186
  1743
                SDL_PIXELFORMAT_ABGR1555,
slouken@4984
  1744
                SDL_PIXELFORMAT_BGRA5551,
slouken@3186
  1745
                SDL_PIXELFORMAT_RGB555,
slouken@3186
  1746
                SDL_PIXELFORMAT_BGR555,
slouken@3186
  1747
                SDL_PIXELFORMAT_ARGB4444,
slouken@4984
  1748
                SDL_PIXELFORMAT_RGBA4444,
slouken@3186
  1749
                SDL_PIXELFORMAT_ABGR4444,
slouken@4984
  1750
                SDL_PIXELFORMAT_BGRA4444,
slouken@3186
  1751
                SDL_PIXELFORMAT_RGB444,
slouken@3186
  1752
                SDL_PIXELFORMAT_ARGB2101010,
slouken@3186
  1753
                SDL_PIXELFORMAT_INDEX8,
slouken@3186
  1754
                SDL_PIXELFORMAT_INDEX4LSB,
slouken@3186
  1755
                SDL_PIXELFORMAT_INDEX4MSB,
slouken@3186
  1756
                SDL_PIXELFORMAT_RGB332,
slouken@3186
  1757
                SDL_PIXELFORMAT_INDEX1LSB,
slouken@3186
  1758
                SDL_PIXELFORMAT_INDEX1MSB,
slouken@3186
  1759
                SDL_PIXELFORMAT_UNKNOWN
slouken@3186
  1760
            };
lestat@3178
  1761
slouken@2753
  1762
            bpp = fmt->BitsPerPixel;
slouken@2753
  1763
            Rmask = fmt->Rmask;
slouken@2753
  1764
            Gmask = fmt->Gmask;
slouken@2753
  1765
            Bmask = fmt->Bmask;
slouken@2753
  1766
            Amask = fmt->Amask;
lestat@3178
  1767
slouken@3186
  1768
            format =
slouken@3186
  1769
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
lestat@3178
  1770
            if (!format) {
lestat@3178
  1771
                SDL_SetError("Unknown pixel format");
lestat@3178
  1772
                return 0;
lestat@3178
  1773
            }
lestat@3178
  1774
lestat@3178
  1775
            /* Search requested format in the supported texture */
lestat@3178
  1776
            /* formats by current renderer                      */
slouken@3186
  1777
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@3186
  1778
                if (renderer->info.texture_formats[it] == format) {
slouken@3186
  1779
                    break;
lestat@3178
  1780
                }
lestat@3178
  1781
            }
lestat@3178
  1782
lestat@3178
  1783
            /* If requested format can't be found, search any best */
lestat@3178
  1784
            /* format which renderer provides                      */
slouken@3186
  1785
            if (it == renderer->info.num_texture_formats) {
slouken@3186
  1786
                pfmt = 0;
slouken@3186
  1787
                for (;;) {
slouken@3186
  1788
                    if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
lestat@3178
  1789
                        break;
lestat@3178
  1790
                    }
lestat@3178
  1791
slouken@3186
  1792
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@3186
  1793
                         it++) {
slouken@3186
  1794
                        if (renderer->info.texture_formats[it] ==
slouken@3186
  1795
                            sdl_pformats[pfmt]) {
slouken@3186
  1796
                            break;
slouken@3186
  1797
                        }
lestat@3178
  1798
                    }
lestat@3178
  1799
slouken@3186
  1800
                    if (it != renderer->info.num_texture_formats) {
slouken@3186
  1801
                        /* The best format has been found */
slouken@3186
  1802
                        break;
lestat@3178
  1803
                    }
lestat@3178
  1804
                    pfmt++;
lestat@3178
  1805
                }
lestat@3178
  1806
lestat@3178
  1807
                /* If any format can't be found, then return an error */
slouken@3186
  1808
                if (it == renderer->info.num_texture_formats) {
slouken@3186
  1809
                    SDL_SetError
slouken@3186
  1810
                        ("Any of the supported pixel formats can't be found");
lestat@3178
  1811
                    return 0;
lestat@3178
  1812
                }
lestat@3178
  1813
lestat@3178
  1814
                /* Convert found pixel format back to color masks */
slouken@3186
  1815
                if (SDL_PixelFormatEnumToMasks
slouken@3186
  1816
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@3186
  1817
                     &Bmask, &Amask) != SDL_TRUE) {
lestat@3178
  1818
                    SDL_SetError("Unknown pixel format");
lestat@3178
  1819
                    return 0;
lestat@3178
  1820
                }
lestat@3178
  1821
            }
slouken@2753
  1822
        } else {
slouken@2753
  1823
            /* Need a format with alpha */
slouken@3196
  1824
            Uint32 it;
lestat@3166
  1825
            int apfmt;
lestat@3166
  1826
lestat@3166
  1827
            /* Pixel formats with alpha, sorted by best first */
slouken@3169
  1828
            static const Uint32 sdl_alpha_pformats[] = {
slouken@3169
  1829
                SDL_PIXELFORMAT_ARGB8888,
slouken@3169
  1830
                SDL_PIXELFORMAT_RGBA8888,
slouken@3169
  1831
                SDL_PIXELFORMAT_ABGR8888,
slouken@3169
  1832
                SDL_PIXELFORMAT_BGRA8888,
slouken@3169
  1833
                SDL_PIXELFORMAT_ARGB1555,
slouken@4984
  1834
                SDL_PIXELFORMAT_RGBA5551,
slouken@3169
  1835
                SDL_PIXELFORMAT_ABGR1555,
slouken@4984
  1836
                SDL_PIXELFORMAT_BGRA5551,
slouken@3169
  1837
                SDL_PIXELFORMAT_ARGB4444,
slouken@4984
  1838
                SDL_PIXELFORMAT_RGBA4444,
slouken@3169
  1839
                SDL_PIXELFORMAT_ABGR4444,
slouken@4984
  1840
                SDL_PIXELFORMAT_BGRA4444,
slouken@3169
  1841
                SDL_PIXELFORMAT_ARGB2101010,
slouken@3169
  1842
                SDL_PIXELFORMAT_UNKNOWN
slouken@3169
  1843
            };
lestat@3166
  1844
lestat@3178
  1845
            if (surface->format->Amask) {
lestat@3178
  1846
                /* If surface already has alpha, then try an original */
lestat@3178
  1847
                /* surface format first                               */
lestat@3178
  1848
                bpp = fmt->BitsPerPixel;
lestat@3178
  1849
                Rmask = fmt->Rmask;
lestat@3178
  1850
                Gmask = fmt->Gmask;
lestat@3178
  1851
                Bmask = fmt->Bmask;
lestat@3178
  1852
                Amask = fmt->Amask;
lestat@3178
  1853
            } else {
lestat@3178
  1854
                bpp = 32;
lestat@3178
  1855
                Rmask = 0x00FF0000;
lestat@3178
  1856
                Gmask = 0x0000FF00;
lestat@3178
  1857
                Bmask = 0x000000FF;
lestat@3178
  1858
                Amask = 0xFF000000;
lestat@3178
  1859
            }
lestat@3166
  1860
slouken@3169
  1861
            format =
slouken@3169
  1862
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
lestat@3166
  1863
            if (!format) {
lestat@3166
  1864
                SDL_SetError("Unknown pixel format");
lestat@3166
  1865
                return 0;
lestat@3166
  1866
            }
lestat@3166
  1867
lestat@3166
  1868
            /* Search this format in the supported texture formats */
lestat@3166
  1869
            /* by current renderer                                 */
slouken@3169
  1870
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@3169
  1871
                if (renderer->info.texture_formats[it] == format) {
slouken@3169
  1872
                    break;
lestat@3166
  1873
                }
lestat@3166
  1874
            }
lestat@3166
  1875
lestat@3166
  1876
            /* If this format can't be found, search any best       */
lestat@3166
  1877
            /* compatible format with alpha which renderer provides */
slouken@3169
  1878
            if (it == renderer->info.num_texture_formats) {
slouken@3169
  1879
                apfmt = 0;
slouken@3169
  1880
                for (;;) {
slouken@3169
  1881
                    if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
lestat@3166
  1882
                        break;
lestat@3166
  1883
                    }
lestat@3166
  1884
slouken@3169
  1885
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@3169
  1886
                         it++) {
slouken@3169
  1887
                        if (renderer->info.texture_formats[it] ==
slouken@3169
  1888
                            sdl_alpha_pformats[apfmt]) {
slouken@3169
  1889
                            break;
slouken@3169
  1890
                        }
lestat@3166
  1891
                    }
lestat@3166
  1892
slouken@3169
  1893
                    if (it != renderer->info.num_texture_formats) {
slouken@3169
  1894
                        /* Compatible format has been found */
slouken@3169
  1895
                        break;
lestat@3166
  1896
                    }
lestat@3166
  1897
                    apfmt++;
lestat@3166
  1898
                }
lestat@3166
  1899
lestat@3166
  1900
                /* If compatible format can't be found, then return an error */
slouken@3169
  1901
                if (it == renderer->info.num_texture_formats) {
lestat@3166
  1902
                    SDL_SetError("Compatible pixel format can't be found");
lestat@3166
  1903
                    return 0;
lestat@3166
  1904
                }
lestat@3166
  1905
lestat@3166
  1906
                /* Convert found pixel format back to color masks */
slouken@3169
  1907
                if (SDL_PixelFormatEnumToMasks
slouken@3169
  1908
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@3169
  1909
                     &Bmask, &Amask) != SDL_TRUE) {
lestat@3166
  1910
                    SDL_SetError("Unknown pixel format");
lestat@3166
  1911
                    return 0;
lestat@3166
  1912
                }
lestat@3166
  1913
            }
slouken@2753
  1914
        }
lestat@3166
  1915
slouken@2753
  1916
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2753
  1917
        if (!format) {
slouken@2753
  1918
            SDL_SetError("Unknown pixel format");
slouken@2753
  1919
            return 0;
slouken@2753
  1920
        }
slouken@2753
  1921
    }
slouken@1895
  1922
slouken@3685
  1923
    texture =
slouken@2753
  1924
        SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
slouken@2753
  1925
                          surface->h);
slouken@3685
  1926
    if (!texture && !requested_format) {
slouken@3057
  1927
        SDL_DisplayMode desktop_mode;
slouken@3057
  1928
        SDL_GetDesktopDisplayMode(&desktop_mode);
slouken@3057
  1929
        format = desktop_mode.format;
slouken@3685
  1930
        texture =
slouken@3057
  1931
            SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
slouken@3057
  1932
                              surface->h);
slouken@3057
  1933
    }
slouken@3685
  1934
    if (!texture) {
slouken@2753
  1935
        return 0;
slouken@2753
  1936
    }
slouken@2753
  1937
    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
slouken@2753
  1938
        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
slouken@2753
  1939
        if (SDL_MUSTLOCK(surface)) {
slouken@3315
  1940
            SDL_LockSurface(surface);
slouken@3685
  1941
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@2753
  1942
                              surface->pitch);
slouken@2753
  1943
            SDL_UnlockSurface(surface);
slouken@2753
  1944
        } else {
slouken@3685
  1945
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@2753
  1946
                              surface->pitch);
slouken@2753
  1947
        }
slouken@2753
  1948
    } else {
slouken@2967
  1949
        SDL_PixelFormat dst_fmt;
slouken@2753
  1950
        SDL_Surface *dst = NULL;
slouken@1895
  1951
slouken@2753
  1952
        /* Set up a destination surface for the texture update */
slouken@2967
  1953
        SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
slouken@2967
  1954
        if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@2967
  1955
            dst_fmt.palette =
slouken@2967
  1956
                SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
slouken@2967
  1957
            if (dst_fmt.palette) {
slouken@2967
  1958
                /*
slouken@2967
  1959
                 * FIXME: Should we try to copy
slouken@2967
  1960
                 * fmt->palette?
slouken@2967
  1961
                 */
slouken@2967
  1962
                SDL_DitherColors(dst_fmt.palette->colors,
slouken@2967
  1963
                                 SDL_BITSPERPIXEL(format));
slouken@2753
  1964
            }
slouken@2967
  1965
        }
slouken@2967
  1966
        dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
slouken@2967
  1967
        if (dst) {
slouken@3685
  1968
            SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
slouken@2967
  1969
            SDL_FreeSurface(dst);
slouken@2967
  1970
        }
slouken@2967
  1971
        if (dst_fmt.palette) {
slouken@2967
  1972
            SDL_FreePalette(dst_fmt.palette);
slouken@2753
  1973
        }
slouken@2753
  1974
        if (!dst) {
slouken@3685
  1975
            SDL_DestroyTexture(texture);
slouken@2753
  1976
            return 0;
slouken@2753
  1977
        }
slouken@2753
  1978
    }
slouken@2222
  1979
slouken@3053
  1980
    {
slouken@3053
  1981
        Uint8 r, g, b, a;
slouken@4929
  1982
        SDL_BlendMode blendMode;
slouken@4929
  1983
        SDL_ScaleMode scaleMode;
slouken@3053
  1984
slouken@3053
  1985
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@3685
  1986
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@3053
  1987
slouken@3053
  1988
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@3685
  1989
        SDL_SetTextureAlphaMod(texture, a);
slouken@3053
  1990
slouken@3053
  1991
        SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@3685
  1992
        SDL_SetTextureBlendMode(texture, blendMode);
slouken@3053
  1993
slouken@3053
  1994
        SDL_GetSurfaceScaleMode(surface, &scaleMode);
slouken@3685
  1995
        SDL_SetTextureScaleMode(texture, scaleMode);
slouken@3053
  1996
    }
slouken@3053
  1997
slouken@2753
  1998
    if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
slouken@3685
  1999
        SDL_SetTexturePalette(texture, fmt->palette->colors, 0,
slouken@2753
  2000
                              fmt->palette->ncolors);
slouken@2753
  2001
    }
slouken@3685
  2002
    return texture;
slouken@1895
  2003
}
slouken@1895
  2004
slouken@1895
  2005
int
slouken@3685
  2006
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@2753
  2007
                 int *w, int *h)
slouken@1895
  2008
{
slouken@3695
  2009
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2010
slouken@2753
  2011
    if (format) {
slouken@2753
  2012
        *format = texture->format;
slouken@2753
  2013
    }
slouken@2753
  2014
    if (access) {
slouken@2753
  2015
        *access = texture->access;
slouken@2753
  2016
    }
slouken@2753
  2017
    if (w) {
slouken@2753
  2018
        *w = texture->w;
slouken@2753
  2019
    }
slouken@2753
  2020
    if (h) {
slouken@2753
  2021
        *h = texture->h;
slouken@2753
  2022
    }
slouken@2753
  2023
    return 0;
slouken@1895
  2024
}
slouken@1895
  2025
slouken@1895
  2026
int
slouken@3685
  2027
SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
slouken@1895
  2028
{
slouken@2753
  2029
    SDL_Renderer *renderer;
slouken@1895
  2030
slouken@3695
  2031
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2032
slouken@2753
  2033
    renderer = texture->renderer;
slouken@2753
  2034
    if (!renderer->QueryTexturePixels) {
slouken@2810
  2035
        SDL_Unsupported();
slouken@2753
  2036
        return -1;
slouken@2753
  2037
    }
slouken@2753
  2038
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1895
  2039
}
slouken@1895
  2040
slouken@1895
  2041
int
slouken@3685
  2042
SDL_SetTexturePalette(SDL_Texture * texture, const SDL_Color * colors,
slouken@2753
  2043
                      int firstcolor, int ncolors)
slouken@1895
  2044
{
slouken@2753
  2045
    SDL_Renderer *renderer;
slouken@1895
  2046
slouken@3695
  2047
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2048
slouken@2753
  2049
    renderer = texture->renderer;
slouken@2753
  2050
    if (!renderer->SetTexturePalette) {
slouken@2810
  2051
        SDL_Unsupported();
slouken@2753
  2052
        return -1;
slouken@2753
  2053
    }
slouken@2753
  2054
    return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  2055
                                       ncolors);
slouken@1895
  2056
}
slouken@1895
  2057
slouken@1895
  2058
int
slouken@3685
  2059
SDL_GetTexturePalette(SDL_Texture * texture, SDL_Color * colors,
slouken@2753
  2060
                      int firstcolor, int ncolors)
slouken@1895
  2061
{
slouken@2753
  2062
    SDL_Renderer *renderer;
slouken@1895
  2063
slouken@3695
  2064
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2065
slouken@2753
  2066
    renderer = texture->renderer;
slouken@2753
  2067
    if (!renderer->GetTexturePalette) {
slouken@2810
  2068
        SDL_Unsupported();
slouken@2753
  2069
        return -1;
slouken@2753
  2070
    }
slouken@2753
  2071
    return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  2072
                                       ncolors);
slouken@1895
  2073
}
slouken@1895
  2074
slouken@1895
  2075
int
slouken@3685
  2076
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@1985
  2077
{
slouken@2753
  2078
    SDL_Renderer *renderer;
slouken@1985
  2079
slouken@3695
  2080
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2081
slouken@2753
  2082
    renderer = texture->renderer;
slouken@2753
  2083
    if (!renderer->SetTextureColorMod) {
slouken@2810
  2084
        SDL_Unsupported();
slouken@2753
  2085
        return -1;
slouken@2753
  2086
    }
slouken@2753
  2087
    if (r < 255 || g < 255 || b < 255) {
slouken@2753
  2088
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  2089
    } else {
slouken@2753
  2090
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  2091
    }
slouken@2753
  2092
    texture->r = r;
slouken@2753
  2093
    texture->g = g;
slouken@2753
  2094
    texture->b = b;
slouken@2753
  2095
    return renderer->SetTextureColorMod(renderer, texture);
slouken@1985
  2096
}
slouken@1985
  2097
slouken@1985
  2098
int
slouken@3685
  2099
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@2753
  2100
                       Uint8 * b)
slouken@1985
  2101
{
slouken@2753
  2102
    SDL_Renderer *renderer;
slouken@1985
  2103
slouken@3695
  2104
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2105
slouken@2753
  2106
    renderer = texture->renderer;
slouken@2753
  2107
    if (r) {
slouken@2753
  2108
        *r = texture->r;
slouken@2753
  2109
    }
slouken@2753
  2110
    if (g) {
slouken@2753
  2111
        *g = texture->g;
slouken@2753
  2112
    }
slouken@2753
  2113
    if (b) {
slouken@2753
  2114
        *b = texture->b;
slouken@2753
  2115
    }
slouken@2753
  2116
    return 0;
slouken@1985
  2117
}
slouken@1985
  2118
slouken@1985
  2119
int
slouken@3685
  2120
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@1985
  2121
{
slouken@2753
  2122
    SDL_Renderer *renderer;
slouken@1985
  2123
slouken@3695
  2124
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2125
slouken@2753
  2126
    renderer = texture->renderer;
slouken@2753
  2127
    if (!renderer->SetTextureAlphaMod) {
slouken@2810
  2128
        SDL_Unsupported();
slouken@2753
  2129
        return -1;
slouken@2753
  2130
    }
slouken@2753
  2131
    if (alpha < 255) {
slouken@2753
  2132
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  2133
    } else {
slouken@2753
  2134
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  2135
    }
slouken@2753
  2136
    texture->a = alpha;
slouken@2753
  2137
    return renderer->SetTextureAlphaMod(renderer, texture);
slouken@1985
  2138
}
slouken@1985
  2139
slouken@1985
  2140
int
slouken@3685
  2141
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@1985
  2142
{
slouken@3695
  2143
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2144
slouken@2753
  2145
    if (alpha) {
slouken@2753
  2146
        *alpha = texture->a;
slouken@2753
  2147
    }
slouken@2753
  2148
    return 0;
slouken@1985
  2149
}
slouken@1985
  2150
slouken@1985
  2151
int
slouken@4929
  2152
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
slouken@1985
  2153
{
slouken@2753
  2154
    SDL_Renderer *renderer;
slouken@1985
  2155
slouken@3695
  2156
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2157
slouken@2753
  2158
    renderer = texture->renderer;
slouken@2753
  2159
    if (!renderer->SetTextureBlendMode) {
slouken@2810
  2160
        SDL_Unsupported();
slouken@2753
  2161
        return -1;
slouken@2753
  2162
    }
slouken@2753
  2163
    texture->blendMode = blendMode;
slouken@2753
  2164
    return renderer->SetTextureBlendMode(renderer, texture);
slouken@1985
  2165
}
slouken@1985
  2166
slouken@1985
  2167
int
slouken@4929
  2168
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
slouken@1985
  2169
{
slouken@3695
  2170
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2171
slouken@2753
  2172
    if (blendMode) {
slouken@2753
  2173
        *blendMode = texture->blendMode;
slouken@2753
  2174
    }
slouken@2753
  2175
    return 0;
slouken@1985
  2176
}
slouken@1985
  2177
slouken@1985
  2178
int
slouken@4929
  2179
SDL_SetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode scaleMode)
slouken@1985
  2180
{
slouken@2753
  2181
    SDL_Renderer *renderer;
slouken@1985
  2182
slouken@3695
  2183
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2184
slouken@2753
  2185
    renderer = texture->renderer;
slouken@2753
  2186
    if (!renderer->SetTextureScaleMode) {
slouken@2810
  2187
        SDL_Unsupported();
slouken@2753
  2188
        return -1;
slouken@2753
  2189
    }
slouken@2753
  2190
    texture->scaleMode = scaleMode;
slouken@2753
  2191
    return renderer->SetTextureScaleMode(renderer, texture);
slouken@1985
  2192
}
slouken@1985
  2193
slouken@1985
  2194
int
slouken@4929
  2195
SDL_GetTextureScaleMode(SDL_Texture * texture, SDL_ScaleMode *scaleMode)
slouken@1985
  2196
{
slouken@3695
  2197
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2198
slouken@2753
  2199
    if (scaleMode) {
slouken@2753
  2200
        *scaleMode = texture->scaleMode;
slouken@2753
  2201
    }
slouken@2753
  2202
    return 0;
slouken@1985
  2203
}
slouken@1985
  2204
slouken@1985
  2205
int
slouken@3685
  2206
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@2753
  2207
                  const void *pixels, int pitch)
slouken@1895
  2208
{
slouken@2753
  2209
    SDL_Renderer *renderer;
slouken@2753
  2210
    SDL_Rect full_rect;
slouken@1895
  2211
slouken@3695
  2212
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2213
slouken@2753
  2214
    renderer = texture->renderer;
slouken@2753
  2215
    if (!renderer->UpdateTexture) {
slouken@2810
  2216
        SDL_Unsupported();
slouken@2753
  2217
        return -1;
slouken@2753
  2218
    }
slouken@2753
  2219
    if (!rect) {
slouken@2753
  2220
        full_rect.x = 0;
slouken@2753
  2221
        full_rect.y = 0;
slouken@2753
  2222
        full_rect.w = texture->w;
slouken@2753
  2223
        full_rect.h = texture->h;
slouken@2753
  2224
        rect = &full_rect;
slouken@2753
  2225
    }
slouken@2753
  2226
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1895
  2227
}
slouken@1895
  2228
slouken@1895
  2229
int
slouken@3685
  2230
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
slouken@2753
  2231
                void **pixels, int *pitch)
slouken@1895
  2232
{
slouken@2753
  2233
    SDL_Renderer *renderer;
slouken@2753
  2234
    SDL_Rect full_rect;
slouken@1895
  2235
slouken@3695
  2236
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2237
slouken@2753
  2238
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2239
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@2753
  2240
        return -1;
slouken@2753
  2241
    }
slouken@2753
  2242
    renderer = texture->renderer;
slouken@2753
  2243
    if (!renderer->LockTexture) {
slouken@2810
  2244
        SDL_Unsupported();
slouken@2753
  2245
        return -1;
slouken@2753
  2246
    }
slouken@2753
  2247
    if (!rect) {
slouken@2753
  2248
        full_rect.x = 0;
slouken@2753
  2249
        full_rect.y = 0;
slouken@2753
  2250
        full_rect.w = texture->w;
slouken@2753
  2251
        full_rect.h = texture->h;
slouken@2753
  2252
        rect = &full_rect;
slouken@2753
  2253
    }
slouken@2753
  2254
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@2753
  2255
                                 pitch);
slouken@1895
  2256
}
slouken@1895
  2257
slouken@1895
  2258
void
slouken@3685
  2259
SDL_UnlockTexture(SDL_Texture * texture)
slouken@1895
  2260
{
slouken@2753
  2261
    SDL_Renderer *renderer;
slouken@1895
  2262
slouken@3695
  2263
    CHECK_TEXTURE_MAGIC(texture, );
slouken@3695
  2264
slouken@2753
  2265
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2266
        return;
slouken@2753
  2267
    }
slouken@2753
  2268
    renderer = texture->renderer;
slouken@2753
  2269
    if (!renderer->UnlockTexture) {
slouken@2753
  2270
        return;
slouken@2753
  2271
    }
slouken@2753
  2272
    renderer->UnlockTexture(renderer, texture);
slouken@1895
  2273
}
slouken@1895
  2274
slouken@1895
  2275
void
slouken@3685
  2276
SDL_DirtyTexture(SDL_Texture * texture, int numrects,
slouken@2753
  2277
                 const SDL_Rect * rects)
slouken@1895
  2278
{
slouken@2753
  2279
    SDL_Renderer *renderer;
slouken@1895
  2280
slouken@3695
  2281
    CHECK_TEXTURE_MAGIC(texture, );
slouken@3695
  2282
slouken@2753
  2283
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2284
        return;
slouken@2753
  2285
    }
slouken@2753
  2286
    renderer = texture->renderer;
slouken@2753
  2287
    if (!renderer->DirtyTexture) {
slouken@2753
  2288
        return;
slouken@2753
  2289
    }
slouken@2753
  2290
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1895
  2291
}
slouken@1895
  2292
slouken@1895
  2293
int
slouken@2884
  2294
SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2884
  2295
{
slouken@2884
  2296
    SDL_Renderer *renderer;
slouken@2884
  2297
slouken@3485
  2298
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2299
    if (!renderer) {
slouken@2884
  2300
        return -1;
slouken@2884
  2301
    }
slouken@2884
  2302
    renderer->r = r;
slouken@2884
  2303
    renderer->g = g;
slouken@2884
  2304
    renderer->b = b;
slouken@2884
  2305
    renderer->a = a;
slouken@2927
  2306
    if (renderer->SetDrawColor) {
slouken@2927
  2307
        return renderer->SetDrawColor(renderer);
slouken@2927
  2308
    } else {
slouken@2927
  2309
        return 0;
slouken@2927
  2310
    }
slouken@2884
  2311
}
slouken@2884
  2312
slouken@2884
  2313
int
slouken@2885
  2314
SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@2884
  2315
{
slouken@2884
  2316
    SDL_Renderer *renderer;
slouken@2884
  2317
slouken@3485
  2318
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2319
    if (!renderer) {
slouken@2884
  2320
        return -1;
slouken@2884
  2321
    }
slouken@2884
  2322
    if (r) {
slouken@2884
  2323
        *r = renderer->r;
slouken@2884
  2324
    }
slouken@2884
  2325
    if (g) {
slouken@2884
  2326
        *g = renderer->g;
slouken@2884
  2327
    }
slouken@2884
  2328
    if (b) {
slouken@2884
  2329
        *b = renderer->b;
slouken@2884
  2330
    }
slouken@2884
  2331
    if (a) {
slouken@2884
  2332
        *a = renderer->a;
slouken@2884
  2333
    }
slouken@2928
  2334
    return 0;
slouken@2884
  2335
}
slouken@2884
  2336
slouken@2884
  2337
int
slouken@4929
  2338
SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode)
slouken@2884
  2339
{
slouken@2884
  2340
    SDL_Renderer *renderer;
slouken@2884
  2341
slouken@3485
  2342
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2343
    if (!renderer) {
slouken@2884
  2344
        return -1;
slouken@2884
  2345
    }
slouken@2927
  2346
    renderer->blendMode = blendMode;
slouken@2927
  2347
    if (renderer->SetDrawBlendMode) {
slouken@2927
  2348
        return renderer->SetDrawBlendMode(renderer);
slouken@2927
  2349
    } else {
slouken@2927
  2350
        return 0;
slouken@2884
  2351
    }
slouken@2884
  2352
}
slouken@2884
  2353
slouken@2884
  2354
int
slouken@4929
  2355
SDL_GetRenderDrawBlendMode(SDL_BlendMode *blendMode)
slouken@2884
  2356
{
slouken@2884
  2357
    SDL_Renderer *renderer;
slouken@2884
  2358
slouken@3485
  2359
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2360
    if (!renderer) {
slouken@2884
  2361
        return -1;
slouken@2884
  2362
    }
slouken@2884
  2363
    *blendMode = renderer->blendMode;
slouken@2884
  2364
    return 0;
slouken@2884
  2365
}
slouken@2884
  2366
slouken@2901
  2367
int
slouken@3596
  2368
SDL_RenderClear()
slouken@3536
  2369
{
slouken@2901
  2370
    SDL_Renderer *renderer;
slouken@3536
  2371
slouken@3485
  2372
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2901
  2373
    if (!renderer) {
slouken@2901
  2374
        return -1;
slouken@2901
  2375
    }
slouken@3596
  2376
    if (!renderer->RenderClear) {
slouken@4929
  2377
        SDL_BlendMode blendMode = renderer->blendMode;
slouken@3596
  2378
        int status;
slouken@3596
  2379
slouken@3596
  2380
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@3596
  2381
            SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@3596
  2382
        }
slouken@3596
  2383
slouken@3596
  2384
        status = SDL_RenderFillRect(NULL);
slouken@3596
  2385
slouken@3596
  2386
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@3596
  2387
            SDL_SetRenderDrawBlendMode(blendMode);
slouken@3596
  2388
        }
slouken@3596
  2389
        return status;
slouken@3596
  2390
    }
slouken@3596
  2391
    return renderer->RenderClear(renderer);
slouken@3596
  2392
}
slouken@3596
  2393
slouken@3596
  2394
int
slouken@3596
  2395
SDL_RenderDrawPoint(int x, int y)
slouken@3596
  2396
{
slouken@3596
  2397
    SDL_Point point;
slouken@3596
  2398
slouken@3596
  2399
    point.x = x;
slouken@3596
  2400
    point.y = y;
slouken@3596
  2401
    return SDL_RenderDrawPoints(&point, 1);
slouken@3596
  2402
}
slouken@3596
  2403
slouken@3596
  2404
int
slouken@3596
  2405
SDL_RenderDrawPoints(const SDL_Point * points, int count)
slouken@3596
  2406
{
slouken@3596
  2407
    SDL_Renderer *renderer;
slouken@3596
  2408
slouken@3596
  2409
    if (!points) {
slouken@3596
  2410
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@3596
  2411
        return -1;
slouken@3596
  2412
    }
slouken@3596
  2413
slouken@3596
  2414
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3596
  2415
    if (!renderer) {
slouken@3596
  2416
        return -1;
slouken@3596
  2417
    }
slouken@3596
  2418
    if (!renderer->RenderDrawPoints) {
slouken@2901
  2419
        SDL_Unsupported();
slouken@2901
  2420
        return -1;
slouken@2901
  2421
    }
slouken@3536
  2422
    if (count < 1) {
slouken@2901
  2423
        return 0;
slouken@2901
  2424
    }
slouken@3596
  2425
    return renderer->RenderDrawPoints(renderer, points, count);
slouken@2901
  2426
}
slouken@2901
  2427
slouken@2901
  2428
int
slouken@3596
  2429
SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
slouken@2901
  2430
{
slouken@3536
  2431
    SDL_Point points[2];
slouken@3536
  2432
slouken@3536
  2433
    points[0].x = x1;
slouken@3536
  2434
    points[0].y = y1;
slouken@3536
  2435
    points[1].x = x2;
slouken@3536
  2436
    points[1].y = y2;
slouken@3596
  2437
    return SDL_RenderDrawLines(points, 2);
slouken@3536
  2438
}
slouken@3536
  2439
slouken@3536
  2440
int
slouken@3596
  2441
SDL_RenderDrawLines(const SDL_Point * points, int count)
slouken@3536
  2442
{
slouken@2901
  2443
    SDL_Renderer *renderer;
slouken@3536
  2444
slouken@3536
  2445
    if (!points) {
slouken@3596
  2446
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@3536
  2447
        return -1;
slouken@2901
  2448
    }
slouken@2901
  2449
slouken@3485
  2450
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2901
  2451
    if (!renderer) {
slouken@2901
  2452
        return -1;
slouken@2901
  2453
    }
slouken@3596
  2454
    if (!renderer->RenderDrawLines) {
slouken@2901
  2455
        SDL_Unsupported();
slouken@2901
  2456
        return -1;
slouken@2901
  2457
    }
slouken@3536
  2458
    if (count < 2) {
slouken@3536
  2459
        return 0;
slouken@3536
  2460
    }
slouken@3596
  2461
    return renderer->RenderDrawLines(renderer, points, count);
slouken@2901
  2462
}
slouken@2884
  2463
slouken@2884
  2464
int
slouken@3596
  2465
SDL_RenderDrawRect(const SDL_Rect * rect)
slouken@3536
  2466
{
slouken@3596
  2467
    return SDL_RenderDrawRects(&rect, 1);
slouken@3536
  2468
}
slouken@3536
  2469
slouken@3536
  2470
int
slouken@3596
  2471
SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
slouken@1895
  2472
{
slouken@2753
  2473
    SDL_Renderer *renderer;
slouken@3536
  2474
    int i;
slouken@3536
  2475
slouken@3536
  2476
    if (!rects) {
slouken@3596
  2477
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@3536
  2478
        return -1;
slouken@3536
  2479
    }
slouken@1895
  2480
slouken@3485
  2481
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2810
  2482
    if (!renderer) {
slouken@2810
  2483
        return -1;
slouken@2810
  2484
    }
slouken@3596
  2485
    if (!renderer->RenderDrawRects) {
slouken@2810
  2486
        SDL_Unsupported();
slouken@2753
  2487
        return -1;
slouken@2753
  2488
    }
slouken@3536
  2489
    if (count < 1) {
slouken@3536
  2490
        return 0;
slouken@3536
  2491
    }
slouken@3536
  2492
    /* Check for NULL rect, which means fill entire window */
slouken@3536
  2493
    for (i = 0; i < count; ++i) {
slouken@3536
  2494
        if (rects[i] == NULL) {
slouken@3685
  2495
            SDL_Window *window = renderer->window;
slouken@3536
  2496
            SDL_Rect full_rect;
slouken@3540
  2497
            const SDL_Rect *rect;
slouken@3536
  2498
slouken@3536
  2499
            full_rect.x = 0;
slouken@3536
  2500
            full_rect.y = 0;
slouken@3536
  2501
            full_rect.w = window->w;
slouken@3536
  2502
            full_rect.h = window->h;
slouken@3536
  2503
            rect = &full_rect;
slouken@3596
  2504
            return renderer->RenderDrawRects(renderer, &rect, 1);
slouken@2753
  2505
        }
slouken@2753
  2506
    }
slouken@3596
  2507
    return renderer->RenderDrawRects(renderer, rects, count);
slouken@3596
  2508
}
slouken@3596
  2509
slouken@3596
  2510
int
slouken@3596
  2511
SDL_RenderFillRect(const SDL_Rect * rect)
slouken@3596
  2512
{
slouken@3596
  2513
    return SDL_RenderFillRects(&rect, 1);
slouken@3596
  2514
}
slouken@3596
  2515
slouken@3596
  2516
int
slouken@3596
  2517
SDL_RenderFillRects(const SDL_Rect ** rects, int count)
slouken@3596
  2518
{
slouken@3596
  2519
    SDL_Renderer *renderer;
slouken@3596
  2520
    int i;
slouken@3596
  2521
slouken@3596
  2522
    if (!rects) {
slouken@3596
  2523
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@3596
  2524
        return -1;
slouken@3596
  2525
    }
slouken@3596
  2526
slouken@3596
  2527
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3596
  2528
    if (!renderer) {
slouken@3596
  2529
        return -1;
slouken@3596
  2530
    }
slouken@3596
  2531
    if (!renderer->RenderFillRects) {
slouken@3596
  2532
        SDL_Unsupported();
slouken@3596
  2533
        return -1;
slouken@3596
  2534
    }
slouken@3596
  2535
    if (count < 1) {
slouken@3596
  2536
        return 0;
slouken@3596
  2537
    }
slouken@3596
  2538
    /* Check for NULL rect, which means fill entire window */
slouken@3596
  2539
    for (i = 0; i < count; ++i) {
slouken@3596
  2540
        if (rects[i] == NULL) {
slouken@3685
  2541
            SDL_Window *window = renderer->window;
slouken@3596
  2542
            SDL_Rect full_rect;
slouken@3596
  2543
            const SDL_Rect *rect;
slouken@3596
  2544
slouken@3596
  2545
            full_rect.x = 0;
slouken@3596
  2546
            full_rect.y = 0;
slouken@3596
  2547
            full_rect.w = window->w;
slouken@3596
  2548
            full_rect.h = window->h;
slouken@3596
  2549
            rect = &full_rect;
slouken@3596
  2550
            return renderer->RenderFillRects(renderer, &rect, 1);
slouken@3596
  2551
        }
slouken@3596
  2552
    }
slouken@3596
  2553
    return renderer->RenderFillRects(renderer, rects, count);
slouken@3596
  2554
}
slouken@3596
  2555
slouken@2884
  2556
int
slouken@3685
  2557
SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
slouken@2753
  2558
               const SDL_Rect * dstrect)
slouken@1895
  2559
{
slouken@2753
  2560
    SDL_Renderer *renderer;
slouken@2753
  2561
    SDL_Window *window;
slouken@2753
  2562
    SDL_Rect real_srcrect;
slouken@2753
  2563
    SDL_Rect real_dstrect;
slouken@1895
  2564
slouken@3695
  2565
    CHECK_TEXTURE_MAGIC(texture, -1);
slouken@3695
  2566
slouken@3485
  2567
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3316
  2568
    if (!renderer) {
slouken@2753
  2569
        return -1;
slouken@2753
  2570
    }
slouken@3316
  2571
    if (texture->renderer != renderer) {
slouken@3316
  2572
        SDL_SetError("Texture was not created with this renderer");
slouken@2810
  2573
        return -1;
slouken@2810
  2574
    }
slouken@2810
  2575
    if (!renderer->RenderCopy) {
slouken@2810
  2576
        SDL_Unsupported();
slouken@2753
  2577
        return -1;
slouken@2753
  2578
    }
slouken@3685
  2579
    window = renderer->window;
slouken@2908
  2580
slouken@2908
  2581
    real_srcrect.x = 0;
slouken@2908
  2582
    real_srcrect.y = 0;
slouken@2908
  2583
    real_srcrect.w = texture->w;
slouken@2908
  2584
    real_srcrect.h = texture->h;
slouken@2753
  2585
    if (srcrect) {
slouken@2908
  2586
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@2908
  2587
            return 0;
slouken@2908
  2588
        }
slouken@2753
  2589
    }
slouken@2908
  2590
slouken@2908
  2591
    real_dstrect.x = 0;
slouken@2908
  2592
    real_dstrect.y = 0;
slouken@2908
  2593
    real_dstrect.w = window->w;
slouken@2908
  2594
    real_dstrect.h = window->h;
slouken@2753
  2595
    if (dstrect) {
slouken@2908
  2596
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@2908
  2597
            return 0;
slouken@2908
  2598
        }
slouken@2912
  2599
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@2912
  2600
        if (dstrect->w != real_dstrect.w) {
slouken@2912
  2601
            int deltax = (real_dstrect.x - dstrect->x);
slouken@2912
  2602
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@3333
  2603
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@3333
  2604
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@2912
  2605
        }
slouken@2912
  2606
        if (dstrect->h != real_dstrect.h) {
slouken@2912
  2607
            int deltay = (real_dstrect.y - dstrect->y);
slouken@2912
  2608
            int deltah = (real_dstrect.h - dstrect->h);
slouken@3333
  2609
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@3333
  2610
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@2912
  2611
        }
slouken@2753
  2612
    }
slouken@1895
  2613
slouken@2753
  2614
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@2753
  2615
                                &real_dstrect);
slouken@1895
  2616
}
slouken@1895
  2617
slouken@3427
  2618
int
slouken@3435
  2619
SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
slouken@3435
  2620
                     void * pixels, int pitch)
slouken@3427
  2621
{
slouken@3427
  2622
    SDL_Renderer *renderer;
slouken@3427
  2623
    SDL_Window *window;
slouken@3427
  2624
    SDL_Rect real_rect;
slouken@3427
  2625
slouken@3485
  2626
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3427
  2627
    if (!renderer) {
slouken@3427
  2628
        return -1;
slouken@3427
  2629
    }
slouken@3427
  2630
    if (!renderer->RenderReadPixels) {
slouken@3427
  2631
        SDL_Unsupported();
slouken@3427
  2632
        return -1;
slouken@3427
  2633
    }
slouken@3685
  2634
    window = renderer->window;
slouken@3427
  2635
slouken@3435
  2636
    if (!format) {
slouken@3685
  2637
        format = window->display->current_mode.format;
slouken@3435
  2638
    }
slouken@3435
  2639
slouken@3427
  2640
    real_rect.x = 0;
slouken@3427
  2641
    real_rect.y = 0;
slouken@3427
  2642
    real_rect.w = window->w;
slouken@3427
  2643
    real_rect.h = window->h;
slouken@3427
  2644
    if (rect) {
slouken@3427
  2645
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@3427
  2646
            return 0;
slouken@3427
  2647
        }
slouken@3427
  2648
        if (real_rect.y > rect->y) {
slouken@3427
  2649
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@3427
  2650
        }
slouken@3427
  2651
        if (real_rect.x > rect->x) {
slouken@3685
  2652
            Uint32 format = SDL_CurrentDisplay->current_mode.format;
slouken@3427
  2653
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@3427
  2654
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@3427
  2655
        }
slouken@3427
  2656
    }
slouken@3427
  2657
slouken@3435
  2658
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@3435
  2659
                                      format, pixels, pitch);
slouken@3427
  2660
}
slouken@3427
  2661
slouken@3427
  2662
int
slouken@3435
  2663
SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
slouken@3435
  2664
                      const void * pixels, int pitch)
slouken@3427
  2665
{
slouken@3427
  2666
    SDL_Renderer *renderer;
slouken@3427
  2667
    SDL_Window *window;
slouken@3427
  2668
    SDL_Rect real_rect;
slouken@3427
  2669
slouken@3485
  2670
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3427
  2671
    if (!renderer) {
slouken@3427
  2672
        return -1;
slouken@3427
  2673
    }
slouken@3427
  2674
    if (!renderer->RenderWritePixels) {
slouken@3427
  2675
        SDL_Unsupported();
slouken@3427
  2676
        return -1;
slouken@3427
  2677
    }
slouken@3685
  2678
    window = renderer->window;
slouken@3427
  2679
slouken@3435
  2680
    if (!format) {
slouken@3685
  2681
        format = window->display->current_mode.format;
slouken@3435
  2682
    }
slouken@3435
  2683
slouken@3427
  2684
    real_rect.x = 0;
slouken@3427
  2685
    real_rect.y = 0;
slouken@3427
  2686
    real_rect.w = window->w;
slouken@3427
  2687
    real_rect.h = window->h;
slouken@3427
  2688
    if (rect) {
slouken@3427
  2689
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@3427
  2690
            return 0;
slouken@3427
  2691
        }
slouken@3427
  2692
        if (real_rect.y > rect->y) {
slouken@3427
  2693
            pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@3427
  2694
        }
slouken@3427
  2695
        if (real_rect.x > rect->x) {
slouken@3685
  2696
            Uint32 format = SDL_CurrentDisplay->current_mode.format;
slouken@3427
  2697
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@3427 </