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