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