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