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