src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 22 Jan 2010 16:17:11 +0000
changeset 3693 d3f330fc2009
parent 3692 ad85eb21a559
child 3694 b0a707f589a6
permissions -rw-r--r--
update window 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@3693
   906
    if (display->windows) {
slouken@3693
   907
        display->windows->prev = window;
slouken@3693
   908
    }
slouken@3685
   909
    display->windows = window;
slouken@3685
   910
slouken@3685
   911
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
   912
        SDL_DestroyWindow(window);
slouken@3685
   913
        return NULL;
slouken@3685
   914
    }
slouken@0
   915
slouken@2753
   916
    if (title) {
slouken@3685
   917
        SDL_SetWindowTitle(window, title);
slouken@2753
   918
    }
slouken@2753
   919
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
   920
        SDL_MaximizeWindow(window);
slouken@2753
   921
    }
slouken@2753
   922
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
   923
        SDL_MinimizeWindow(window);
slouken@2753
   924
    }
slouken@2753
   925
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
   926
        SDL_ShowWindow(window);
slouken@3685
   927
    }
slouken@3685
   928
    SDL_UpdateWindowGrab(window);
slouken@3685
   929
slouken@3685
   930
    return window;
slouken@1895
   931
}
slouken@0
   932
slouken@3685
   933
SDL_Window *
slouken@1895
   934
SDL_CreateWindowFrom(const void *data)
slouken@1895
   935
{
slouken@2753
   936
    SDL_VideoDisplay *display;
slouken@3685
   937
    SDL_Window *window;
slouken@0
   938
slouken@2753
   939
    if (!_this) {
slouken@2753
   940
        SDL_UninitializedVideo();
slouken@3685
   941
        return NULL;
slouken@3685
   942
    }
slouken@3685
   943
    display = SDL_CurrentDisplay;
slouken@3685
   944
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3685
   945
    window->id = _this->next_object_id++;
slouken@3685
   946
    window->flags = SDL_WINDOW_FOREIGN;
slouken@3685
   947
    window->display = display;
slouken@3685
   948
    window->next = display->windows;
slouken@3693
   949
    if (display->windows) {
slouken@3693
   950
        display->windows->prev = window;
slouken@3693
   951
    }
slouken@3685
   952
    display->windows = window;
slouken@1895
   953
slouken@2753
   954
    if (!_this->CreateWindowFrom ||
slouken@3685
   955
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
   956
        SDL_DestroyWindow(window);
slouken@3685
   957
        return NULL;
slouken@3685
   958
    }
slouken@3685
   959
    return window;
slouken@1895
   960
}
slouken@1895
   961
slouken@1924
   962
int
slouken@1928
   963
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
   964
{
slouken@3057
   965
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@3057
   966
                                  SDL_WINDOW_OPENGL |
slouken@3057
   967
                                  SDL_WINDOW_BORDERLESS |
slouken@3057
   968
                                  SDL_WINDOW_RESIZABLE |
slouken@3486
   969
                                  SDL_WINDOW_INPUT_GRABBED |
slouken@3486
   970
                                  SDL_WINDOW_FOREIGN);
slouken@2753
   971
    char *title = window->title;
slouken@1956
   972
slouken@2753
   973
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
   974
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
   975
        return -1;
slouken@2753
   976
    }
slouken@3057
   977
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@3057
   978
        if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
   979
            SDL_GL_LoadLibrary(NULL);
slouken@3057
   980
        } else {
slouken@3057
   981
            SDL_GL_UnloadLibrary();
slouken@3057
   982
        }
slouken@3057
   983
    }
slouken@3057
   984
slouken@3057
   985
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
   986
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
   987
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
   988
    } else {
slouken@3057
   989
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
   990
    }
slouken@3057
   991
slouken@3057
   992
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
   993
        _this->DestroyWindow(_this, window);
slouken@2753
   994
    }
slouken@3057
   995
slouken@2753
   996
    window->title = NULL;
slouken@3057
   997
    window->flags = (flags & allowed_flags);
slouken@1956
   998
slouken@3057
   999
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1000
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1001
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1002
                SDL_GL_UnloadLibrary();
slouken@3057
  1003
            }
slouken@3057
  1004
            return -1;
slouken@3057
  1005
        }
slouken@2753
  1006
    }
slouken@3057
  1007
slouken@2753
  1008
    if (title) {
slouken@3685
  1009
        SDL_SetWindowTitle(window, title);
slouken@2753
  1010
        SDL_free(title);
slouken@2753
  1011
    }
slouken@2753
  1012
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
  1013
        SDL_MaximizeWindow(window);
slouken@2753
  1014
    }
slouken@2753
  1015
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
  1016
        SDL_MinimizeWindow(window);
slouken@2753
  1017
    }
slouken@2753
  1018
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
  1019
        SDL_ShowWindow(window);
slouken@2753
  1020
    }
slouken@2875
  1021
    SDL_UpdateWindowGrab(window);
slouken@2875
  1022
slouken@2753
  1023
    return 0;
slouken@1924
  1024
}
slouken@1924
  1025
slouken@3417
  1026
static __inline__ SDL_Renderer *
slouken@3485
  1027
SDL_GetCurrentRenderer(SDL_bool create)
slouken@3417
  1028
{
slouken@3417
  1029
    if (!_this) {
slouken@3417
  1030
        SDL_UninitializedVideo();
slouken@3417
  1031
        return NULL;
slouken@3417
  1032
    }
slouken@3417
  1033
    if (!SDL_CurrentRenderer) {
slouken@3485
  1034
        if (!create) {
slouken@3485
  1035
            SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
slouken@3485
  1036
            return NULL;
slouken@3485
  1037
        }
slouken@3417
  1038
        if (SDL_CreateRenderer(0, -1, 0) < 0) {
slouken@3417
  1039
            return NULL;
slouken@3417
  1040
        }
slouken@3417
  1041
    }
slouken@3417
  1042
    return SDL_CurrentRenderer;
slouken@3417
  1043
}
slouken@3417
  1044
slouken@1895
  1045
Uint32
slouken@3685
  1046
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1047
{
slouken@2753
  1048
    if (!window) {
slouken@2753
  1049
        return 0;
slouken@2753
  1050
    }
slouken@3685
  1051
    return window->id;
slouken@3685
  1052
}
slouken@3685
  1053
slouken@3685
  1054
SDL_Window *
slouken@3685
  1055
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1056
{
slouken@3685
  1057
    SDL_Window *window;
slouken@3685
  1058
    int i;
slouken@3685
  1059
slouken@3685
  1060
    if (!_this) {
slouken@3685
  1061
        return NULL;
slouken@3685
  1062
    }
slouken@3685
  1063
    /* FIXME: Should we keep a separate hash table for these? */
slouken@3685
  1064
    for (i = _this->num_displays; i--;) {
slouken@3685
  1065
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@3685
  1066
        for (window = display->windows; window; window = window->next) {
slouken@3685
  1067
            if (window->id == id) {
slouken@3685
  1068
                return window;
slouken@3685
  1069
            }
slouken@3685
  1070
        }
slouken@3685
  1071
    }
slouken@3685
  1072
    return NULL;
slouken@3685
  1073
}
slouken@3685
  1074
slouken@3685
  1075
Uint32
slouken@3685
  1076
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1077
{
slouken@3685
  1078
    if (!window) {
slouken@3685
  1079
        return 0;
slouken@3685
  1080
    }
slouken@2753
  1081
    return window->flags;
slouken@1895
  1082
}
slouken@1895
  1083
slouken@1895
  1084
void
slouken@3685
  1085
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1086
{
slouken@2753
  1087
    if (!window || title == window->title) {
slouken@2753
  1088
        return;
slouken@2753
  1089
    }
slouken@2753
  1090
    if (window->title) {
slouken@2753
  1091
        SDL_free(window->title);
slouken@2753
  1092
    }
slouken@2753
  1093
    if (title) {
slouken@2753
  1094
        window->title = SDL_strdup(title);
slouken@2753
  1095
    } else {
slouken@2753
  1096
        window->title = NULL;
slouken@2753
  1097
    }
slouken@1956
  1098
slouken@2753
  1099
    if (_this->SetWindowTitle) {
slouken@2753
  1100
        _this->SetWindowTitle(_this, window);
slouken@2753
  1101
    }
slouken@1895
  1102
}
slouken@1895
  1103
slouken@2753
  1104
const char *
slouken@3685
  1105
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1106
{
slouken@2753
  1107
    if (!window) {
slouken@2753
  1108
        return NULL;
slouken@2753
  1109
    }
slouken@2753
  1110
    return window->title;
slouken@1895
  1111
}
slouken@1895
  1112
slouken@1895
  1113
void
slouken@3685
  1114
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1115
{
slouken@2967
  1116
    if (!window) {
slouken@2967
  1117
        return;
slouken@2967
  1118
    }
slouken@2967
  1119
    if (_this->SetWindowIcon) {
slouken@2967
  1120
        _this->SetWindowIcon(_this, window, icon);
slouken@2967
  1121
    }
slouken@2967
  1122
}
slouken@2967
  1123
slouken@2967
  1124
void
slouken@3685
  1125
SDL_SetWindowData(SDL_Window * window, void *userdata)
slouken@1895
  1126
{
slouken@2753
  1127
    if (!window) {
slouken@2753
  1128
        return;
slouken@2753
  1129
    }
slouken@2753
  1130
    window->userdata = userdata;
slouken@1895
  1131
}
slouken@1895
  1132
slouken@2753
  1133
void *
slouken@3685
  1134
SDL_GetWindowData(SDL_Window * window)
slouken@1895
  1135
{
slouken@2753
  1136
    if (!window) {
slouken@2753
  1137
        return NULL;
slouken@2753
  1138
    }
slouken@2753
  1139
    return window->userdata;
slouken@1895
  1140
}
slouken@1895
  1141
slouken@1895
  1142
void
slouken@3685
  1143
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1144
{
slouken@2753
  1145
    if (!window) {
slouken@2753
  1146
        return;
slouken@2753
  1147
    }
slouken@2875
  1148
    if (x != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1149
        window->x = x;
slouken@2753
  1150
    }
slouken@2875
  1151
    if (y != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1152
        window->y = y;
slouken@2753
  1153
    }
slouken@2753
  1154
    if (_this->SetWindowPosition) {
slouken@2753
  1155
        _this->SetWindowPosition(_this, window);
slouken@2753
  1156
    }
slouken@3685
  1157
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
  1158
}
slouken@1895
  1159
