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