src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 21:02:57 +0000
changeset 1924 69217fdd2c0a
parent 1923 d4572b97b08f
child 1926 307355678142
permissions -rw-r--r--
If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Added OpenGL renderer error checking.
Use fast-path texture formats in the OpenGL renderer.
     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 SDL_VIDEO_OPENGL
   283         SDL_AddRenderDriver(i, &GL_RenderDriver);
   284 #endif
   285         if (_this->displays[i].num_render_drivers > 0) {
   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 int
   837 SDL_RecreateWindow(SDL_Window * window)
   838 {
   839     if ((window->flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   840         window->flags &= ~SDL_WINDOW_OPENGL;
   841         SDL_SetError("No OpenGL support in video driver");
   842         return -1;
   843     }
   844     if (_this->DestroyWindow) {
   845         _this->DestroyWindow(_this, window);
   846     }
   847     return _this->CreateWindow(_this, window);
   848 }
   849 
   850 SDL_Window *
   851 SDL_GetWindowFromID(SDL_WindowID windowID)
   852 {
   853     int i, j;
   854 
   855     if (!_this) {
   856         SDL_UninitializedVideo();
   857         return NULL;
   858     }
   859 
   860     for (i = 0; i < _this->num_displays; ++i) {
   861         SDL_VideoDisplay *display = &_this->displays[i];
   862         for (j = 0; j < display->num_windows; ++j) {
   863             SDL_Window *window = &display->windows[j];
   864             if (window->id == windowID) {
   865                 return window;
   866             }
   867         }
   868     }
   869     return NULL;
   870 }
   871 
   872 SDL_VideoDisplay *
   873 SDL_GetDisplayFromWindow(SDL_Window * window)
   874 {
   875     if (!_this) {
   876         SDL_UninitializedVideo();
   877         return NULL;
   878     }
   879     return &_this->displays[window->display];
   880 }
   881 
   882 Uint32
   883 SDL_GetWindowFlags(SDL_WindowID windowID)
   884 {
   885     SDL_Window *window = SDL_GetWindowFromID(windowID);
   886 
   887     if (!window) {
   888         return 0;
   889     }
   890     return window->flags;
   891 }
   892 
   893 void
   894 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   895 {
   896     SDL_Window *window = SDL_GetWindowFromID(windowID);
   897 
   898     if (!window) {
   899         return;
   900     }
   901     if (window->title) {
   902         SDL_free(window->title);
   903     }
   904     window->title = SDL_strdup(title);
   905 
   906     if (_this->SetWindowTitle) {
   907         _this->SetWindowTitle(_this, window);
   908     }
   909 }
   910 
   911 const char *
   912 SDL_GetWindowTitle(SDL_WindowID windowID)
   913 {
   914     SDL_Window *window = SDL_GetWindowFromID(windowID);
   915 
   916     if (!window) {
   917         return NULL;
   918     }
   919     return window->title;
   920 }
   921 
   922 void
   923 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   924 {
   925     SDL_Window *window = SDL_GetWindowFromID(windowID);
   926 
   927     if (!window) {
   928         return;
   929     }
   930     window->userdata = userdata;
   931 }
   932 
   933 void *
   934 SDL_GetWindowData(SDL_WindowID windowID)
   935 {
   936     SDL_Window *window = SDL_GetWindowFromID(windowID);
   937 
   938     if (!window) {
   939         return NULL;
   940     }
   941     return window->userdata;
   942 }
   943 
   944 void
   945 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   946 {
   947     SDL_Window *window = SDL_GetWindowFromID(windowID);
   948 
   949     if (!window) {
   950         return;
   951     }
   952 
   953     if (x != SDL_WINDOWPOS_UNDEFINED) {
   954         window->x = x;
   955     }
   956     if (y != SDL_WINDOWPOS_UNDEFINED) {
   957         window->y = y;
   958     }
   959 
   960     if (_this->SetWindowPosition) {
   961         _this->SetWindowPosition(_this, window);
   962     }
   963 }
   964 
   965 void
   966 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   967 {
   968     SDL_Window *window = SDL_GetWindowFromID(windowID);
   969 
   970     if (!window) {
   971         return;
   972     }
   973     if (x) {
   974         *x = window->x;
   975     }
   976     if (y) {
   977         *y = window->y;
   978     }
   979 }
   980 
   981 void
   982 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   983 {
   984     SDL_Window *window = SDL_GetWindowFromID(windowID);
   985 
   986     if (!window) {
   987         return;
   988     }
   989 
   990     window->w = w;
   991     window->h = h;
   992 
   993     if (_this->SetWindowSize) {
   994         _this->SetWindowSize(_this, window);
   995     }
   996 }
   997 
   998 void
   999 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
  1000 {
  1001     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1002 
  1003     if (!window) {
  1004         return;
  1005     }
  1006     if (w) {
  1007         *w = window->w;
  1008     }
  1009     if (h) {
  1010         *h = window->h;
  1011     }
  1012 }
  1013 
  1014 void
  1015 SDL_ShowWindow(SDL_WindowID windowID)
  1016 {
  1017     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1018 
  1019     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1020         return;
  1021     }
  1022 
  1023     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1024 
  1025     if (_this->ShowWindow) {
  1026         _this->ShowWindow(_this, window);
  1027     }
  1028 }
  1029 
  1030 void
  1031 SDL_HideWindow(SDL_WindowID windowID)
  1032 {
  1033     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1034 
  1035     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1036         return;
  1037     }
  1038 
  1039     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1040 
  1041     if (_this->HideWindow) {
  1042         _this->HideWindow(_this, window);
  1043     }
  1044 }
  1045 
  1046 void
  1047 SDL_RaiseWindow(SDL_WindowID windowID)
  1048 {
  1049     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1050 
  1051     if (!window) {
  1052         return;
  1053     }
  1054 
  1055     if (_this->RaiseWindow) {
  1056         _this->RaiseWindow(_this, window);
  1057     }
  1058 }
  1059 
  1060 void
  1061 SDL_MaximizeWindow(SDL_WindowID windowID)
  1062 {
  1063     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1064 
  1065     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1066         return;
  1067     }
  1068 
  1069     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1070 
  1071     if (_this->MaximizeWindow) {
  1072         _this->MaximizeWindow(_this, window);
  1073     }
  1074 }
  1075 
  1076 void
  1077 SDL_MinimizeWindow(SDL_WindowID windowID)
  1078 {
  1079     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1080 
  1081     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1082         return;
  1083     }
  1084 
  1085     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1086 
  1087     if (_this->MinimizeWindow) {
  1088         _this->MinimizeWindow(_this, window);
  1089     }
  1090 }
  1091 
  1092 void
  1093 SDL_RestoreWindow(SDL_WindowID windowID)
  1094 {
  1095     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1096 
  1097     if (!window
  1098         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1099         return;
  1100     }
  1101 
  1102     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1103 
  1104     if (_this->RestoreWindow) {
  1105         _this->RestoreWindow(_this, window);
  1106     }
  1107 }
  1108 
  1109 int
  1110 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1111 {
  1112     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1113 
  1114     if (!window) {
  1115         return -1;
  1116     }
  1117 
  1118     if (fullscreen) {
  1119         fullscreen = SDL_WINDOW_FULLSCREEN;
  1120     }
  1121     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1122         return 0;
  1123     }
  1124 
  1125     if (fullscreen) {
  1126         window->flags |= SDL_WINDOW_FULLSCREEN;
  1127 
  1128         if (FULLSCREEN_VISIBLE(window)) {
  1129             SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1130 
  1131             /* Hide any other fullscreen windows */
  1132             int i;
  1133             for (i = 0; i < display->num_windows; ++i) {
  1134                 SDL_Window *other = &display->windows[i];
  1135                 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
  1136                     SDL_MinimizeWindow(other->id);
  1137                 }
  1138             }
  1139 
  1140             SDL_SetDisplayMode(display->fullscreen_mode);
  1141         }
  1142     } else {
  1143         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1144 
  1145         if (FULLSCREEN_VISIBLE(window)) {
  1146             SDL_SetDisplayMode(NULL);
  1147         }
  1148     }
  1149     return 0;
  1150 }
  1151 
  1152 void
  1153 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1154 {
  1155     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1156 
  1157     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1158         return;
  1159     }
  1160 
  1161     if (mode) {
  1162         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1163     } else {
  1164         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1165     }
  1166 
  1167     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1168         _this->SetWindowGrab(_this, window);
  1169     }
  1170 }
  1171 
  1172 int
  1173 SDL_GetWindowGrab(SDL_WindowID windowID)
  1174 {
  1175     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1176 
  1177     if (!window) {
  1178         return 0;
  1179     }
  1180 
  1181     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1182 }
  1183 
  1184 void
  1185 SDL_OnWindowShown(SDL_Window * window)
  1186 {
  1187 }
  1188 
  1189 void
  1190 SDL_OnWindowHidden(SDL_Window * window)
  1191 {
  1192 }
  1193 
  1194 void
  1195 SDL_OnWindowFocusGained(SDL_Window * window)
  1196 {
  1197     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1198 
  1199     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1200         SDL_SetDisplayMode(display->fullscreen_mode);
  1201     }
  1202     if (display->gamma && _this->SetDisplayGammaRamp) {
  1203         _this->SetDisplayGammaRamp(_this, display->gamma);
  1204     }
  1205     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1206         _this->SetWindowGrab(_this, window);
  1207     }
  1208 }
  1209 
  1210 void
  1211 SDL_OnWindowFocusLost(SDL_Window * window)
  1212 {
  1213     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1214 
  1215     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1216         SDL_MinimizeWindow(window->id);
  1217         SDL_SetDisplayMode(NULL);
  1218     }
  1219     if (display->gamma && _this->SetDisplayGammaRamp) {
  1220         _this->SetDisplayGammaRamp(_this, display->saved_gamma);
  1221     }
  1222     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1223         _this->SetWindowGrab(_this, window);
  1224     }
  1225 }
  1226 
  1227 SDL_WindowID
  1228 SDL_GetFocusWindow(void)
  1229 {
  1230     SDL_VideoDisplay *display;
  1231     int i;
  1232 
  1233     if (!_this) {
  1234         return 0;
  1235     }
  1236 
  1237     display = &SDL_CurrentDisplay;
  1238     for (i = 0; i < display->num_windows; ++i) {
  1239         SDL_Window *window = &display->windows[i];
  1240 
  1241         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1242             return window->id;
  1243         }
  1244     }
  1245     return 0;
  1246 }
  1247 
  1248 void
  1249 SDL_DestroyWindow(SDL_WindowID windowID)
  1250 {
  1251     int i, j;
  1252 
  1253     if (!_this) {
  1254         return;
  1255     }
  1256 
  1257     /* Restore video mode, etc. */
  1258     SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  1259 
  1260     for (i = 0; i < _this->num_displays; ++i) {
  1261         SDL_VideoDisplay *display = &_this->displays[i];
  1262         for (j = 0; j < display->num_windows; ++j) {
  1263             SDL_Window *window = &display->windows[j];
  1264             if (window->id != windowID) {
  1265                 continue;
  1266             }
  1267             if (window->renderer) {
  1268                 SDL_DestroyRenderer(window->id);
  1269             }
  1270             if (_this->DestroyWindow) {
  1271                 _this->DestroyWindow(_this, window);
  1272             }
  1273             if (window->title) {
  1274                 SDL_free(window->title);
  1275             }
  1276             SDL_free(window);
  1277             if (j != display->num_windows - 1) {
  1278                 SDL_memcpy(&display->windows[i],
  1279                            &display->windows[i + 1],
  1280                            (display->num_windows - i - 1) * sizeof(*window));
  1281             }
  1282             --display->num_windows;
  1283             return;
  1284         }
  1285     }
  1286 }
  1287 
  1288 void
  1289 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1290 {
  1291     SDL_VideoDisplay *display = &_this->displays[displayIndex];
  1292     SDL_RenderDriver *render_drivers;
  1293 
  1294     render_drivers =
  1295         SDL_realloc(display->render_drivers,
  1296                     (display->num_render_drivers +
  1297                      1) * sizeof(*render_drivers));
  1298     if (render_drivers) {
  1299         render_drivers[display->num_render_drivers] = *driver;
  1300         display->render_drivers = render_drivers;
  1301         display->num_render_drivers++;
  1302     }
  1303 }
  1304 
  1305 int
  1306 SDL_GetNumRenderers(void)
  1307 {
  1308     if (_this) {
  1309         return SDL_CurrentDisplay.num_render_drivers;
  1310     }
  1311     return 0;
  1312 }
  1313 
  1314 int
  1315 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1316 {
  1317     if (!_this) {
  1318         SDL_UninitializedVideo();
  1319         return -1;
  1320     }
  1321 
  1322     if (index >= SDL_GetNumRenderers()) {
  1323         SDL_SetError("index must be in the range of 0 - %d",
  1324                      SDL_GetNumRenderers() - 1);
  1325         return -1;
  1326     }
  1327     if (index < 0) {
  1328         if (!SDL_CurrentDisplay.current_renderer) {
  1329             SDL_SetError("There is no current renderer");
  1330             return -1;
  1331         }
  1332         *info = SDL_CurrentDisplay.current_renderer->info;
  1333     } else {
  1334         *info = SDL_CurrentDisplay.render_drivers[index].info;
  1335     }
  1336     return 0;
  1337 }
  1338 
  1339 int
  1340 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1341 {
  1342     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1343 
  1344     if (!window) {
  1345         return 0;
  1346     }
  1347 
  1348     if (index < 0) {
  1349         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1350         int n = SDL_GetNumRenderers();
  1351         for (index = 0; index < n; ++index) {
  1352             SDL_RenderDriver *driver =
  1353                 &SDL_CurrentDisplay.render_drivers[index];
  1354 
  1355             if (override) {
  1356                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1357                     break;
  1358                 }
  1359             } else {
  1360                 if ((driver->info.flags & flags) == flags) {
  1361                     break;
  1362                 }
  1363             }
  1364         }
  1365         if (index == n) {
  1366             SDL_SetError("Couldn't find matching render driver");
  1367             return -1;
  1368         }
  1369     }
  1370 
  1371     if (index >= SDL_GetNumRenderers()) {
  1372         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1373                      SDL_GetNumRenderers() - 1);
  1374         return -1;
  1375     }
  1376 
  1377     /* Free any existing renderer */
  1378     SDL_DestroyRenderer(windowID);
  1379 
  1380     /* Create a new renderer instance */
  1381     window->renderer =
  1382         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1383                                                                 flags);
  1384     SDL_SelectRenderer(window->id);
  1385 
  1386     return 0;
  1387 }
  1388 
  1389 int
  1390 SDL_SelectRenderer(SDL_WindowID windowID)
  1391 {
  1392     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1393     SDL_Renderer *renderer;
  1394 
  1395     if (!window || !window->renderer) {
  1396         return -1;
  1397     }
  1398     renderer = window->renderer;
  1399     if (renderer && renderer->ActivateRenderer) {
  1400         if (renderer->ActivateRenderer(renderer) < 0) {
  1401             return -1;
  1402         }
  1403     }
  1404     SDL_CurrentDisplay.current_renderer = renderer;
  1405     return 0;
  1406 }
  1407 
  1408 SDL_TextureID
  1409 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1410 {
  1411     int hash;
  1412     SDL_Renderer *renderer;
  1413     SDL_Texture *texture;
  1414 
  1415     if (!_this) {
  1416         SDL_UninitializedVideo();
  1417         return 0;
  1418     }
  1419 
  1420     renderer = SDL_CurrentDisplay.current_renderer;
  1421     if (!renderer || !renderer->CreateTexture) {
  1422         return 0;
  1423     }
  1424 
  1425     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1426     if (!texture) {
  1427         SDL_OutOfMemory();
  1428         return 0;
  1429     }
  1430 
  1431     texture->id = _this->next_object_id++;
  1432     texture->format = format;
  1433     texture->access = access;
  1434     texture->w = w;
  1435     texture->h = h;
  1436     texture->renderer = renderer;
  1437 
  1438     if (renderer->CreateTexture(renderer, texture) < 0) {
  1439         if (renderer->DestroyTexture) {
  1440             renderer->DestroyTexture(renderer, texture);
  1441         }
  1442         SDL_free(texture);
  1443         return 0;
  1444     }
  1445 
  1446     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1447     texture->next = SDL_CurrentDisplay.textures[hash];
  1448     SDL_CurrentDisplay.textures[hash] = texture;
  1449 
  1450     return texture->id;
  1451 }
  1452 
  1453 SDL_TextureID
  1454 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1455 {
  1456     SDL_TextureID textureID;
  1457     Uint32 surface_flags = surface->flags;
  1458     SDL_PixelFormat *fmt = surface->format;
  1459     Uint8 alpha;
  1460     SDL_Rect bounds;
  1461     SDL_Surface dst;
  1462     int bpp;
  1463     Uint32 Rmask, Gmask, Bmask, Amask;
  1464 
  1465     if (!surface) {
  1466         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1467         return 0;
  1468     }
  1469 
  1470     if (format) {
  1471         if (!SDL_PixelFormatEnumToMasks
  1472             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1473             SDL_SetError("Unknown pixel format");
  1474             return 0;
  1475         }
  1476     } else {
  1477         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1478             bpp = fmt->BitsPerPixel;
  1479             Rmask = fmt->Rmask;
  1480             Gmask = fmt->Gmask;
  1481             Bmask = fmt->Bmask;
  1482             Amask = fmt->Amask;
  1483         } else {
  1484             /* Need a format with alpha */
  1485             bpp = 32;
  1486             Rmask = 0x00FF0000;
  1487             Gmask = 0x0000FF00;
  1488             Bmask = 0x000000FF;
  1489             Amask = 0xFF000000;
  1490         }
  1491         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1492         if (!format) {
  1493             SDL_SetError("Unknown pixel format");
  1494             return 0;
  1495         }
  1496     }
  1497 
  1498     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1499     if (!textureID) {
  1500         return 0;
  1501     }
  1502 
  1503     /* Set up a destination surface for the texture update */
  1504     SDL_zero(dst);
  1505     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1506     if (!dst.format) {
  1507         SDL_DestroyTexture(textureID);
  1508         return 0;
  1509     }
  1510     dst.w = surface->w;
  1511     dst.h = surface->h;
  1512     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1513         dst.flags |= SDL_PREALLOC;
  1514     } else {
  1515         dst.pitch = SDL_CalculatePitch(&dst);
  1516         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1517         if (!dst.pixels) {
  1518             SDL_DestroyTexture(textureID);
  1519             SDL_FreeFormat(dst.format);
  1520             SDL_OutOfMemory();
  1521             return 0;
  1522         }
  1523     }
  1524 
  1525     /* Copy the palette if any */
  1526     if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1527         if (fmt->palette) {
  1528             SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1529                                   fmt->palette->ncolors);
  1530             SDL_SetSurfacePalette(&dst, fmt->palette);
  1531         } else {
  1532             dst.format->palette =
  1533                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1534             if (!dst.format->palette) {
  1535                 SDL_DestroyTexture(textureID);
  1536                 SDL_FreeFormat(dst.format);
  1537                 return 0;
  1538             }
  1539             SDL_DitherColors(dst.format->palette->colors,
  1540                              SDL_BITSPERPIXEL(format));
  1541         }
  1542     }
  1543 
  1544     /* Make the texture transparent if the surface has colorkey */
  1545     if (surface_flags & SDL_SRCCOLORKEY) {
  1546         int row;
  1547         int length = dst.w * dst.format->BytesPerPixel;
  1548         Uint8 *p = (Uint8 *) dst.pixels;
  1549         for (row = 0; row < dst.h; ++row) {
  1550             SDL_memset(p, 0, length);
  1551             p += dst.pitch;
  1552         }
  1553     }
  1554 
  1555     /* Copy over the alpha channel */
  1556     if (surface_flags & SDL_SRCALPHA) {
  1557         if (fmt->Amask) {
  1558             surface->flags &= ~SDL_SRCALPHA;
  1559         } else {
  1560             /* FIXME: Need to make sure the texture has an alpha channel
  1561              *        and copy 'alpha' into the texture alpha channel.
  1562              */
  1563             alpha = surface->format->alpha;
  1564             SDL_SetAlpha(surface, 0, 0);
  1565         }
  1566     }
  1567 
  1568     /* Copy over the image data */
  1569     bounds.x = 0;
  1570     bounds.y = 0;
  1571     bounds.w = surface->w;
  1572     bounds.h = surface->h;
  1573     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1574 
  1575     /* Clean up the original surface */
  1576     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1577         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1578         if (fmt->Amask) {
  1579             surface->flags |= SDL_SRCALPHA;
  1580         } else {
  1581             SDL_SetAlpha(surface, aflags, alpha);
  1582         }
  1583     }
  1584 
  1585     /* Update the texture */
  1586     if (dst.flags & SDL_PREALLOC) {
  1587         SDL_UnlockTexture(textureID);
  1588     } else {
  1589         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1590         SDL_free(dst.pixels);
  1591     }
  1592     SDL_FreeFormat(dst.format);
  1593 
  1594     return textureID;
  1595 }
  1596 
  1597 static __inline__ SDL_Texture *
  1598 SDL_GetTextureFromID(SDL_TextureID textureID)
  1599 {
  1600     int hash;
  1601     SDL_Texture *texture;
  1602 
  1603     if (!_this) {
  1604         return NULL;
  1605     }
  1606 
  1607     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1608     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1609          texture = texture->next) {
  1610         if (texture->id == textureID) {
  1611             return texture;
  1612         }
  1613     }
  1614     return NULL;
  1615 }
  1616 
  1617 int
  1618 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1619                  int *w, int *h)
  1620 {
  1621     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1622 
  1623     if (!texture) {
  1624         return -1;
  1625     }
  1626 
  1627     if (format) {
  1628         *format = texture->format;
  1629     }
  1630     if (access) {
  1631         *access = texture->access;
  1632     }
  1633     if (w) {
  1634         *w = texture->w;
  1635     }
  1636     if (h) {
  1637         *h = texture->h;
  1638     }
  1639     return 0;
  1640 }
  1641 
  1642 int
  1643 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1644 {
  1645     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1646     SDL_Renderer *renderer;
  1647 
  1648     if (!texture) {
  1649         return -1;
  1650     }
  1651 
  1652     renderer = texture->renderer;
  1653     if (!renderer->QueryTexturePixels) {
  1654         return -1;
  1655     }
  1656     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1657 }
  1658 
  1659 int
  1660 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1661                       int firstcolor, int ncolors)
  1662 {
  1663     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1664     SDL_Renderer *renderer;
  1665 
  1666     if (!texture) {
  1667         return -1;
  1668     }
  1669 
  1670     renderer = texture->renderer;
  1671     if (!renderer->SetTexturePalette) {
  1672         return -1;
  1673     }
  1674     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1675                                        ncolors);
  1676 }
  1677 
  1678 int
  1679 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1680                       int firstcolor, int ncolors)
  1681 {
  1682     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1683     SDL_Renderer *renderer;
  1684 
  1685     if (!texture) {
  1686         return -1;
  1687     }
  1688 
  1689     renderer = texture->renderer;
  1690     if (!renderer->GetTexturePalette) {
  1691         return -1;
  1692     }
  1693     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1694                                        ncolors);
  1695 }
  1696 
  1697 int
  1698 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1699                   const void *pixels, int pitch)
  1700 {
  1701     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1702     SDL_Renderer *renderer;
  1703     SDL_Rect full_rect;
  1704 
  1705     if (!texture) {
  1706         return -1;
  1707     }
  1708 
  1709     renderer = texture->renderer;
  1710     if (!renderer->UpdateTexture) {
  1711         return -1;
  1712     }
  1713 
  1714     if (!rect) {
  1715         full_rect.x = 0;
  1716         full_rect.y = 0;
  1717         full_rect.w = texture->w;
  1718         full_rect.h = texture->h;
  1719         rect = &full_rect;
  1720     }
  1721 
  1722     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1723 }
  1724 
  1725 int
  1726 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1727                 void **pixels, int *pitch)
  1728 {
  1729     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1730     SDL_Renderer *renderer;
  1731     SDL_Rect full_rect;
  1732 
  1733     if (!texture) {
  1734         return -1;
  1735     }
  1736 
  1737     renderer = texture->renderer;
  1738     if (!renderer->LockTexture) {
  1739         return -1;
  1740     }
  1741 
  1742     if (!rect) {
  1743         full_rect.x = 0;
  1744         full_rect.y = 0;
  1745         full_rect.w = texture->w;
  1746         full_rect.h = texture->h;
  1747         rect = &full_rect;
  1748     }
  1749 
  1750     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1751                                  pitch);
  1752 }
  1753 
  1754 void
  1755 SDL_UnlockTexture(SDL_TextureID textureID)
  1756 {
  1757     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1758     SDL_Renderer *renderer;
  1759 
  1760     if (!texture) {
  1761         return;
  1762     }
  1763 
  1764     renderer = texture->renderer;
  1765     if (!renderer->UnlockTexture) {
  1766         return;
  1767     }
  1768     renderer->UnlockTexture(renderer, texture);
  1769 }
  1770 
  1771 void
  1772 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1773                  const SDL_Rect * rects)
  1774 {
  1775     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1776     SDL_Renderer *renderer;
  1777 
  1778     if (!texture) {
  1779         return;
  1780     }
  1781 
  1782     renderer = texture->renderer;
  1783     if (!renderer->DirtyTexture) {
  1784         return;
  1785     }
  1786     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1787 }
  1788 
  1789 int
  1790 SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
  1791 {
  1792     SDL_Renderer *renderer;
  1793     SDL_Window *window;
  1794     SDL_Rect real_rect;
  1795 
  1796     if (!_this) {
  1797         SDL_UninitializedVideo();
  1798         return -1;
  1799     }
  1800 
  1801     renderer = SDL_CurrentDisplay.current_renderer;
  1802     if (!renderer || !renderer->RenderFill) {
  1803         return -1;
  1804     }
  1805 
  1806     window = SDL_GetWindowFromID(renderer->window);
  1807     real_rect.x = 0;
  1808     real_rect.y = 0;
  1809     real_rect.w = window->w;
  1810     real_rect.h = window->h;
  1811     if (rect) {
  1812         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1813             return 0;
  1814         }
  1815     }
  1816 
  1817     return renderer->RenderFill(renderer, &real_rect, color);
  1818 }
  1819 
  1820 int
  1821 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1822                const SDL_Rect * dstrect, int blendMode, int scaleMode)
  1823 {
  1824     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1825     SDL_Renderer *renderer;
  1826     SDL_Window *window;
  1827     SDL_Rect real_srcrect;
  1828     SDL_Rect real_dstrect;
  1829 
  1830     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1831         return -1;
  1832     }
  1833 
  1834     renderer = SDL_CurrentDisplay.current_renderer;
  1835     if (!renderer || !renderer->RenderCopy) {
  1836         return -1;
  1837     }
  1838 
  1839     window = SDL_GetWindowFromID(renderer->window);
  1840     if (srcrect) {
  1841         real_srcrect = *srcrect;
  1842     } else {
  1843         real_srcrect.x = 0;
  1844         real_srcrect.y = 0;
  1845         real_srcrect.w = texture->w;
  1846         real_srcrect.h = texture->h;
  1847     }
  1848     if (dstrect) {
  1849         real_dstrect = *dstrect;
  1850     } else {
  1851         real_dstrect.x = 0;
  1852         real_dstrect.y = 0;
  1853         real_dstrect.w = window->w;
  1854         real_dstrect.h = window->h;
  1855     }
  1856 
  1857     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  1858                                 &real_dstrect, blendMode, scaleMode);
  1859 }
  1860 
  1861 void
  1862 SDL_RenderPresent(void)
  1863 {
  1864     SDL_Renderer *renderer;
  1865 
  1866     if (!_this) {
  1867         SDL_UninitializedVideo();
  1868         return;
  1869     }
  1870 
  1871     renderer = SDL_CurrentDisplay.current_renderer;
  1872     if (!renderer || !renderer->RenderPresent) {
  1873         return;
  1874     }
  1875     renderer->RenderPresent(renderer);
  1876 }
  1877 
  1878 void
  1879 SDL_DestroyTexture(SDL_TextureID textureID)
  1880 {
  1881     int hash;
  1882     SDL_Texture *prev, *texture;
  1883     SDL_Renderer *renderer;
  1884 
  1885     if (!_this) {
  1886         SDL_UninitializedVideo();
  1887         return;
  1888     }
  1889 
  1890     /* Look up the texture in the hash table */
  1891     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1892     prev = NULL;
  1893     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1894          prev = texture, texture = texture->next) {
  1895         if (texture->id == textureID) {
  1896             break;
  1897         }
  1898     }
  1899     if (!texture) {
  1900         return;
  1901     }
  1902 
  1903     /* Unlink the texture from the list */
  1904     if (prev) {
  1905         prev->next = texture->next;
  1906     } else {
  1907         SDL_CurrentDisplay.textures[hash] = texture->next;
  1908     }
  1909 
  1910     /* Free the texture */
  1911     renderer = texture->renderer;
  1912     renderer->DestroyTexture(renderer, texture);
  1913     SDL_free(texture);
  1914 }
  1915 
  1916 void
  1917 SDL_DestroyRenderer(SDL_WindowID windowID)
  1918 {
  1919     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1920     SDL_Renderer *renderer;
  1921     int i;
  1922 
  1923     if (!window) {
  1924         return;
  1925     }
  1926 
  1927     renderer = window->renderer;
  1928     if (!renderer) {
  1929         return;
  1930     }
  1931 
  1932     /* Free existing textures for this renderer */
  1933     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1934         SDL_Texture *texture;
  1935         SDL_Texture *prev = NULL;
  1936         SDL_Texture *next;
  1937         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1938              texture = next) {
  1939             next = texture->next;
  1940             if (texture->renderer == renderer) {
  1941                 if (prev) {
  1942                     prev->next = next;
  1943                 } else {
  1944                     SDL_CurrentDisplay.textures[i] = next;
  1945                 }
  1946                 renderer->DestroyTexture(renderer, texture);
  1947                 SDL_free(texture);
  1948             } else {
  1949                 prev = texture;
  1950             }
  1951         }
  1952     }
  1953 
  1954     /* Free the renderer instance */
  1955     renderer->DestroyRenderer(renderer);
  1956 
  1957     /* Clear references */
  1958     window->renderer = NULL;
  1959     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1960         SDL_CurrentDisplay.current_renderer = NULL;
  1961     }
  1962 }
  1963 
  1964 void
  1965 SDL_VideoQuit(void)
  1966 {
  1967     int i, j;
  1968 
  1969     if (!_this) {
  1970         return;
  1971     }
  1972 
  1973     /* Halt event processing before doing anything else */
  1974     SDL_StopEventLoop();
  1975 
  1976     /* Clean up the system video */
  1977     for (i = _this->num_displays; i--;) {
  1978         SDL_VideoDisplay *display = &_this->displays[i];
  1979         for (j = display->num_windows; j--;) {
  1980             SDL_DestroyWindow(display->windows[i].id);
  1981         }
  1982         if (display->windows) {
  1983             SDL_free(display->windows);
  1984             display->windows = NULL;
  1985         }
  1986         display->num_windows = 0;
  1987     }
  1988     _this->VideoQuit(_this);
  1989 
  1990     for (i = _this->num_displays; i--;) {
  1991         SDL_VideoDisplay *display = &_this->displays[i];
  1992         for (j = display->num_display_modes; j--;) {
  1993             if (display->display_modes[j].driverdata) {
  1994                 SDL_free(display->display_modes[j].driverdata);
  1995                 display->display_modes[j].driverdata = NULL;
  1996             }
  1997         }
  1998         if (display->display_modes) {
  1999             SDL_free(display->display_modes);
  2000             display->display_modes = NULL;
  2001         }
  2002         if (display->desktop_mode.driverdata) {
  2003             SDL_free(display->desktop_mode.driverdata);
  2004             display->desktop_mode.driverdata = NULL;
  2005         }
  2006         if (display->palette) {
  2007             SDL_FreePalette(display->palette);
  2008             display->palette = NULL;
  2009         }
  2010         if (display->gamma) {
  2011             SDL_free(display->gamma);
  2012         }
  2013         if (display->driverdata) {
  2014             SDL_free(display->driverdata);
  2015         }
  2016     }
  2017     if (_this->displays) {
  2018         SDL_free(_this->displays);
  2019         _this->displays = NULL;
  2020     }
  2021     _this->free(_this);
  2022     _this = NULL;
  2023 }
  2024 
  2025 int
  2026 SDL_GL_LoadLibrary(const char *path)
  2027 {
  2028     int retval;
  2029 
  2030     if (!_this) {
  2031         SDL_UninitializedVideo();
  2032         return -1;
  2033     }
  2034 
  2035     if (_this->GL_LoadLibrary) {
  2036         retval = _this->GL_LoadLibrary(_this, path);
  2037     } else {
  2038         SDL_SetError("No dynamic GL support in video driver");
  2039         retval = -1;
  2040     }
  2041     return (retval);
  2042 }
  2043 
  2044 void *
  2045 SDL_GL_GetProcAddress(const char *proc)
  2046 {
  2047     void *func;
  2048 
  2049     if (!_this) {
  2050         SDL_UninitializedVideo();
  2051         return NULL;
  2052     }
  2053 
  2054     func = NULL;
  2055     if (_this->GL_GetProcAddress) {
  2056         if (_this->gl_config.driver_loaded) {
  2057             func = _this->GL_GetProcAddress(_this, proc);
  2058         } else {
  2059             SDL_SetError("No GL driver has been loaded");
  2060         }
  2061     } else {
  2062         SDL_SetError("No dynamic GL support in video driver");
  2063     }
  2064     return func;
  2065 }
  2066 
  2067 int
  2068 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2069 {
  2070     int retval;
  2071 
  2072     if (!_this) {
  2073         SDL_UninitializedVideo();
  2074         return -1;
  2075     }
  2076 
  2077     retval = 0;
  2078     switch (attr) {
  2079     case SDL_GL_RED_SIZE:
  2080         _this->gl_config.red_size = value;
  2081         break;
  2082     case SDL_GL_GREEN_SIZE:
  2083         _this->gl_config.green_size = value;
  2084         break;
  2085     case SDL_GL_BLUE_SIZE:
  2086         _this->gl_config.blue_size = value;
  2087         break;
  2088     case SDL_GL_ALPHA_SIZE:
  2089         _this->gl_config.alpha_size = value;
  2090         break;
  2091     case SDL_GL_DOUBLEBUFFER:
  2092         _this->gl_config.double_buffer = value;
  2093         break;
  2094     case SDL_GL_BUFFER_SIZE:
  2095         _this->gl_config.buffer_size = value;
  2096         break;
  2097     case SDL_GL_DEPTH_SIZE:
  2098         _this->gl_config.depth_size = value;
  2099         break;
  2100     case SDL_GL_STENCIL_SIZE:
  2101         _this->gl_config.stencil_size = value;
  2102         break;
  2103     case SDL_GL_ACCUM_RED_SIZE:
  2104         _this->gl_config.accum_red_size = value;
  2105         break;
  2106     case SDL_GL_ACCUM_GREEN_SIZE:
  2107         _this->gl_config.accum_green_size = value;
  2108         break;
  2109     case SDL_GL_ACCUM_BLUE_SIZE:
  2110         _this->gl_config.accum_blue_size = value;
  2111         break;
  2112     case SDL_GL_ACCUM_ALPHA_SIZE:
  2113         _this->gl_config.accum_alpha_size = value;
  2114         break;
  2115     case SDL_GL_STEREO:
  2116         _this->gl_config.stereo = value;
  2117         break;
  2118     case SDL_GL_MULTISAMPLEBUFFERS:
  2119         _this->gl_config.multisamplebuffers = value;
  2120         break;
  2121     case SDL_GL_MULTISAMPLESAMPLES:
  2122         _this->gl_config.multisamplesamples = value;
  2123         break;
  2124     case SDL_GL_ACCELERATED_VISUAL:
  2125         _this->gl_config.accelerated = value;
  2126         break;
  2127     default:
  2128         SDL_SetError("Unknown OpenGL attribute");
  2129         retval = -1;
  2130         break;
  2131     }
  2132     return retval;
  2133 }
  2134 
  2135 int
  2136 SDL_GL_GetWindowAttribute(SDL_WindowID windowID, SDL_GLattr attr, int *value)
  2137 {
  2138     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2139     int retval;
  2140 
  2141     if (!window) {
  2142         return -1;
  2143     }
  2144 
  2145     if (_this->GL_GetWindowAttribute) {
  2146         retval = _this->GL_GetWindowAttribute(_this, window, attr, value);
  2147     } else {
  2148         *value = 0;
  2149         SDL_SetError("GL_GetWindowAttribute not supported");
  2150         retval = -1;
  2151     }
  2152     return retval;
  2153 }
  2154 
  2155 SDL_GLContext
  2156 SDL_GL_CreateContext(SDL_WindowID windowID)
  2157 {
  2158     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2159 
  2160     if (!window) {
  2161         return NULL;
  2162     }
  2163     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2164         SDL_SetError("The specified window isn't an OpenGL window");
  2165         return NULL;
  2166     }
  2167     return _this->GL_CreateContext(_this, window);
  2168 }
  2169 
  2170 int
  2171 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2172 {
  2173     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2174 
  2175     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2176         SDL_SetError("The specified window isn't an OpenGL window");
  2177         return -1;
  2178     }
  2179     return _this->GL_MakeCurrent(_this, window, context);
  2180 }
  2181 
  2182 int
  2183 SDL_GL_SetSwapInterval(int interval)
  2184 {
  2185     if (!_this) {
  2186         SDL_UninitializedVideo();
  2187         return -1;
  2188     }
  2189 
  2190     if (_this->GL_SetSwapInterval) {
  2191         return _this->GL_SetSwapInterval(_this, interval);
  2192     } else {
  2193         SDL_SetError("Setting the swap interval is not supported");
  2194         return -1;
  2195     }
  2196 }
  2197 
  2198 int
  2199 SDL_GL_GetSwapInterval(void)
  2200 {
  2201     if (!_this) {
  2202         SDL_UninitializedVideo();
  2203         return -1;
  2204     }
  2205 
  2206     if (_this->GL_GetSwapInterval) {
  2207         return _this->GL_GetSwapInterval(_this);
  2208     } else {
  2209         SDL_SetError("Getting the swap interval is not supported");
  2210         return -1;
  2211     }
  2212 }
  2213 
  2214 void
  2215 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2216 {
  2217     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2218 
  2219     if (!window) {
  2220         return;
  2221     }
  2222     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2223         SDL_SetError("The specified window isn't an OpenGL window");
  2224         return;
  2225     }
  2226     return _this->GL_SwapWindow(_this, window);
  2227 }
  2228 
  2229 void
  2230 SDL_GL_DeleteContext(SDL_GLContext context)
  2231 {
  2232     if (!_this || !context) {
  2233         return;
  2234     }
  2235     _this->GL_DeleteContext(_this, context);
  2236 }
  2237 
  2238 #if 0                           // FIXME
  2239 /* Utility function used by SDL_WM_SetIcon();
  2240  * flags & 1 for color key, flags & 2 for alpha channel. */
  2241 static void
  2242 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2243 {
  2244     int x, y;
  2245     Uint32 colorkey;
  2246 #define SET_MASKBIT(icon, x, y, mask) \
  2247 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2248 
  2249     colorkey = icon->format->colorkey;
  2250     switch (icon->format->BytesPerPixel) {
  2251     case 1:
  2252         {
  2253             Uint8 *pixels;
  2254             for (y = 0; y < icon->h; ++y) {
  2255                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2256                 for (x = 0; x < icon->w; ++x) {
  2257                     if (*pixels++ == colorkey) {
  2258                         SET_MASKBIT(icon, x, y, mask);
  2259                     }
  2260                 }
  2261             }
  2262         }
  2263         break;
  2264 
  2265     case 2:
  2266         {
  2267             Uint16 *pixels;
  2268             for (y = 0; y < icon->h; ++y) {
  2269                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2270                 for (x = 0; x < icon->w; ++x) {
  2271                     if ((flags & 1) && *pixels == colorkey) {
  2272                         SET_MASKBIT(icon, x, y, mask);
  2273                     } else if ((flags & 2)
  2274                                && (*pixels & icon->format->Amask) == 0) {
  2275                         SET_MASKBIT(icon, x, y, mask);
  2276                     }
  2277                     pixels++;
  2278                 }
  2279             }
  2280         }
  2281         break;
  2282 
  2283     case 4:
  2284         {
  2285             Uint32 *pixels;
  2286             for (y = 0; y < icon->h; ++y) {
  2287                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2288                 for (x = 0; x < icon->w; ++x) {
  2289                     if ((flags & 1) && *pixels == colorkey) {
  2290                         SET_MASKBIT(icon, x, y, mask);
  2291                     } else if ((flags & 2)
  2292                                && (*pixels & icon->format->Amask) == 0) {
  2293                         SET_MASKBIT(icon, x, y, mask);
  2294                     }
  2295                     pixels++;
  2296                 }
  2297             }
  2298         }
  2299         break;
  2300     }
  2301 }
  2302 
  2303 /*
  2304  * Sets the window manager icon for the display window.
  2305  */
  2306 void
  2307 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2308 {
  2309     if (icon && _this->SetIcon) {
  2310         /* Generate a mask if necessary, and create the icon! */
  2311         if (mask == NULL) {
  2312             int mask_len = icon->h * (icon->w + 7) / 8;
  2313             int flags = 0;
  2314             mask = (Uint8 *) SDL_malloc(mask_len);
  2315             if (mask == NULL) {
  2316                 return;
  2317             }
  2318             SDL_memset(mask, ~0, mask_len);
  2319             if (icon->flags & SDL_SRCCOLORKEY)
  2320                 flags |= 1;
  2321             if (icon->flags & SDL_SRCALPHA)
  2322                 flags |= 2;
  2323             if (flags) {
  2324                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2325             }
  2326             _this->SetIcon(_this, icon, mask);
  2327             SDL_free(mask);
  2328         } else {
  2329             _this->SetIcon(_this, icon, mask);
  2330         }
  2331     }
  2332 }
  2333 #endif
  2334 
  2335 SDL_bool
  2336 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2337 {
  2338     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2339 
  2340     if (!window || !_this->GetWindowWMInfo) {
  2341         return SDL_FALSE;
  2342     }
  2343     return (_this->GetWindowWMInfo(_this, window, info));
  2344 }
  2345 
  2346 /* vi: set ts=4 sw=4 expandtab: */