src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jun 2009 04:37:27 +0000
changeset 3162 dc1eb82ffdaa
parent 3161 494559cc723b
child 3166 3a63a5824557
permissions -rw-r--r--
Von: Thomas Zimmermann
Betreff: [SDL] [PATCH] Make static variables const
Datum: Tue, 19 May 2009 19:45:37 +0200

Hi,

this is a set of simple changes which make some of SDL's internal static
arrays constant. The purpose is to shrink the number of write-able
static bytes and thus increase the number of memory pages shared between
SDL applications.

The patch set is against trunk@4513. Each of the attached patch files is
specific to a sub-system. The set is completed by a second mail, because
of the list's 40 KiB limit.

The files readelf-r4513.txt and readelf-const-patch.txt where made by
calling 'readelf -S libSDL.so'. They show the difference in ELF sections
without and with the patch. Some numbers measured on my x86-64:

Before

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000008170 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 00000000003045e0 001045e0
00000000000023d0 0000000000000000 WA 0 0 32
[23] .data PROGBITS 00000000003076e0 001076e0
0000000000004988 0000000000000000 WA 0 0 32

After

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000009a50 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 0000000000306040 00106040
0000000000002608 0000000000000000 WA 0 0 32
[23] .data PROGBITS 0000000000309360 00109360
0000000000002e88 0000000000000000 WA 0 0 32

The size of the write-able data section decreased considerably. Some
entries became const-after-relocation, while most of its content went
straight into the read-only data section.

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