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