src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Jun 2009 02:45:55 +0000
changeset 3169 f294338ca6eb
parent 3166 3a63a5824557
child 3178 72edc980789b
permissions -rw-r--r--
indent
     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 VideoBootStrap *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     SDL_Renderer *renderer;
  1602     int bpp;
  1603     Uint32 Rmask, Gmask, Bmask, Amask;
  1604 
  1605     if (!surface) {
  1606         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1607         return 0;
  1608     }
  1609     fmt = surface->format;
  1610 
  1611     renderer = SDL_CurrentDisplay.current_renderer;
  1612     if (!renderer) {
  1613         return 0;
  1614     }
  1615 
  1616     if (format) {
  1617         if (!SDL_PixelFormatEnumToMasks
  1618             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1619             SDL_SetError("Unknown pixel format");
  1620             return 0;
  1621         }
  1622     } else {
  1623         /* FIXME: Get the best supported texture format */
  1624         if (surface->format->Amask
  1625             || !(surface->map->info.flags &
  1626                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1627             bpp = fmt->BitsPerPixel;
  1628             Rmask = fmt->Rmask;
  1629             Gmask = fmt->Gmask;
  1630             Bmask = fmt->Bmask;
  1631             Amask = fmt->Amask;
  1632         } else {
  1633             /* Need a format with alpha */
  1634             int it;
  1635             int apfmt;
  1636 
  1637             /* Pixel formats with alpha, sorted by best first */
  1638             static const Uint32 sdl_alpha_pformats[] = {
  1639                 SDL_PIXELFORMAT_ARGB8888,
  1640                 SDL_PIXELFORMAT_RGBA8888,
  1641                 SDL_PIXELFORMAT_ABGR8888,
  1642                 SDL_PIXELFORMAT_BGRA8888,
  1643                 SDL_PIXELFORMAT_ARGB1555,
  1644                 SDL_PIXELFORMAT_ABGR1555,
  1645                 SDL_PIXELFORMAT_ARGB4444,
  1646                 SDL_PIXELFORMAT_ABGR4444,
  1647                 SDL_PIXELFORMAT_ARGB2101010,
  1648                 SDL_PIXELFORMAT_UNKNOWN
  1649             };
  1650 
  1651             bpp = 32;
  1652             Rmask = 0x00FF0000;
  1653             Gmask = 0x0000FF00;
  1654             Bmask = 0x000000FF;
  1655             Amask = 0xFF000000;
  1656 
  1657             format =
  1658                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1659             if (!format) {
  1660                 SDL_SetError("Unknown pixel format");
  1661                 return 0;
  1662             }
  1663 
  1664             /* Search this format in the supported texture formats */
  1665             /* by current renderer                                 */
  1666             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1667                 if (renderer->info.texture_formats[it] == format) {
  1668                     break;
  1669                 }
  1670             }
  1671 
  1672             /* If this format can't be found, search any best       */
  1673             /* compatible format with alpha which renderer provides */
  1674             if (it == renderer->info.num_texture_formats) {
  1675                 apfmt = 0;
  1676                 for (;;) {
  1677                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1678                         break;
  1679                     }
  1680 
  1681                     for (it = 0; it < renderer->info.num_texture_formats;
  1682                          it++) {
  1683                         if (renderer->info.texture_formats[it] ==
  1684                             sdl_alpha_pformats[apfmt]) {
  1685                             break;
  1686                         }
  1687                     }
  1688 
  1689                     if (it != renderer->info.num_texture_formats) {
  1690                         /* Compatible format has been found */
  1691                         break;
  1692                     }
  1693                     apfmt++;
  1694                 }
  1695 
  1696                 /* If compatible format can't be found, then return an error */
  1697                 if (it == renderer->info.num_texture_formats) {
  1698                     SDL_SetError("Compatible pixel format can't be found");
  1699                     return 0;
  1700                 }
  1701 
  1702                 /* Convert found pixel format back to color masks */
  1703                 if (SDL_PixelFormatEnumToMasks
  1704                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1705                      &Bmask, &Amask) != SDL_TRUE) {
  1706                     SDL_SetError("Unknown pixel format");
  1707                     return 0;
  1708                 }
  1709             }
  1710         }
  1711 
  1712         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1713         if (!format) {
  1714             SDL_SetError("Unknown pixel format");
  1715             return 0;
  1716         }
  1717     }
  1718 
  1719     textureID =
  1720         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1721                           surface->h);
  1722     if (!textureID && !requested_format) {
  1723         SDL_DisplayMode desktop_mode;
  1724         SDL_GetDesktopDisplayMode(&desktop_mode);
  1725         format = desktop_mode.format;
  1726         textureID =
  1727             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1728                               surface->h);
  1729     }
  1730     if (!textureID) {
  1731         return 0;
  1732     }
  1733     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1734         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1735         if (SDL_MUSTLOCK(surface)) {
  1736             if (SDL_LockSurface(surface) < 0) {
  1737                 SDL_DestroyTexture(textureID);
  1738                 return 0;
  1739             }
  1740             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1741                               surface->pitch);
  1742             SDL_UnlockSurface(surface);
  1743         } else {
  1744             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1745                               surface->pitch);
  1746         }
  1747     } else {
  1748         SDL_PixelFormat dst_fmt;
  1749         SDL_Surface *dst = NULL;
  1750 
  1751         /* Set up a destination surface for the texture update */
  1752         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  1753         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1754             dst_fmt.palette =
  1755                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1756             if (dst_fmt.palette) {
  1757                 /*
  1758                  * FIXME: Should we try to copy
  1759                  * fmt->palette?
  1760                  */
  1761                 SDL_DitherColors(dst_fmt.palette->colors,
  1762                                  SDL_BITSPERPIXEL(format));
  1763             }
  1764         }
  1765         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  1766         if (dst) {
  1767             SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
  1768             SDL_FreeSurface(dst);
  1769         }
  1770         if (dst_fmt.palette) {
  1771             SDL_FreePalette(dst_fmt.palette);
  1772         }
  1773         if (!dst) {
  1774             SDL_DestroyTexture(textureID);
  1775             return 0;
  1776         }
  1777     }
  1778 
  1779     {
  1780         Uint8 r, g, b, a;
  1781         int blendMode;
  1782         int scaleMode;
  1783 
  1784         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1785         SDL_SetTextureColorMod(textureID, r, g, b);
  1786 
  1787         SDL_GetSurfaceAlphaMod(surface, &a);
  1788         SDL_SetTextureAlphaMod(textureID, a);
  1789 
  1790         SDL_GetSurfaceBlendMode(surface, &blendMode);
  1791         SDL_SetTextureBlendMode(textureID, blendMode);
  1792 
  1793         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  1794         SDL_SetTextureScaleMode(textureID, scaleMode);
  1795     }
  1796 
  1797     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1798         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1799                               fmt->palette->ncolors);
  1800     }
  1801     return textureID;
  1802 }
  1803 
  1804 static __inline__ SDL_Texture *
  1805 SDL_GetTextureFromID(SDL_TextureID textureID)
  1806 {
  1807     int hash;
  1808     SDL_Texture *texture;
  1809 
  1810     if (!_this) {
  1811         return NULL;
  1812     }
  1813     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1814     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1815          texture = texture->next) {
  1816         if (texture->id == textureID) {
  1817             return texture;
  1818         }
  1819     }
  1820     return NULL;
  1821 }
  1822 
  1823 int
  1824 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1825                  int *w, int *h)
  1826 {
  1827     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1828 
  1829     if (!texture) {
  1830         return -1;
  1831     }
  1832     if (format) {
  1833         *format = texture->format;
  1834     }
  1835     if (access) {
  1836         *access = texture->access;
  1837     }
  1838     if (w) {
  1839         *w = texture->w;
  1840     }
  1841     if (h) {
  1842         *h = texture->h;
  1843     }
  1844     return 0;
  1845 }
  1846 
  1847 int
  1848 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1849 {
  1850     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1851     SDL_Renderer *renderer;
  1852 
  1853     if (!texture) {
  1854         return -1;
  1855     }
  1856     renderer = texture->renderer;
  1857     if (!renderer->QueryTexturePixels) {
  1858         SDL_Unsupported();
  1859         return -1;
  1860     }
  1861     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1862 }
  1863 
  1864 int
  1865 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1866                       int firstcolor, int ncolors)
  1867 {
  1868     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1869     SDL_Renderer *renderer;
  1870 
  1871     if (!texture) {
  1872         return -1;
  1873     }
  1874     renderer = texture->renderer;
  1875     if (!renderer->SetTexturePalette) {
  1876         SDL_Unsupported();
  1877         return -1;
  1878     }
  1879     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1880                                        ncolors);
  1881 }
  1882 
  1883 int
  1884 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1885                       int firstcolor, int ncolors)
  1886 {
  1887     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1888     SDL_Renderer *renderer;
  1889 
  1890     if (!texture) {
  1891         return -1;
  1892     }
  1893     renderer = texture->renderer;
  1894     if (!renderer->GetTexturePalette) {
  1895         SDL_Unsupported();
  1896         return -1;
  1897     }
  1898     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1899                                        ncolors);
  1900 }
  1901 
  1902 int
  1903 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  1904 {
  1905     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1906     SDL_Renderer *renderer;
  1907 
  1908     if (!texture) {
  1909         return -1;
  1910     }
  1911     renderer = texture->renderer;
  1912     if (!renderer->SetTextureColorMod) {
  1913         SDL_Unsupported();
  1914         return -1;
  1915     }
  1916     if (r < 255 || g < 255 || b < 255) {
  1917         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1918     } else {
  1919         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1920     }
  1921     texture->r = r;
  1922     texture->g = g;
  1923     texture->b = b;
  1924     return renderer->SetTextureColorMod(renderer, texture);
  1925 }
  1926 
  1927 int
  1928 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  1929                        Uint8 * b)
  1930 {
  1931     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1932     SDL_Renderer *renderer;
  1933 
  1934     if (!texture) {
  1935         return -1;
  1936     }
  1937     renderer = texture->renderer;
  1938     if (r) {
  1939         *r = texture->r;
  1940     }
  1941     if (g) {
  1942         *g = texture->g;
  1943     }
  1944     if (b) {
  1945         *b = texture->b;
  1946     }
  1947     return 0;
  1948 }
  1949 
  1950 int
  1951 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  1952 {
  1953     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1954     SDL_Renderer *renderer;
  1955 
  1956     if (!texture) {
  1957         return -1;
  1958     }
  1959     renderer = texture->renderer;
  1960     if (!renderer->SetTextureAlphaMod) {
  1961         SDL_Unsupported();
  1962         return -1;
  1963     }
  1964     if (alpha < 255) {
  1965         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1966     } else {
  1967         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1968     }
  1969     texture->a = alpha;
  1970     return renderer->SetTextureAlphaMod(renderer, texture);
  1971 }
  1972 
  1973 int
  1974 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  1975 {
  1976     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1977 
  1978     if (!texture) {
  1979         return -1;
  1980     }
  1981     if (alpha) {
  1982         *alpha = texture->a;
  1983     }
  1984     return 0;
  1985 }
  1986 
  1987 int
  1988 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  1989 {
  1990     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1991     SDL_Renderer *renderer;
  1992 
  1993     if (!texture) {
  1994         return -1;
  1995     }
  1996     renderer = texture->renderer;
  1997     if (!renderer->SetTextureBlendMode) {
  1998         SDL_Unsupported();
  1999         return -1;
  2000     }
  2001     texture->blendMode = blendMode;
  2002     return renderer->SetTextureBlendMode(renderer, texture);
  2003 }
  2004 
  2005 int
  2006 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  2007 {
  2008     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2009 
  2010     if (!texture) {
  2011         return -1;
  2012     }
  2013     if (blendMode) {
  2014         *blendMode = texture->blendMode;
  2015     }
  2016     return 0;
  2017 }
  2018 
  2019 int
  2020 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  2021 {
  2022     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2023     SDL_Renderer *renderer;
  2024 
  2025     if (!texture) {
  2026         return -1;
  2027     }
  2028     renderer = texture->renderer;
  2029     if (!renderer->SetTextureScaleMode) {
  2030         SDL_Unsupported();
  2031         return -1;
  2032     }
  2033     texture->scaleMode = scaleMode;
  2034     return renderer->SetTextureScaleMode(renderer, texture);
  2035 }
  2036 
  2037 int
  2038 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  2039 {
  2040     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2041 
  2042     if (!texture) {
  2043         return -1;
  2044     }
  2045     if (scaleMode) {
  2046         *scaleMode = texture->scaleMode;
  2047     }
  2048     return 0;
  2049 }
  2050 
  2051 int
  2052 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  2053                   const void *pixels, int pitch)
  2054 {
  2055     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2056     SDL_Renderer *renderer;
  2057     SDL_Rect full_rect;
  2058 
  2059     if (!texture) {
  2060         return -1;
  2061     }
  2062     renderer = texture->renderer;
  2063     if (!renderer->UpdateTexture) {
  2064         SDL_Unsupported();
  2065         return -1;
  2066     }
  2067     if (!rect) {
  2068         full_rect.x = 0;
  2069         full_rect.y = 0;
  2070         full_rect.w = texture->w;
  2071         full_rect.h = texture->h;
  2072         rect = &full_rect;
  2073     }
  2074     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2075 }
  2076 
  2077 int
  2078 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  2079                 void **pixels, int *pitch)
  2080 {
  2081     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2082     SDL_Renderer *renderer;
  2083     SDL_Rect full_rect;
  2084 
  2085     if (!texture) {
  2086         return -1;
  2087     }
  2088     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2089         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2090         return -1;
  2091     }
  2092     renderer = texture->renderer;
  2093     if (!renderer->LockTexture) {
  2094         SDL_Unsupported();
  2095         return -1;
  2096     }
  2097     if (!rect) {
  2098         full_rect.x = 0;
  2099         full_rect.y = 0;
  2100         full_rect.w = texture->w;
  2101         full_rect.h = texture->h;
  2102         rect = &full_rect;
  2103     }
  2104     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2105                                  pitch);
  2106 }
  2107 
  2108 void
  2109 SDL_UnlockTexture(SDL_TextureID textureID)
  2110 {
  2111     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2112     SDL_Renderer *renderer;
  2113 
  2114     if (!texture) {
  2115         return;
  2116     }
  2117     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2118         return;
  2119     }
  2120     renderer = texture->renderer;
  2121     if (!renderer->UnlockTexture) {
  2122         return;
  2123     }
  2124     renderer->UnlockTexture(renderer, texture);
  2125 }
  2126 
  2127 void
  2128 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  2129                  const SDL_Rect * rects)
  2130 {
  2131     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2132     SDL_Renderer *renderer;
  2133 
  2134     if (!texture) {
  2135         return;
  2136     }
  2137     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2138         return;
  2139     }
  2140     renderer = texture->renderer;
  2141     if (!renderer->DirtyTexture) {
  2142         return;
  2143     }
  2144     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2145 }
  2146 
  2147 int
  2148 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2149 {
  2150     SDL_Renderer *renderer;
  2151 
  2152     if (!_this) {
  2153         SDL_UninitializedVideo();
  2154         return -1;
  2155     }
  2156     renderer = SDL_CurrentDisplay.current_renderer;
  2157     if (!renderer) {
  2158         return -1;
  2159     }
  2160     renderer->r = r;
  2161     renderer->g = g;
  2162     renderer->b = b;
  2163     renderer->a = a;
  2164     if (renderer->SetDrawColor) {
  2165         return renderer->SetDrawColor(renderer);
  2166     } else {
  2167         return 0;
  2168     }
  2169 }
  2170 
  2171 int
  2172 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2173 {
  2174     SDL_Renderer *renderer;
  2175 
  2176     if (!_this) {
  2177         SDL_UninitializedVideo();
  2178         return -1;
  2179     }
  2180     renderer = SDL_CurrentDisplay.current_renderer;
  2181     if (!renderer) {
  2182         return -1;
  2183     }
  2184     if (r) {
  2185         *r = renderer->r;
  2186     }
  2187     if (g) {
  2188         *g = renderer->g;
  2189     }
  2190     if (b) {
  2191         *b = renderer->b;
  2192     }
  2193     if (a) {
  2194         *a = renderer->a;
  2195     }
  2196     return 0;
  2197 }
  2198 
  2199 int
  2200 SDL_SetRenderDrawBlendMode(int blendMode)
  2201 {
  2202     SDL_Renderer *renderer;
  2203 
  2204     if (!_this) {
  2205         SDL_UninitializedVideo();
  2206         return -1;
  2207     }
  2208     renderer = SDL_CurrentDisplay.current_renderer;
  2209     if (!renderer) {
  2210         return -1;
  2211     }
  2212     renderer->blendMode = blendMode;
  2213     if (renderer->SetDrawBlendMode) {
  2214         return renderer->SetDrawBlendMode(renderer);
  2215     } else {
  2216         return 0;
  2217     }
  2218 }
  2219 
  2220 int
  2221 SDL_GetRenderDrawBlendMode(int *blendMode)
  2222 {
  2223     SDL_Renderer *renderer;
  2224 
  2225     if (!_this) {
  2226         SDL_UninitializedVideo();
  2227         return -1;
  2228     }
  2229     renderer = SDL_CurrentDisplay.current_renderer;
  2230     if (!renderer) {
  2231         return -1;
  2232     }
  2233     *blendMode = renderer->blendMode;
  2234     return 0;
  2235 }
  2236 
  2237 int
  2238 SDL_RenderPoint(int x, int y)
  2239 {
  2240     SDL_Renderer *renderer;
  2241     SDL_Window *window;
  2242 
  2243     if (!_this) {
  2244         SDL_UninitializedVideo();
  2245         return -1;
  2246     }
  2247     renderer = SDL_CurrentDisplay.current_renderer;
  2248     if (!renderer) {
  2249         return -1;
  2250     }
  2251     if (!renderer->RenderPoint) {
  2252         SDL_Unsupported();
  2253         return -1;
  2254     }
  2255     window = SDL_GetWindowFromID(renderer->window);
  2256     if (x < 0 || y < 0 || x >= window->w || y >= window->h) {
  2257         return 0;
  2258     }
  2259     return renderer->RenderPoint(renderer, x, y);
  2260 }
  2261 
  2262 int
  2263 SDL_RenderLine(int x1, int y1, int x2, int y2)
  2264 {
  2265     SDL_Renderer *renderer;
  2266     SDL_Window *window;
  2267     SDL_Rect real_rect;
  2268 
  2269     if (x1 == x2 && y1 == y2) {
  2270         return SDL_RenderPoint(x1, y1);
  2271     }
  2272 
  2273     if (!_this) {
  2274         SDL_UninitializedVideo();
  2275         return -1;
  2276     }
  2277     renderer = SDL_CurrentDisplay.current_renderer;
  2278     if (!renderer) {
  2279         return -1;
  2280     }
  2281     if (!renderer->RenderLine) {
  2282         SDL_Unsupported();
  2283         return -1;
  2284     }
  2285     window = SDL_GetWindowFromID(renderer->window);
  2286 
  2287     real_rect.x = 0;
  2288     real_rect.y = 0;
  2289     real_rect.w = window->w;
  2290     real_rect.h = window->h;
  2291     if (!SDL_IntersectRectAndLine(&real_rect, &x1, &y1, &x2, &y2)) {
  2292         return (0);
  2293     }
  2294     return renderer->RenderLine(renderer, x1, y1, x2, y2);
  2295 }
  2296 
  2297 int
  2298 SDL_RenderFill(const SDL_Rect * rect)
  2299 {
  2300     SDL_Renderer *renderer;
  2301     SDL_Window *window;
  2302     SDL_Rect real_rect;
  2303 
  2304     if (!_this) {
  2305         SDL_UninitializedVideo();
  2306         return -1;
  2307     }
  2308     renderer = SDL_CurrentDisplay.current_renderer;
  2309     if (!renderer) {
  2310         return -1;
  2311     }
  2312     if (!renderer->RenderFill) {
  2313         SDL_Unsupported();
  2314         return -1;
  2315     }
  2316     window = SDL_GetWindowFromID(renderer->window);
  2317 
  2318     real_rect.x = 0;
  2319     real_rect.y = 0;
  2320     real_rect.w = window->w;
  2321     real_rect.h = window->h;
  2322     if (rect) {
  2323         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2324             return 0;
  2325         }
  2326     }
  2327     return renderer->RenderFill(renderer, &real_rect);
  2328 }
  2329 
  2330 int
  2331 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2332                const SDL_Rect * dstrect)
  2333 {
  2334     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2335     SDL_Renderer *renderer;
  2336     SDL_Window *window;
  2337     SDL_Rect real_srcrect;
  2338     SDL_Rect real_dstrect;
  2339 
  2340     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  2341         return -1;
  2342     }
  2343     renderer = SDL_CurrentDisplay.current_renderer;
  2344     if (!renderer) {
  2345         return -1;
  2346     }
  2347     if (!renderer->RenderCopy) {
  2348         SDL_Unsupported();
  2349         return -1;
  2350     }
  2351     window = SDL_GetWindowFromID(renderer->window);
  2352 
  2353     real_srcrect.x = 0;
  2354     real_srcrect.y = 0;
  2355     real_srcrect.w = texture->w;
  2356     real_srcrect.h = texture->h;
  2357     if (srcrect) {
  2358         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2359             return 0;
  2360         }
  2361     }
  2362 
  2363     real_dstrect.x = 0;
  2364     real_dstrect.y = 0;
  2365     real_dstrect.w = window->w;
  2366     real_dstrect.h = window->h;
  2367     if (dstrect) {
  2368         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2369             return 0;
  2370         }
  2371         /* Clip srcrect by the same amount as dstrect was clipped */
  2372         if (dstrect->w != real_dstrect.w) {
  2373             int deltax = (real_dstrect.x - dstrect->x);
  2374             int deltaw = (real_dstrect.w - dstrect->w);
  2375             real_srcrect.x += (deltax * dstrect->w) / real_srcrect.w;
  2376             real_srcrect.w += (deltaw * dstrect->w) / real_srcrect.w;
  2377         }
  2378         if (dstrect->h != real_dstrect.h) {
  2379             int deltay = (real_dstrect.y - dstrect->y);
  2380             int deltah = (real_dstrect.h - dstrect->h);
  2381             real_srcrect.y += (deltay * dstrect->h) / real_srcrect.h;
  2382             real_srcrect.h += (deltah * dstrect->h) / real_srcrect.h;
  2383         }
  2384     }
  2385 
  2386     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2387                                 &real_dstrect);
  2388 }
  2389 
  2390 void
  2391 SDL_RenderPresent(void)
  2392 {
  2393     SDL_Renderer *renderer;
  2394 
  2395     if (!_this) {
  2396         SDL_UninitializedVideo();
  2397         return;
  2398     }
  2399     renderer = SDL_CurrentDisplay.current_renderer;
  2400     if (!renderer || !renderer->RenderPresent) {
  2401         return;
  2402     }
  2403     renderer->RenderPresent(renderer);
  2404 }
  2405 
  2406 void
  2407 SDL_DestroyTexture(SDL_TextureID textureID)
  2408 {
  2409     int hash;
  2410     SDL_Texture *prev, *texture;
  2411     SDL_Renderer *renderer;
  2412 
  2413     if (!_this) {
  2414         SDL_UninitializedVideo();
  2415         return;
  2416     }
  2417     /* Look up the texture in the hash table */
  2418     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2419     prev = NULL;
  2420     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2421          prev = texture, texture = texture->next) {
  2422         if (texture->id == textureID) {
  2423             break;
  2424         }
  2425     }
  2426     if (!texture) {
  2427         return;
  2428     }
  2429     /* Unlink the texture from the list */
  2430     if (prev) {
  2431         prev->next = texture->next;
  2432     } else {
  2433         SDL_CurrentDisplay.textures[hash] = texture->next;
  2434     }
  2435 
  2436     /* Free the texture */
  2437     renderer = texture->renderer;
  2438     renderer->DestroyTexture(renderer, texture);
  2439     SDL_free(texture);
  2440 }
  2441 
  2442 void
  2443 SDL_DestroyRenderer(SDL_WindowID windowID)
  2444 {
  2445     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2446     SDL_Renderer *renderer;
  2447     int i;
  2448 
  2449     if (!window) {
  2450         return;
  2451     }
  2452     renderer = window->renderer;
  2453     if (!renderer) {
  2454         return;
  2455     }
  2456     /* Free existing textures for this renderer */
  2457     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2458         SDL_Texture *texture;
  2459         SDL_Texture *prev = NULL;
  2460         SDL_Texture *next;
  2461         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2462              texture = next) {
  2463             next = texture->next;
  2464             if (texture->renderer == renderer) {
  2465                 if (prev) {
  2466                     prev->next = next;
  2467                 } else {
  2468                     SDL_CurrentDisplay.textures[i] = next;
  2469                 }
  2470                 renderer->DestroyTexture(renderer, texture);
  2471                 SDL_free(texture);
  2472             } else {
  2473                 prev = texture;
  2474             }
  2475         }
  2476     }
  2477 
  2478     /* Free the renderer instance */
  2479     renderer->DestroyRenderer(renderer);
  2480 
  2481     /* Clear references */
  2482     window->renderer = NULL;
  2483     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2484         SDL_CurrentDisplay.current_renderer = NULL;
  2485     }
  2486 }
  2487 
  2488 SDL_bool
  2489 SDL_IsScreenSaverEnabled()
  2490 {
  2491     if (!_this) {
  2492         return SDL_TRUE;
  2493     }
  2494     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2495 }
  2496 
  2497 void
  2498 SDL_EnableScreenSaver()
  2499 {
  2500     if (!_this) {
  2501         return;
  2502     }
  2503     if (!_this->suspend_screensaver) {
  2504         return;
  2505     }
  2506     _this->suspend_screensaver = SDL_FALSE;
  2507     if (_this->SuspendScreenSaver) {
  2508         _this->SuspendScreenSaver(_this);
  2509     }
  2510 }
  2511 
  2512 void
  2513 SDL_DisableScreenSaver()
  2514 {
  2515     if (!_this) {
  2516         return;
  2517     }
  2518     if (_this->suspend_screensaver) {
  2519         return;
  2520     }
  2521     _this->suspend_screensaver = SDL_TRUE;
  2522     if (_this->SuspendScreenSaver) {
  2523         _this->SuspendScreenSaver(_this);
  2524     }
  2525 }
  2526 
  2527 void
  2528 SDL_VideoQuit(void)
  2529 {
  2530     int i, j;
  2531 
  2532     if (!_this) {
  2533         return;
  2534     }
  2535     /* Halt event processing before doing anything else */
  2536     SDL_StopEventLoop();
  2537     SDL_EnableScreenSaver();
  2538 
  2539     /* Clean up the system video */
  2540     for (i = _this->num_displays; i--;) {
  2541         SDL_VideoDisplay *display = &_this->displays[i];
  2542         for (j = display->num_windows; j--;) {
  2543             SDL_DestroyWindow(display->windows[i].id);
  2544         }
  2545         if (display->windows) {
  2546             SDL_free(display->windows);
  2547             display->windows = NULL;
  2548         }
  2549         display->num_windows = 0;
  2550         if (display->render_drivers) {
  2551             SDL_free(display->render_drivers);
  2552             display->render_drivers = NULL;
  2553         }
  2554         display->num_render_drivers = 0;
  2555     }
  2556     _this->VideoQuit(_this);
  2557 
  2558     for (i = _this->num_displays; i--;) {
  2559         SDL_VideoDisplay *display = &_this->displays[i];
  2560         for (j = display->num_display_modes; j--;) {
  2561             if (display->display_modes[j].driverdata) {
  2562                 SDL_free(display->display_modes[j].driverdata);
  2563                 display->display_modes[j].driverdata = NULL;
  2564             }
  2565         }
  2566         if (display->display_modes) {
  2567             SDL_free(display->display_modes);
  2568             display->display_modes = NULL;
  2569         }
  2570         if (display->desktop_mode.driverdata) {
  2571             SDL_free(display->desktop_mode.driverdata);
  2572             display->desktop_mode.driverdata = NULL;
  2573         }
  2574         if (display->palette) {
  2575             SDL_FreePalette(display->palette);
  2576             display->palette = NULL;
  2577         }
  2578         if (display->gamma) {
  2579             SDL_free(display->gamma);
  2580             display->gamma = NULL;
  2581         }
  2582         if (display->driverdata) {
  2583             SDL_free(display->driverdata);
  2584             display->driverdata = NULL;
  2585         }
  2586     }
  2587     if (_this->displays) {
  2588         SDL_free(_this->displays);
  2589         _this->displays = NULL;
  2590     }
  2591     _this->free(_this);
  2592     _this = NULL;
  2593 }
  2594 
  2595 int
  2596 SDL_GL_LoadLibrary(const char *path)
  2597 {
  2598     int retval;
  2599 
  2600     if (!_this) {
  2601         SDL_UninitializedVideo();
  2602         return -1;
  2603     }
  2604     if (_this->gl_config.driver_loaded) {
  2605         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2606             SDL_SetError("OpenGL library already loaded");
  2607             return -1;
  2608         }
  2609         retval = 0;
  2610     } else {
  2611         if (!_this->GL_LoadLibrary) {
  2612             SDL_SetError("No dynamic GL support in video driver");
  2613             return -1;
  2614         }
  2615         retval = _this->GL_LoadLibrary(_this, path);
  2616     }
  2617     if (retval == 0) {
  2618         ++_this->gl_config.driver_loaded;
  2619     }
  2620     return (retval);
  2621 }
  2622 
  2623 void *
  2624 SDL_GL_GetProcAddress(const char *proc)
  2625 {
  2626     void *func;
  2627 
  2628     if (!_this) {
  2629         SDL_UninitializedVideo();
  2630         return NULL;
  2631     }
  2632     func = NULL;
  2633     if (_this->GL_GetProcAddress) {
  2634         if (_this->gl_config.driver_loaded) {
  2635             func = _this->GL_GetProcAddress(_this, proc);
  2636         } else {
  2637             SDL_SetError("No GL driver has been loaded");
  2638         }
  2639     } else {
  2640         SDL_SetError("No dynamic GL support in video driver");
  2641     }
  2642     return func;
  2643 }
  2644 
  2645 void
  2646 SDL_GL_UnloadLibrary(void)
  2647 {
  2648     if (!_this) {
  2649         SDL_UninitializedVideo();
  2650         return;
  2651     }
  2652     if (_this->gl_config.driver_loaded > 0) {
  2653         if (--_this->gl_config.driver_loaded > 0) {
  2654             return;
  2655         }
  2656         if (_this->GL_UnloadLibrary) {
  2657             _this->GL_UnloadLibrary(_this);
  2658         }
  2659     }
  2660 }
  2661 
  2662 SDL_bool
  2663 SDL_GL_ExtensionSupported(const char *extension)
  2664 {
  2665 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2666     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2667     const char *extensions;
  2668     const char *start;
  2669     const char *where, *terminator;
  2670 
  2671     /* Extension names should not have spaces. */
  2672     where = SDL_strchr(extension, ' ');
  2673     if (where || *extension == '\0') {
  2674         return SDL_FALSE;
  2675     }
  2676     /* See if there's an environment variable override */
  2677     start = SDL_getenv(extension);
  2678     if (start && *start == '0') {
  2679         return SDL_FALSE;
  2680     }
  2681     /* Lookup the available extensions */
  2682     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2683     if (glGetStringFunc) {
  2684         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2685     } else {
  2686         extensions = NULL;
  2687     }
  2688     if (!extensions) {
  2689         return SDL_FALSE;
  2690     }
  2691     /*
  2692      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2693      * extensions string. Don't be fooled by sub-strings, etc.
  2694      */
  2695 
  2696     start = extensions;
  2697 
  2698     for (;;) {
  2699         where = SDL_strstr(start, extension);
  2700         if (!where)
  2701             break;
  2702 
  2703         terminator = where + SDL_strlen(extension);
  2704         if (where == start || *(where - 1) == ' ')
  2705             if (*terminator == ' ' || *terminator == '\0')
  2706                 return SDL_TRUE;
  2707 
  2708         start = terminator;
  2709     }
  2710     return SDL_FALSE;
  2711 #else
  2712     return SDL_FALSE;
  2713 #endif
  2714 }
  2715 
  2716 int
  2717 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2718 {
  2719 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2720     int retval;
  2721 
  2722     if (!_this) {
  2723         SDL_UninitializedVideo();
  2724         return -1;
  2725     }
  2726     retval = 0;
  2727     switch (attr) {
  2728     case SDL_GL_RED_SIZE:
  2729         _this->gl_config.red_size = value;
  2730         break;
  2731     case SDL_GL_GREEN_SIZE:
  2732         _this->gl_config.green_size = value;
  2733         break;
  2734     case SDL_GL_BLUE_SIZE:
  2735         _this->gl_config.blue_size = value;
  2736         break;
  2737     case SDL_GL_ALPHA_SIZE:
  2738         _this->gl_config.alpha_size = value;
  2739         break;
  2740     case SDL_GL_DOUBLEBUFFER:
  2741         _this->gl_config.double_buffer = value;
  2742         break;
  2743     case SDL_GL_BUFFER_SIZE:
  2744         _this->gl_config.buffer_size = value;
  2745         break;
  2746     case SDL_GL_DEPTH_SIZE:
  2747         _this->gl_config.depth_size = value;
  2748         break;
  2749     case SDL_GL_STENCIL_SIZE:
  2750         _this->gl_config.stencil_size = value;
  2751         break;
  2752     case SDL_GL_ACCUM_RED_SIZE:
  2753         _this->gl_config.accum_red_size = value;
  2754         break;
  2755     case SDL_GL_ACCUM_GREEN_SIZE:
  2756         _this->gl_config.accum_green_size = value;
  2757         break;
  2758     case SDL_GL_ACCUM_BLUE_SIZE:
  2759         _this->gl_config.accum_blue_size = value;
  2760         break;
  2761     case SDL_GL_ACCUM_ALPHA_SIZE:
  2762         _this->gl_config.accum_alpha_size = value;
  2763         break;
  2764     case SDL_GL_STEREO:
  2765         _this->gl_config.stereo = value;
  2766         break;
  2767     case SDL_GL_MULTISAMPLEBUFFERS:
  2768         _this->gl_config.multisamplebuffers = value;
  2769         break;
  2770     case SDL_GL_MULTISAMPLESAMPLES:
  2771         _this->gl_config.multisamplesamples = value;
  2772         break;
  2773     case SDL_GL_ACCELERATED_VISUAL:
  2774         _this->gl_config.accelerated = value;
  2775         break;
  2776     case SDL_GL_RETAINED_BACKING:
  2777         _this->gl_config.retained_backing = value;
  2778         break;
  2779     case SDL_GL_CONTEXT_MAJOR_VERSION:
  2780         _this->gl_config.major_version = value;
  2781         break;
  2782     case SDL_GL_CONTEXT_MINOR_VERSION:
  2783         _this->gl_config.minor_version = value;
  2784         break;
  2785     default:
  2786         SDL_SetError("Unknown OpenGL attribute");
  2787         retval = -1;
  2788         break;
  2789     }
  2790     return retval;
  2791 #else
  2792     SDL_Unsupported();
  2793     return -1;
  2794 #endif /* SDL_VIDEO_OPENGL */
  2795 }
  2796 
  2797 int
  2798 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2799 {
  2800 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2801     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2802     GLenum(APIENTRY * glGetErrorFunc) (void);
  2803     GLenum attrib = 0;
  2804     GLenum error = 0;
  2805 
  2806     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2807     if (!glGetIntegervFunc) {
  2808         return -1;
  2809     }
  2810 
  2811     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  2812     if (!glGetErrorFunc) {
  2813         return -1;
  2814     }
  2815 
  2816     /* Clear value in any case */
  2817     *value = 0;
  2818 
  2819     switch (attr) {
  2820     case SDL_GL_RETAINED_BACKING:
  2821         *value = _this->gl_config.retained_backing;
  2822         return 0;
  2823     case SDL_GL_RED_SIZE:
  2824         attrib = GL_RED_BITS;
  2825         break;
  2826     case SDL_GL_BLUE_SIZE:
  2827         attrib = GL_BLUE_BITS;
  2828         break;
  2829     case SDL_GL_GREEN_SIZE:
  2830         attrib = GL_GREEN_BITS;
  2831         break;
  2832     case SDL_GL_ALPHA_SIZE:
  2833         attrib = GL_ALPHA_BITS;
  2834         break;
  2835     case SDL_GL_DOUBLEBUFFER:
  2836 #ifndef SDL_VIDEO_OPENGL_ES
  2837         attrib = GL_DOUBLEBUFFER;
  2838         break;
  2839 #else
  2840         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  2841         /* parameter which switches double buffer to single buffer. OpenGL ES */
  2842         /* SDL driver must set proper value after initialization              */
  2843         *value = _this->gl_config.double_buffer;
  2844         return 0;
  2845 #endif
  2846     case SDL_GL_DEPTH_SIZE:
  2847         attrib = GL_DEPTH_BITS;
  2848         break;
  2849     case SDL_GL_STENCIL_SIZE:
  2850         attrib = GL_STENCIL_BITS;
  2851         break;
  2852 #ifndef SDL_VIDEO_OPENGL_ES
  2853     case SDL_GL_ACCUM_RED_SIZE:
  2854         attrib = GL_ACCUM_RED_BITS;
  2855         break;
  2856     case SDL_GL_ACCUM_GREEN_SIZE:
  2857         attrib = GL_ACCUM_GREEN_BITS;
  2858         break;
  2859     case SDL_GL_ACCUM_BLUE_SIZE:
  2860         attrib = GL_ACCUM_BLUE_BITS;
  2861         break;
  2862     case SDL_GL_ACCUM_ALPHA_SIZE:
  2863         attrib = GL_ACCUM_ALPHA_BITS;
  2864         break;
  2865     case SDL_GL_STEREO:
  2866         attrib = GL_STEREO;
  2867         break;
  2868 #else
  2869     case SDL_GL_ACCUM_RED_SIZE:
  2870     case SDL_GL_ACCUM_GREEN_SIZE:
  2871     case SDL_GL_ACCUM_BLUE_SIZE:
  2872     case SDL_GL_ACCUM_ALPHA_SIZE:
  2873     case SDL_GL_STEREO:
  2874         /* none of these are supported in OpenGL ES */
  2875         *value = 0;
  2876         return 0;
  2877 #endif
  2878     case SDL_GL_MULTISAMPLEBUFFERS:
  2879 #ifndef SDL_VIDEO_OPENGL_ES
  2880         attrib = GL_SAMPLE_BUFFERS_ARB;
  2881 #else
  2882         attrib = GL_SAMPLE_BUFFERS;
  2883 #endif
  2884         break;
  2885     case SDL_GL_MULTISAMPLESAMPLES:
  2886 #ifndef SDL_VIDEO_OPENGL_ES
  2887         attrib = GL_SAMPLES_ARB;
  2888 #else
  2889         attrib = GL_SAMPLES;
  2890 #endif
  2891         break;
  2892     case SDL_GL_BUFFER_SIZE:
  2893         {
  2894             GLint bits = 0;
  2895             GLint component;
  2896 
  2897             /*
  2898              * there doesn't seem to be a single flag in OpenGL
  2899              * for this!
  2900              */
  2901             glGetIntegervFunc(GL_RED_BITS, &component);
  2902             bits += component;
  2903             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2904             bits += component;
  2905             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2906             bits += component;
  2907             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2908             bits += component;
  2909 
  2910             *value = bits;
  2911             return 0;
  2912         }
  2913     case SDL_GL_ACCELERATED_VISUAL:
  2914         {
  2915             /* FIXME: How do we get this information? */
  2916             *value = (_this->gl_config.accelerated != 0);
  2917             return 0;
  2918         }
  2919     default:
  2920         SDL_SetError("Unknown OpenGL attribute");
  2921         return -1;
  2922     }
  2923 
  2924     glGetIntegervFunc(attrib, (GLint *) value);
  2925     error = glGetErrorFunc();
  2926     if (error != GL_NO_ERROR) {
  2927         switch (error) {
  2928         case GL_INVALID_ENUM:
  2929             {
  2930                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  2931             }
  2932             break;
  2933         case GL_INVALID_VALUE:
  2934             {
  2935                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  2936             }
  2937             break;
  2938         default:
  2939             {
  2940                 SDL_SetError("OpenGL error: %08X", error);
  2941             }
  2942             break;
  2943         }
  2944         return -1;
  2945     }
  2946     return 0;
  2947 #else
  2948     SDL_Unsupported();
  2949     return -1;
  2950 #endif /* SDL_VIDEO_OPENGL */
  2951 }
  2952 
  2953 SDL_GLContext
  2954 SDL_GL_CreateContext(SDL_WindowID windowID)
  2955 {
  2956     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2957 
  2958     if (!window) {
  2959         return NULL;
  2960     }
  2961     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2962         SDL_SetError("The specified window isn't an OpenGL window");
  2963         return NULL;
  2964     }
  2965     return _this->GL_CreateContext(_this, window);
  2966 }
  2967 
  2968 int
  2969 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2970 {
  2971     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2972 
  2973     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2974         SDL_SetError("The specified window isn't an OpenGL window");
  2975         return -1;
  2976     }
  2977     if (!context) {
  2978         window = NULL;
  2979     }
  2980     return _this->GL_MakeCurrent(_this, window, context);
  2981 }
  2982 
  2983 int
  2984 SDL_GL_SetSwapInterval(int interval)
  2985 {
  2986     if (!_this) {
  2987         SDL_UninitializedVideo();
  2988         return -1;
  2989     }
  2990     if (_this->GL_SetSwapInterval) {
  2991         return _this->GL_SetSwapInterval(_this, interval);
  2992     } else {
  2993         SDL_SetError("Setting the swap interval is not supported");
  2994         return -1;
  2995     }
  2996 }
  2997 
  2998 int
  2999 SDL_GL_GetSwapInterval(void)
  3000 {
  3001     if (!_this) {
  3002         SDL_UninitializedVideo();
  3003         return -1;
  3004     }
  3005     if (_this->GL_GetSwapInterval) {
  3006         return _this->GL_GetSwapInterval(_this);
  3007     } else {
  3008         SDL_SetError("Getting the swap interval is not supported");
  3009         return -1;
  3010     }
  3011 }
  3012 
  3013 void
  3014 SDL_GL_SwapWindow(SDL_WindowID windowID)
  3015 {
  3016     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3017 
  3018     if (!window) {
  3019         return;
  3020     }
  3021     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3022         SDL_SetError("The specified window isn't an OpenGL window");
  3023         return;
  3024     }
  3025     _this->GL_SwapWindow(_this, window);
  3026 }
  3027 
  3028 void
  3029 SDL_GL_DeleteContext(SDL_GLContext context)
  3030 {
  3031     if (!_this || !context) {
  3032         return;
  3033     }
  3034     _this->GL_MakeCurrent(_this, NULL, NULL);
  3035     _this->GL_DeleteContext(_this, context);
  3036 }
  3037 
  3038 #if 0                           // FIXME
  3039 /*
  3040  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3041  * & 2 for alpha channel.
  3042  */
  3043 static void
  3044 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3045 {
  3046     int x, y;
  3047     Uint32 colorkey;
  3048 #define SET_MASKBIT(icon, x, y, mask) \
  3049 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3050 
  3051     colorkey = icon->format->colorkey;
  3052     switch (icon->format->BytesPerPixel) {
  3053     case 1:
  3054         {
  3055             Uint8 *pixels;
  3056             for (y = 0; y < icon->h; ++y) {
  3057                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3058                 for (x = 0; x < icon->w; ++x) {
  3059                     if (*pixels++ == colorkey) {
  3060                         SET_MASKBIT(icon, x, y, mask);
  3061                     }
  3062                 }
  3063             }
  3064         }
  3065         break;
  3066 
  3067     case 2:
  3068         {
  3069             Uint16 *pixels;
  3070             for (y = 0; y < icon->h; ++y) {
  3071                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3072                 for (x = 0; x < icon->w; ++x) {
  3073                     if ((flags & 1) && *pixels == colorkey) {
  3074                         SET_MASKBIT(icon, x, y, mask);
  3075                     } else if ((flags & 2)
  3076                                && (*pixels & icon->format->Amask) == 0) {
  3077                         SET_MASKBIT(icon, x, y, mask);
  3078                     }
  3079                     pixels++;
  3080                 }
  3081             }
  3082         }
  3083         break;
  3084 
  3085     case 4:
  3086         {
  3087             Uint32 *pixels;
  3088             for (y = 0; y < icon->h; ++y) {
  3089                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3090                 for (x = 0; x < icon->w; ++x) {
  3091                     if ((flags & 1) && *pixels == colorkey) {
  3092                         SET_MASKBIT(icon, x, y, mask);
  3093                     } else if ((flags & 2)
  3094                                && (*pixels & icon->format->Amask) == 0) {
  3095                         SET_MASKBIT(icon, x, y, mask);
  3096                     }
  3097                     pixels++;
  3098                 }
  3099             }
  3100         }
  3101         break;
  3102     }
  3103 }
  3104 
  3105 /*
  3106  * Sets the window manager icon for the display window.
  3107  */
  3108 void
  3109 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3110 {
  3111     if (icon && _this->SetIcon) {
  3112         /* Generate a mask if necessary, and create the icon! */
  3113         if (mask == NULL) {
  3114             int mask_len = icon->h * (icon->w + 7) / 8;
  3115             int flags = 0;
  3116             mask = (Uint8 *) SDL_malloc(mask_len);
  3117             if (mask == NULL) {
  3118                 return;
  3119             }
  3120             SDL_memset(mask, ~0, mask_len);
  3121             if (icon->flags & SDL_SRCCOLORKEY)
  3122                 flags |= 1;
  3123             if (icon->flags & SDL_SRCALPHA)
  3124                 flags |= 2;
  3125             if (flags) {
  3126                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3127             }
  3128             _this->SetIcon(_this, icon, mask);
  3129             SDL_free(mask);
  3130         } else {
  3131             _this->SetIcon(_this, icon, mask);
  3132         }
  3133     }
  3134 }
  3135 #endif
  3136 
  3137 SDL_bool
  3138 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  3139 {
  3140     SDL_Window *window = SDL_GetWindowFromID(windowID);
  3141 
  3142     if (!window || !_this->GetWindowWMInfo) {
  3143         return SDL_FALSE;
  3144     }
  3145     return (_this->GetWindowWMInfo(_this, window, info));
  3146 }
  3147 
  3148 /* vi: set ts=4 sw=4 expandtab: */