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