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