src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Jan 2010 07:28:51 +0000
changeset 3683 4c5ab6841fdc
parent 3678 8e961ef35d4b
child 3685 64ce267332c6
permissions -rw-r--r--
From: Jjgod Jiang
Date: Mon, 18 Jan 2010 17:46:35 +0800
Subject: [PATCH] Polish text input handling for Mac OS X

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