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