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