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