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