slouken@1895
  1160
void
slouken@3685
  1161
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1162
{
slouken@2753
  1163
    if (!window) {
slouken@2753
  1164
        return;
slouken@2753
  1165
    }
slouken@2753
  1166
    if (x) {
slouken@2753
  1167
        *x = window->x;
slouken@2753
  1168
    }
slouken@2753
  1169
    if (y) {
slouken@2753
  1170
        *y = window->y;
slouken@2753
  1171
    }
slouken@1895
  1172
}
slouken@1895
  1173
slouken@1895
  1174
void
slouken@3685
  1175
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1176
{
slouken@2753
  1177
    if (!window) {
slouken@2753
  1178
        return;
slouken@2753
  1179
    }
slouken@2753
  1180
    window->w = w;
slouken@2753
  1181
    window->h = h;
slouken@1895
  1182
slouken@2753
  1183
    if (_this->SetWindowSize) {
slouken@2753
  1184
        _this->SetWindowSize(_this, window);
slouken@2753
  1185
    }
slouken@2849
  1186
    SDL_OnWindowResized(window);
slouken@1895
  1187
}
slouken@1895
  1188
slouken@1895
  1189
void
slouken@3685
  1190
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1191
{
slouken@2860
  1192
    if (window) {
slouken@2849
  1193
        if (w) {
slouken@2849
  1194
            *w = window->w;
slouken@2849
  1195
        }
slouken@2849
  1196
        if (h) {
slouken@2849
  1197
            *h = window->h;
slouken@2849
  1198
        }
slouken@2849
  1199
    } else {
slouken@2849
  1200
        if (w) {
slouken@2849
  1201
            *w = 0;
slouken@2849
  1202
        }
slouken@2849
  1203
        if (h) {
slouken@2849
  1204
            *h = 0;
slouken@2849
  1205
        }
slouken@2753
  1206
    }
slouken@1895
  1207
}
slouken@1895
  1208
slouken@1895
  1209
void
slouken@3685
  1210
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1211
{
slouken@2753
  1212
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1213
        return;
slouken@2753
  1214
    }
slouken@1895
  1215
slouken@2753
  1216
    if (_this->ShowWindow) {
slouken@2753
  1217
        _this->ShowWindow(_this, window);
slouken@2753
  1218
    }
slouken@3685
  1219
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1220
}
slouken@1895
  1221
slouken@1895
  1222
void
slouken@3685
  1223
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1224
{
slouken@2753
  1225
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1226
        return;
slouken@2753
  1227
    }
slouken@1895
  1228
slouken@2753
  1229
    if (_this->HideWindow) {
slouken@2753
  1230
        _this->HideWindow(_this, window);
slouken@2753
  1231
    }
slouken@3685
  1232
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1233
}
slouken@1895
  1234
slouken@1895
  1235
void
slouken@3685
  1236
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1237
{
slouken@2753
  1238
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1239
        return;
slouken@2753
  1240
    }
slouken@2753
  1241
    if (_this->RaiseWindow) {
slouken@2753
  1242
        _this->RaiseWindow(_this, window);
slouken@3502
  1243
    } else {
slouken@3502
  1244
        /* FIXME: What we really want is a way to request focus */
slouken@3685
  1245
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@2753
  1246
    }
slouken@1895
  1247
}
slouken@1895
  1248
slouken@1895
  1249
void
slouken@3685
  1250
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1251
{
slouken@2753
  1252
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@2753
  1253
        return;
slouken@2753
  1254
    }
slouken@1895
  1255
slouken@2753
  1256
    if (_this->MaximizeWindow) {
slouken@2753
  1257
        _this->MaximizeWindow(_this, window);
slouken@2753
  1258
    }
slouken@3685
  1259
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1260
}
slouken@1895
  1261
slouken@1895
  1262
void
slouken@3685
  1263
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1264
{
slouken@2753
  1265
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@2753
  1266
        return;
slouken@2753
  1267
    }
slouken@1895
  1268
slouken@2753
  1269
    if (_this->MinimizeWindow) {
slouken@2753
  1270
        _this->MinimizeWindow(_this, window);
slouken@2753
  1271
    }
slouken@3685
  1272
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1273
}
slouken@1895
  1274
slouken@1895
  1275
void
slouken@3685
  1276
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1277
{
slouken@2753
  1278
    if (!window
slouken@2934
  1279
        || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1280
        return;
slouken@2753
  1281
    }
slouken@1895
  1282
slouken@2753
  1283
    if (_this->RestoreWindow) {
slouken@2753
  1284
        _this->RestoreWindow(_this, window);
slouken@2753
  1285
    }
slouken@3685
  1286
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1287
}
slouken@1895
  1288
slouken@1895
  1289
int
slouken@3685
  1290
SDL_SetWindowFullscreen(SDL_Window * window, int fullscreen)
slouken@1895
  1291
{
slouken@2753
  1292
    if (!window) {
slouken@2753
  1293
        return -1;
slouken@2753
  1294
    }
slouken@2753
  1295
    if (fullscreen) {
slouken@2753
  1296
        fullscreen = SDL_WINDOW_FULLSCREEN;
slouken@2753
  1297
    }
slouken@2753
  1298
    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
slouken@2753
  1299
        return 0;
slouken@2753
  1300
    }
slouken@2753
  1301
    if (fullscreen) {
slouken@2753
  1302
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
  1303
slouken@3502
  1304
        SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@2753
  1305
    } else {
slouken@2753
  1306
        window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@1895
  1307
slouken@3502
  1308
        SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@2753
  1309
    }
slouken@2753
  1310
    return 0;
slouken@1895
  1311
}
slouken@1895
  1312
slouken@1895
  1313
void
slouken@3685
  1314
SDL_SetWindowGrab(SDL_Window * window, int mode)
slouken@1895
  1315
{
slouken@2753
  1316
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@2753
  1317
        return;
slouken@2753
  1318
    }
slouken@2753
  1319
    if (mode) {
slouken@2753
  1320
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1321
    } else {
slouken@2753
  1322
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1323
    }
slouken@2875
  1324
    SDL_UpdateWindowGrab(window);
slouken@2875
  1325
}
slouken@1895
  1326
slouken@2875
  1327
static void
slouken@2876
  1328
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1329
{
slouken@2753
  1330
    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
slouken@2753
  1331
        _this->SetWindowGrab(_this, window);
slouken@2753
  1332
    }
slouken@1895
  1333
}
slouken@1895
  1334
slouken@1895
  1335
int
slouken@3685
  1336
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  1337
{
slouken@2753
  1338
    if (!window) {
slouken@2753
  1339
        return 0;
slouken@2753
  1340
    }
slouken@2753
  1341
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1342
}
slouken@1895
  1343
slouken@1895
  1344
void
slouken@1895
  1345
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1346
{
slouken@3685
  1347
    SDL_RaiseWindow(window);
slouken@3502
  1348
    SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@1895
  1349
}
slouken@1895
  1350
slouken@1895
  1351
void
slouken@1895
  1352
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1353
{
slouken@3502
  1354
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@1895
  1355
}
slouken@1895
  1356
slouken@1895
  1357
void
slouken@1970
  1358
SDL_OnWindowResized(SDL_Window * window)
slouken@1970
  1359
{
slouken@2753
  1360
    SDL_Renderer *renderer = window->renderer;
slouken@1970
  1361
slouken@2753
  1362
    if (renderer && renderer->DisplayModeChanged) {
slouken@2753
  1363
        renderer->DisplayModeChanged(renderer);
slouken@2753
  1364
    }
slouken@1970
  1365
}
slouken@1970
  1366
slouken@1970
  1367
void
slouken@3502
  1368
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  1369
{
slouken@3502
  1370
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3502
  1371
}
slouken@3502
  1372
slouken@3502
  1373
void
slouken@3502
  1374
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  1375
{
slouken@3685
  1376
    SDL_RaiseWindow(window);
slouken@3502
  1377
    SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@3502
  1378
}
slouken@3502
  1379
slouken@3502
  1380
void
slouken@1895
  1381
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1382
{
slouken@3685
  1383
    SDL_VideoDisplay *display = window->display;
slouken@1895
  1384
slouken@2753
  1385
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@3500
  1386
        _this->SetDisplayGammaRamp(_this, display, display->gamma);
slouken@2753
  1387
    }
slouken@2876
  1388
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1389
        && _this->SetWindowGrab) {
slouken@2753
  1390
        _this->SetWindowGrab(_this, window);
slouken@2753
  1391
    }
slouken@1895
  1392
}
slouken@1895
  1393
slouken@1895
  1394
void
slouken@1895
  1395
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1396
{
slouken@3685
  1397
    SDL_VideoDisplay *display = window->display;
slouken@1895
  1398
slouken@3512
  1399
    /* If we're fullscreen on a single-head system and lose focus, minimize */
slouken@3512
  1400
    if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
slouken@3512
  1401
        _this->num_displays == 1) {
slouken@3685
  1402
        SDL_MinimizeWindow(window);
slouken@3512
  1403
    }
slouken@3512
  1404
slouken@2753
  1405
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@3500
  1406
        _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
slouken@2753
  1407
    }
slouken@2876
  1408
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1409
        && _this->SetWindowGrab) {
slouken@2753
  1410
        _this->SetWindowGrab(_this, window);
slouken@2753
  1411
    }
slouken@1895
  1412
}
slouken@1895
  1413
slouken@3685
  1414
SDL_Window *
slouken@1895
  1415
SDL_GetFocusWindow(void)
slouken@1895
  1416
{
slouken@2753
  1417
    SDL_VideoDisplay *display;
slouken@3685
  1418
    SDL_Window *window;
slouken@1895
  1419
slouken@2753
  1420
    if (!_this) {
slouken@3685
  1421
        return NULL;
slouken@3685
  1422
    }
slouken@3685
  1423
    display = SDL_CurrentDisplay;
slouken@3685
  1424
    for (window = display->windows; window; window = window->next) {
slouken@2753
  1425
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  1426
            return window;
slouken@2753
  1427
        }
slouken@2753
  1428
    }
slouken@3685
  1429
    return NULL;
slouken@1895
  1430
}
slouken@1895
  1431
slouken@1895
  1432
