src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 10:17:49 +0000
changeset 3316 9cf18687b761
parent 3315 87ffdad11607
child 3318 f085091f74cd
permissions -rw-r--r--
Fixed bug #764

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