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