src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2008 15:10:03 +0000
changeset 2735 204be4fc2726
parent 2702 11eb9ae3c88c
child 2740 22c96d28449f
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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