void
slouken@3685
  1433
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  1434
{
slouken@3685
  1435
    SDL_VideoDisplay *display;
slouken@3685
  1436
slouken@3685
  1437
    if (!_this || !window || !window->id) {
slouken@3685
  1438
        SDL_SetError("Invalid window");
slouken@2753
  1439
        return;
slouken@2753
  1440
    }
slouken@3503
  1441
slouken@3685
  1442
    if (window->title) {
slouken@3685
  1443
        SDL_free(window->title);
slouken@3685
  1444
    }
slouken@3685
  1445
    if (window->renderer) {
slouken@3685
  1446
        SDL_DestroyRenderer(window);
slouken@3685
  1447
    }
slouken@3685
  1448
slouken@3685
  1449
    /* Restore video mode, etc. */
slouken@3685
  1450
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3685
  1451
slouken@3685
  1452
    if (_this->DestroyWindow) {
slouken@3685
  1453
        _this->DestroyWindow(_this, window);
slouken@3685
  1454
    }
slouken@3685
  1455
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  1456
        SDL_GL_UnloadLibrary();
slouken@3685
  1457
    }
slouken@3685
  1458
slouken@3685
  1459
    /* Unlink the window from the list */
slouken@3685
  1460
    display = window->display;
slouken@3693
  1461
    if (window->next) {
slouken@3693
  1462
        window->next->prev = window->prev;
slouken@3693
  1463
    }
slouken@3685
  1464
    if (window->prev) {
slouken@3685
  1465
        window->prev->next = window->next;
slouken@3685
  1466
    } else {
slouken@3685
  1467
        display->windows = window->next;
slouken@3685
  1468
    }
slouken@3685
  1469
slouken@3685
  1470
    /* Clear the ID so we know it was destroyed */
slouken@3685
  1471
    window->id = 0;
slouken@3685
  1472
slouken@3685
  1473
    SDL_free(window);
slouken@1895
  1474
}
slouken@1895
  1475
slouken@1895
  1476
void
slouken@3500
  1477
SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
slouken@1895
  1478
{
slouken@2753
  1479
    SDL_RenderDriver *render_drivers;
slouken@1895
  1480
slouken@2753
  1481
    render_drivers =
slouken@2753
  1482
        SDL_realloc(display->render_drivers,
slouken@2753
  1483
                    (display->num_render_drivers +
slouken@2753
  1484
                     1) * sizeof(*render_drivers));
slouken@2753
  1485
    if (render_drivers) {
slouken@2753
  1486
        render_drivers[display->num_render_drivers] = *driver;
slouken@2753
  1487
        display->render_drivers = render_drivers;
slouken@2753
  1488
        display->num_render_drivers++;
slouken@2753
  1489
    }
slouken@1895
  1490
}
slouken@1895
  1491
slouken@1895
  1492
int
slouken@1969
  1493
SDL_GetNumRenderDrivers(void)
slouken@1895
  1494
{
slouken@2753
  1495
    if (_this) {
slouken@3685
  1496
        return SDL_CurrentDisplay->num_render_drivers;
slouken@2753
  1497
    }
slouken@2753
  1498
    return 0;
slouken@1895
  1499
}
slouken@1895
  1500
slouken@1895
  1501
int
slouken@1969
  1502
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
slouken@1895
  1503
{
slouken@2753
  1504
    if (!_this) {
slouken@2753
  1505
        SDL_UninitializedVideo();
slouken@2753
  1506
        return -1;
slouken@2753
  1507
    }
slouken@2753
  1508
    if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
slouken@2753
  1509
        SDL_SetError("index must be in the range of 0 - %d",
slouken@2753
  1510
                     SDL_GetNumRenderDrivers() - 1);
slouken@2753
  1511
        return -1;
slouken@2753
  1512
    }
slouken@3685
  1513
    *info = SDL_CurrentDisplay->render_drivers[index].info;
slouken@2753
  1514
    return 0;
slouken@1895
  1515
}
slouken@1895
  1516
slouken@1895
  1517
int
slouken@3685
  1518
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
slouken@1895
  1519
{
slouken@2753
  1520
    if (!window) {
slouken@3685
  1521
        SDL_SetError("Invalid window");
slouken@3091
  1522
        return -1;
slouken@2753
  1523
    }
slouken@3424
  1524
slouken@3424
  1525
    /* Free any existing renderer */
slouken@3685
  1526
    SDL_DestroyRenderer(window);
slouken@3424
  1527
slouken@2753
  1528
    if (index < 0) {
slouken@3488
  1529
        char *override = SDL_getenv("SDL_VIDEO_RENDERER");
slouken@3488
  1530
        int n = SDL_GetNumRenderDrivers();
slouken@3488
  1531
lestat@3489
  1532
#if SDL_VIDEO_RENDER_OGL
slouken@3488
  1533
        if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
slouken@3488
  1534
            override = "opengl";
slouken@3488
  1535
        }
lestat@3489
  1536
#endif /* SDL_VIDEO_RENDER_OGL */
lestat@3489
  1537
#if SDL_VIDEO_RENDER_OGL_ES
lestat@3489
  1538
        if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
lestat@3489
  1539
            override = "opengl_es";
lestat@3489
  1540
        }
lestat@3489
  1541
#endif /* SDL_VIDEO_RENDER_OGL_ES */
slouken@3424
  1542
        if (override) {
slouken@3488
  1543
            for (index = 0; index < n; ++index) {
slouken@3424
  1544
                SDL_RenderDriver *driver =
slouken@3685
  1545
                    &SDL_CurrentDisplay->render_drivers[index];
slouken@3488
  1546
slouken@3424
  1547
                if (SDL_strcasecmp(override, driver->info.name) == 0) {
slouken@3488
  1548
                    /* Create a new renderer instance */
slouken@3488
  1549
                    window->renderer = driver->CreateRenderer(window, flags);
slouken@3424
  1550
                    break;
slouken@3424
  1551
                }
slouken@3424
  1552
            }
slouken@3488
  1553
        } else {
slouken@3488
  1554
            for (index = 0; index < n; ++index) {
slouken@3488
  1555
                SDL_RenderDriver *driver =
slouken@3685
  1556
                    &SDL_CurrentDisplay->render_drivers[index];
slouken@3488
  1557
slouken@3488
  1558
                if ((driver->info.flags & flags) == flags) {
slouken@3488
  1559
                    /* Create a new renderer instance */
slouken@3488
  1560
                    window->renderer = driver->CreateRenderer(window, flags);
slouken@3488
  1561
                    if (window->renderer) {
slouken@3488
  1562
                        /* Yay, we got one! */
slouken@3488
  1563
                        break;
slouken@3488
  1564
                    }
slouken@2753
  1565
                }
slouken@2753
  1566
            }
slouken@2753
  1567
        }
slouken@2753
  1568
        if (index == n) {
slouken@2753
  1569
            SDL_SetError("Couldn't find matching render driver");
slouken@2753
  1570
            return -1;
slouken@2753
  1571
        }
slouken@3424
  1572
    } else {
slouken@3424
  1573
        if (index >= SDL_GetNumRenderDrivers()) {
slouken@3424
  1574
            SDL_SetError("index must be -1 or in the range of 0 - %d",
slouken@3424
  1575
                         SDL_GetNumRenderDrivers() - 1);
slouken@3424
  1576
            return -1;
slouken@3424
  1577
        }
slouken@3424
  1578
        /* Create a new renderer instance */
slouken@3685
  1579
        window->renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags);
slouken@2753
  1580
    }
slouken@3092
  1581
slouken@3139
  1582
    if (window->renderer == NULL) {
slouken@3139
  1583
        /* Assuming renderer set its error */
slouken@3139
  1584
        return -1;
slouken@3092
  1585
    }
slouken@3092
  1586
slouken@3685
  1587
    SDL_SelectRenderer(window);
slouken@1895
  1588
slouken@2753
  1589
    return 0;
slouken@1895
  1590
}
slouken@1895
  1591
slouken@1895
  1592
int
slouken@3685
  1593
SDL_SelectRenderer(SDL_Window * window)
slouken@1895
  1594
{
slouken@2753
  1595
    SDL_Renderer *renderer;
slouken@1895
  1596
slouken@3318
  1597
    if (!window) {
slouken@3685
  1598
        SDL_SetError("Invalid window");
slouken@2753
  1599
        return -1;
slouken@2753
  1600
    }
slouken@2753
  1601
    renderer = window->renderer;
slouken@3485
  1602
    if (!renderer) {
slouken@3485
  1603
        SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
slouken@3485
  1604
        return -1;
slouken@3485
  1605
    }
slouken@3485
  1606
    if (renderer->ActivateRenderer) {
slouken@3485
  1607
        if (renderer->ActivateRenderer(renderer) < 0) {
slouken@2753
  1608
            return -1;
slouken@2753
  1609
        }
slouken@2753
  1610
    }
slouken@3685
  1611
    SDL_CurrentDisplay->current_renderer = renderer;
slouken@2753
  1612
    return 0;
slouken@1895
  1613
}
slouken@1895
  1614
slouken@1969
  1615
int
slouken@1969
  1616
SDL_GetRendererInfo(SDL_RendererInfo * info)
slouken@1969
  1617
{
slouken@3485
  1618
    SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
slouken@3417
  1619
    if (!renderer) {
slouken@2753
  1620
        return -1;
slouken@2753
  1621
    }
slouken@3417
  1622
    *info = renderer->info;
slouken@2753
  1623
    return 0;
slouken@1969
  1624
}
slouken@1969
  1625
slouken@3685
  1626
SDL_Texture *
slouken@1895
  1627
SDL_CreateTexture(Uint32 format, int access, int w, int h)
slouken@1895
  1628
{
slouken@2753
  1629
    SDL_Renderer *renderer;
slouken@2753
  1630
    SDL_Texture *texture;
slouken@1895
  1631
slouken@3485
  1632
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2810
  1633
    if (!renderer) {
slouken@2810
  1634
        return 0;
slouken@2810
  1635
    }
slouken@2810
  1636
    if (!renderer->CreateTexture) {
slouken@2810
  1637
        SDL_Unsupported();
slouken@2753
  1638
        return 0;
slouken@2753
  1639
    }
slouken@2753
  1640
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@2753
  1641
    if (!texture) {
slouken@2753
  1642
        SDL_OutOfMemory();
slouken@2753
  1643
        return 0;
slouken@2753
  1644
    }
slouken@2753
  1645
    texture->format = format;
slouken@2753
  1646
    texture->access = access;
slouken@2753
  1647
    texture->w = w;
slouken@2753
  1648
    texture->h = h;
slouken@2753
  1649
    texture->r = 255;
slouken@2753
  1650
    texture->g = 255;
slouken@2753
  1651
    texture->b = 255;
slouken@2753
  1652
    texture->a = 255;
slouken@2753
  1653
    texture->renderer = renderer;
slouken@3685
  1654
    texture->next = renderer->textures;
slouken@3692
  1655
    if (renderer->textures) {
slouken@3692
  1656
        renderer->textures->prev = texture;
slouken@3692
  1657
    }
slouken@3685
  1658
    renderer->textures = texture;
slouken@1895
  1659
slouken@2753
  1660
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@3685
  1661
        SDL_DestroyTexture(texture);
slouken@2753
  1662
        return 0;
slouken@2753
  1663
    }
