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