src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Aug 2007 00:54:53 +0000
changeset 2257 340942cfda48
parent 2245 989fb86ad1ec
child 2266 e61ad15a205f
permissions -rw-r--r--
Moved the colorkey and per-surface alpha into the blit info,
in preparation for support for general color channel modulation.

Removed and consolidated some data in the blit info.
     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 
   943     if (!window || title == window->title) {
   944         return;
   945     }
   946 
   947     if (window->title) {
   948         SDL_free(window->title);
   949     }
   950     if (title) {
   951         window->title = SDL_strdup(title);
   952     } else {
   953         window->title = NULL;
   954     }
   955 
   956     if (_this->SetWindowTitle) {
   957         _this->SetWindowTitle(_this, window);
   958     }
   959 }
   960 
   961 const char *
   962 SDL_GetWindowTitle(SDL_WindowID windowID)
   963 {
   964     SDL_Window *window = SDL_GetWindowFromID(windowID);
   965 
   966     if (!window) {
   967         return NULL;
   968     }
   969     return window->title;
   970 }
   971 
   972 void
   973 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   974 {
   975     SDL_Window *window = SDL_GetWindowFromID(windowID);
   976 
   977     if (!window) {
   978         return;
   979     }
   980     window->userdata = userdata;
   981 }
   982 
   983 void *
   984 SDL_GetWindowData(SDL_WindowID windowID)
   985 {
   986     SDL_Window *window = SDL_GetWindowFromID(windowID);
   987 
   988     if (!window) {
   989         return NULL;
   990     }
   991     return window->userdata;
   992 }
   993 
   994 void
   995 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   996 {
   997     SDL_Window *window = SDL_GetWindowFromID(windowID);
   998     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   999 
  1000     if (!window) {
  1001         return;
  1002     }
  1003 
  1004     if (x == SDL_WINDOWPOS_CENTERED) {
  1005         window->x = (display->current_mode.w - window->w) / 2;
  1006     } else if (x != SDL_WINDOWPOS_UNDEFINED) {
  1007         window->x = x;
  1008     }
  1009     if (y == SDL_WINDOWPOS_CENTERED) {
  1010         window->y = (display->current_mode.h - window->h) / 2;
  1011     } else if (y != SDL_WINDOWPOS_UNDEFINED) {
  1012         window->y = y;
  1013     }
  1014 
  1015     if (_this->SetWindowPosition) {
  1016         _this->SetWindowPosition(_this, window);
  1017     }
  1018 }
  1019 
  1020 void
  1021 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
  1022 {
  1023     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1024 
  1025     if (!window) {
  1026         return;
  1027     }
  1028     if (x) {
  1029         *x = window->x;
  1030     }
  1031     if (y) {
  1032         *y = window->y;
  1033     }
  1034 }
  1035 
  1036 void
  1037 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
  1038 {
  1039     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1040 
  1041     if (!window) {
  1042         return;
  1043     }
  1044 
  1045     window->w = w;
  1046     window->h = h;
  1047 
  1048     if (_this->SetWindowSize) {
  1049         _this->SetWindowSize(_this, window);
  1050     }
  1051 }
  1052 
  1053 void
  1054 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
  1055 {
  1056     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1057 
  1058     if (!window) {
  1059         return;
  1060     }
  1061     if (w) {
  1062         *w = window->w;
  1063     }
  1064     if (h) {
  1065         *h = window->h;
  1066     }
  1067 }
  1068 
  1069 void
  1070 SDL_ShowWindow(SDL_WindowID windowID)
  1071 {
  1072     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1073 
  1074     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1075         return;
  1076     }
  1077 
  1078     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1079 
  1080     if (_this->ShowWindow) {
  1081         _this->ShowWindow(_this, window);
  1082     }
  1083 }
  1084 
  1085 void
  1086 SDL_HideWindow(SDL_WindowID windowID)
  1087 {
  1088     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1089 
  1090     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1091         return;
  1092     }
  1093 
  1094     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1095 
  1096     if (_this->HideWindow) {
  1097         _this->HideWindow(_this, window);
  1098     }
  1099 }
  1100 
  1101 void
  1102 SDL_RaiseWindow(SDL_WindowID windowID)
  1103 {
  1104     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1105 
  1106     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1107         return;
  1108     }
  1109 
  1110     if (_this->RaiseWindow) {
  1111         _this->RaiseWindow(_this, window);
  1112     }
  1113 }
  1114 
  1115 void
  1116 SDL_MaximizeWindow(SDL_WindowID windowID)
  1117 {
  1118     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1119 
  1120     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1121         return;
  1122     }
  1123 
  1124     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1125 
  1126     if (_this->MaximizeWindow) {
  1127         _this->MaximizeWindow(_this, window);
  1128     }
  1129 }
  1130 
  1131 void
  1132 SDL_MinimizeWindow(SDL_WindowID windowID)
  1133 {
  1134     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1135 
  1136     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1137         return;
  1138     }
  1139 
  1140     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1141 
  1142     if (_this->MinimizeWindow) {
  1143         _this->MinimizeWindow(_this, window);
  1144     }
  1145 }
  1146 
  1147 void
  1148 SDL_RestoreWindow(SDL_WindowID windowID)
  1149 {
  1150     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1151 
  1152     if (!window
  1153         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1154         return;
  1155     }
  1156 
  1157     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1158 
  1159     if (_this->RestoreWindow) {
  1160         _this->RestoreWindow(_this, window);
  1161     }
  1162 }
  1163 
  1164 int
  1165 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1166 {
  1167     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1168 
  1169     if (!window) {
  1170         return -1;
  1171     }
  1172 
  1173     if (fullscreen) {
  1174         fullscreen = SDL_WINDOW_FULLSCREEN;
  1175     }
  1176     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1177         return 0;
  1178     }
  1179 
  1180     if (fullscreen) {
  1181         window->flags |= SDL_WINDOW_FULLSCREEN;
  1182 
  1183         if (FULLSCREEN_VISIBLE(window)) {
  1184             SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1185 
  1186             /* Hide any other fullscreen windows */
  1187             int i;
  1188             for (i = 0; i < display->num_windows; ++i) {
  1189                 SDL_Window *other = &display->windows[i];
  1190                 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
  1191                     SDL_MinimizeWindow(other->id);
  1192                 }
  1193             }
  1194 
  1195             SDL_SetDisplayMode(&display->fullscreen_mode);
  1196         }
  1197     } else {
  1198         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1199 
  1200         if (FULLSCREEN_VISIBLE(window)) {
  1201             SDL_SetDisplayMode(NULL);
  1202         }
  1203     }
  1204     return 0;
  1205 }
  1206 
  1207 void
  1208 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1209 {
  1210     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1211 
  1212     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1213         return;
  1214     }
  1215 
  1216     if (mode) {
  1217         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1218     } else {
  1219         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1220     }
  1221 
  1222     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1223         _this->SetWindowGrab(_this, window);
  1224     }
  1225 }
  1226 
  1227 int
  1228 SDL_GetWindowGrab(SDL_WindowID windowID)
  1229 {
  1230     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1231 
  1232     if (!window) {
  1233         return 0;
  1234     }
  1235 
  1236     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1237 }
  1238 
  1239 void
  1240 SDL_OnWindowShown(SDL_Window * window)
  1241 {
  1242 }
  1243 
  1244 void
  1245 SDL_OnWindowHidden(SDL_Window * window)
  1246 {
  1247 }
  1248 
  1249 void
  1250 SDL_OnWindowResized(SDL_Window * window)
  1251 {
  1252     SDL_Renderer *renderer = window->renderer;
  1253 
  1254     if (renderer && renderer->DisplayModeChanged) {
  1255         renderer->DisplayModeChanged(renderer);
  1256     }
  1257 }
  1258 
  1259 void
  1260 SDL_OnWindowFocusGained(SDL_Window * window)
  1261 {
  1262     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1263 
  1264     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1265         SDL_SetDisplayMode(&display->fullscreen_mode);
  1266     }
  1267     if (display->gamma && _this->SetDisplayGammaRamp) {
  1268         _this->SetDisplayGammaRamp(_this, display->gamma);
  1269     }
  1270     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1271         _this->SetWindowGrab(_this, window);
  1272     }
  1273 }
  1274 
  1275 void
  1276 SDL_OnWindowFocusLost(SDL_Window * window)
  1277 {
  1278     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1279 
  1280     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1281         SDL_MinimizeWindow(window->id);
  1282         SDL_SetDisplayMode(NULL);
  1283     }
  1284     if (display->gamma && _this->SetDisplayGammaRamp) {
  1285         _this->SetDisplayGammaRamp(_this, display->saved_gamma);
  1286     }
  1287     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1288         _this->SetWindowGrab(_this, window);
  1289     }
  1290 }
  1291 
  1292 SDL_WindowID
  1293 SDL_GetFocusWindow(void)
  1294 {
  1295     SDL_VideoDisplay *display;
  1296     int i;
  1297 
  1298     if (!_this) {
  1299         return 0;
  1300     }
  1301 
  1302     display = &SDL_CurrentDisplay;
  1303     for (i = 0; i < display->num_windows; ++i) {
  1304         SDL_Window *window = &display->windows[i];
  1305 
  1306         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1307             return window->id;
  1308         }
  1309     }
  1310     return 0;
  1311 }
  1312 
  1313 void
  1314 SDL_DestroyWindow(SDL_WindowID windowID)
  1315 {
  1316     int i, j;
  1317 
  1318     if (!_this) {
  1319         return;
  1320     }
  1321 
  1322     /* Restore video mode, etc. */
  1323     SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  1324 
  1325     for (i = 0; i < _this->num_displays; ++i) {
  1326         SDL_VideoDisplay *display = &_this->displays[i];
  1327         for (j = 0; j < display->num_windows; ++j) {
  1328             SDL_Window *window = &display->windows[j];
  1329             if (window->id != windowID) {
  1330                 continue;
  1331             }
  1332             if (window->renderer) {
  1333                 SDL_DestroyRenderer(window->id);
  1334             }
  1335             if (_this->DestroyWindow) {
  1336                 _this->DestroyWindow(_this, window);
  1337             }
  1338             if (window->title) {
  1339                 SDL_free(window->title);
  1340             }
  1341             if (j != display->num_windows - 1) {
  1342                 SDL_memcpy(&display->windows[i],
  1343                            &display->windows[i + 1],
  1344                            (display->num_windows - i - 1) * sizeof(*window));
  1345             }
  1346             --display->num_windows;
  1347             return;
  1348         }
  1349     }
  1350 }
  1351 
  1352 void
  1353 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1354 {
  1355     SDL_VideoDisplay *display;
  1356     SDL_RenderDriver *render_drivers;
  1357 
  1358     if (displayIndex >= _this->num_displays) {
  1359         return;
  1360     }
  1361     display = &_this->displays[displayIndex];
  1362 
  1363     render_drivers =
  1364         SDL_realloc(display->render_drivers,
  1365                     (display->num_render_drivers +
  1366                      1) * sizeof(*render_drivers));
  1367     if (render_drivers) {
  1368         render_drivers[display->num_render_drivers] = *driver;
  1369         display->render_drivers = render_drivers;
  1370         display->num_render_drivers++;
  1371     }
  1372 }
  1373 
  1374 int
  1375 SDL_GetNumRenderDrivers(void)
  1376 {
  1377     if (_this) {
  1378         return SDL_CurrentDisplay.num_render_drivers;
  1379     }
  1380     return 0;
  1381 }
  1382 
  1383 int
  1384 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1385 {
  1386     if (!_this) {
  1387         SDL_UninitializedVideo();
  1388         return -1;
  1389     }
  1390 
  1391     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1392         SDL_SetError("index must be in the range of 0 - %d",
  1393                      SDL_GetNumRenderDrivers() - 1);
  1394         return -1;
  1395     }
  1396     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1397     return 0;
  1398 }
  1399 
  1400 int
  1401 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1402 {
  1403     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1404 
  1405     if (!window) {
  1406         return 0;
  1407     }
  1408 
  1409     if (index < 0) {
  1410         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1411         int n = SDL_GetNumRenderDrivers();
  1412         for (index = 0; index < n; ++index) {
  1413             SDL_RenderDriver *driver =
  1414                 &SDL_CurrentDisplay.render_drivers[index];
  1415 
  1416             if (override) {
  1417                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1418                     break;
  1419                 }
  1420             } else {
  1421                 if ((driver->info.flags & flags) == flags) {
  1422                     break;
  1423                 }
  1424             }
  1425         }
  1426         if (index == n) {
  1427             SDL_SetError("Couldn't find matching render driver");
  1428             return -1;
  1429         }
  1430     }
  1431 
  1432     if (index >= SDL_GetNumRenderDrivers()) {
  1433         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1434                      SDL_GetNumRenderDrivers() - 1);
  1435         return -1;
  1436     }
  1437 
  1438     /* Free any existing renderer */
  1439     SDL_DestroyRenderer(windowID);
  1440 
  1441     /* Create a new renderer instance */
  1442     window->renderer =
  1443         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1444                                                                 flags);
  1445     SDL_SelectRenderer(window->id);
  1446 
  1447     return 0;
  1448 }
  1449 
  1450 int
  1451 SDL_SelectRenderer(SDL_WindowID windowID)
  1452 {
  1453     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1454     SDL_Renderer *renderer;
  1455 
  1456     if (!window || !window->renderer) {
  1457         return -1;
  1458     }
  1459     renderer = window->renderer;
  1460     if (renderer && renderer->ActivateRenderer) {
  1461         if (renderer->ActivateRenderer(renderer) < 0) {
  1462             return -1;
  1463         }
  1464     }
  1465     SDL_CurrentDisplay.current_renderer = renderer;
  1466     return 0;
  1467 }
  1468 
  1469 int
  1470 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1471 {
  1472     if (!_this) {
  1473         SDL_UninitializedVideo();
  1474         return -1;
  1475     }
  1476 
  1477     if (!SDL_CurrentDisplay.current_renderer) {
  1478         SDL_SetError("There is no current renderer");
  1479         return -1;
  1480     }
  1481     *info = SDL_CurrentDisplay.current_renderer->info;
  1482     return 0;
  1483 }
  1484 
  1485 SDL_TextureID
  1486 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1487 {
  1488     int hash;
  1489     SDL_Renderer *renderer;
  1490     SDL_Texture *texture;
  1491 
  1492     if (!_this) {
  1493         SDL_UninitializedVideo();
  1494         return 0;
  1495     }
  1496 
  1497     renderer = SDL_CurrentDisplay.current_renderer;
  1498     if (!renderer || !renderer->CreateTexture) {
  1499         return 0;
  1500     }
  1501 
  1502     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1503     if (!texture) {
  1504         SDL_OutOfMemory();
  1505         return 0;
  1506     }
  1507 
  1508     texture->id = _this->next_object_id++;
  1509     texture->format = format;
  1510     texture->access = access;
  1511     texture->w = w;
  1512     texture->h = h;
  1513     texture->r = 255;
  1514     texture->g = 255;
  1515     texture->b = 255;
  1516     texture->a = 255;
  1517     texture->renderer = renderer;
  1518 
  1519     if (renderer->CreateTexture(renderer, texture) < 0) {
  1520         if (renderer->DestroyTexture) {
  1521             renderer->DestroyTexture(renderer, texture);
  1522         }
  1523         SDL_free(texture);
  1524         return 0;
  1525     }
  1526 
  1527     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1528     texture->next = SDL_CurrentDisplay.textures[hash];
  1529     SDL_CurrentDisplay.textures[hash] = texture;
  1530 
  1531     return texture->id;
  1532 }
  1533 
  1534 SDL_TextureID
  1535 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1536 {
  1537     SDL_TextureID textureID;
  1538     Uint32 surface_flags = surface->flags;
  1539     SDL_PixelFormat *fmt = surface->format;
  1540     int bpp;
  1541     Uint32 Rmask, Gmask, Bmask, Amask;
  1542 
  1543     if (!surface) {
  1544         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1545         return 0;
  1546     }
  1547 
  1548     if (format) {
  1549         if (!SDL_PixelFormatEnumToMasks
  1550             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1551             SDL_SetError("Unknown pixel format");
  1552             return 0;
  1553         }
  1554     } else {
  1555         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1556             bpp = fmt->BitsPerPixel;
  1557             Rmask = fmt->Rmask;
  1558             Gmask = fmt->Gmask;
  1559             Bmask = fmt->Bmask;
  1560             Amask = fmt->Amask;
  1561         } else {
  1562             /* Need a format with alpha */
  1563             bpp = 32;
  1564             Rmask = 0x00FF0000;
  1565             Gmask = 0x0000FF00;
  1566             Bmask = 0x000000FF;
  1567             Amask = 0xFF000000;
  1568         }
  1569         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1570         if (!format) {
  1571             SDL_SetError("Unknown pixel format");
  1572             return 0;
  1573         }
  1574     }
  1575 
  1576     textureID =
  1577         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1578                           surface->h);
  1579     if (!textureID) {
  1580         return 0;
  1581     }
  1582 
  1583     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1584         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1585         if (SDL_MUSTLOCK(surface)) {
  1586             if (SDL_LockSurface(surface) < 0) {
  1587                 SDL_DestroyTexture(textureID);
  1588                 return 0;
  1589             }
  1590             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1591                               surface->pitch);
  1592             SDL_UnlockSurface(surface);
  1593         } else {
  1594             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1595                               surface->pitch);
  1596         }
  1597     } else {
  1598         Uint32 cmod;
  1599         SDL_Rect bounds;
  1600         SDL_Surface dst;
  1601 
  1602         /* Set up a destination surface for the texture update */
  1603         SDL_zero(dst);
  1604         dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1605         if (!dst.format) {
  1606             SDL_DestroyTexture(textureID);
  1607             return 0;
  1608         }
  1609         dst.w = surface->w;
  1610         dst.h = surface->h;
  1611         dst.pitch = SDL_CalculatePitch(&dst);
  1612         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1613         if (!dst.pixels) {
  1614             SDL_DestroyTexture(textureID);
  1615             SDL_FreeFormat(dst.format);
  1616             SDL_OutOfMemory();
  1617             return 0;
  1618         }
  1619 
  1620         /* Copy the palette if any */
  1621         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1622             if (fmt->palette) {
  1623                 SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1624                                       fmt->palette->ncolors);
  1625                 SDL_SetSurfacePalette(&dst, fmt->palette);
  1626             } else {
  1627                 dst.format->palette =
  1628                     SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1629                 if (!dst.format->palette) {
  1630                     SDL_DestroyTexture(textureID);
  1631                     SDL_FreeFormat(dst.format);
  1632                     return 0;
  1633                 }
  1634                 SDL_DitherColors(dst.format->palette->colors,
  1635                                  SDL_BITSPERPIXEL(format));
  1636             }
  1637         }
  1638 
  1639         /* Make the texture transparent if the surface has colorkey */
  1640         if (surface_flags & SDL_SRCCOLORKEY) {
  1641             int row;
  1642             int length = dst.w * dst.format->BytesPerPixel;
  1643             Uint8 *p = (Uint8 *) dst.pixels;
  1644             for (row = 0; row < dst.h; ++row) {
  1645                 SDL_memset(p, 0, length);
  1646                 p += dst.pitch;
  1647             }
  1648         }
  1649 
  1650         /* Copy over the alpha channel */
  1651         cmod = surface->map->cmod;
  1652         if (surface_flags & SDL_SRCALPHA) {
  1653             if (fmt->Amask) {
  1654                 surface->flags &= ~SDL_SRCALPHA;
  1655             } else {
  1656                 /* FIXME: Need to make sure the texture has an alpha channel
  1657                  *        and copy 'alpha' into the texture alpha channel.
  1658                  */
  1659                 SDL_SetAlpha(surface, 0, 0);
  1660             }
  1661         }
  1662 
  1663         /* Copy over the image data */
  1664         bounds.x = 0;
  1665         bounds.y = 0;
  1666         bounds.w = surface->w;
  1667         bounds.h = surface->h;
  1668         SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1669 
  1670         /* Clean up the original surface */
  1671         if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1672             Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1673             if (fmt->Amask) {
  1674                 surface->flags |= SDL_SRCALPHA;
  1675             } else {
  1676                 SDL_SetAlpha(surface, aflags, (cmod >> 24));
  1677             }
  1678         }
  1679 
  1680         /* Update the texture */
  1681         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1682         SDL_free(dst.pixels);
  1683         SDL_FreeFormat(dst.format);
  1684     }
  1685 
  1686     return textureID;
  1687 }
  1688 
  1689 static __inline__ SDL_Texture *
  1690 SDL_GetTextureFromID(SDL_TextureID textureID)
  1691 {
  1692     int hash;
  1693     SDL_Texture *texture;
  1694 
  1695     if (!_this) {
  1696         return NULL;
  1697     }
  1698 
  1699     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1700     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1701          texture = texture->next) {
  1702         if (texture->id == textureID) {
  1703             return texture;
  1704         }
  1705     }
  1706     return NULL;
  1707 }
  1708 
  1709 int
  1710 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1711                  int *w, int *h)
  1712 {
  1713     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1714 
  1715     if (!texture) {
  1716         return -1;
  1717     }
  1718 
  1719     if (format) {
  1720         *format = texture->format;
  1721     }
  1722     if (access) {
  1723         *access = texture->access;
  1724     }
  1725     if (w) {
  1726         *w = texture->w;
  1727     }
  1728     if (h) {
  1729         *h = texture->h;
  1730     }
  1731     return 0;
  1732 }
  1733 
  1734 int
  1735 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1736 {
  1737     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1738     SDL_Renderer *renderer;
  1739 
  1740     if (!texture) {
  1741         return -1;
  1742     }
  1743 
  1744     renderer = texture->renderer;
  1745     if (!renderer->QueryTexturePixels) {
  1746         return -1;
  1747     }
  1748     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1749 }
  1750 
  1751 int
  1752 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1753                       int firstcolor, int ncolors)
  1754 {
  1755     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1756     SDL_Renderer *renderer;
  1757 
  1758     if (!texture) {
  1759         return -1;
  1760     }
  1761 
  1762     renderer = texture->renderer;
  1763     if (!renderer->SetTexturePalette) {
  1764         return -1;
  1765     }
  1766     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1767                                        ncolors);
  1768 }
  1769 
  1770 int
  1771 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1772                       int firstcolor, int ncolors)
  1773 {
  1774     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1775     SDL_Renderer *renderer;
  1776 
  1777     if (!texture) {
  1778         return -1;
  1779     }
  1780 
  1781     renderer = texture->renderer;
  1782     if (!renderer->GetTexturePalette) {
  1783         return -1;
  1784     }
  1785     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1786                                        ncolors);
  1787 }
  1788 
  1789 int
  1790 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  1791 {
  1792     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1793     SDL_Renderer *renderer;
  1794 
  1795     if (!texture) {
  1796         return -1;
  1797     }
  1798 
  1799     renderer = texture->renderer;
  1800     if (!renderer->SetTextureColorMod) {
  1801         return -1;
  1802     }
  1803     if (r < 255 || g < 255 || b < 255) {
  1804         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1805     } else {
  1806         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1807     }
  1808     texture->r = r;
  1809     texture->g = g;
  1810     texture->b = b;
  1811     return renderer->SetTextureColorMod(renderer, texture);
  1812 }
  1813 
  1814 int
  1815 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  1816                        Uint8 * b)
  1817 {
  1818     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1819     SDL_Renderer *renderer;
  1820 
  1821     if (!texture) {
  1822         return -1;
  1823     }
  1824 
  1825     renderer = texture->renderer;
  1826     if (r) {
  1827         *r = texture->r;
  1828     }
  1829     if (g) {
  1830         *g = texture->g;
  1831     }
  1832     if (b) {
  1833         *b = texture->b;
  1834     }
  1835     return 0;
  1836 }
  1837 
  1838 int
  1839 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  1840 {
  1841     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1842     SDL_Renderer *renderer;
  1843 
  1844     if (!texture) {
  1845         return -1;
  1846     }
  1847 
  1848     renderer = texture->renderer;
  1849     if (!renderer->SetTextureAlphaMod) {
  1850         return -1;
  1851     }
  1852     if (alpha < 255) {
  1853         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1854     } else {
  1855         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1856     }
  1857     texture->a = alpha;
  1858     return renderer->SetTextureAlphaMod(renderer, texture);
  1859 }
  1860 
  1861 int
  1862 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  1863 {
  1864     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1865 
  1866     if (!texture) {
  1867         return -1;
  1868     }
  1869 
  1870     if (alpha) {
  1871         *alpha = texture->a;
  1872     }
  1873     return 0;
  1874 }
  1875 
  1876 int
  1877 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  1878 {
  1879     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1880     SDL_Renderer *renderer;
  1881 
  1882     if (!texture) {
  1883         return -1;
  1884     }
  1885 
  1886     renderer = texture->renderer;
  1887     if (!renderer->SetTextureBlendMode) {
  1888         return -1;
  1889     }
  1890     texture->blendMode = blendMode;
  1891     return renderer->SetTextureBlendMode(renderer, texture);
  1892 }
  1893 
  1894 int
  1895 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  1896 {
  1897     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1898 
  1899     if (!texture) {
  1900         return -1;
  1901     }
  1902 
  1903     if (blendMode) {
  1904         *blendMode = texture->blendMode;
  1905     }
  1906     return 0;
  1907 }
  1908 
  1909 int
  1910 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  1911 {
  1912     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1913     SDL_Renderer *renderer;
  1914 
  1915     if (!texture) {
  1916         return -1;
  1917     }
  1918 
  1919     renderer = texture->renderer;
  1920     if (!renderer->SetTextureScaleMode) {
  1921         return -1;
  1922     }
  1923     texture->scaleMode = scaleMode;
  1924     return renderer->SetTextureScaleMode(renderer, texture);
  1925 }
  1926 
  1927 int
  1928 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  1929 {
  1930     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1931 
  1932     if (!texture) {
  1933         return -1;
  1934     }
  1935 
  1936     if (scaleMode) {
  1937         *scaleMode = texture->scaleMode;
  1938     }
  1939     return 0;
  1940 }
  1941 
  1942 int
  1943 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1944                   const void *pixels, int pitch)
  1945 {
  1946     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1947     SDL_Renderer *renderer;
  1948     SDL_Rect full_rect;
  1949 
  1950     if (!texture) {
  1951         return -1;
  1952     }
  1953 
  1954     renderer = texture->renderer;
  1955     if (!renderer->UpdateTexture) {
  1956         return -1;
  1957     }
  1958 
  1959     if (!rect) {
  1960         full_rect.x = 0;
  1961         full_rect.y = 0;
  1962         full_rect.w = texture->w;
  1963         full_rect.h = texture->h;
  1964         rect = &full_rect;
  1965     }
  1966 
  1967     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1968 }
  1969 
  1970 int
  1971 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1972                 void **pixels, int *pitch)
  1973 {
  1974     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1975     SDL_Renderer *renderer;
  1976     SDL_Rect full_rect;
  1977 
  1978     if (!texture) {
  1979         return -1;
  1980     }
  1981     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1982         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1983         return -1;
  1984     }
  1985 
  1986     renderer = texture->renderer;
  1987     if (!renderer->LockTexture) {
  1988         return -1;
  1989     }
  1990 
  1991     if (!rect) {
  1992         full_rect.x = 0;
  1993         full_rect.y = 0;
  1994         full_rect.w = texture->w;
  1995         full_rect.h = texture->h;
  1996         rect = &full_rect;
  1997     }
  1998 
  1999     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2000                                  pitch);
  2001 }
  2002 
  2003 void
  2004 SDL_UnlockTexture(SDL_TextureID textureID)
  2005 {
  2006     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2007     SDL_Renderer *renderer;
  2008 
  2009     if (!texture) {
  2010         return;
  2011     }
  2012     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2013         return;
  2014     }
  2015 
  2016     renderer = texture->renderer;
  2017     if (!renderer->UnlockTexture) {
  2018         return;
  2019     }
  2020     renderer->UnlockTexture(renderer, texture);
  2021 }
  2022 
  2023 void
  2024 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  2025                  const SDL_Rect * rects)
  2026 {
  2027     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2028     SDL_Renderer *renderer;
  2029 
  2030     if (!texture) {
  2031         return;
  2032     }
  2033     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2034         return;
  2035     }
  2036 
  2037     renderer = texture->renderer;
  2038     if (!renderer->DirtyTexture) {
  2039         return;
  2040     }
  2041     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2042 }
  2043 
  2044 int
  2045 SDL_RenderFill(Uint8 r, Uint8 g, Uint8 b, Uint8 a, const SDL_Rect * rect)
  2046 {
  2047     SDL_Renderer *renderer;
  2048     SDL_Window *window;
  2049     SDL_Rect real_rect;
  2050 
  2051     if (!_this) {
  2052         SDL_UninitializedVideo();
  2053         return -1;
  2054     }
  2055 
  2056     renderer = SDL_CurrentDisplay.current_renderer;
  2057     if (!renderer || !renderer->RenderFill) {
  2058         return -1;
  2059     }
  2060 
  2061     window = SDL_GetWindowFromID(renderer->window);
  2062     real_rect.x = 0;
  2063     real_rect.y = 0;
  2064     real_rect.w = window->w;
  2065     real_rect.h = window->h;
  2066     if (rect) {
  2067         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2068             return 0;
  2069         }
  2070     }
  2071 
  2072     return renderer->RenderFill(renderer, r, g, b, a, &real_rect);
  2073 }
  2074 
  2075 int
  2076 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2077                const SDL_Rect * dstrect)
  2078 {
  2079     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2080     SDL_Renderer *renderer;
  2081     SDL_Window *window;
  2082     SDL_Rect real_srcrect;
  2083     SDL_Rect real_dstrect;
  2084 
  2085     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  2086         return -1;
  2087     }
  2088 
  2089     renderer = SDL_CurrentDisplay.current_renderer;
  2090     if (!renderer || !renderer->RenderCopy) {
  2091         return -1;
  2092     }
  2093 
  2094     window = SDL_GetWindowFromID(renderer->window);
  2095     if (srcrect) {
  2096         real_srcrect = *srcrect;
  2097     } else {
  2098         real_srcrect.x = 0;
  2099         real_srcrect.y = 0;
  2100         real_srcrect.w = texture->w;
  2101         real_srcrect.h = texture->h;
  2102     }
  2103     if (dstrect) {
  2104         real_dstrect = *dstrect;
  2105     } else {
  2106         real_dstrect.x = 0;
  2107         real_dstrect.y = 0;
  2108         real_dstrect.w = window->w;
  2109         real_dstrect.h = window->h;
  2110     }
  2111 
  2112     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2113                                 &real_dstrect);
  2114 }
  2115 
  2116 void
  2117 SDL_RenderPresent(void)
  2118 {
  2119     SDL_Renderer *renderer;
  2120 
  2121     if (!_this) {
  2122         SDL_UninitializedVideo();
  2123         return;
  2124     }
  2125 
  2126     renderer = SDL_CurrentDisplay.current_renderer;
  2127     if (!renderer || !renderer->RenderPresent) {
  2128         return;
  2129     }
  2130     renderer->RenderPresent(renderer);
  2131 }
  2132 
  2133 void
  2134 SDL_DestroyTexture(SDL_TextureID textureID)
  2135 {
  2136     int hash;
  2137     SDL_Texture *prev, *texture;
  2138     SDL_Renderer *renderer;
  2139 
  2140     if (!_this) {
  2141         SDL_UninitializedVideo();
  2142         return;
  2143     }
  2144 
  2145     /* Look up the texture in the hash table */
  2146     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2147     prev = NULL;
  2148     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2149          prev = texture, texture = texture->next) {
  2150         if (texture->id == textureID) {
  2151             break;
  2152         }
  2153     }
  2154     if (!texture) {
  2155         return;
  2156     }
  2157 
  2158     /* Unlink the texture from the list */
  2159     if (prev) {
  2160         prev->next = texture->next;
  2161     } else {
  2162         SDL_CurrentDisplay.textures[hash] = texture->next;
  2163     }
  2164 
  2165     /* Free the texture */
  2166     renderer = texture->renderer;
  2167     renderer->DestroyTexture(renderer, texture);
  2168     SDL_free(texture);
  2169 }
  2170 
  2171 void
  2172 SDL_DestroyRenderer(SDL_WindowID windowID)
  2173 {
  2174     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2175     SDL_Renderer *renderer;
  2176     int i;
  2177 
  2178     if (!window) {
  2179         return;
  2180     }
  2181 
  2182     renderer = window->renderer;
  2183     if (!renderer) {
  2184         return;
  2185     }
  2186 
  2187     /* Free existing textures for this renderer */
  2188     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2189         SDL_Texture *texture;
  2190         SDL_Texture *prev = NULL;
  2191         SDL_Texture *next;
  2192         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2193              texture = next) {
  2194             next = texture->next;
  2195             if (texture->renderer == renderer) {
  2196                 if (prev) {
  2197                     prev->next = next;
  2198                 } else {
  2199                     SDL_CurrentDisplay.textures[i] = next;
  2200                 }
  2201                 renderer->DestroyTexture(renderer, texture);
  2202                 SDL_free(texture);
  2203             } else {
  2204                 prev = texture;
  2205             }
  2206         }
  2207     }
  2208 
  2209     /* Free the renderer instance */
  2210     renderer->DestroyRenderer(renderer);
  2211 
  2212     /* Clear references */
  2213     window->renderer = NULL;
  2214     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2215         SDL_CurrentDisplay.current_renderer = NULL;
  2216     }
  2217 }
  2218 
  2219 void
  2220 SDL_VideoQuit(void)
  2221 {
  2222     int i, j;
  2223 
  2224     if (!_this) {
  2225         return;
  2226     }
  2227 
  2228     /* Halt event processing before doing anything else */
  2229     SDL_StopEventLoop();
  2230 
  2231     /* Clean up the system video */
  2232     for (i = _this->num_displays; i--;) {
  2233         SDL_VideoDisplay *display = &_this->displays[i];
  2234         for (j = display->num_windows; j--;) {
  2235             SDL_DestroyWindow(display->windows[i].id);
  2236         }
  2237         if (display->windows) {
  2238             SDL_free(display->windows);
  2239             display->windows = NULL;
  2240         }
  2241         display->num_windows = 0;
  2242     }
  2243     _this->VideoQuit(_this);
  2244 
  2245     for (i = _this->num_displays; i--;) {
  2246         SDL_VideoDisplay *display = &_this->displays[i];
  2247         for (j = display->num_display_modes; j--;) {
  2248             if (display->display_modes[j].driverdata) {
  2249                 SDL_free(display->display_modes[j].driverdata);
  2250                 display->display_modes[j].driverdata = NULL;
  2251             }
  2252         }
  2253         if (display->display_modes) {
  2254             SDL_free(display->display_modes);
  2255             display->display_modes = NULL;
  2256         }
  2257         if (display->desktop_mode.driverdata) {
  2258             SDL_free(display->desktop_mode.driverdata);
  2259             display->desktop_mode.driverdata = NULL;
  2260         }
  2261         if (display->palette) {
  2262             SDL_FreePalette(display->palette);
  2263             display->palette = NULL;
  2264         }
  2265         if (display->gamma) {
  2266             SDL_free(display->gamma);
  2267         }
  2268         if (display->driverdata) {
  2269             SDL_free(display->driverdata);
  2270         }
  2271     }
  2272     if (_this->displays) {
  2273         SDL_free(_this->displays);
  2274         _this->displays = NULL;
  2275     }
  2276     _this->free(_this);
  2277     _this = NULL;
  2278 }
  2279 
  2280 int
  2281 SDL_GL_LoadLibrary(const char *path)
  2282 {
  2283     int retval;
  2284 
  2285     if (!_this) {
  2286         SDL_UninitializedVideo();
  2287         return -1;
  2288     }
  2289 
  2290     if (_this->GL_LoadLibrary) {
  2291         retval = _this->GL_LoadLibrary(_this, path);
  2292     } else {
  2293         SDL_SetError("No dynamic GL support in video driver");
  2294         retval = -1;
  2295     }
  2296     return (retval);
  2297 }
  2298 
  2299 void *
  2300 SDL_GL_GetProcAddress(const char *proc)
  2301 {
  2302     void *func;
  2303 
  2304     if (!_this) {
  2305         SDL_UninitializedVideo();
  2306         return NULL;
  2307     }
  2308 
  2309     func = NULL;
  2310     if (_this->GL_GetProcAddress) {
  2311         if (_this->gl_config.driver_loaded) {
  2312             func = _this->GL_GetProcAddress(_this, proc);
  2313         } else {
  2314             SDL_SetError("No GL driver has been loaded");
  2315         }
  2316     } else {
  2317         SDL_SetError("No dynamic GL support in video driver");
  2318     }
  2319     return func;
  2320 }
  2321 
  2322 SDL_bool
  2323 SDL_GL_ExtensionSupported(const char *extension)
  2324 {
  2325 #if SDL_VIDEO_OPENGL
  2326     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2327     const char *extensions;
  2328     const char *start;
  2329     const char *where, *terminator;
  2330 
  2331     /* Extension names should not have spaces. */
  2332     where = SDL_strchr(extension, ' ');
  2333     if (where || *extension == '\0') {
  2334         return SDL_FALSE;
  2335     }
  2336 
  2337     /* See if there's an environment variable override */
  2338     start = SDL_getenv(extension);
  2339     if (start && *start == '0') {
  2340         return SDL_FALSE;
  2341     }
  2342 
  2343     /* Lookup the available extensions */
  2344     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2345     if (glGetStringFunc) {
  2346         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2347     } else {
  2348         extensions = NULL;
  2349     }
  2350     if (!extensions) {
  2351         return SDL_FALSE;
  2352     }
  2353 
  2354     /* It takes a bit of care to be fool-proof about parsing the
  2355      * OpenGL extensions string. Don't be fooled by sub-strings,
  2356      * etc. */
  2357 
  2358     start = extensions;
  2359 
  2360     for (;;) {
  2361         where = SDL_strstr(start, extension);
  2362         if (!where)
  2363             break;
  2364 
  2365         terminator = where + SDL_strlen(extension);
  2366         if (where == start || *(where - 1) == ' ')
  2367             if (*terminator == ' ' || *terminator == '\0')
  2368                 return SDL_TRUE;
  2369 
  2370         start = terminator;
  2371     }
  2372     return SDL_FALSE;
  2373 #else
  2374     return SDL_FALSE;
  2375 #endif
  2376 }
  2377 
  2378 int
  2379 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2380 {
  2381 #if SDL_VIDEO_OPENGL
  2382     int retval;
  2383 
  2384     if (!_this) {
  2385         SDL_UninitializedVideo();
  2386         return -1;
  2387     }
  2388 
  2389     retval = 0;
  2390     switch (attr) {
  2391     case SDL_GL_RED_SIZE:
  2392         _this->gl_config.red_size = value;
  2393         break;
  2394     case SDL_GL_GREEN_SIZE:
  2395         _this->gl_config.green_size = value;
  2396         break;
  2397     case SDL_GL_BLUE_SIZE:
  2398         _this->gl_config.blue_size = value;
  2399         break;
  2400     case SDL_GL_ALPHA_SIZE:
  2401         _this->gl_config.alpha_size = value;
  2402         break;
  2403     case SDL_GL_DOUBLEBUFFER:
  2404         _this->gl_config.double_buffer = value;
  2405         break;
  2406     case SDL_GL_BUFFER_SIZE:
  2407         _this->gl_config.buffer_size = value;
  2408         break;
  2409     case SDL_GL_DEPTH_SIZE:
  2410         _this->gl_config.depth_size = value;
  2411         break;
  2412     case SDL_GL_STENCIL_SIZE:
  2413         _this->gl_config.stencil_size = value;
  2414         break;
  2415     case SDL_GL_ACCUM_RED_SIZE:
  2416         _this->gl_config.accum_red_size = value;
  2417         break;
  2418     case SDL_GL_ACCUM_GREEN_SIZE:
  2419         _this->gl_config.accum_green_size = value;
  2420         break;
  2421     case SDL_GL_ACCUM_BLUE_SIZE:
  2422         _this->gl_config.accum_blue_size = value;
  2423         break;
  2424     case SDL_GL_ACCUM_ALPHA_SIZE:
  2425         _this->gl_config.accum_alpha_size = value;
  2426         break;
  2427     case SDL_GL_STEREO:
  2428         _this->gl_config.stereo = value;
  2429         break;
  2430     case SDL_GL_MULTISAMPLEBUFFERS:
  2431         _this->gl_config.multisamplebuffers = value;
  2432         break;
  2433     case SDL_GL_MULTISAMPLESAMPLES:
  2434         _this->gl_config.multisamplesamples = value;
  2435         break;
  2436     case SDL_GL_ACCELERATED_VISUAL:
  2437         _this->gl_config.accelerated = value;
  2438         break;
  2439     default:
  2440         SDL_SetError("Unknown OpenGL attribute");
  2441         retval = -1;
  2442         break;
  2443     }
  2444     return retval;
  2445 #else
  2446     SDL_Unsupported();
  2447     return -1;
  2448 #endif /* SDL_VIDEO_OPENGL */
  2449 }
  2450 
  2451 int
  2452 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2453 {
  2454 #if SDL_VIDEO_OPENGL
  2455     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2456     GLenum attrib = 0;
  2457 
  2458     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2459     if (!glGetIntegervFunc) {
  2460         return -1;
  2461     }
  2462     switch (attr) {
  2463     case SDL_GL_RED_SIZE:
  2464         attrib = GL_RED_BITS;
  2465         break;
  2466     case SDL_GL_BLUE_SIZE:
  2467         attrib = GL_BLUE_BITS;
  2468         break;
  2469     case SDL_GL_GREEN_SIZE:
  2470         attrib = GL_GREEN_BITS;
  2471         break;
  2472     case SDL_GL_ALPHA_SIZE:
  2473         attrib = GL_ALPHA_BITS;
  2474         break;
  2475     case SDL_GL_DOUBLEBUFFER:
  2476         attrib = GL_DOUBLEBUFFER;
  2477         break;
  2478     case SDL_GL_DEPTH_SIZE:
  2479         attrib = GL_DEPTH_BITS;
  2480         break;
  2481     case SDL_GL_STENCIL_SIZE:
  2482         attrib = GL_STENCIL_BITS;
  2483         break;
  2484     case SDL_GL_ACCUM_RED_SIZE:
  2485         attrib = GL_ACCUM_RED_BITS;
  2486         break;
  2487     case SDL_GL_ACCUM_GREEN_SIZE:
  2488         attrib = GL_ACCUM_GREEN_BITS;
  2489         break;
  2490     case SDL_GL_ACCUM_BLUE_SIZE:
  2491         attrib = GL_ACCUM_BLUE_BITS;
  2492         break;
  2493     case SDL_GL_ACCUM_ALPHA_SIZE:
  2494         attrib = GL_ACCUM_ALPHA_BITS;
  2495         break;
  2496     case SDL_GL_STEREO:
  2497         attrib = GL_STEREO;
  2498         break;
  2499     case SDL_GL_MULTISAMPLEBUFFERS:
  2500         attrib = GL_SAMPLE_BUFFERS_ARB;
  2501         break;
  2502     case SDL_GL_MULTISAMPLESAMPLES:
  2503         attrib = GL_SAMPLES_ARB;
  2504         break;
  2505     case SDL_GL_BUFFER_SIZE:
  2506         {
  2507             GLint bits = 0;
  2508             GLint component;
  2509 
  2510             /* there doesn't seem to be a single flag in OpenGL for this! */
  2511             glGetIntegervFunc(GL_RED_BITS, &component);
  2512             bits += component;
  2513             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2514             bits += component;
  2515             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2516             bits += component;
  2517             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2518             bits += component;
  2519 
  2520             *value = bits;
  2521             return 0;
  2522         }
  2523     case SDL_GL_ACCELERATED_VISUAL:
  2524         {
  2525             /* FIXME: How do we get this information? */
  2526             *value = (_this->gl_config.accelerated != 0);
  2527             return 0;
  2528         }
  2529     default:
  2530         SDL_SetError("Unknown OpenGL attribute");
  2531         return -1;
  2532     }
  2533 
  2534     glGetIntegervFunc(attrib, (GLint *) value);
  2535     return 0;
  2536 #else
  2537     SDL_Unsupported();
  2538     return -1;
  2539 #endif /* SDL_VIDEO_OPENGL */
  2540 }
  2541 
  2542 SDL_GLContext
  2543 SDL_GL_CreateContext(SDL_WindowID windowID)
  2544 {
  2545     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2546 
  2547     if (!window) {
  2548         return NULL;
  2549     }
  2550     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2551         SDL_SetError("The specified window isn't an OpenGL window");
  2552         return NULL;
  2553     }
  2554     return _this->GL_CreateContext(_this, window);
  2555 }
  2556 
  2557 int
  2558 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2559 {
  2560     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2561 
  2562     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2563         SDL_SetError("The specified window isn't an OpenGL window");
  2564         return -1;
  2565     }
  2566     if (!context) {
  2567         window = NULL;
  2568     }
  2569     if (window) {
  2570         if (window->context == context) {
  2571             return 0;
  2572         }
  2573         window->context = context;
  2574     }
  2575     return _this->GL_MakeCurrent(_this, window, context);
  2576 }
  2577 
  2578 int
  2579 SDL_GL_SetSwapInterval(int interval)
  2580 {
  2581     if (!_this) {
  2582         SDL_UninitializedVideo();
  2583         return -1;
  2584     }
  2585 
  2586     if (_this->GL_SetSwapInterval) {
  2587         return _this->GL_SetSwapInterval(_this, interval);
  2588     } else {
  2589         SDL_SetError("Setting the swap interval is not supported");
  2590         return -1;
  2591     }
  2592 }
  2593 
  2594 int
  2595 SDL_GL_GetSwapInterval(void)
  2596 {
  2597     if (!_this) {
  2598         SDL_UninitializedVideo();
  2599         return -1;
  2600     }
  2601 
  2602     if (_this->GL_GetSwapInterval) {
  2603         return _this->GL_GetSwapInterval(_this);
  2604     } else {
  2605         SDL_SetError("Getting the swap interval is not supported");
  2606         return -1;
  2607     }
  2608 }
  2609 
  2610 void
  2611 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2612 {
  2613     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2614 
  2615     if (!window) {
  2616         return;
  2617     }
  2618     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2619         SDL_SetError("The specified window isn't an OpenGL window");
  2620         return;
  2621     }
  2622     _this->GL_SwapWindow(_this, window);
  2623 }
  2624 
  2625 void
  2626 SDL_GL_DeleteContext(SDL_GLContext context)
  2627 {
  2628     if (!_this || !context) {
  2629         return;
  2630     }
  2631     _this->GL_MakeCurrent(_this, NULL, NULL);
  2632     _this->GL_DeleteContext(_this, context);
  2633 }
  2634 
  2635 #if 0                           // FIXME
  2636 /* Utility function used by SDL_WM_SetIcon();
  2637  * flags & 1 for color key, flags & 2 for alpha channel. */
  2638 static void
  2639 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2640 {
  2641     int x, y;
  2642     Uint32 colorkey;
  2643 #define SET_MASKBIT(icon, x, y, mask) \
  2644 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2645 
  2646     colorkey = icon->format->colorkey;
  2647     switch (icon->format->BytesPerPixel) {
  2648     case 1:
  2649         {
  2650             Uint8 *pixels;
  2651             for (y = 0; y < icon->h; ++y) {
  2652                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2653                 for (x = 0; x < icon->w; ++x) {
  2654                     if (*pixels++ == colorkey) {
  2655                         SET_MASKBIT(icon, x, y, mask);
  2656                     }
  2657                 }
  2658             }
  2659         }
  2660         break;
  2661 
  2662     case 2:
  2663         {
  2664             Uint16 *pixels;
  2665             for (y = 0; y < icon->h; ++y) {
  2666                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2667                 for (x = 0; x < icon->w; ++x) {
  2668                     if ((flags & 1) && *pixels == colorkey) {
  2669                         SET_MASKBIT(icon, x, y, mask);
  2670                     } else if ((flags & 2)
  2671                                && (*pixels & icon->format->Amask) == 0) {
  2672                         SET_MASKBIT(icon, x, y, mask);
  2673                     }
  2674                     pixels++;
  2675                 }
  2676             }
  2677         }
  2678         break;
  2679 
  2680     case 4:
  2681         {
  2682             Uint32 *pixels;
  2683             for (y = 0; y < icon->h; ++y) {
  2684                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2685                 for (x = 0; x < icon->w; ++x) {
  2686                     if ((flags & 1) && *pixels == colorkey) {
  2687                         SET_MASKBIT(icon, x, y, mask);
  2688                     } else if ((flags & 2)
  2689                                && (*pixels & icon->format->Amask) == 0) {
  2690                         SET_MASKBIT(icon, x, y, mask);
  2691                     }
  2692                     pixels++;
  2693                 }
  2694             }
  2695         }
  2696         break;
  2697     }
  2698 }
  2699 
  2700 /*
  2701  * Sets the window manager icon for the display window.
  2702  */
  2703 void
  2704 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2705 {
  2706     if (icon && _this->SetIcon) {
  2707         /* Generate a mask if necessary, and create the icon! */
  2708         if (mask == NULL) {
  2709             int mask_len = icon->h * (icon->w + 7) / 8;
  2710             int flags = 0;
  2711             mask = (Uint8 *) SDL_malloc(mask_len);
  2712             if (mask == NULL) {
  2713                 return;
  2714             }
  2715             SDL_memset(mask, ~0, mask_len);
  2716             if (icon->flags & SDL_SRCCOLORKEY)
  2717                 flags |= 1;
  2718             if (icon->flags & SDL_SRCALPHA)
  2719                 flags |= 2;
  2720             if (flags) {
  2721                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2722             }
  2723             _this->SetIcon(_this, icon, mask);
  2724             SDL_free(mask);
  2725         } else {
  2726             _this->SetIcon(_this, icon, mask);
  2727         }
  2728     }
  2729 }
  2730 #endif
  2731 
  2732 SDL_bool
  2733 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2734 {
  2735     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2736 
  2737     if (!window || !_this->GetWindowWMInfo) {
  2738         return SDL_FALSE;
  2739     }
  2740     return (_this->GetWindowWMInfo(_this, window, info));
  2741 }
  2742 
  2743 /* vi: set ts=4 sw=4 expandtab: */