src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 24 Nov 2009 04:48:12 +0000
changeset 3485 e77a69aae239
parent 3450 173199bce642
child 3486 c87dbbde2bc2
permissions -rw-r--r--
Mason Wheeler to sdl

I updated SDL, and suddenly my SDL frames stopped working. They'd "initialize" full of gibberish, and I couldn't render anything to them. After a bit of digging, I found a problem: the renderer initialization routine in my SDL frame code wasn't getting called anymore.

procedure TSdlFrame.Paint;
begin
if SDL_SelectRenderer(FWindowID) = -1 then
CreateRenderer;
SDL_RenderPresent;
end;

function TSdlFrame.CreateRenderer: boolean;
const
pf: tagPIXELFORMATDESCRIPTOR = (nSize: sizeof(pf); nVersion: 1;
dwFlags: PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
iPixelType: PFD_TYPE_RGBA; cColorBits: 24; cAlphaBits: 8;
iLayerType: PFD_MAIN_PLANE);

RENDERERS: array[TRendererType] of AnsiString = ('software', 'gdi', 'opengl', 'd3d');
var
pFormat: integer;
begin
if (SDL_SelectRenderer(FWindowID) = 0) then
begin
result := true;
Exit;
end;
if FRendererType = rtOpenGL then
begin
pFormat := ChoosePixelFormat(canvas.Handle, @pf);
if not SetPixelFormat(canvas.Handle, pFormat, @pf) then
outputDebugString(PChar(SysErrorMessage(GetLastError)));
if wglCreateContext(canvas.Handle) = 0 then
outputDebugString(PChar(SysErrorMessage(GetLastError)));
end;
if (SDL_CreateRenderer(FWindowID, SDL_RendererIndex(RENDERERS[FRendererType]), [sdlrPresentFlip3, sdlrAccelerated]) = 0) then
begin
SDL_ShowWindow(FWindowID);
assert(SDL_SetRenderDrawColor(0, 0, 0, 255) = 0);
FFlags := SDL_GetWindowFlags(FWindowID);
if assigned(FOnAvailable) then
FOnAvailable(self);
end
else outputDebugString(pChar(format('SDL_CreateRenderer failed: %s', [sdl_GetError])));
result := SDL_SelectRenderer(FWindowID) = 0;
end;

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