slouken@3685
  1664
    return texture;
slouken@1895
  1665
}
slouken@1895
  1666
slouken@3685
  1667
SDL_Texture *
slouken@2222
  1668
SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
slouken@1895
  1669
{
slouken@3685
  1670
    SDL_Texture *texture;
slouken@3057
  1671
    Uint32 requested_format = format;
slouken@2753
  1672
    SDL_PixelFormat *fmt;
lestat@3166
  1673
    SDL_Renderer *renderer;
slouken@2753
  1674
    int bpp;
slouken@2753
  1675
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
  1676
slouken@2753
  1677
    if (!surface) {
slouken@2753
  1678
        SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
slouken@2753
  1679
        return 0;
slouken@2753
  1680
    }
slouken@2753
  1681
    fmt = surface->format;
slouken@1895
  1682
slouken@3485
  1683
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
lestat@3166
  1684
    if (!renderer) {
lestat@3166
  1685
        return 0;
lestat@3166
  1686
    }
lestat@3166
  1687
slouken@2753
  1688
    if (format) {
slouken@2753
  1689
        if (!SDL_PixelFormatEnumToMasks
slouken@2753
  1690
            (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@2753
  1691
            SDL_SetError("Unknown pixel format");
slouken@2753
  1692
            return 0;
slouken@2753
  1693
        }
slouken@2753
  1694
    } else {
slouken@2753
  1695
        if (surface->format->Amask
slouken@2753
  1696
            || !(surface->map->info.flags &
slouken@2753
  1697
                 (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
slouken@3196
  1698
            Uint32 it;
lestat@3178
  1699
            int pfmt;
lestat@3178
  1700
lestat@3178
  1701
            /* Pixel formats, sorted by best first */
slouken@3186
  1702
            static const Uint32 sdl_pformats[] = {
slouken@3186
  1703
                SDL_PIXELFORMAT_ARGB8888,
slouken@3186
  1704
                SDL_PIXELFORMAT_RGBA8888,
slouken@3186
  1705
                SDL_PIXELFORMAT_ABGR8888,
slouken@3186
  1706
                SDL_PIXELFORMAT_BGRA8888,
slouken@3186
  1707
                SDL_PIXELFORMAT_RGB888,
slouken@3186
  1708
                SDL_PIXELFORMAT_BGR888,
slouken@3186
  1709
                SDL_PIXELFORMAT_RGB24,
slouken@3186
  1710
                SDL_PIXELFORMAT_BGR24,
slouken@3186
  1711
                SDL_PIXELFORMAT_RGB565,
slouken@3186
  1712
                SDL_PIXELFORMAT_BGR565,
slouken@3186
  1713
                SDL_PIXELFORMAT_ARGB1555,
slouken@3186
  1714
                SDL_PIXELFORMAT_ABGR1555,
slouken@3186
  1715
                SDL_PIXELFORMAT_RGB555,
slouken@3186
  1716
                SDL_PIXELFORMAT_BGR555,
slouken@3186
  1717
                SDL_PIXELFORMAT_ARGB4444,
slouken@3186
  1718
                SDL_PIXELFORMAT_ABGR4444,
slouken@3186
  1719
                SDL_PIXELFORMAT_RGB444,
slouken@3186
  1720
                SDL_PIXELFORMAT_ARGB2101010,
slouken@3186
  1721
                SDL_PIXELFORMAT_INDEX8,
slouken@3186
  1722
                SDL_PIXELFORMAT_INDEX4LSB,
slouken@3186
  1723
                SDL_PIXELFORMAT_INDEX4MSB,
slouken@3186
  1724
                SDL_PIXELFORMAT_RGB332,
slouken@3186
  1725
                SDL_PIXELFORMAT_INDEX1LSB,
slouken@3186
  1726
                SDL_PIXELFORMAT_INDEX1MSB,
slouken@3186
  1727
                SDL_PIXELFORMAT_UNKNOWN
slouken@3186
  1728
            };
lestat@3178
  1729
slouken@2753
  1730
            bpp = fmt->BitsPerPixel;
slouken@2753
  1731
            Rmask = fmt->Rmask;
slouken@2753
  1732
            Gmask = fmt->Gmask;
slouken@2753
  1733
            Bmask = fmt->Bmask;
slouken@2753
  1734
            Amask = fmt->Amask;
lestat@3178
  1735
slouken@3186
  1736
            format =
slouken@3186
  1737
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
lestat@3178
  1738
            if (!format) {
lestat@3178
  1739
                SDL_SetError("Unknown pixel format");
lestat@3178
  1740
                return 0;
lestat@3178
  1741
            }
lestat@3178
  1742
lestat@3178
  1743
            /* Search requested format in the supported texture */
lestat@3178
  1744
            /* formats by current renderer                      */
slouken@3186
  1745
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@3186
  1746
                if (renderer->info.texture_formats[it] == format) {
slouken@3186
  1747
                    break;
lestat@3178
  1748
                }
lestat@3178
  1749
            }
lestat@3178
  1750
lestat@3178
  1751
            /* If requested format can't be found, search any best */
lestat@3178
  1752
            /* format which renderer provides                      */
slouken@3186
  1753
            if (it == renderer->info.num_texture_formats) {
slouken@3186
  1754
                pfmt = 0;
slouken@3186
  1755
                for (;;) {
slouken@3186
  1756
                    if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
lestat@3178
  1757
                        break;
lestat@3178
  1758
                    }
lestat@3178
  1759
slouken@3186
  1760
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@3186
  1761
                         it++) {
slouken@3186
  1762
                        if (renderer->info.texture_formats[it] ==
slouken@3186
  1763
                            sdl_pformats[pfmt]) {
slouken@3186
  1764
                            break;
slouken@3186
  1765
                        }
lestat@3178
  1766
                    }
lestat@3178
  1767
slouken@3186
  1768
                    if (it != renderer->info.num_texture_formats) {
slouken@3186
  1769
                        /* The best format has been found */
slouken@3186
  1770
                        break;
lestat@3178
  1771
                    }
lestat@3178
  1772
                    pfmt++;
lestat@3178
  1773
                }
lestat@3178
  1774
lestat@3178
  1775
                /* If any format can't be found, then return an error */
slouken@3186
  1776
                if (it == renderer->info.num_texture_formats) {
slouken@3186
  1777
                    SDL_SetError
slouken@3186
  1778
                        ("Any of the supported pixel formats can't be found");
lestat@3178
  1779
                    return 0;
lestat@3178
  1780
                }
lestat@3178
  1781
lestat@3178
  1782
                /* Convert found pixel format back to color masks */
slouken@3186
  1783
                if (SDL_PixelFormatEnumToMasks
slouken@3186
  1784
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@3186
  1785
                     &Bmask, &Amask) != SDL_TRUE) {
lestat@3178
  1786
                    SDL_SetError("Unknown pixel format");
lestat@3178
  1787
                    return 0;
lestat@3178
  1788
                }
lestat@3178
  1789
            }
slouken@2753
  1790
        } else {
slouken@2753
  1791
            /* Need a format with alpha */
slouken@3196
  1792
            Uint32 it;
lestat@3166
  1793
            int apfmt;
lestat@3166
  1794
lestat@3166
  1795
            /* Pixel formats with alpha, sorted by best first */
slouken@3169
  1796
            static const Uint32 sdl_alpha_pformats[] = {
slouken@3169
  1797
                SDL_PIXELFORMAT_ARGB8888,
slouken@3169
  1798
                SDL_PIXELFORMAT_RGBA8888,
slouken@3169
  1799
                SDL_PIXELFORMAT_ABGR8888,
slouken@3169
  1800
                SDL_PIXELFORMAT_BGRA8888,
slouken@3169
  1801
                SDL_PIXELFORMAT_ARGB1555,
slouken@3169
  1802
                SDL_PIXELFORMAT_ABGR1555,
slouken@3169
  1803
                SDL_PIXELFORMAT_ARGB4444,
slouken@3169
  1804
                SDL_PIXELFORMAT_ABGR4444,
slouken@3169
  1805
                SDL_PIXELFORMAT_ARGB2101010,
slouken@3169
  1806
                SDL_PIXELFORMAT_UNKNOWN
slouken@3169
  1807
            };
lestat@3166
  1808
lestat@3178
  1809
            if (surface->format->Amask) {
lestat@3178
  1810
                /* If surface already has alpha, then try an original */
lestat@3178
  1811
                /* surface format first                               */
lestat@3178
  1812
                bpp = fmt->BitsPerPixel;
lestat@3178
  1813
                Rmask = fmt->Rmask;
lestat@3178
  1814
                Gmask = fmt->Gmask;
lestat@3178
  1815
                Bmask = fmt->Bmask;
lestat@3178
  1816
                Amask = fmt->Amask;
lestat@3178
  1817
            } else {
lestat@3178
  1818
                bpp = 32;
lestat@3178
  1819
                Rmask = 0x00FF0000;
lestat@3178
  1820
                Gmask = 0x0000FF00;
lestat@3178
  1821
                Bmask = 0x000000FF;
lestat@3178
  1822
                Amask = 0xFF000000;
lestat@3178
  1823
            }
lestat@3166
  1824
slouken@3169
  1825
            format =
slouken@3169
  1826
                SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
lestat@3166
  1827
            if (!format) {
lestat@3166
  1828
                SDL_SetError("Unknown pixel format");
lestat@3166
  1829
                return 0;
lestat@3166
  1830
            }
lestat@3166
  1831
lestat@3166
  1832
            /* Search this format in the supported texture formats */
lestat@3166
  1833
            /* by current renderer                                 */
slouken@3169
  1834
            for (it = 0; it < renderer->info.num_texture_formats; it++) {
slouken@3169
  1835
                if (renderer->info.texture_formats[it] == format) {
slouken@3169
  1836
                    break;
lestat@3166
  1837
                }
lestat@3166
  1838
            }
lestat@3166
  1839
lestat@3166
  1840
            /* If this format can't be found, search any best       */
lestat@3166
  1841
            /* compatible format with alpha which renderer provides */
slouken@3169
  1842
            if (it == renderer->info.num_texture_formats) {
slouken@3169
  1843
                apfmt = 0;
slouken@3169
  1844
                for (;;) {
slouken@3169
  1845
                    if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
lestat@3166
  1846
                        break;
lestat@3166
  1847
                    }
lestat@3166
  1848
slouken@3169
  1849
                    for (it = 0; it < renderer->info.num_texture_formats;
slouken@3169
  1850
                         it++) {
slouken@3169
  1851
                        if (renderer->info.texture_formats[it] ==
slouken@3169
  1852
                            sdl_alpha_pformats[apfmt]) {
slouken@3169
  1853
                            break;
slouken@3169
  1854
                        }
lestat@3166
  1855
                    }
lestat@3166
  1856
slouken@3169
  1857
                    if (it != renderer->info.num_texture_formats) {
slouken@3169
  1858
                        /* Compatible format has been found */
slouken@3169
  1859
                        break;
lestat@3166
  1860
                    }
lestat@3166
  1861
                    apfmt++;
lestat@3166
  1862
                }
lestat@3166
  1863
lestat@3166
  1864
                /* If compatible format can't be found, then return an error */
slouken@3169
  1865
                if (it == renderer->info.num_texture_formats) {
lestat@3166
  1866
                    SDL_SetError("Compatible pixel format can't be found");
lestat@3166
  1867
                    return 0;
lestat@3166
  1868
                }
lestat@3166
  1869
lestat@3166
  1870
                /* Convert found pixel format back to color masks */
slouken@3169
  1871
                if (SDL_PixelFormatEnumToMasks
slouken@3169
  1872
                    (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
slouken@3169
  1873
                     &Bmask, &Amask) != SDL_TRUE) {
lestat@3166
  1874
                    SDL_SetError("Unknown pixel format");
lestat@3166
  1875
                    return 0;
lestat@3166
  1876
                }
lestat@3166
  1877
            }
slouken@2753
  1878
        }
