src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 18:04:05 -0800
changeset 5062 e8916fe9cfc8
parent 5048 187d7d446306
child 5103 797b37c0c046
permissions -rw-r--r--
Fixed bug #925

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