src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 16:23:16 +0000
changeset 3333 b334b4f7dfa0
parent 3318 f085091f74cd
child 3417 64a60c5d502e
permissions -rw-r--r--
[SDL] Bad math in SDL_RenderCopy

Mason Wheeler to sdl

When I tried to render an image using something other than NULL for srcrect, it got horribly distorted. I traced it down to the fact that the math in the rectangle adjustments performed just before the call to renderer->RenderCopy is written inside out. It should look like this:

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