lestat@3166
  1879
slouken@2753
  1880
        format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2753
  1881
        if (!format) {
slouken@2753
  1882
            SDL_SetError("Unknown pixel format");
slouken@2753
  1883
            return 0;
slouken@2753
  1884
        }
slouken@2753
  1885
    }
slouken@1895
  1886
slouken@3685
  1887
    texture =
slouken@2753
  1888
        SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
slouken@2753
  1889
                          surface->h);
slouken@3685
  1890
    if (!texture && !requested_format) {
slouken@3057
  1891
        SDL_DisplayMode desktop_mode;
slouken@3057
  1892
        SDL_GetDesktopDisplayMode(&desktop_mode);
slouken@3057
  1893
        format = desktop_mode.format;
slouken@3685
  1894
        texture =
slouken@3057
  1895
            SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
slouken@3057
  1896
                              surface->h);
slouken@3057
  1897
    }
slouken@3685
  1898
    if (!texture) {
slouken@2753
  1899
        return 0;
slouken@2753
  1900
    }
slouken@2753
  1901
    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
slouken@2753
  1902
        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
slouken@2753
  1903
        if (SDL_MUSTLOCK(surface)) {
slouken@3315
  1904
            SDL_LockSurface(surface);
slouken@3685
  1905
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@2753
  1906
                              surface->pitch);
slouken@2753
  1907
            SDL_UnlockSurface(surface);
slouken@2753
  1908
        } else {
slouken@3685
  1909
            SDL_UpdateTexture(texture, NULL, surface->pixels,
slouken@2753
  1910
                              surface->pitch);
slouken@2753
  1911
        }
slouken@2753
  1912
    } else {
slouken@2967
  1913
        SDL_PixelFormat dst_fmt;
slouken@2753
  1914
        SDL_Surface *dst = NULL;
slouken@1895
  1915
slouken@2753
  1916
        /* Set up a destination surface for the texture update */
slouken@2967
  1917
        SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
slouken@2967
  1918
        if (SDL_ISPIXELFORMAT_INDEXED(format)) {
slouken@2967
  1919
            dst_fmt.palette =
slouken@2967
  1920
                SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
slouken@2967
  1921
            if (dst_fmt.palette) {
slouken@2967
  1922
                /*
slouken@2967
  1923
                 * FIXME: Should we try to copy
slouken@2967
  1924
                 * fmt->palette?
slouken@2967
  1925
                 */
slouken@2967
  1926
                SDL_DitherColors(dst_fmt.palette->colors,
slouken@2967
  1927
                                 SDL_BITSPERPIXEL(format));
slouken@2753
  1928
            }
slouken@2967
  1929
        }
slouken@2967
  1930
        dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
slouken@2967
  1931
        if (dst) {
slouken@3685
  1932
            SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
slouken@2967
  1933
            SDL_FreeSurface(dst);
slouken@2967
  1934
        }
slouken@2967
  1935
        if (dst_fmt.palette) {
slouken@2967
  1936
            SDL_FreePalette(dst_fmt.palette);
slouken@2753
  1937
        }
slouken@2753
  1938
        if (!dst) {
slouken@3685
  1939
            SDL_DestroyTexture(texture);
slouken@2753
  1940
            return 0;
slouken@2753
  1941
        }
slouken@2753
  1942
    }
slouken@2222
  1943
slouken@3053
  1944
    {
slouken@3053
  1945
        Uint8 r, g, b, a;
slouken@3053
  1946
        int blendMode;
slouken@3053
  1947
        int scaleMode;
slouken@3053
  1948
slouken@3053
  1949
        SDL_GetSurfaceColorMod(surface, &r, &g, &b);
slouken@3685
  1950
        SDL_SetTextureColorMod(texture, r, g, b);
slouken@3053
  1951
slouken@3053
  1952
        SDL_GetSurfaceAlphaMod(surface, &a);
slouken@3685
  1953
        SDL_SetTextureAlphaMod(texture, a);
slouken@3053
  1954
slouken@3053
  1955
        SDL_GetSurfaceBlendMode(surface, &blendMode);
slouken@3685
  1956
        SDL_SetTextureBlendMode(texture, blendMode);
slouken@3053
  1957
slouken@3053
  1958
        SDL_GetSurfaceScaleMode(surface, &scaleMode);
slouken@3685
  1959
        SDL_SetTextureScaleMode(texture, scaleMode);
slouken@3053
  1960
    }
slouken@3053
  1961
slouken@2753
  1962
    if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
slouken@3685
  1963
        SDL_SetTexturePalette(texture, fmt->palette->colors, 0,
slouken@2753
  1964
                              fmt->palette->ncolors);
slouken@2753
  1965
    }
slouken@3685
  1966
    return texture;
slouken@1895
  1967
}
slouken@1895
  1968
slouken@1895
  1969
int
slouken@3685
  1970
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
slouken@2753
  1971
                 int *w, int *h)
slouken@1895
  1972
{
slouken@2753
  1973
    if (!texture) {
slouken@2753
  1974
        return -1;
slouken@2753
  1975
    }
slouken@2753
  1976
    if (format) {
slouken@2753
  1977
        *format = texture->format;
slouken@2753
  1978
    }
slouken@2753
  1979
    if (access) {
slouken@2753
  1980
        *access = texture->access;
slouken@2753
  1981
    }
slouken@2753
  1982
    if (w) {
slouken@2753
  1983
        *w = texture->w;
slouken@2753
  1984
    }
slouken@2753
  1985
    if (h) {
slouken@2753
  1986
        *h = texture->h;
slouken@2753
  1987
    }
slouken@2753
  1988
    return 0;
slouken@1895
  1989
}
slouken@1895
  1990
slouken@1895
  1991
int
slouken@3685
  1992
SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
slouken@1895
  1993
{
slouken@2753
  1994
    SDL_Renderer *renderer;
slouken@1895
  1995
slouken@2753
  1996
    if (!texture) {
slouken@2753
  1997
        return -1;
slouken@2753
  1998
    }
slouken@2753
  1999
    renderer = texture->renderer;
slouken@2753
  2000
    if (!renderer->QueryTexturePixels) {
slouken@2810
  2001
        SDL_Unsupported();
slouken@2753
  2002
        return -1;
slouken@2753
  2003
    }
slouken@2753
  2004
    return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
slouken@1895
  2005
}
slouken@1895
  2006
slouken@1895
  2007
int
slouken@3685
  2008
SDL_SetTexturePalette(SDL_Texture * texture, const SDL_Color * colors,
slouken@2753
  2009
                      int firstcolor, int ncolors)
slouken@1895
  2010
{
slouken@2753
  2011
    SDL_Renderer *renderer;
slouken@1895
  2012
slouken@2753
  2013
    if (!texture) {
slouken@2753
  2014
        return -1;
slouken@2753
  2015
    }
slouken@2753
  2016
    renderer = texture->renderer;
slouken@2753
  2017
    if (!renderer->SetTexturePalette) {
slouken@2810
  2018
        SDL_Unsupported();
slouken@2753
  2019
        return -1;
slouken@2753
  2020
    }
slouken@2753
  2021
    return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  2022
                                       ncolors);
slouken@1895
  2023
}
slouken@1895
  2024
slouken@1895
  2025
int
slouken@3685
  2026
SDL_GetTexturePalette(SDL_Texture * texture, SDL_Color * colors,
slouken@2753
  2027
                      int firstcolor, int ncolors)
slouken@1895
  2028
{
slouken@2753
  2029
    SDL_Renderer *renderer;
slouken@1895
  2030
slouken@2753
  2031
    if (!texture) {
slouken@2753
  2032
        return -1;
slouken@2753
  2033
    }
slouken@2753
  2034
    renderer = texture->renderer;
slouken@2753
  2035
    if (!renderer->GetTexturePalette) {
slouken@2810
  2036
        SDL_Unsupported();
slouken@2753
  2037
        return -1;
slouken@2753
  2038
    }
slouken@2753
  2039
    return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
slouken@2753
  2040
                                       ncolors);
slouken@1895
  2041
}
slouken@1895
  2042
slouken@1895
  2043
