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