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