int
slouken@3685
  2044
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
slouken@1985
  2045
{
slouken@2753
  2046
    SDL_Renderer *renderer;
slouken@1985
  2047
slouken@2753
  2048
    if (!texture) {
slouken@2753
  2049
        return -1;
slouken@2753
  2050
    }
slouken@2753
  2051
    renderer = texture->renderer;
slouken@2753
  2052
    if (!renderer->SetTextureColorMod) {
slouken@2810
  2053
        SDL_Unsupported();
slouken@2753
  2054
        return -1;
slouken@2753
  2055
    }
slouken@2753
  2056
    if (r < 255 || g < 255 || b < 255) {
slouken@2753
  2057
        texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  2058
    } else {
slouken@2753
  2059
        texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
slouken@2753
  2060
    }
slouken@2753
  2061
    texture->r = r;
slouken@2753
  2062
    texture->g = g;
slouken@2753
  2063
    texture->b = b;
slouken@2753
  2064
    return renderer->SetTextureColorMod(renderer, texture);
slouken@1985
  2065
}
slouken@1985
  2066
slouken@1985
  2067
int
slouken@3685
  2068
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
slouken@2753
  2069
                       Uint8 * b)
slouken@1985
  2070
{
slouken@2753
  2071
    SDL_Renderer *renderer;
slouken@1985
  2072
slouken@2753
  2073
    if (!texture) {
slouken@2753
  2074
        return -1;
slouken@2753
  2075
    }
slouken@2753
  2076
    renderer = texture->renderer;
slouken@2753
  2077
    if (r) {
slouken@2753
  2078
        *r = texture->r;
slouken@2753
  2079
    }
slouken@2753
  2080
    if (g) {
slouken@2753
  2081
        *g = texture->g;
slouken@2753
  2082
    }
slouken@2753
  2083
    if (b) {
slouken@2753
  2084
        *b = texture->b;
slouken@2753
  2085
    }
slouken@2753
  2086
    return 0;
slouken@1985
  2087
}
slouken@1985
  2088
slouken@1985
  2089
int
slouken@3685
  2090
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
slouken@1985
  2091
{
slouken@2753
  2092
    SDL_Renderer *renderer;
slouken@1985
  2093
slouken@2753
  2094
    if (!texture) {
slouken@2753
  2095
        return -1;
slouken@2753
  2096
    }
slouken@2753
  2097
    renderer = texture->renderer;
slouken@2753
  2098
    if (!renderer->SetTextureAlphaMod) {
slouken@2810
  2099
        SDL_Unsupported();
slouken@2753
  2100
        return -1;
slouken@2753
  2101
    }
slouken@2753
  2102
    if (alpha < 255) {
slouken@2753
  2103
        texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  2104
    } else {
slouken@2753
  2105
        texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
slouken@2753
  2106
    }
slouken@2753
  2107
    texture->a = alpha;
slouken@2753
  2108
    return renderer->SetTextureAlphaMod(renderer, texture);
slouken@1985
  2109
}
slouken@1985
  2110
slouken@1985
  2111
int
slouken@3685
  2112
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
slouken@1985
  2113
{
slouken@2753
  2114
    if (!texture) {
slouken@2753
  2115
        return -1;
slouken@2753
  2116
    }
slouken@2753
  2117
    if (alpha) {
slouken@2753
  2118
        *alpha = texture->a;
slouken@2753
  2119
    }
slouken@2753
  2120
    return 0;
slouken@1985
  2121
}
slouken@1985
  2122
slouken@1985
  2123
int
slouken@3685
  2124
SDL_SetTextureBlendMode(SDL_Texture * texture, int blendMode)
slouken@1985
  2125
{
slouken@2753
  2126
    SDL_Renderer *renderer;
slouken@1985
  2127
slouken@2753
  2128
    if (!texture) {
slouken@2753
  2129
        return -1;
slouken@2753
  2130
    }
slouken@2753
  2131
    renderer = texture->renderer;
slouken@2753
  2132
    if (!renderer->SetTextureBlendMode) {
slouken@2810
  2133
        SDL_Unsupported();
slouken@2753
  2134
        return -1;
slouken@2753
  2135
    }
slouken@2753
  2136
    texture->blendMode = blendMode;
slouken@2753
  2137
    return renderer->SetTextureBlendMode(renderer, texture);
slouken@1985
  2138
}
slouken@1985
  2139
slouken@1985
  2140
int
slouken@3685
  2141
SDL_GetTextureBlendMode(SDL_Texture * texture, int *blendMode)
slouken@1985
  2142
{
slouken@2753
  2143
    if (!texture) {
slouken@2753
  2144
        return -1;
slouken@2753
  2145
    }
slouken@2753
  2146
    if (blendMode) {
slouken@2753
  2147
        *blendMode = texture->blendMode;
slouken@2753
  2148
    }
slouken@2753
  2149
    return 0;
slouken@1985
  2150
}
slouken@1985
  2151
slouken@1985
  2152
int
slouken@3685
  2153
SDL_SetTextureScaleMode(SDL_Texture * texture, int scaleMode)
slouken@1985
  2154
{
slouken@2753
  2155
    SDL_Renderer *renderer;
slouken@1985
  2156
slouken@2753
  2157
    if (!texture) {
slouken@2753
  2158
        return -1;
slouken@2753
  2159
    }
slouken@2753
  2160
    renderer = texture->renderer;
slouken@2753
  2161
    if (!renderer->SetTextureScaleMode) {
slouken@2810
  2162
        SDL_Unsupported();
slouken@2753
  2163
        return -1;
slouken@2753
  2164
    }
slouken@2753
  2165
    texture->scaleMode = scaleMode;
slouken@2753
  2166
    return renderer->SetTextureScaleMode(renderer, texture);
slouken@1985
  2167
}
slouken@1985
  2168
slouken@1985
  2169
int
slouken@3685
  2170
SDL_GetTextureScaleMode(SDL_Texture * texture, int *scaleMode)
slouken@1985
  2171
{
slouken@2753
  2172
    if (!texture) {
slouken@2753
  2173
        return -1;
slouken@2753
  2174
    }
slouken@2753
  2175
    if (scaleMode) {
slouken@2753
  2176
        *scaleMode = texture->scaleMode;
slouken@2753
  2177
    }
slouken@2753
  2178
    return 0;
slouken@1985
  2179
}
slouken@1985
  2180
slouken@1985
  2181
int
slouken@3685
  2182
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
slouken@2753
  2183
                  const void *pixels, int pitch)
slouken@1895
  2184
{
slouken@2753
  2185
    SDL_Renderer *renderer;
slouken@2753
  2186
    SDL_Rect full_rect;
slouken@1895
  2187
slouken@2753
  2188
    if (!texture) {
slouken@2753
  2189
        return -1;
slouken@2753
  2190
    }
slouken@2753
  2191
    renderer = texture->renderer;
slouken@2753
  2192
    if (!renderer->UpdateTexture) {
slouken@2810
  2193
        SDL_Unsupported();
slouken@2753
  2194
        return -1;
slouken@2753
  2195
    }
slouken@2753
  2196
    if (!rect) {
slouken@2753
  2197
        full_rect.x = 0;
slouken@2753
  2198
        full_rect.y = 0;
slouken@2753
  2199
        full_rect.w = texture->w;
slouken@2753
  2200
        full_rect.h = texture->h;
slouken@2753
  2201
        rect = &full_rect;
slouken@2753
  2202
    }
slouken@2753
  2203
    return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
slouken@1895
  2204
}
slouken@1895
  2205
slouken@1895
  2206
int
slouken@3685
  2207
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
slouken@2753
  2208
                void **pixels, int *pitch)
slouken@1895
  2209
{
slouken@2753
  2210
    SDL_Renderer *renderer;
slouken@2753
  2211
    SDL_Rect full_rect;
slouken@1895
  2212
slouken@2753
  2213
    if (!texture) {
slouken@2753
  2214
        return -1;
slouken@2753
  2215
    }
slouken@2753
  2216
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2217
        SDL_SetError("SDL_LockTexture(): texture must be streaming");
slouken@2753
  2218
        return -1;
slouken@2753
  2219
    }
slouken@2753
  2220
    renderer = texture->renderer;
slouken@2753
  2221
    if (!renderer->LockTexture) {
slouken@2810
  2222
        SDL_Unsupported();
slouken@2753
  2223
        return -1;
slouken@2753
  2224
    }
slouken@2753
  2225
    if (!rect) {
slouken@2753
  2226
        full_rect.x = 0;
slouken@2753
  2227
        full_rect.y = 0;
slouken@2753
  2228
        full_rect.w = texture->w;
slouken@2753
  2229
        full_rect.h = texture->h;
slouken@2753
  2230
        rect = &full_rect;
slouken@2753
  2231
    }
slouken@2753
  2232
    return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
slouken@2753
  2233
                                 pitch);
slouken@1895
  2234
}
slouken@1895
  2235
slouken@1895
  2236
void
slouken@3685
  2237
SDL_UnlockTexture(SDL_Texture * texture)
slouken@1895
  2238
{
slouken@2753
  2239
    SDL_Renderer *renderer;
slouken@1895
  2240
slouken@2753
  2241
    if (!texture) {
slouken@2753
  2242
        return;
slouken@2753
  2243
    }
slouken@2753
  2244
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2245
        return;
slouken@2753
  2246
    }
slouken@2753
  2247
    renderer = texture->renderer;
slouken@2753
  2248
    if (!renderer->UnlockTexture) {
slouken@2753
  2249
        return;
slouken@2753
  2250
    }
slouken@2753
  2251
    renderer->UnlockTexture(renderer, texture);
slouken@1895
  2252
}
slouken@1895
  2253
slouken@1895
  2254
void
slouken@3685
  2255
SDL_DirtyTexture(SDL_Texture * texture, int numrects,
slouken@2753
  2256
                 const SDL_Rect * rects)
slouken@1895
  2257
{
slouken@2753
  2258
    SDL_Renderer *renderer;
slouken@1895
  2259
slouken@2753
  2260
    if (!texture) {
slouken@2753
  2261
        return;
slouken@2753
  2262
    }
slouken@2753
  2263
    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
slouken@2753
  2264
        return;
slouken@2753
  2265
    }
slouken@2753
  2266
    renderer = texture->renderer;
slouken@2753
  2267
    if (!renderer->DirtyTexture) {
slouken@2753
  2268
        return;
slouken@2753
  2269
    }
