src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jul 2006 08:46:57 +0000
changeset 1919 00816063b9c9
parent 1918 092bd3a019c5
child 1920 8a162bfdc838
permissions -rw-r--r--
Fixed to compile while the OpenGL renderer is under construction
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_sysvideo.h"
    28 #include "SDL_blit.h"
    29 #include "SDL_pixels_c.h"
    30 #include "SDL_renderer_gl.h"
    31 #include "SDL_renderer_sw.h"
    32 #include "../events/SDL_sysevents.h"
    33 #include "../events/SDL_events_c.h"
    34 
    35 /* Available video drivers */
    36 static VideoBootStrap *bootstrap[] = {
    37 #if SDL_VIDEO_DRIVER_QUARTZ
    38     &QZ_bootstrap,
    39 #endif
    40 #if SDL_VIDEO_DRIVER_X11
    41     &X11_bootstrap,
    42 #endif
    43 #if SDL_VIDEO_DRIVER_DGA
    44     &DGA_bootstrap,
    45 #endif
    46 #if SDL_VIDEO_DRIVER_NANOX
    47     &NX_bootstrap,
    48 #endif
    49 #if SDL_VIDEO_DRIVER_IPOD
    50     &iPod_bootstrap,
    51 #endif
    52 #if SDL_VIDEO_DRIVER_QTOPIA
    53     &Qtopia_bootstrap,
    54 #endif
    55 #if SDL_VIDEO_DRIVER_WSCONS
    56     &WSCONS_bootstrap,
    57 #endif
    58 #if SDL_VIDEO_DRIVER_FBCON
    59     &FBCON_bootstrap,
    60 #endif
    61 #if SDL_VIDEO_DRIVER_DIRECTFB
    62     &DirectFB_bootstrap,
    63 #endif
    64 #if SDL_VIDEO_DRIVER_PS2GS
    65     &PS2GS_bootstrap,
    66 #endif
    67 #if SDL_VIDEO_DRIVER_GGI
    68     &GGI_bootstrap,
    69 #endif
    70 #if SDL_VIDEO_DRIVER_VGL
    71     &VGL_bootstrap,
    72 #endif
    73 #if SDL_VIDEO_DRIVER_SVGALIB
    74     &SVGALIB_bootstrap,
    75 #endif
    76 #if SDL_VIDEO_DRIVER_GAPI
    77     &GAPI_bootstrap,
    78 #endif
    79 #if SDL_VIDEO_DRIVER_WIN32
    80     &WIN32_bootstrap,
    81 #endif
    82 #if SDL_VIDEO_DRIVER_BWINDOW
    83     &BWINDOW_bootstrap,
    84 #endif
    85 #if SDL_VIDEO_DRIVER_TOOLBOX
    86     &TOOLBOX_bootstrap,
    87 #endif
    88 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
    89     &DSp_bootstrap,
    90 #endif
    91 #if SDL_VIDEO_DRIVER_CYBERGRAPHICS
    92     &CGX_bootstrap,
    93 #endif
    94 #if SDL_VIDEO_DRIVER_PHOTON
    95     &ph_bootstrap,
    96 #endif
    97 #if SDL_VIDEO_DRIVER_EPOC
    98     &EPOC_bootstrap,
    99 #endif
   100 #if SDL_VIDEO_DRIVER_XBIOS
   101     &XBIOS_bootstrap,
   102 #endif
   103 #if SDL_VIDEO_DRIVER_GEM
   104     &GEM_bootstrap,
   105 #endif
   106 #if SDL_VIDEO_DRIVER_PICOGUI
   107     &PG_bootstrap,
   108 #endif
   109 #if SDL_VIDEO_DRIVER_DC
   110     &DC_bootstrap,
   111 #endif
   112 #if SDL_VIDEO_DRIVER_RISCOS
   113     &RISCOS_bootstrap,
   114 #endif
   115 #if SDL_VIDEO_DRIVER_OS2FS
   116     &OS2FSLib_bootstrap,
   117 #endif
   118 #if SDL_VIDEO_DRIVER_AALIB
   119     &AALIB_bootstrap,
   120 #endif
   121 #if SDL_VIDEO_DRIVER_DUMMY
   122     &DUMMY_bootstrap,
   123 #endif
   124 #if SDL_VIDEO_DRIVER_GLSDL
   125     &glSDL_bootstrap,
   126 #endif
   127     NULL
   128 };
   129 
   130 static SDL_VideoDevice *_this = NULL;
   131 
   132 /* Various local functions */
   133 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   134 void SDL_VideoQuit(void);
   135 
   136 static int
   137 cmpmodes(const void *A, const void *B)
   138 {
   139     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   140     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   141 
   142     if (a.w != b.w) {
   143         return b.w - a.w;
   144     }
   145     if (a.h != b.h) {
   146         return b.h - a.h;
   147     }
   148     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   149         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   150     }
   151     if (a.refresh_rate != b.refresh_rate) {
   152         return b.refresh_rate - a.refresh_rate;
   153     }
   154     return 0;
   155 }
   156 
   157 static void
   158 SDL_UninitializedVideo()
   159 {
   160     SDL_SetError("Video subsystem has not been initialized");
   161 }
   162 
   163 int
   164 SDL_GetNumVideoDrivers(void)
   165 {
   166     return SDL_arraysize(bootstrap) - 1;
   167 }
   168 
   169 const char *
   170 SDL_GetVideoDriver(int index)
   171 {
   172     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   173         return bootstrap[index]->name;
   174     }
   175     return NULL;
   176 }
   177 
   178 /*
   179  * Initialize the video and event subsystems -- determine native pixel format
   180  */
   181 int
   182 SDL_VideoInit(const char *driver_name, Uint32 flags)
   183 {
   184     SDL_VideoDevice *video;
   185     int index;
   186     int i;
   187 
   188     /* Toggle the event thread flags, based on OS requirements */
   189 #if defined(MUST_THREAD_EVENTS)
   190     flags |= SDL_INIT_EVENTTHREAD;
   191 #elif defined(CANT_THREAD_EVENTS)
   192     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   193         SDL_SetError("OS doesn't support threaded events");
   194         return -1;
   195     }
   196 #endif
   197 
   198     /* Start the event loop */
   199     if (SDL_StartEventLoop(flags) < 0) {
   200         return -1;
   201     }
   202 
   203     /* Check to make sure we don't overwrite '_this' */
   204     if (_this != NULL) {
   205         SDL_VideoQuit();
   206     }
   207 
   208     /* Select the proper video driver */
   209     index = 0;
   210     video = NULL;
   211     if (driver_name == NULL) {
   212         driver_name = SDL_getenv("SDL_VIDEODRIVER");
   213     }
   214     if (driver_name != NULL) {
   215         for (i = 0; bootstrap[i]; ++i) {
   216             if (SDL_strncmp(bootstrap[i]->name, driver_name,
   217                             SDL_strlen(bootstrap[i]->name)) == 0) {
   218                 if (bootstrap[i]->available()) {
   219                     video = bootstrap[i]->create(index);
   220                 }
   221                 break;
   222             }
   223         }
   224     } else {
   225         for (i = 0; bootstrap[i]; ++i) {
   226             if (bootstrap[i]->available()) {
   227                 video = bootstrap[i]->create(index);
   228                 if (video != NULL) {
   229                     break;
   230                 }
   231             }
   232         }
   233     }
   234     if (video == NULL) {
   235         if (driver_name) {
   236             SDL_SetError("%s not available", driver_name);
   237         } else {
   238             SDL_SetError("No available video device");
   239         }
   240         return -1;
   241     }
   242     _this = video;
   243     _this->name = bootstrap[i]->name;
   244     _this->next_object_id = 1;
   245 
   246 
   247     /* Set some very sane GL defaults */
   248     _this->gl_config.driver_loaded = 0;
   249     _this->gl_config.dll_handle = NULL;
   250     _this->gl_config.red_size = 3;
   251     _this->gl_config.green_size = 3;
   252     _this->gl_config.blue_size = 2;
   253     _this->gl_config.alpha_size = 0;
   254     _this->gl_config.buffer_size = 0;
   255     _this->gl_config.depth_size = 16;
   256     _this->gl_config.stencil_size = 0;
   257     _this->gl_config.double_buffer = 1;
   258     _this->gl_config.accum_red_size = 0;
   259     _this->gl_config.accum_green_size = 0;
   260     _this->gl_config.accum_blue_size = 0;
   261     _this->gl_config.accum_alpha_size = 0;
   262     _this->gl_config.stereo = 0;
   263     _this->gl_config.multisamplebuffers = 0;
   264     _this->gl_config.multisamplesamples = 0;
   265     _this->gl_config.accelerated = -1;  /* not known, don't set */
   266 
   267     /* Initialize the video subsystem */
   268     if (_this->VideoInit(_this) < 0) {
   269         SDL_VideoQuit();
   270         return -1;
   271     }
   272 
   273     /* Make sure some displays were added */
   274     if (_this->num_displays == 0) {
   275         SDL_SetError("The video driver did not add any displays");
   276         SDL_VideoQuit();
   277         return (-1);
   278     }
   279 
   280     /* The software renderer is always available */
   281     for (i = 0; i < _this->num_displays; ++i) {
   282         if (_this->displays[i].num_render_drivers > 0) {
   283 #if 0 //SDL_VIDEO_OPENGL
   284             SDL_AddRenderDriver(i, &GL_RenderDriver);
   285 #endif
   286             SDL_AddRenderDriver(i, &SW_RenderDriver);
   287         }
   288     }
   289 
   290     /* We're ready to go! */
   291     return 0;
   292 }
   293 
   294 const char *
   295 SDL_GetCurrentVideoDriver()
   296 {
   297     if (!_this) {
   298         SDL_UninitializedVideo();
   299         return NULL;
   300     }
   301     return _this->name;
   302 }
   303 
   304 SDL_VideoDevice *
   305 SDL_GetVideoDevice()
   306 {
   307     return _this;
   308 }
   309 
   310 int
   311 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   312 {
   313     SDL_VideoDisplay display;
   314 
   315     SDL_zero(display);
   316     if (desktop_mode) {
   317         display.desktop_mode = *desktop_mode;
   318     }
   319     display.current_mode = display.desktop_mode;
   320 
   321     return SDL_AddVideoDisplay(&display);
   322 }
   323 
   324 int
   325 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   326 {
   327     SDL_VideoDisplay *displays;
   328     int index = -1;
   329 
   330     displays =
   331         SDL_realloc(_this->displays,
   332                     (_this->num_displays + 1) * sizeof(*displays));
   333     if (displays) {
   334         index = _this->num_displays++;
   335         displays[index] = *display;
   336         displays[index].device = _this;
   337         _this->displays = displays;
   338     } else {
   339         SDL_OutOfMemory();
   340     }
   341     return index;
   342 }
   343 
   344 int
   345 SDL_GetNumVideoDisplays(void)
   346 {
   347     if (!_this) {
   348         SDL_UninitializedVideo();
   349         return 0;
   350     }
   351     return _this->num_displays;
   352 }
   353 
   354 int
   355 SDL_SelectVideoDisplay(int index)
   356 {
   357     if (!_this) {
   358         SDL_UninitializedVideo();
   359         return (-1);
   360     }
   361     if (index >= 0) {
   362         if (index >= _this->num_displays) {
   363             SDL_SetError("index must be in the range 0 - %d",
   364                          _this->num_displays - 1);
   365             return -1;
   366         }
   367         _this->current_display = index;
   368     }
   369     return _this->current_display;
   370 }
   371 
   372 SDL_bool
   373 SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
   374 {
   375     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   376     SDL_DisplayMode *modes;
   377     int i, nmodes;
   378 
   379     /* Make sure we don't already have the mode in the list */
   380     modes = display->display_modes;
   381     nmodes = display->num_display_modes;
   382     for (i = nmodes; i--;) {
   383         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   384             return SDL_FALSE;
   385         }
   386     }
   387 
   388     /* Go ahead and add the new mode */
   389     if (nmodes == display->max_display_modes) {
   390         modes =
   391             SDL_realloc(modes,
   392                         (display->max_display_modes + 32) * sizeof(*modes));
   393         if (!modes) {
   394             return SDL_FALSE;
   395         }
   396         display->display_modes = modes;
   397         display->max_display_modes += 32;
   398     }
   399     modes[nmodes] = *mode;
   400     display->num_display_modes++;
   401 
   402     return SDL_TRUE;
   403 }
   404 
   405 int
   406 SDL_GetNumDisplayModes()
   407 {
   408     if (_this) {
   409         SDL_VideoDisplay *display = &SDL_CurrentDisplay;
   410         if (!display->num_display_modes && _this->GetDisplayModes) {
   411             _this->GetDisplayModes(_this);
   412             SDL_qsort(display->display_modes, display->num_display_modes,
   413                       sizeof(SDL_DisplayMode), cmpmodes);
   414         }
   415         return display->num_display_modes;
   416     }
   417     return 0;
   418 }
   419 
   420 const SDL_DisplayMode *
   421 SDL_GetDisplayMode(int index)
   422 {
   423     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   424         SDL_SetError("index must be in the range of 0 - %d",
   425                      SDL_GetNumDisplayModes() - 1);
   426         return NULL;
   427     }
   428     return &SDL_CurrentDisplay.display_modes[index];
   429 }
   430 
   431 const SDL_DisplayMode *
   432 SDL_GetDesktopDisplayMode(void)
   433 {
   434     if (_this) {
   435         return &SDL_CurrentDisplay.desktop_mode;
   436     }
   437     return NULL;
   438 }
   439 
   440 const SDL_DisplayMode *
   441 SDL_GetCurrentDisplayMode(void)
   442 {
   443     if (_this) {
   444         return &SDL_CurrentDisplay.current_mode;
   445     }
   446     return NULL;
   447 }
   448 
   449 SDL_DisplayMode *
   450 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   451                           SDL_DisplayMode * closest)
   452 {
   453     Uint32 target_format;
   454     int target_refresh_rate;
   455     int i;
   456     SDL_DisplayMode *current, *match;
   457 
   458     if (!_this || !mode || !closest) {
   459         return NULL;
   460     }
   461 
   462     /* Default to the desktop format */
   463     if (mode->format) {
   464         target_format = mode->format;
   465     } else {
   466         target_format = SDL_CurrentDisplay.desktop_mode.format;
   467     }
   468 
   469     /* Default to the desktop refresh rate */
   470     if (mode->refresh_rate) {
   471         target_refresh_rate = mode->refresh_rate;
   472     } else {
   473         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   474     }
   475 
   476     match = NULL;
   477     for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
   478         current = &SDL_CurrentDisplay.display_modes[i];
   479 
   480         if ((current->w && current->h) &&
   481             (current->w < mode->w || current->h < mode->h)) {
   482             /* Out of sorted modes large enough here */
   483             break;
   484         }
   485         if (!match || current->w < match->w || current->h < match->h) {
   486             match = current;
   487             continue;
   488         }
   489         if (current->format != match->format) {
   490             /* Sorted highest depth to lowest */
   491             if (current->format == target_format ||
   492                 (SDL_BITSPERPIXEL(current->format) >=
   493                  SDL_BITSPERPIXEL(target_format)
   494                  && SDL_PIXELTYPE(current->format) ==
   495                  SDL_PIXELTYPE(target_format))) {
   496                 match = current;
   497             }
   498             continue;
   499         }
   500         if (current->refresh_rate != match->refresh_rate) {
   501             /* Sorted highest refresh to lowest */
   502             if (current->refresh_rate >= target_refresh_rate) {
   503                 match = current;
   504             }
   505         }
   506     }
   507     if (match) {
   508         if (match->format) {
   509             closest->format = match->format;
   510         } else {
   511             closest->format = mode->format;
   512         }
   513         if (match->w && match->h) {
   514             closest->w = match->w;
   515             closest->h = match->h;
   516         } else {
   517             closest->w = mode->w;
   518             closest->h = mode->h;
   519         }
   520         if (match->refresh_rate) {
   521             closest->refresh_rate = match->refresh_rate;
   522         } else {
   523             closest->refresh_rate = mode->refresh_rate;
   524         }
   525         closest->driverdata = match->driverdata;
   526 
   527         /* Pick some reasonable defaults if the app and driver don't care */
   528         if (!closest->format) {
   529             closest->format = SDL_PixelFormat_RGB888;
   530         }
   531         if (!closest->w) {
   532             closest->w = 640;
   533         }
   534         if (!closest->h) {
   535             closest->h = 480;
   536         }
   537         return closest;
   538     }
   539     return NULL;
   540 }
   541 
   542 int
   543 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   544 {
   545     SDL_VideoDisplay *display;
   546     SDL_DisplayMode display_mode;
   547     int i, ncolors;
   548 
   549     if (!_this) {
   550         SDL_UninitializedVideo();
   551         return -1;
   552     }
   553 
   554     if (!mode) {
   555         mode = SDL_GetDesktopDisplayMode();
   556     }
   557     display = &SDL_CurrentDisplay;
   558     display_mode = *mode;
   559 
   560     /* Default to the current mode */
   561     if (!display_mode.format) {
   562         display_mode.format = display->current_mode.format;
   563     }
   564     if (!display_mode.w) {
   565         display_mode.w = display->current_mode.w;
   566     }
   567     if (!display_mode.h) {
   568         display_mode.h = display->current_mode.h;
   569     }
   570     if (!display_mode.refresh_rate) {
   571         display_mode.refresh_rate = display->current_mode.refresh_rate;
   572     }
   573 
   574     /* Get a good video mode, the closest one possible */
   575     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   576         SDL_SetError("No video mode large enough for %dx%d",
   577                      display_mode.w, display_mode.h);
   578         return -1;
   579     }
   580 
   581     /* See if there's anything left to do */
   582     if (SDL_memcmp
   583         (&display_mode, SDL_GetCurrentDisplayMode(),
   584          sizeof(display_mode)) == 0) {
   585         return 0;
   586     }
   587 
   588     /* Actually change the display mode */
   589     if (_this->SetDisplayMode(_this, &display_mode) < 0) {
   590         return -1;
   591     }
   592     display->current_mode = display_mode;
   593 
   594     /* Set up a palette, if necessary */
   595     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   596         ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   597     } else {
   598         ncolors = 0;
   599     }
   600     if ((!ncolors && display->palette) || (ncolors && !display->palette)
   601         || (ncolors && ncolors != display->palette->ncolors)) {
   602         if (display->palette) {
   603             SDL_FreePalette(display->palette);
   604             display->palette = NULL;
   605         }
   606         if (ncolors) {
   607             display->palette = SDL_AllocPalette(ncolors);
   608             if (!display->palette) {
   609                 return -1;
   610             }
   611             SDL_DitherColors(display->palette->colors,
   612                              SDL_BITSPERPIXEL(display_mode.format));
   613         }
   614     }
   615 
   616     /* Move any fullscreen windows into position */
   617     for (i = 0; i < display->num_windows; ++i) {
   618         SDL_Window *window = &display->windows[i];
   619         if (FULLSCREEN_VISIBLE(window)) {
   620             SDL_SetWindowPosition(window->id,
   621                                   ((display_mode.w - window->w) / 2),
   622                                   ((display_mode.h - window->h) / 2));
   623         }
   624     }
   625 
   626     return 0;
   627 }
   628 
   629 int
   630 SDL_SetFullscreenDisplayMode(const SDL_DisplayMode * mode)
   631 {
   632     SDL_VideoDisplay *display;
   633     int i;
   634 
   635     if (!_this) {
   636         SDL_UninitializedVideo();
   637         return -1;
   638     }
   639 
   640     display = &SDL_CurrentDisplay;
   641     if (mode) {
   642         SDL_GetClosestDisplayMode(mode, &display->desired_mode);
   643         display->fullscreen_mode = &display->desired_mode;
   644     } else {
   645         display->fullscreen_mode = NULL;
   646     }
   647 
   648     /* Actually set the mode if we have a fullscreen window visible */
   649     for (i = 0; i < display->num_windows; ++i) {
   650         SDL_Window *window = &display->windows[i];
   651         if (FULLSCREEN_VISIBLE(window)) {
   652             return SDL_SetDisplayMode(display->fullscreen_mode);
   653         }
   654     }
   655     return 0;
   656 }
   657 
   658 const SDL_DisplayMode *
   659 SDL_GetFullscreenDisplayMode(void)
   660 {
   661     if (_this) {
   662         return SDL_CurrentDisplay.fullscreen_mode;
   663     }
   664     return NULL;
   665 }
   666 
   667 int
   668 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   669 {
   670     SDL_Palette *palette;
   671     int status = 0;
   672 
   673     if (!_this) {
   674         SDL_UninitializedVideo();
   675         return -1;
   676     }
   677     palette = SDL_CurrentDisplay.palette;
   678     if (!palette) {
   679         SDL_SetError("Display mode does not have a palette");
   680         return -1;
   681     }
   682 
   683     status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
   684 
   685     if (_this->SetDisplayPalette) {
   686         if (_this->SetDisplayPalette(_this, palette) < 0) {
   687             status = -1;
   688         }
   689     }
   690     return status;
   691 }
   692 
   693 int
   694 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   695 {
   696     SDL_Palette *palette;
   697 
   698     if (!_this) {
   699         SDL_UninitializedVideo();
   700         return -1;
   701     }
   702 
   703     palette = SDL_CurrentDisplay.palette;
   704     if (!palette->ncolors) {
   705         SDL_SetError("Display mode does not have a palette");
   706         return -1;
   707     }
   708 
   709     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   710         SDL_SetError("Palette indices are out of range");
   711         return -1;
   712     }
   713 
   714     SDL_memcpy(colors, &palette->colors[firstcolor],
   715                ncolors * sizeof(*colors));
   716     return 0;
   717 }
   718 
   719 SDL_WindowID
   720 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   721 {
   722     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   723                                   SDL_WINDOW_OPENGL |
   724                                   SDL_WINDOW_SHOWN |
   725                                   SDL_WINDOW_BORDERLESS |
   726                                   SDL_WINDOW_RESIZABLE |
   727                                   SDL_WINDOW_MAXIMIZED |
   728                                   SDL_WINDOW_MINIMIZED |
   729                                   SDL_WINDOW_INPUT_GRABBED);
   730     SDL_VideoDisplay *display;
   731     SDL_Window window;
   732     int num_windows;
   733     SDL_Window *windows;
   734 
   735     if (!_this) {
   736         SDL_UninitializedVideo();
   737         return 0;
   738     }
   739 
   740     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   741         SDL_SetError("No OpenGL support in video driver");
   742         return 0;
   743     }
   744 
   745     SDL_zero(window);
   746     window.id = _this->next_object_id++;
   747     window.title = title ? SDL_strdup(title) : NULL;
   748     window.x = x;
   749     window.y = y;
   750     window.w = w;
   751     window.h = h;
   752     window.flags = (flags & allowed_flags);
   753     window.display = _this->current_display;
   754 
   755     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   756         if (window.title) {
   757             SDL_free(window.title);
   758         }
   759         return 0;
   760     }
   761 
   762     display = &SDL_CurrentDisplay;
   763     num_windows = display->num_windows;
   764     windows =
   765         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   766     if (!windows) {
   767         if (_this->DestroyWindow) {
   768             _this->DestroyWindow(_this, &window);
   769         }
   770         if (window.title) {
   771             SDL_free(window.title);
   772         }
   773         return 0;
   774     }
   775     windows[num_windows] = window;
   776     display->windows = windows;
   777     display->num_windows++;
   778 
   779     if (FULLSCREEN_VISIBLE(&window)) {
   780         /* Hide any other fullscreen windows */
   781         int i;
   782         for (i = 0; i < display->num_windows; ++i) {
   783             SDL_Window *other = &display->windows[i];
   784             if (other->id != window.id && FULLSCREEN_VISIBLE(other)) {
   785                 SDL_MinimizeWindow(other->id);
   786             }
   787         }
   788         SDL_SetDisplayMode(display->fullscreen_mode);
   789     }
   790 
   791     return window.id;
   792 }
   793 
   794 SDL_WindowID
   795 SDL_CreateWindowFrom(const void *data)
   796 {
   797     SDL_VideoDisplay *display;
   798     SDL_Window window;
   799     int num_windows;
   800     SDL_Window *windows;
   801 
   802     if (!_this) {
   803         SDL_UninitializedVideo();
   804         return (0);
   805     }
   806 
   807     SDL_zero(window);
   808     window.id = _this->next_object_id++;
   809     window.display = _this->current_display;
   810 
   811     if (!_this->CreateWindowFrom ||
   812         _this->CreateWindowFrom(_this, &window, data) < 0) {
   813         return 0;
   814     }
   815 
   816     display = &SDL_CurrentDisplay;
   817     num_windows = display->num_windows;
   818     windows =
   819         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   820     if (!windows) {
   821         if (_this->DestroyWindow) {
   822             _this->DestroyWindow(_this, &window);
   823         }
   824         if (window.title) {
   825             SDL_free(window.title);
   826         }
   827         return 0;
   828     }
   829     windows[num_windows] = window;
   830     display->windows = windows;
   831     display->num_windows++;
   832 
   833     return window.id;
   834 }
   835 
   836 SDL_Window *
   837 SDL_GetWindowFromID(SDL_WindowID windowID)
   838 {
   839     int i, j;
   840 
   841     if (!_this) {
   842         SDL_UninitializedVideo();
   843         return NULL;
   844     }
   845 
   846     for (i = 0; i < _this->num_displays; ++i) {
   847         SDL_VideoDisplay *display = &_this->displays[i];
   848         for (j = 0; j < display->num_windows; ++j) {
   849             SDL_Window *window = &display->windows[j];
   850             if (window->id == windowID) {
   851                 return window;
   852             }
   853         }
   854     }
   855     return NULL;
   856 }
   857 
   858 SDL_VideoDisplay *
   859 SDL_GetDisplayFromWindow(SDL_Window * window)
   860 {
   861     if (!_this) {
   862         SDL_UninitializedVideo();
   863         return NULL;
   864     }
   865     return &_this->displays[window->display];
   866 }
   867 
   868 Uint32
   869 SDL_GetWindowFlags(SDL_WindowID windowID)
   870 {
   871     SDL_Window *window = SDL_GetWindowFromID(windowID);
   872 
   873     if (!window) {
   874         return 0;
   875     }
   876     return window->flags;
   877 }
   878 
   879 void
   880 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   881 {
   882     SDL_Window *window = SDL_GetWindowFromID(windowID);
   883 
   884     if (!window) {
   885         return;
   886     }
   887     if (window->title) {
   888         SDL_free(window->title);
   889     }
   890     window->title = SDL_strdup(title);
   891 
   892     if (_this->SetWindowTitle) {
   893         _this->SetWindowTitle(_this, window);
   894     }
   895 }
   896 
   897 const char *
   898 SDL_GetWindowTitle(SDL_WindowID windowID)
   899 {
   900     SDL_Window *window = SDL_GetWindowFromID(windowID);
   901 
   902     if (!window) {
   903         return NULL;
   904     }
   905     return window->title;
   906 }
   907 
   908 void
   909 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   910 {
   911     SDL_Window *window = SDL_GetWindowFromID(windowID);
   912 
   913     if (!window) {
   914         return;
   915     }
   916     window->userdata = userdata;
   917 }
   918 
   919 void *
   920 SDL_GetWindowData(SDL_WindowID windowID)
   921 {
   922     SDL_Window *window = SDL_GetWindowFromID(windowID);
   923 
   924     if (!window) {
   925         return NULL;
   926     }
   927     return window->userdata;
   928 }
   929 
   930 void
   931 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   932 {
   933     SDL_Window *window = SDL_GetWindowFromID(windowID);
   934 
   935     if (!window) {
   936         return;
   937     }
   938 
   939     if (x != SDL_WINDOWPOS_UNDEFINED) {
   940         window->x = x;
   941     }
   942     if (y != SDL_WINDOWPOS_UNDEFINED) {
   943         window->y = y;
   944     }
   945 
   946     if (_this->SetWindowPosition) {
   947         _this->SetWindowPosition(_this, window);
   948     }
   949 }
   950 
   951 void
   952 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   953 {
   954     SDL_Window *window = SDL_GetWindowFromID(windowID);
   955 
   956     if (!window) {
   957         return;
   958     }
   959     if (x) {
   960         *x = window->x;
   961     }
   962     if (y) {
   963         *y = window->y;
   964     }
   965 }
   966 
   967 void
   968 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   969 {
   970     SDL_Window *window = SDL_GetWindowFromID(windowID);
   971 
   972     if (!window) {
   973         return;
   974     }
   975 
   976     window->w = w;
   977     window->h = h;
   978 
   979     if (_this->SetWindowSize) {
   980         _this->SetWindowSize(_this, window);
   981     }
   982 }
   983 
   984 void
   985 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   986 {
   987     SDL_Window *window = SDL_GetWindowFromID(windowID);
   988 
   989     if (!window) {
   990         return;
   991     }
   992     if (w) {
   993         *w = window->w;
   994     }
   995     if (h) {
   996         *h = window->h;
   997     }
   998 }
   999 
  1000 void
  1001 SDL_ShowWindow(SDL_WindowID windowID)
  1002 {
  1003     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1004 
  1005     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1006         return;
  1007     }
  1008 
  1009     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1010 
  1011     if (_this->ShowWindow) {
  1012         _this->ShowWindow(_this, window);
  1013     }
  1014 }
  1015 
  1016 void
  1017 SDL_HideWindow(SDL_WindowID windowID)
  1018 {
  1019     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1020 
  1021     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1022         return;
  1023     }
  1024 
  1025     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1026 
  1027     if (_this->HideWindow) {
  1028         _this->HideWindow(_this, window);
  1029     }
  1030 }
  1031 
  1032 void
  1033 SDL_RaiseWindow(SDL_WindowID windowID)
  1034 {
  1035     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1036 
  1037     if (!window) {
  1038         return;
  1039     }
  1040 
  1041     if (_this->RaiseWindow) {
  1042         _this->RaiseWindow(_this, window);
  1043     }
  1044 }
  1045 
  1046 void
  1047 SDL_MaximizeWindow(SDL_WindowID windowID)
  1048 {
  1049     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1050 
  1051     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1052         return;
  1053     }
  1054 
  1055     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1056 
  1057     if (_this->MaximizeWindow) {
  1058         _this->MaximizeWindow(_this, window);
  1059     }
  1060 }
  1061 
  1062 void
  1063 SDL_MinimizeWindow(SDL_WindowID windowID)
  1064 {
  1065     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1066 
  1067     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1068         return;
  1069     }
  1070 
  1071     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1072 
  1073     if (_this->MinimizeWindow) {
  1074         _this->MinimizeWindow(_this, window);
  1075     }
  1076 }
  1077 
  1078 void
  1079 SDL_RestoreWindow(SDL_WindowID windowID)
  1080 {
  1081     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1082 
  1083     if (!window
  1084         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1085         return;
  1086     }
  1087 
  1088     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1089 
  1090     if (_this->RestoreWindow) {
  1091         _this->RestoreWindow(_this, window);
  1092     }
  1093 }
  1094 
  1095 int
  1096 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1097 {
  1098     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1099 
  1100     if (!window) {
  1101         return -1;
  1102     }
  1103 
  1104     if (fullscreen) {
  1105         fullscreen = SDL_WINDOW_FULLSCREEN;
  1106     }
  1107     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1108         return 0;
  1109     }
  1110 
  1111     if (fullscreen) {
  1112         window->flags |= SDL_WINDOW_FULLSCREEN;
  1113 
  1114         if (FULLSCREEN_VISIBLE(window)) {
  1115             SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1116 
  1117             /* Hide any other fullscreen windows */
  1118             int i;
  1119             for (i = 0; i < display->num_windows; ++i) {
  1120                 SDL_Window *other = &display->windows[i];
  1121                 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
  1122                     SDL_MinimizeWindow(other->id);
  1123                 }
  1124             }
  1125 
  1126             SDL_SetDisplayMode(display->fullscreen_mode);
  1127         }
  1128     } else {
  1129         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1130 
  1131         if (FULLSCREEN_VISIBLE(window)) {
  1132             SDL_SetDisplayMode(NULL);
  1133         }
  1134     }
  1135     return 0;
  1136 }
  1137 
  1138 void
  1139 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1140 {
  1141     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1142 
  1143     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1144         return;
  1145     }
  1146 
  1147     if (mode) {
  1148         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1149     } else {
  1150         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1151     }
  1152 
  1153     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1154         _this->SetWindowGrab(_this, window);
  1155     }
  1156 }
  1157 
  1158 int
  1159 SDL_GetWindowGrab(SDL_WindowID windowID)
  1160 {
  1161     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1162 
  1163     if (!window) {
  1164         return 0;
  1165     }
  1166 
  1167     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1168 }
  1169 
  1170 void
  1171 SDL_OnWindowShown(SDL_Window * window)
  1172 {
  1173 }
  1174 
  1175 void
  1176 SDL_OnWindowHidden(SDL_Window * window)
  1177 {
  1178 }
  1179 
  1180 void
  1181 SDL_OnWindowFocusGained(SDL_Window * window)
  1182 {
  1183     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1184 
  1185     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1186         SDL_SetDisplayMode(display->fullscreen_mode);
  1187     }
  1188     if (display->gamma && _this->SetDisplayGammaRamp) {
  1189         _this->SetDisplayGammaRamp(_this, display->gamma);
  1190     }
  1191     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1192         _this->SetWindowGrab(_this, window);
  1193     }
  1194 }
  1195 
  1196 void
  1197 SDL_OnWindowFocusLost(SDL_Window * window)
  1198 {
  1199     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1200 
  1201     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1202         SDL_MinimizeWindow(window->id);
  1203         SDL_SetDisplayMode(NULL);
  1204     }
  1205     if (display->gamma && _this->SetDisplayGammaRamp) {
  1206         _this->SetDisplayGammaRamp(_this, display->saved_gamma);
  1207     }
  1208     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1209         _this->SetWindowGrab(_this, window);
  1210     }
  1211 }
  1212 
  1213 SDL_WindowID
  1214 SDL_GetFocusWindow(void)
  1215 {
  1216     SDL_VideoDisplay *display;
  1217     int i;
  1218 
  1219     if (!_this) {
  1220         return 0;
  1221     }
  1222 
  1223     display = &SDL_CurrentDisplay;
  1224     for (i = 0; i < display->num_windows; ++i) {
  1225         SDL_Window *window = &display->windows[i];
  1226 
  1227         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1228             return window->id;
  1229         }
  1230     }
  1231     return 0;
  1232 }
  1233 
  1234 void
  1235 SDL_DestroyWindow(SDL_WindowID windowID)
  1236 {
  1237     int i, j;
  1238 
  1239     if (!_this) {
  1240         return;
  1241     }
  1242 
  1243     /* Restore video mode, etc. */
  1244     SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  1245 
  1246     for (i = 0; i < _this->num_displays; ++i) {
  1247         SDL_VideoDisplay *display = &_this->displays[i];
  1248         for (j = 0; j < display->num_windows; ++j) {
  1249             SDL_Window *window = &display->windows[j];
  1250             if (window->id != windowID) {
  1251                 continue;
  1252             }
  1253             if (window->renderer) {
  1254                 SDL_DestroyRenderer(window->id);
  1255             }
  1256             if (_this->DestroyWindow) {
  1257                 _this->DestroyWindow(_this, window);
  1258             }
  1259             if (window->title) {
  1260                 SDL_free(window->title);
  1261             }
  1262             if (j != display->num_windows - 1) {
  1263                 SDL_memcpy(&display->windows[i],
  1264                            &display->windows[i + 1],
  1265                            (display->num_windows - i - 1) * sizeof(*window));
  1266             }
  1267             --display->num_windows;
  1268             return;
  1269         }
  1270     }
  1271 }
  1272 
  1273 void
  1274 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1275 {
  1276     SDL_VideoDisplay *display = &_this->displays[displayIndex];
  1277     SDL_RenderDriver *render_drivers;
  1278 
  1279     render_drivers =
  1280         SDL_realloc(display->render_drivers,
  1281                     (display->num_render_drivers +
  1282                      1) * sizeof(*render_drivers));
  1283     if (render_drivers) {
  1284         render_drivers[display->num_render_drivers] = *driver;
  1285         display->render_drivers = render_drivers;
  1286         display->num_render_drivers++;
  1287     }
  1288 }
  1289 
  1290 int
  1291 SDL_GetNumRenderers(void)
  1292 {
  1293     if (_this) {
  1294         return SDL_CurrentDisplay.num_render_drivers;
  1295     }
  1296     return 0;
  1297 }
  1298 
  1299 int
  1300 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1301 {
  1302     if (!_this) {
  1303         SDL_UninitializedVideo();
  1304         return -1;
  1305     }
  1306 
  1307     if (index >= SDL_GetNumRenderers()) {
  1308         SDL_SetError("index must be in the range of 0 - %d",
  1309                      SDL_GetNumRenderers() - 1);
  1310         return -1;
  1311     }
  1312     if (index < 0) {
  1313         if (!SDL_CurrentDisplay.current_renderer) {
  1314             SDL_SetError("There is no current renderer");
  1315             return -1;
  1316         }
  1317         *info = SDL_CurrentDisplay.current_renderer->info;
  1318     } else {
  1319         *info = SDL_CurrentDisplay.render_drivers[index].info;
  1320     }
  1321     return 0;
  1322 }
  1323 
  1324 int
  1325 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1326 {
  1327     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1328 
  1329     if (!window) {
  1330         return 0;
  1331     }
  1332 
  1333     if (index < 0) {
  1334         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1335         int n = SDL_GetNumRenderers();
  1336         for (index = 0; index < n; ++index) {
  1337             SDL_RenderDriver *driver =
  1338                 &SDL_CurrentDisplay.render_drivers[index];
  1339 
  1340             if (override) {
  1341                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1342                     break;
  1343                 }
  1344             } else {
  1345                 if ((driver->info.flags & flags) == flags) {
  1346                     break;
  1347                 }
  1348             }
  1349         }
  1350         if (index == n) {
  1351             SDL_SetError("Couldn't find matching render driver");
  1352             return -1;
  1353         }
  1354     }
  1355 
  1356     if (index >= SDL_GetNumRenderers()) {
  1357         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1358                      SDL_GetNumRenderers() - 1);
  1359         return -1;
  1360     }
  1361 
  1362     /* Free any existing renderer */
  1363     SDL_DestroyRenderer(windowID);
  1364 
  1365     /* Create a new renderer instance */
  1366     window->renderer =
  1367         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1368                                                                 flags);
  1369     if (!window->renderer) {
  1370         return -1;
  1371     }
  1372     SDL_CurrentDisplay.current_renderer = window->renderer;
  1373 
  1374     return 0;
  1375 }
  1376 
  1377 int
  1378 SDL_SelectRenderer(SDL_WindowID windowID)
  1379 {
  1380     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1381 
  1382     if (!window || !window->renderer) {
  1383         return -1;
  1384     }
  1385     SDL_CurrentDisplay.current_renderer = window->renderer;
  1386     return 0;
  1387 }
  1388 
  1389 SDL_TextureID
  1390 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1391 {
  1392     int hash;
  1393     SDL_Renderer *renderer;
  1394     SDL_Texture *texture;
  1395 
  1396     if (!_this) {
  1397         SDL_UninitializedVideo();
  1398         return 0;
  1399     }
  1400 
  1401     renderer = SDL_CurrentDisplay.current_renderer;
  1402     if (!renderer || !renderer->CreateTexture) {
  1403         return 0;
  1404     }
  1405 
  1406     texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
  1407     if (!texture) {
  1408         SDL_OutOfMemory();
  1409         return 0;
  1410     }
  1411 
  1412     SDL_zerop(texture);
  1413     texture->id = _this->next_object_id++;
  1414     texture->format = format;
  1415     texture->access = access;
  1416     texture->w = w;
  1417     texture->h = h;
  1418     texture->renderer = renderer;
  1419 
  1420     if (renderer->CreateTexture(renderer, texture) < 0) {
  1421         SDL_free(texture);
  1422         return 0;
  1423     }
  1424 
  1425     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1426     texture->next = SDL_CurrentDisplay.textures[hash];
  1427     SDL_CurrentDisplay.textures[hash] = texture;
  1428 
  1429     return texture->id;
  1430 }
  1431 
  1432 SDL_TextureID
  1433 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1434 {
  1435     SDL_TextureID textureID;
  1436     Uint32 surface_flags = surface->flags;
  1437     SDL_PixelFormat *fmt = surface->format;
  1438     Uint8 alpha;
  1439     SDL_Rect bounds;
  1440     SDL_Surface dst;
  1441     int bpp;
  1442     Uint32 Rmask, Gmask, Bmask, Amask;
  1443 
  1444     if (!surface) {
  1445         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1446         return 0;
  1447     }
  1448 
  1449     if (format) {
  1450         if (!SDL_PixelFormatEnumToMasks
  1451             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1452             SDL_SetError("Unknown pixel format");
  1453             return 0;
  1454         }
  1455     } else {
  1456         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1457             bpp = fmt->BitsPerPixel;
  1458             Rmask = fmt->Rmask;
  1459             Gmask = fmt->Gmask;
  1460             Bmask = fmt->Bmask;
  1461             Amask = fmt->Amask;
  1462         } else {
  1463             /* Need a format with alpha */
  1464             bpp = 32;
  1465             Rmask = 0x00FF0000;
  1466             Gmask = 0x0000FF00;
  1467             Bmask = 0x000000FF;
  1468             Amask = 0xFF000000;
  1469         }
  1470         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1471         if (!format) {
  1472             SDL_SetError("Unknown pixel format");
  1473             return 0;
  1474         }
  1475     }
  1476 
  1477     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1478     if (!textureID) {
  1479         return 0;
  1480     }
  1481 
  1482     /* Set up a destination surface for the texture update */
  1483     SDL_zero(dst);
  1484     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1485     if (!dst.format) {
  1486         SDL_DestroyTexture(textureID);
  1487         return 0;
  1488     }
  1489     dst.w = surface->w;
  1490     dst.h = surface->h;
  1491     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1492         dst.flags |= SDL_PREALLOC;
  1493     } else {
  1494         dst.pitch = SDL_CalculatePitch(&dst);
  1495         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1496         if (!dst.pixels) {
  1497             SDL_DestroyTexture(textureID);
  1498             SDL_FreeFormat(dst.format);
  1499             SDL_OutOfMemory();
  1500             return 0;
  1501         }
  1502     }
  1503 
  1504     /* Copy the palette if any */
  1505     if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1506         if (fmt->palette) {
  1507             SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1508                                   fmt->palette->ncolors);
  1509             SDL_SetSurfacePalette(&dst, fmt->palette);
  1510         } else {
  1511             dst.format->palette =
  1512                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1513             if (!dst.format->palette) {
  1514                 SDL_DestroyTexture(textureID);
  1515                 SDL_FreeFormat(dst.format);
  1516                 return 0;
  1517             }
  1518             SDL_DitherColors(dst.format->palette->colors,
  1519                              SDL_BITSPERPIXEL(format));
  1520         }
  1521     }
  1522 
  1523     /* Make the texture transparent if the surface has colorkey */
  1524     if (surface_flags & SDL_SRCCOLORKEY) {
  1525         int row;
  1526         int length = dst.w * dst.format->BytesPerPixel;
  1527         Uint8 *p = (Uint8 *) dst.pixels;
  1528         for (row = 0; row < dst.h; ++row) {
  1529             SDL_memset(p, 0, length);
  1530             p += dst.pitch;
  1531         }
  1532     }
  1533 
  1534     /* Copy over the alpha channel */
  1535     if (surface_flags & SDL_SRCALPHA) {
  1536         if (fmt->Amask) {
  1537             surface->flags &= ~SDL_SRCALPHA;
  1538         } else {
  1539             /* FIXME: Need to make sure the texture has an alpha channel
  1540              *        and copy 'alpha' into the texture alpha channel.
  1541              */
  1542             alpha = surface->format->alpha;
  1543             SDL_SetAlpha(surface, 0, 0);
  1544         }
  1545     }
  1546 
  1547     /* Copy over the image data */
  1548     bounds.x = 0;
  1549     bounds.y = 0;
  1550     bounds.w = surface->w;
  1551     bounds.h = surface->h;
  1552     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1553 
  1554     /* Clean up the original surface */
  1555     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1556         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1557         if (fmt->Amask) {
  1558             surface->flags |= SDL_SRCALPHA;
  1559         } else {
  1560             SDL_SetAlpha(surface, aflags, alpha);
  1561         }
  1562     }
  1563 
  1564     /* Update the texture */
  1565     if (dst.flags & SDL_PREALLOC) {
  1566         SDL_UnlockTexture(textureID);
  1567     } else {
  1568         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1569         SDL_free(dst.pixels);
  1570     }
  1571     SDL_FreeFormat(dst.format);
  1572 
  1573     return textureID;
  1574 }
  1575 
  1576 static __inline__ SDL_Texture *
  1577 SDL_GetTextureFromID(SDL_TextureID textureID)
  1578 {
  1579     int hash;
  1580     SDL_Texture *texture;
  1581 
  1582     if (!_this) {
  1583         return NULL;
  1584     }
  1585 
  1586     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1587     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1588          texture = texture->next) {
  1589         if (texture->id == textureID) {
  1590             return texture;
  1591         }
  1592     }
  1593     return NULL;
  1594 }
  1595 
  1596 int
  1597 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1598                  int *w, int *h)
  1599 {
  1600     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1601 
  1602     if (!texture) {
  1603         return -1;
  1604     }
  1605 
  1606     if (format) {
  1607         *format = texture->format;
  1608     }
  1609     if (access) {
  1610         *access = texture->access;
  1611     }
  1612     if (w) {
  1613         *w = texture->w;
  1614     }
  1615     if (h) {
  1616         *h = texture->h;
  1617     }
  1618     return 0;
  1619 }
  1620 
  1621 int
  1622 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1623 {
  1624     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1625     SDL_Renderer *renderer;
  1626 
  1627     if (!texture) {
  1628         return -1;
  1629     }
  1630 
  1631     renderer = texture->renderer;
  1632     if (!renderer->QueryTexturePixels) {
  1633         return -1;
  1634     }
  1635     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1636 }
  1637 
  1638 int
  1639 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1640                       int firstcolor, int ncolors)
  1641 {
  1642     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1643     SDL_Renderer *renderer;
  1644 
  1645     if (!texture) {
  1646         return -1;
  1647     }
  1648 
  1649     renderer = texture->renderer;
  1650     if (!renderer->SetTexturePalette) {
  1651         return -1;
  1652     }
  1653     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1654                                        ncolors);
  1655 }
  1656 
  1657 int
  1658 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1659                       int firstcolor, int ncolors)
  1660 {
  1661     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1662     SDL_Renderer *renderer;
  1663 
  1664     if (!texture) {
  1665         return -1;
  1666     }
  1667 
  1668     renderer = texture->renderer;
  1669     if (!renderer->GetTexturePalette) {
  1670         return -1;
  1671     }
  1672     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1673                                        ncolors);
  1674 }
  1675 
  1676 int
  1677 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1678                   const void *pixels, int pitch)
  1679 {
  1680     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1681     SDL_Renderer *renderer;
  1682     SDL_Rect full_rect;
  1683 
  1684     if (!texture) {
  1685         return -1;
  1686     }
  1687 
  1688     renderer = texture->renderer;
  1689     if (!renderer->UpdateTexture) {
  1690         return -1;
  1691     }
  1692 
  1693     if (!rect) {
  1694         full_rect.x = 0;
  1695         full_rect.y = 0;
  1696         full_rect.w = texture->w;
  1697         full_rect.h = texture->h;
  1698         rect = &full_rect;
  1699     }
  1700 
  1701     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1702 }
  1703 
  1704 int
  1705 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1706                 void **pixels, int *pitch)
  1707 {
  1708     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1709     SDL_Renderer *renderer;
  1710     SDL_Rect full_rect;
  1711 
  1712     if (!texture) {
  1713         return -1;
  1714     }
  1715 
  1716     renderer = texture->renderer;
  1717     if (!renderer->LockTexture) {
  1718         return -1;
  1719     }
  1720 
  1721     if (!rect) {
  1722         full_rect.x = 0;
  1723         full_rect.y = 0;
  1724         full_rect.w = texture->w;
  1725         full_rect.h = texture->h;
  1726         rect = &full_rect;
  1727     }
  1728 
  1729     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1730                                  pitch);
  1731 }
  1732 
  1733 void
  1734 SDL_UnlockTexture(SDL_TextureID textureID)
  1735 {
  1736     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1737     SDL_Renderer *renderer;
  1738 
  1739     if (!texture) {
  1740         return;
  1741     }
  1742 
  1743     renderer = texture->renderer;
  1744     if (!renderer->UnlockTexture) {
  1745         return;
  1746     }
  1747     renderer->UnlockTexture(renderer, texture);
  1748 }
  1749 
  1750 void
  1751 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1752                  const SDL_Rect * rects)
  1753 {
  1754     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1755     SDL_Renderer *renderer;
  1756 
  1757     if (!texture) {
  1758         return;
  1759     }
  1760 
  1761     renderer = texture->renderer;
  1762     if (!renderer->DirtyTexture) {
  1763         return;
  1764     }
  1765     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1766 }
  1767 
  1768 int
  1769 SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
  1770 {
  1771     SDL_Renderer *renderer;
  1772     SDL_Window *window;
  1773     SDL_Rect real_rect;
  1774 
  1775     if (!_this) {
  1776         SDL_UninitializedVideo();
  1777         return -1;
  1778     }
  1779 
  1780     renderer = SDL_CurrentDisplay.current_renderer;
  1781     if (!renderer || !renderer->RenderFill) {
  1782         return -1;
  1783     }
  1784 
  1785     window = SDL_GetWindowFromID(renderer->window);
  1786     real_rect.x = 0;
  1787     real_rect.y = 0;
  1788     real_rect.w = window->w;
  1789     real_rect.h = window->h;
  1790     if (rect) {
  1791         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1792             return 0;
  1793         }
  1794     }
  1795 
  1796     return renderer->RenderFill(renderer, &real_rect, color);
  1797 }
  1798 
  1799 int
  1800 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1801                const SDL_Rect * dstrect, int blendMode, int scaleMode)
  1802 {
  1803     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1804     SDL_Renderer *renderer;
  1805     SDL_Window *window;
  1806     SDL_Rect real_srcrect;
  1807     SDL_Rect real_dstrect;
  1808 
  1809     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1810         return -1;
  1811     }
  1812 
  1813     renderer = SDL_CurrentDisplay.current_renderer;
  1814     if (!renderer || !renderer->RenderCopy) {
  1815         return -1;
  1816     }
  1817 
  1818     window = SDL_GetWindowFromID(renderer->window);
  1819     if (srcrect) {
  1820         real_srcrect = *srcrect;
  1821     } else {
  1822         real_srcrect.x = 0;
  1823         real_srcrect.y = 0;
  1824         real_srcrect.w = texture->w;
  1825         real_srcrect.h = texture->h;
  1826     }
  1827     if (dstrect) {
  1828         real_dstrect = *dstrect;
  1829     } else {
  1830         real_dstrect.x = 0;
  1831         real_dstrect.y = 0;
  1832         real_dstrect.w = window->w;
  1833         real_dstrect.h = window->h;
  1834     }
  1835 
  1836     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  1837                                 &real_dstrect, blendMode, scaleMode);
  1838 }
  1839 
  1840 void
  1841 SDL_RenderPresent(void)
  1842 {
  1843     SDL_Renderer *renderer;
  1844 
  1845     if (!_this) {
  1846         SDL_UninitializedVideo();
  1847         return;
  1848     }
  1849 
  1850     renderer = SDL_CurrentDisplay.current_renderer;
  1851     if (!renderer || !renderer->RenderPresent) {
  1852         return;
  1853     }
  1854     renderer->RenderPresent(renderer);
  1855 }
  1856 
  1857 void
  1858 SDL_DestroyTexture(SDL_TextureID textureID)
  1859 {
  1860     int hash;
  1861     SDL_Texture *prev, *texture;
  1862     SDL_Renderer *renderer;
  1863 
  1864     if (!_this) {
  1865         SDL_UninitializedVideo();
  1866         return;
  1867     }
  1868 
  1869     /* Look up the texture in the hash table */
  1870     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1871     prev = NULL;
  1872     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1873          prev = texture, texture = texture->next) {
  1874         if (texture->id == textureID) {
  1875             break;
  1876         }
  1877     }
  1878     if (!texture) {
  1879         return;
  1880     }
  1881 
  1882     /* Unlink the texture from the list */
  1883     if (prev) {
  1884         prev->next = texture->next;
  1885     } else {
  1886         SDL_CurrentDisplay.textures[hash] = texture->next;
  1887     }
  1888 
  1889     /* Free the texture */
  1890     renderer = texture->renderer;
  1891     renderer->DestroyTexture(renderer, texture);
  1892     SDL_free(texture);
  1893 }
  1894 
  1895 void
  1896 SDL_DestroyRenderer(SDL_WindowID windowID)
  1897 {
  1898     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1899     SDL_Renderer *renderer;
  1900     int i;
  1901 
  1902     if (!window) {
  1903         return;
  1904     }
  1905 
  1906     renderer = window->renderer;
  1907     if (!renderer) {
  1908         return;
  1909     }
  1910 
  1911     /* Free existing textures for this renderer */
  1912     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1913         SDL_Texture *texture;
  1914         SDL_Texture *prev = NULL;
  1915         SDL_Texture *next;
  1916         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1917              texture = next) {
  1918             next = texture->next;
  1919             if (texture->renderer == renderer) {
  1920                 if (prev) {
  1921                     prev->next = next;
  1922                 } else {
  1923                     SDL_CurrentDisplay.textures[i] = next;
  1924                 }
  1925                 renderer->DestroyTexture(renderer, texture);
  1926                 SDL_free(texture);
  1927             } else {
  1928                 prev = texture;
  1929             }
  1930         }
  1931     }
  1932 
  1933     /* Free the renderer instance */
  1934     renderer->DestroyRenderer(renderer);
  1935 
  1936     /* Clear references */
  1937     window->renderer = NULL;
  1938     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1939         SDL_CurrentDisplay.current_renderer = NULL;
  1940     }
  1941 }
  1942 
  1943 void
  1944 SDL_VideoQuit(void)
  1945 {
  1946     int i, j;
  1947 
  1948     if (!_this) {
  1949         return;
  1950     }
  1951 
  1952     /* Halt event processing before doing anything else */
  1953     SDL_StopEventLoop();
  1954 
  1955     /* Clean up the system video */
  1956     for (i = _this->num_displays; i--;) {
  1957         SDL_VideoDisplay *display = &_this->displays[i];
  1958         for (j = display->num_windows; j--;) {
  1959             SDL_DestroyWindow(display->windows[i].id);
  1960         }
  1961         if (display->windows) {
  1962             SDL_free(display->windows);
  1963             display->windows = NULL;
  1964         }
  1965         display->num_windows = 0;
  1966     }
  1967     _this->VideoQuit(_this);
  1968 
  1969     for (i = _this->num_displays; i--;) {
  1970         SDL_VideoDisplay *display = &_this->displays[i];
  1971         for (j = display->num_display_modes; j--;) {
  1972             if (display->display_modes[j].driverdata) {
  1973                 SDL_free(display->display_modes[j].driverdata);
  1974                 display->display_modes[j].driverdata = NULL;
  1975             }
  1976         }
  1977         if (display->display_modes) {
  1978             SDL_free(display->display_modes);
  1979             display->display_modes = NULL;
  1980         }
  1981         if (display->desktop_mode.driverdata) {
  1982             SDL_free(display->desktop_mode.driverdata);
  1983             display->desktop_mode.driverdata = NULL;
  1984         }
  1985         if (display->palette) {
  1986             SDL_FreePalette(display->palette);
  1987             display->palette = NULL;
  1988         }
  1989         if (display->gamma) {
  1990             SDL_free(display->gamma);
  1991         }
  1992         if (display->driverdata) {
  1993             SDL_free(display->driverdata);
  1994         }
  1995     }
  1996     if (_this->displays) {
  1997         SDL_free(_this->displays);
  1998         _this->displays = NULL;
  1999     }
  2000     _this->free(_this);
  2001     _this = NULL;
  2002 }
  2003 
  2004 int
  2005 SDL_GL_LoadLibrary(const char *path)
  2006 {
  2007     int retval;
  2008 
  2009     if (!_this) {
  2010         SDL_UninitializedVideo();
  2011         return -1;
  2012     }
  2013 
  2014     if (_this->GL_LoadLibrary) {
  2015         retval = _this->GL_LoadLibrary(_this, path);
  2016     } else {
  2017         SDL_SetError("No dynamic GL support in video driver");
  2018         retval = -1;
  2019     }
  2020     return (retval);
  2021 }
  2022 
  2023 void *
  2024 SDL_GL_GetProcAddress(const char *proc)
  2025 {
  2026     void *func;
  2027 
  2028     if (!_this) {
  2029         SDL_UninitializedVideo();
  2030         return NULL;
  2031     }
  2032 
  2033     func = NULL;
  2034     if (_this->GL_GetProcAddress) {
  2035         if (_this->gl_config.driver_loaded) {
  2036             func = _this->GL_GetProcAddress(_this, proc);
  2037         } else {
  2038             SDL_SetError("No GL driver has been loaded");
  2039         }
  2040     } else {
  2041         SDL_SetError("No dynamic GL support in video driver");
  2042     }
  2043     return func;
  2044 }
  2045 
  2046 int
  2047 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2048 {
  2049     int retval;
  2050 
  2051     if (!_this) {
  2052         SDL_UninitializedVideo();
  2053         return -1;
  2054     }
  2055 
  2056     retval = 0;
  2057     switch (attr) {
  2058     case SDL_GL_RED_SIZE:
  2059         _this->gl_config.red_size = value;
  2060         break;
  2061     case SDL_GL_GREEN_SIZE:
  2062         _this->gl_config.green_size = value;
  2063         break;
  2064     case SDL_GL_BLUE_SIZE:
  2065         _this->gl_config.blue_size = value;
  2066         break;
  2067     case SDL_GL_ALPHA_SIZE:
  2068         _this->gl_config.alpha_size = value;
  2069         break;
  2070     case SDL_GL_DOUBLEBUFFER:
  2071         _this->gl_config.double_buffer = value;
  2072         break;
  2073     case SDL_GL_BUFFER_SIZE:
  2074         _this->gl_config.buffer_size = value;
  2075         break;
  2076     case SDL_GL_DEPTH_SIZE:
  2077         _this->gl_config.depth_size = value;
  2078         break;
  2079     case SDL_GL_STENCIL_SIZE:
  2080         _this->gl_config.stencil_size = value;
  2081         break;
  2082     case SDL_GL_ACCUM_RED_SIZE:
  2083         _this->gl_config.accum_red_size = value;
  2084         break;
  2085     case SDL_GL_ACCUM_GREEN_SIZE:
  2086         _this->gl_config.accum_green_size = value;
  2087         break;
  2088     case SDL_GL_ACCUM_BLUE_SIZE:
  2089         _this->gl_config.accum_blue_size = value;
  2090         break;
  2091     case SDL_GL_ACCUM_ALPHA_SIZE:
  2092         _this->gl_config.accum_alpha_size = value;
  2093         break;
  2094     case SDL_GL_STEREO:
  2095         _this->gl_config.stereo = value;
  2096         break;
  2097     case SDL_GL_MULTISAMPLEBUFFERS:
  2098         _this->gl_config.multisamplebuffers = value;
  2099         break;
  2100     case SDL_GL_MULTISAMPLESAMPLES:
  2101         _this->gl_config.multisamplesamples = value;
  2102         break;
  2103     case SDL_GL_ACCELERATED_VISUAL:
  2104         _this->gl_config.accelerated = value;
  2105         break;
  2106     default:
  2107         SDL_SetError("Unknown OpenGL attribute");
  2108         retval = -1;
  2109         break;
  2110     }
  2111     return retval;
  2112 }
  2113 
  2114 int
  2115 SDL_GL_GetWindowAttribute(SDL_WindowID windowID, SDL_GLattr attr, int *value)
  2116 {
  2117     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2118     int retval;
  2119 
  2120     if (!window) {
  2121         return -1;
  2122     }
  2123 
  2124     if (_this->GL_GetWindowAttribute) {
  2125         retval = _this->GL_GetWindowAttribute(_this, window, attr, value);
  2126     } else {
  2127         *value = 0;
  2128         SDL_SetError("GL_GetWindowAttribute not supported");
  2129         retval = -1;
  2130     }
  2131     return retval;
  2132 }
  2133 
  2134 SDL_GLContext
  2135 SDL_GL_CreateContext(SDL_WindowID windowID)
  2136 {
  2137     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2138 
  2139     if (!window) {
  2140         return NULL;
  2141     }
  2142     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2143         SDL_SetError("The specified window isn't an OpenGL window");
  2144         return NULL;
  2145     }
  2146     return _this->GL_CreateContext(_this, window);
  2147 }
  2148 
  2149 int
  2150 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2151 {
  2152     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2153 
  2154     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2155         SDL_SetError("The specified window isn't an OpenGL window");
  2156         return -1;
  2157     }
  2158     return _this->GL_MakeCurrent(_this, window, context);
  2159 }
  2160 
  2161 int
  2162 SDL_GL_SetSwapInterval(int interval)
  2163 {
  2164     if (!_this) {
  2165         SDL_UninitializedVideo();
  2166         return -1;
  2167     }
  2168 
  2169     if (_this->GL_SetSwapInterval) {
  2170         return _this->GL_SetSwapInterval(_this, interval);
  2171     } else {
  2172         SDL_SetError("Setting the swap interval is not supported");
  2173         return -1;
  2174     }
  2175 }
  2176 
  2177 int
  2178 SDL_GL_GetSwapInterval(void)
  2179 {
  2180     if (!_this) {
  2181         SDL_UninitializedVideo();
  2182         return -1;
  2183     }
  2184 
  2185     if (_this->GL_GetSwapInterval) {
  2186         return _this->GL_GetSwapInterval(_this);
  2187     } else {
  2188         SDL_SetError("Getting the swap interval is not supported");
  2189         return -1;
  2190     }
  2191 }
  2192 
  2193 void
  2194 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2195 {
  2196     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2197 
  2198     if (!window) {
  2199         return;
  2200     }
  2201     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2202         SDL_SetError("The specified window isn't an OpenGL window");
  2203         return;
  2204     }
  2205     return _this->GL_SwapWindow(_this, window);
  2206 }
  2207 
  2208 void
  2209 SDL_GL_DeleteContext(SDL_GLContext context)
  2210 {
  2211     if (!_this || !context) {
  2212         return;
  2213     }
  2214     _this->GL_DeleteContext(_this, context);
  2215 }
  2216 
  2217 #if 0                           // FIXME
  2218 /* Utility function used by SDL_WM_SetIcon();
  2219  * flags & 1 for color key, flags & 2 for alpha channel. */
  2220 static void
  2221 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2222 {
  2223     int x, y;
  2224     Uint32 colorkey;
  2225 #define SET_MASKBIT(icon, x, y, mask) \
  2226 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2227 
  2228     colorkey = icon->format->colorkey;
  2229     switch (icon->format->BytesPerPixel) {
  2230     case 1:
  2231         {
  2232             Uint8 *pixels;
  2233             for (y = 0; y < icon->h; ++y) {
  2234                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2235                 for (x = 0; x < icon->w; ++x) {
  2236                     if (*pixels++ == colorkey) {
  2237                         SET_MASKBIT(icon, x, y, mask);
  2238                     }
  2239                 }
  2240             }
  2241         }
  2242         break;
  2243 
  2244     case 2:
  2245         {
  2246             Uint16 *pixels;
  2247             for (y = 0; y < icon->h; ++y) {
  2248                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2249                 for (x = 0; x < icon->w; ++x) {
  2250                     if ((flags & 1) && *pixels == colorkey) {
  2251                         SET_MASKBIT(icon, x, y, mask);
  2252                     } else if ((flags & 2)
  2253                                && (*pixels & icon->format->Amask) == 0) {
  2254                         SET_MASKBIT(icon, x, y, mask);
  2255                     }
  2256                     pixels++;
  2257                 }
  2258             }
  2259         }
  2260         break;
  2261 
  2262     case 4:
  2263         {
  2264             Uint32 *pixels;
  2265             for (y = 0; y < icon->h; ++y) {
  2266                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2267                 for (x = 0; x < icon->w; ++x) {
  2268                     if ((flags & 1) && *pixels == colorkey) {
  2269                         SET_MASKBIT(icon, x, y, mask);
  2270                     } else if ((flags & 2)
  2271                                && (*pixels & icon->format->Amask) == 0) {
  2272                         SET_MASKBIT(icon, x, y, mask);
  2273                     }
  2274                     pixels++;
  2275                 }
  2276             }
  2277         }
  2278         break;
  2279     }
  2280 }
  2281 
  2282 /*
  2283  * Sets the window manager icon for the display window.
  2284  */
  2285 void
  2286 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2287 {
  2288     if (icon && _this->SetIcon) {
  2289         /* Generate a mask if necessary, and create the icon! */
  2290         if (mask == NULL) {
  2291             int mask_len = icon->h * (icon->w + 7) / 8;
  2292             int flags = 0;
  2293             mask = (Uint8 *) SDL_malloc(mask_len);
  2294             if (mask == NULL) {
  2295                 return;
  2296             }
  2297             SDL_memset(mask, ~0, mask_len);
  2298             if (icon->flags & SDL_SRCCOLORKEY)
  2299                 flags |= 1;
  2300             if (icon->flags & SDL_SRCALPHA)
  2301                 flags |= 2;
  2302             if (flags) {
  2303                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2304             }
  2305             _this->SetIcon(_this, icon, mask);
  2306             SDL_free(mask);
  2307         } else {
  2308             _this->SetIcon(_this, icon, mask);
  2309         }
  2310     }
  2311 }
  2312 #endif
  2313 
  2314 SDL_bool
  2315 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2316 {
  2317     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2318 
  2319     if (!window || !_this->GetWindowWMInfo) {
  2320         return SDL_FALSE;
  2321     }
  2322     return (_this->GetWindowWMInfo(_this, window, info));
  2323 }
  2324 
  2325 /* vi: set ts=4 sw=4 expandtab: */