slouken@2753
  2270
    renderer->DirtyTexture(renderer, texture, numrects, rects);
slouken@1895
  2271
}
slouken@1895
  2272
slouken@1895
  2273
int
slouken@2884
  2274
SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2884
  2275
{
slouken@2884
  2276
    SDL_Renderer *renderer;
slouken@2884
  2277
slouken@3485
  2278
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2279
    if (!renderer) {
slouken@2884
  2280
        return -1;
slouken@2884
  2281
    }
slouken@2884
  2282
    renderer->r = r;
slouken@2884
  2283
    renderer->g = g;
slouken@2884
  2284
    renderer->b = b;
slouken@2884
  2285
    renderer->a = a;
slouken@2927
  2286
    if (renderer->SetDrawColor) {
slouken@2927
  2287
        return renderer->SetDrawColor(renderer);
slouken@2927
  2288
    } else {
slouken@2927
  2289
        return 0;
slouken@2927
  2290
    }
slouken@2884
  2291
}
slouken@2884
  2292
slouken@2884
  2293
int
slouken@2885
  2294
SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@2884
  2295
{
slouken@2884
  2296
    SDL_Renderer *renderer;
slouken@2884
  2297
slouken@3485
  2298
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2299
    if (!renderer) {
slouken@2884
  2300
        return -1;
slouken@2884
  2301
    }
slouken@2884
  2302
    if (r) {
slouken@2884
  2303
        *r = renderer->r;
slouken@2884
  2304
    }
slouken@2884
  2305
    if (g) {
slouken@2884
  2306
        *g = renderer->g;
slouken@2884
  2307
    }
slouken@2884
  2308
    if (b) {
slouken@2884
  2309
        *b = renderer->b;
slouken@2884
  2310
    }
slouken@2884
  2311
    if (a) {
slouken@2884
  2312
        *a = renderer->a;
slouken@2884
  2313
    }
slouken@2928
  2314
    return 0;
slouken@2884
  2315
}
slouken@2884
  2316
slouken@2884
  2317
int
slouken@2884
  2318
SDL_SetRenderDrawBlendMode(int blendMode)
slouken@2884
  2319
{
slouken@2884
  2320
    SDL_Renderer *renderer;
slouken@2884
  2321
slouken@3485
  2322
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2323
    if (!renderer) {
slouken@2884
  2324
        return -1;
slouken@2884
  2325
    }
slouken@2927
  2326
    renderer->blendMode = blendMode;
slouken@2927
  2327
    if (renderer->SetDrawBlendMode) {
slouken@2927
  2328
        return renderer->SetDrawBlendMode(renderer);
slouken@2927
  2329
    } else {
slouken@2927
  2330
        return 0;
slouken@2884
  2331
    }
slouken@2884
  2332
}
slouken@2884
  2333
slouken@2884
  2334
int
slouken@2884
  2335
SDL_GetRenderDrawBlendMode(int *blendMode)
slouken@2884
  2336
{
slouken@2884
  2337
    SDL_Renderer *renderer;
slouken@2884
  2338
slouken@3485
  2339
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2884
  2340
    if (!renderer) {
slouken@2884
  2341
        return -1;
slouken@2884
  2342
    }
slouken@2884
  2343
    *blendMode = renderer->blendMode;
slouken@2884
  2344
    return 0;
slouken@2884
  2345
}
slouken@2884
  2346
slouken@2901
  2347
int
slouken@3596
  2348
SDL_RenderClear()
slouken@3536
  2349
{
slouken@2901
  2350
    SDL_Renderer *renderer;
slouken@3536
  2351
slouken@3485
  2352
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2901
  2353
    if (!renderer) {
slouken@2901
  2354
        return -1;
slouken@2901
  2355
    }
slouken@3596
  2356
    if (!renderer->RenderClear) {
slouken@3596
  2357
        int blendMode = renderer->blendMode;
slouken@3596
  2358
        int status;
slouken@3596
  2359
slouken@3596
  2360
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@3596
  2361
            SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@3596
  2362
        }
slouken@3596
  2363
slouken@3596
  2364
        status = SDL_RenderFillRect(NULL);
slouken@3596
  2365
slouken@3596
  2366
        if (blendMode >= SDL_BLENDMODE_BLEND) {
slouken@3596
  2367
            SDL_SetRenderDrawBlendMode(blendMode);
slouken@3596
  2368
        }
slouken@3596
  2369
        return status;
slouken@3596
  2370
    }
slouken@3596
  2371
    return renderer->RenderClear(renderer);
slouken@3596
  2372
}
slouken@3596
  2373
slouken@3596
  2374
int
slouken@3596
  2375
SDL_RenderDrawPoint(int x, int y)
slouken@3596
  2376
{
slouken@3596
  2377
    SDL_Point point;
slouken@3596
  2378
slouken@3596
  2379
    point.x = x;
slouken@3596
  2380
    point.y = y;
slouken@3596
  2381
    return SDL_RenderDrawPoints(&point, 1);
slouken@3596
  2382
}
slouken@3596
  2383
slouken@3596
  2384
int
slouken@3596
  2385
SDL_RenderDrawPoints(const SDL_Point * points, int count)
slouken@3596
  2386
{
slouken@3596
  2387
    SDL_Renderer *renderer;
slouken@3596
  2388
slouken@3596
  2389
    if (!points) {
slouken@3596
  2390
        SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
slouken@3596
  2391
        return -1;
slouken@3596
  2392
    }
slouken@3596
  2393
slouken@3596
  2394
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3596
  2395
    if (!renderer) {
slouken@3596
  2396
        return -1;
slouken@3596
  2397
    }
slouken@3596
  2398
    if (!renderer->RenderDrawPoints) {
slouken@2901
  2399
        SDL_Unsupported();
slouken@2901
  2400
        return -1;
slouken@2901
  2401
    }
slouken@3536
  2402
    if (count < 1) {
slouken@2901
  2403
        return 0;
slouken@2901
  2404
    }
slouken@3596
  2405
    return renderer->RenderDrawPoints(renderer, points, count);
slouken@2901
  2406
}
slouken@2901
  2407
slouken@2901
  2408
int
slouken@3596
  2409
SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
slouken@2901
  2410
{
slouken@3536
  2411
    SDL_Point points[2];
slouken@3536
  2412
slouken@3536
  2413
    points[0].x = x1;
slouken@3536
  2414
    points[0].y = y1;
slouken@3536
  2415
    points[1].x = x2;
slouken@3536
  2416
    points[1].y = y2;
slouken@3596
  2417
    return SDL_RenderDrawLines(points, 2);
slouken@3536
  2418
}
slouken@3536
  2419
slouken@3536
  2420
int
slouken@3596
  2421
SDL_RenderDrawLines(const SDL_Point * points, int count)
slouken@3536
  2422
{
slouken@2901
  2423
    SDL_Renderer *renderer;
slouken@3536
  2424
slouken@3536
  2425
    if (!points) {
slouken@3596
  2426
        SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
slouken@3536
  2427
        return -1;
slouken@2901
  2428
    }
slouken@2901
  2429
slouken@3485
  2430
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2901
  2431
    if (!renderer) {
slouken@2901
  2432
        return -1;
slouken@2901
  2433
    }
slouken@3596
  2434
    if (!renderer->RenderDrawLines) {
slouken@2901
  2435
        SDL_Unsupported();
slouken@2901
  2436
        return -1;
slouken@2901
  2437
    }
slouken@3536
  2438
    if (count < 2) {
slouken@3536
  2439
        return 0;
slouken@3536
  2440
    }
slouken@3596
  2441
    return renderer->RenderDrawLines(renderer, points, count);
slouken@2901
  2442
}
slouken@2884
  2443
slouken@2884
  2444
int
slouken@3596
  2445
SDL_RenderDrawRect(const SDL_Rect * rect)
slouken@3536
  2446
{
slouken@3596
  2447
    return SDL_RenderDrawRects(&rect, 1);
slouken@3536
  2448
}
slouken@3536
  2449
slouken@3536
  2450
int
slouken@3596
  2451
SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
slouken@1895
  2452
{
slouken@2753
  2453
    SDL_Renderer *renderer;
slouken@3536
  2454
    int i;
slouken@3536
  2455
slouken@3536
  2456
    if (!rects) {
slouken@3596
  2457
        SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
slouken@3536
  2458
        return -1;
slouken@3536
  2459
    }
slouken@1895
  2460
slouken@3485
  2461
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2810
  2462
    if (!renderer) {
slouken@2810
  2463
        return -1;
slouken@2810
  2464
    }
slouken@3596
  2465
    if (!renderer->RenderDrawRects) {
slouken@2810
  2466
        SDL_Unsupported();
slouken@2753
  2467
        return -1;
slouken@2753
  2468
    }
slouken@3536
  2469
    if (count < 1) {
slouken@3536
  2470
        return 0;
slouken@3536
  2471
    }
slouken@3536
  2472
    /* Check for NULL rect, which means fill entire window */
slouken@3536
  2473
    for (i = 0; i < count; ++i) {
slouken@3536
  2474
        if (rects[i] == NULL) {
slouken@3685
  2475
            SDL_Window *window = renderer->window;
slouken@3536
  2476
            SDL_Rect full_rect;
slouken@3540
  2477
            const SDL_Rect *rect;
slouken@3536
  2478
slouken@3536
  2479
            full_rect.x = 0;
slouken@3536
  2480
            full_rect.y = 0;
slouken@3536
  2481
            full_rect.w = window->w;
slouken@3536
  2482
            full_rect.h = window->h;
slouken@3536
  2483
            rect = &full_rect;
slouken@3596
  2484
            return renderer->RenderDrawRects(renderer, &rect, 1);
slouken@2753
  2485
        }
slouken@2753
  2486
    }
slouken@3596
  2487
    return renderer->RenderDrawRects(renderer, rects, count);
slouken@3596
  2488
}
slouken@3596
  2489
slouken@3596
  2490
int
slouken@3596
  2491
SDL_RenderFillRect(const SDL_Rect * rect)
slouken@3596
  2492
{
slouken@3596
  2493
    return SDL_RenderFillRects(&rect, 1);
slouken@3596
  2494
}
slouken@3596
  2495
slouken@3596
  2496
int
slouken@3596
  2497
SDL_RenderFillRects(const SDL_Rect ** rects, int count)
slouken@3596
  2498
{
slouken@3596
  2499
    SDL_Renderer *renderer;
slouken@3596
  2500
    int i;
slouken@3596
  2501
slouken@3596
  2502
    if (!rects) {
slouken@3596
  2503
        SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
slouken@3596
  2504
        return -1;
slouken@3596
  2505
    }
slouken@3596
  2506
slouken@3596
  2507
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3596
  2508
    if (!renderer) {
slouken@3596
  2509
        return -1;
slouken@3596
  2510
    }
slouken@3596
  2511
    if (!renderer->RenderFillRects) {
slouken@3596
  2512
        SDL_Unsupported();
slouken@3596
  2513
        return -1;
slouken@3596
  2514
    }
slouken@3596
  2515
    if (count < 1) {
slouken@3596
  2516
        return 0;
slouken@3596
  2517
    }
slouken@3596
  2518
    /* Check for NULL rect, which means fill entire window */
slouken@3596
  2519
    for (i = 0; i < count; ++i) {
slouken@3596
  2520
        if (rects[i] == NULL) {
slouken@3685
  2521
            SDL_Window *window = renderer->window;
slouken@3596
  2522
            SDL_Rect full_rect;
slouken@3596
  2523
            const SDL_Rect *rect;
slouken@3596
  2524
slouken@3596
  2525
            full_rect.x = 0;
slouken@3596
  2526
            full_rect.y = 0;
slouken@3596
  2527
            full_rect.w = window->w;
slouken@3596
  2528
            full_rect.h = window->h;
slouken@3596
  2529
            rect = &full_rect;
slouken@3596
  2530
            return renderer->RenderFillRects(renderer, &rect, 1);
slouken@3596
  2531
        }
slouken@3596
  2532
    }
slouken@3596
  2533
    return renderer->RenderFillRects(renderer, rects, count);
slouken@3596
  2534
}
slouken@3596
  2535
slouken@2884
  2536
int
slouken@3685
  2537
SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
slouken@2753
  2538
               const SDL_Rect * dstrect)
slouken@1895
  2539
{
slouken@2753
  2540
    SDL_Renderer *renderer;
slouken@2753
  2541
    SDL_Window *window;
slouken@2753
  2542
    SDL_Rect real_srcrect;
slouken@2753
  2543
    SDL_Rect real_dstrect;
slouken@1895
  2544
slouken@3485
  2545
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3316
  2546
    if (!renderer) {
slouken@2753
  2547
        return -1;
slouken@2753
  2548
    }
slouken@3316
  2549
    if (!texture) {
slouken@3316
  2550
        SDL_SetError("Texture not found");
slouken@3316
  2551
        return -1;
slouken@3316
  2552
    }
slouken@3316
  2553
    if (texture->renderer != renderer) {
slouken@3316
  2554
        SDL_SetError("Texture was not created with this renderer");
slouken@2810
  2555
        return -1;
slouken@2810
  2556
    }
slouken@2810
  2557
    if (!renderer->RenderCopy) {
slouken@2810
  2558
        SDL_Unsupported();
slouken@2753
  2559
        return -1;
slouken@2753
  2560
    }
slouken@3685
  2561
    window = renderer->window;
slouken@2908
  2562
slouken@2908
  2563
    real_srcrect.x = 0;
slouken@2908
  2564
    real_srcrect.y = 0;
slouken@2908
  2565
    real_srcrect.w = texture->w;
slouken@2908
  2566
    real_srcrect.h = texture->h;
slouken@2753
  2567
    if (srcrect) {
slouken@2908
  2568
        if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
slouken@2908
  2569
            return 0;
slouken@2908
  2570
        }
slouken@2753
  2571
    }
slouken@2908
  2572
slouken@2908
  2573
    real_dstrect.x = 0;
slouken@2908
  2574
    real_dstrect.y = 0;
slouken@2908
  2575
    real_dstrect.w = window->w;
slouken@2908
  2576
    real_dstrect.h = window->h;
slouken@2753
  2577
    if (dstrect) {
slouken@2908
  2578
        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
slouken@2908
  2579
            return 0;
slouken@2908
  2580
        }
slouken@2912
  2581
        /* Clip srcrect by the same amount as dstrect was clipped */
slouken@2912
  2582
        if (dstrect->w != real_dstrect.w) {
slouken@2912
  2583
            int deltax = (real_dstrect.x - dstrect->x);
slouken@2912
  2584
            int deltaw = (real_dstrect.w - dstrect->w);
slouken@3333
  2585
            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
slouken@3333
  2586
            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
slouken@2912
  2587
        }
slouken@2912
  2588
        if (dstrect->h != real_dstrect.h) {
slouken@2912
  2589
            int deltay = (real_dstrect.y - dstrect->y);
slouken@2912
  2590
            int deltah = (real_dstrect.h - dstrect->h);
slouken@3333
  2591
            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
slouken@3333
  2592
            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
slouken@2912
  2593
        }
slouken@2753
  2594
    }
slouken@1895
  2595
slouken@2753
  2596
    return renderer->RenderCopy(renderer, texture, &real_srcrect,
slouken@2753
  2597
                                &real_dstrect);
slouken@1895
  2598
}
slouken@1895
  2599
slouken@3427
  2600
int
slouken@3435
  2601
SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
slouken@3435
  2602
                     void * pixels, int pitch)
slouken@3427
  2603
{
slouken@3427
  2604
    SDL_Renderer *renderer;
slouken@3427
  2605
    SDL_Window *window;
slouken@3427
  2606
    SDL_Rect real_rect;
slouken@3427
  2607
slouken@3485
  2608
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3427
  2609
    if (!renderer) {
slouken@3427
  2610
        return -1;
slouken@3427
  2611
    }
slouken@3427
  2612
    if (!renderer->RenderReadPixels) {
slouken@3427
  2613
        SDL_Unsupported();
slouken@3427
  2614
        return -1;
slouken@3427
  2615
    }
slouken@3685
  2616
    window = renderer->window;
slouken@3427
  2617
slouken@3435
  2618
    if (!format) {
slouken@3685
  2619
        format = window->display->current_mode.format;
slouken@3435
  2620
    }
slouken@3435
  2621
slouken@3427
  2622
    real_rect.x = 0;
slouken@3427
  2623
    real_rect.y = 0;
slouken@3427
  2624
    real_rect.w = window->w;
slouken@3427
  2625
    real_rect.h = window->h;
slouken@3427
  2626
    if (rect) {
slouken@3427
  2627
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@3427
  2628
            return 0;
slouken@3427
  2629
        }
slouken@3427
  2630
        if (real_rect.y > rect->y) {
slouken@3427
  2631
            pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@3427
  2632
        }
slouken@3427
  2633
        if (real_rect.x > rect->x) {
slouken@3685
  2634
            Uint32 format = SDL_CurrentDisplay->current_mode.format;
slouken@3427
  2635
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@3427
  2636
            pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@3427
  2637
        }
slouken@3427
  2638
    }
slouken@3427
  2639
slouken@3435
  2640
    return renderer->RenderReadPixels(renderer, &real_rect,
slouken@3435
  2641
                                      format, pixels, pitch);
slouken@3427
  2642
}
slouken@3427
  2643
slouken@3427
  2644
int
slouken@3435
  2645
SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
slouken@3435
  2646
                      const void * pixels, int pitch)
slouken@3427
  2647
{
slouken@3427
  2648
    SDL_Renderer *renderer;
slouken@3427
  2649
    SDL_Window *window;
slouken@3427
  2650
    SDL_Rect real_rect;
slouken@3427
  2651
slouken@3485
  2652
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@3427
  2653
    if (!renderer) {
slouken@3427
  2654
        return -1;
slouken@3427
  2655
    }
slouken@3427
  2656
    if (!renderer->RenderWritePixels) {
slouken@3427
  2657
        SDL_Unsupported();
slouken@3427
  2658
        return -1;
slouken@3427
  2659
    }
slouken@3685
  2660
    window = renderer->window;
slouken@3427
  2661
slouken@3435
  2662
    if (!format) {
slouken@3685
  2663
        format = window->display->current_mode.format;
slouken@3435
  2664
    }
slouken@3435
  2665
slouken@3427
  2666
    real_rect.x = 0;
slouken@3427
  2667
    real_rect.y = 0;
slouken@3427
  2668
    real_rect.w = window->w;
slouken@3427
  2669
    real_rect.h = window->h;
slouken@3427
  2670
    if (rect) {
slouken@3427
  2671
        if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
slouken@3427
  2672
            return 0;
slouken@3427
  2673
        }
slouken@3427
  2674
        if (real_rect.y > rect->y) {
slouken@3427
  2675
            pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
slouken@3427
  2676
        }
slouken@3427
  2677
        if (real_rect.x > rect->x) {
slouken@3685
  2678
            Uint32 format = SDL_CurrentDisplay->current_mode.format;
slouken@3427
  2679
            int bpp = SDL_BYTESPERPIXEL(format);
slouken@3427
  2680
            pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
slouken@3427
  2681
        }
slouken@3427
  2682
    }
slouken@3427
  2683
slouken@3435
  2684
    return renderer->RenderWritePixels(renderer, &real_rect,
slouken@3435
  2685
                                       format, pixels, pitch);
slouken@3427
  2686
}
slouken@3427
  2687
slouken@1895
  2688
void
slouken@1895
  2689
SDL_RenderPresent(void)
slouken@1895
  2690
{
slouken@2753
  2691
    SDL_Renderer *renderer;
slouken@1895
  2692
slouken@3485
  2693
    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
slouken@2753
  2694
    if (!renderer || !renderer->RenderPresent) {
slouken@2753
  2695
        return;
slouken@2753
  2696
    }
slouken@2753
  2697
    renderer->RenderPresent(renderer);
slouken@1895
  2698
}
slouken@1895
  2699
slouken@1895
  2700
void
slouken@3685
  2701
SDL_DestroyTexture(SDL_Texture * texture)
slouken@1895
  2702
{
slouken@2753
  2703
    SDL_Renderer *renderer;
slouken@1895
  2704
slouken@3685
  2705
    if (!texture || !texture->renderer) {
slouken@3685
  2706
        SDL_SetError("Invalid texture");
slouken@2753
  2707
        return;
slouken@2753
  2708
    }
slouken@3685
  2709
slouken@3685
  2710
    renderer = texture->renderer;
slouken@3692
  2711
    if (texture->next) {
slouken@3692
  2712
        texture->next->prev = texture->prev;
slouken@3692
  2713
    }
slouken@3685
  2714
    if (texture->prev) {