src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 30 Nov 2008 21:58:23 +0000
changeset 2810 27cb878a278e
parent 2789 985001797115
child 2814 e841aa93e6be
permissions -rw-r--r--
Implemented the X11 (non-OpenGL) renderer, no alpha or scaling available.
     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) {
  1480         return 0;
  1481     }
  1482     if (!renderer->CreateTexture) {
  1483         SDL_Unsupported();
  1484         return 0;
  1485     }
  1486     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1487     if (!texture) {
  1488         SDL_OutOfMemory();
  1489         return 0;
  1490     }
  1491     texture->id = _this->next_object_id++;
  1492     texture->format = format;
  1493     texture->access = access;
  1494     texture->w = w;
  1495     texture->h = h;
  1496     texture->r = 255;
  1497     texture->g = 255;
  1498     texture->b = 255;
  1499     texture->a = 255;
  1500     texture->renderer = renderer;
  1501 
  1502     if (renderer->CreateTexture(renderer, texture) < 0) {
  1503         if (renderer->DestroyTexture) {
  1504             renderer->DestroyTexture(renderer, texture);
  1505         }
  1506         SDL_free(texture);
  1507         return 0;
  1508     }
  1509     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1510     texture->next = SDL_CurrentDisplay.textures[hash];
  1511     SDL_CurrentDisplay.textures[hash] = texture;
  1512 
  1513     return texture->id;
  1514 }
  1515 
  1516 SDL_TextureID
  1517 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1518 {
  1519     SDL_TextureID textureID;
  1520     SDL_PixelFormat *fmt;
  1521     int bpp;
  1522     Uint32 Rmask, Gmask, Bmask, Amask;
  1523 
  1524     if (!surface) {
  1525         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1526         return 0;
  1527     }
  1528     fmt = surface->format;
  1529 
  1530     if (format) {
  1531         if (!SDL_PixelFormatEnumToMasks
  1532             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1533             SDL_SetError("Unknown pixel format");
  1534             return 0;
  1535         }
  1536     } else {
  1537         if (surface->format->Amask
  1538             || !(surface->map->info.flags &
  1539                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1540             bpp = fmt->BitsPerPixel;
  1541             Rmask = fmt->Rmask;
  1542             Gmask = fmt->Gmask;
  1543             Bmask = fmt->Bmask;
  1544             Amask = fmt->Amask;
  1545         } else {
  1546             /* Need a format with alpha */
  1547             bpp = 32;
  1548             Rmask = 0x00FF0000;
  1549             Gmask = 0x0000FF00;
  1550             Bmask = 0x000000FF;
  1551             Amask = 0xFF000000;
  1552         }
  1553         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1554         if (!format) {
  1555             SDL_SetError("Unknown pixel format");
  1556             return 0;
  1557         }
  1558     }
  1559 
  1560     textureID =
  1561         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1562                           surface->h);
  1563     if (!textureID) {
  1564         return 0;
  1565     }
  1566     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1567         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1568         if (SDL_MUSTLOCK(surface)) {
  1569             if (SDL_LockSurface(surface) < 0) {
  1570                 SDL_DestroyTexture(textureID);
  1571                 return 0;
  1572             }
  1573             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1574                               surface->pitch);
  1575             SDL_UnlockSurface(surface);
  1576         } else {
  1577             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1578                               surface->pitch);
  1579         }
  1580     } else {
  1581         SDL_PixelFormat *dst_fmt;
  1582         SDL_Surface *dst = NULL;
  1583 
  1584         /* Set up a destination surface for the texture update */
  1585         dst_fmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1586         if (dst_fmt) {
  1587             if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1588                 dst_fmt->palette =
  1589                     SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1590                 if (dst_fmt->palette) {
  1591                     /*
  1592                      * FIXME: Should we try to copy
  1593                      * fmt->palette?
  1594                      */
  1595                     SDL_DitherColors(dst_fmt->palette->colors,
  1596                                      SDL_BITSPERPIXEL(format));
  1597                 }
  1598             }
  1599             dst = SDL_ConvertSurface(surface, dst_fmt, 0);
  1600             if (dst) {
  1601                 SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
  1602                 SDL_FreeSurface(dst);
  1603             }
  1604             if (dst_fmt->palette) {
  1605                 SDL_FreePalette(dst_fmt->palette);
  1606             }
  1607             SDL_FreeFormat(dst_fmt);
  1608         }
  1609         if (!dst) {
  1610             SDL_DestroyTexture(textureID);
  1611             return 0;
  1612         }
  1613     }
  1614 
  1615     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1616         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1617                               fmt->palette->ncolors);
  1618     }
  1619     return textureID;
  1620 }
  1621 
  1622 static __inline__ SDL_Texture *
  1623 SDL_GetTextureFromID(SDL_TextureID textureID)
  1624 {
  1625     int hash;
  1626     SDL_Texture *texture;
  1627 
  1628     if (!_this) {
  1629         return NULL;
  1630     }
  1631     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1632     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1633          texture = texture->next) {
  1634         if (texture->id == textureID) {
  1635             return texture;
  1636         }
  1637     }
  1638     return NULL;
  1639 }
  1640 
  1641 int
  1642 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1643                  int *w, int *h)
  1644 {
  1645     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1646 
  1647     if (!texture) {
  1648         return -1;
  1649     }
  1650     if (format) {
  1651         *format = texture->format;
  1652     }
  1653     if (access) {
  1654         *access = texture->access;
  1655     }
  1656     if (w) {
  1657         *w = texture->w;
  1658     }
  1659     if (h) {
  1660         *h = texture->h;
  1661     }
  1662     return 0;
  1663 }
  1664 
  1665 int
  1666 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1667 {
  1668     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1669     SDL_Renderer *renderer;
  1670 
  1671     if (!texture) {
  1672         return -1;
  1673     }
  1674     renderer = texture->renderer;
  1675     if (!renderer->QueryTexturePixels) {
  1676         SDL_Unsupported();
  1677         return -1;
  1678     }
  1679     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1680 }
  1681 
  1682 int
  1683 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1684                       int firstcolor, int ncolors)
  1685 {
  1686     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1687     SDL_Renderer *renderer;
  1688 
  1689     if (!texture) {
  1690         return -1;
  1691     }
  1692     renderer = texture->renderer;
  1693     if (!renderer->SetTexturePalette) {
  1694         SDL_Unsupported();
  1695         return -1;
  1696     }
  1697     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1698                                        ncolors);
  1699 }
  1700 
  1701 int
  1702 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1703                       int firstcolor, int ncolors)
  1704 {
  1705     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1706     SDL_Renderer *renderer;
  1707 
  1708     if (!texture) {
  1709         return -1;
  1710     }
  1711     renderer = texture->renderer;
  1712     if (!renderer->GetTexturePalette) {
  1713         SDL_Unsupported();
  1714         return -1;
  1715     }
  1716     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1717                                        ncolors);
  1718 }
  1719 
  1720 int
  1721 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  1722 {
  1723     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1724     SDL_Renderer *renderer;
  1725 
  1726     if (!texture) {
  1727         return -1;
  1728     }
  1729     renderer = texture->renderer;
  1730     if (!renderer->SetTextureColorMod) {
  1731         SDL_Unsupported();
  1732         return -1;
  1733     }
  1734     if (r < 255 || g < 255 || b < 255) {
  1735         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1736     } else {
  1737         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1738     }
  1739     texture->r = r;
  1740     texture->g = g;
  1741     texture->b = b;
  1742     return renderer->SetTextureColorMod(renderer, texture);
  1743 }
  1744 
  1745 int
  1746 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  1747                        Uint8 * b)
  1748 {
  1749     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1750     SDL_Renderer *renderer;
  1751 
  1752     if (!texture) {
  1753         return -1;
  1754     }
  1755     renderer = texture->renderer;
  1756     if (r) {
  1757         *r = texture->r;
  1758     }
  1759     if (g) {
  1760         *g = texture->g;
  1761     }
  1762     if (b) {
  1763         *b = texture->b;
  1764     }
  1765     return 0;
  1766 }
  1767 
  1768 int
  1769 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  1770 {
  1771     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1772     SDL_Renderer *renderer;
  1773 
  1774     if (!texture) {
  1775         return -1;
  1776     }
  1777     renderer = texture->renderer;
  1778     if (!renderer->SetTextureAlphaMod) {
  1779         SDL_Unsupported();
  1780         return -1;
  1781     }
  1782     if (alpha < 255) {
  1783         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1784     } else {
  1785         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1786     }
  1787     texture->a = alpha;
  1788     return renderer->SetTextureAlphaMod(renderer, texture);
  1789 }
  1790 
  1791 int
  1792 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  1793 {
  1794     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1795 
  1796     if (!texture) {
  1797         return -1;
  1798     }
  1799     if (alpha) {
  1800         *alpha = texture->a;
  1801     }
  1802     return 0;
  1803 }
  1804 
  1805 int
  1806 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  1807 {
  1808     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1809     SDL_Renderer *renderer;
  1810 
  1811     if (!texture) {
  1812         return -1;
  1813     }
  1814     renderer = texture->renderer;
  1815     if (!renderer->SetTextureBlendMode) {
  1816         SDL_Unsupported();
  1817         return -1;
  1818     }
  1819     texture->blendMode = blendMode;
  1820     return renderer->SetTextureBlendMode(renderer, texture);
  1821 }
  1822 
  1823 int
  1824 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  1825 {
  1826     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1827 
  1828     if (!texture) {
  1829         return -1;
  1830     }
  1831     if (blendMode) {
  1832         *blendMode = texture->blendMode;
  1833     }
  1834     return 0;
  1835 }
  1836 
  1837 int
  1838 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  1839 {
  1840     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1841     SDL_Renderer *renderer;
  1842 
  1843     if (!texture) {
  1844         return -1;
  1845     }
  1846     renderer = texture->renderer;
  1847     if (!renderer->SetTextureScaleMode) {
  1848         SDL_Unsupported();
  1849         return -1;
  1850     }
  1851     texture->scaleMode = scaleMode;
  1852     return renderer->SetTextureScaleMode(renderer, texture);
  1853 }
  1854 
  1855 int
  1856 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  1857 {
  1858     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1859 
  1860     if (!texture) {
  1861         return -1;
  1862     }
  1863     if (scaleMode) {
  1864         *scaleMode = texture->scaleMode;
  1865     }
  1866     return 0;
  1867 }
  1868 
  1869 int
  1870 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1871                   const void *pixels, int pitch)
  1872 {
  1873     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1874     SDL_Renderer *renderer;
  1875     SDL_Rect full_rect;
  1876 
  1877     if (!texture) {
  1878         return -1;
  1879     }
  1880     renderer = texture->renderer;
  1881     if (!renderer->UpdateTexture) {
  1882         SDL_Unsupported();
  1883         return -1;
  1884     }
  1885     if (!rect) {
  1886         full_rect.x = 0;
  1887         full_rect.y = 0;
  1888         full_rect.w = texture->w;
  1889         full_rect.h = texture->h;
  1890         rect = &full_rect;
  1891     }
  1892     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1893 }
  1894 
  1895 int
  1896 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1897                 void **pixels, int *pitch)
  1898 {
  1899     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1900     SDL_Renderer *renderer;
  1901     SDL_Rect full_rect;
  1902 
  1903     if (!texture) {
  1904         return -1;
  1905     }
  1906     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1907         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1908         return -1;
  1909     }
  1910     renderer = texture->renderer;
  1911     if (!renderer->LockTexture) {
  1912         SDL_Unsupported();
  1913         return -1;
  1914     }
  1915     if (!rect) {
  1916         full_rect.x = 0;
  1917         full_rect.y = 0;
  1918         full_rect.w = texture->w;
  1919         full_rect.h = texture->h;
  1920         rect = &full_rect;
  1921     }
  1922     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1923                                  pitch);
  1924 }
  1925 
  1926 void
  1927 SDL_UnlockTexture(SDL_TextureID textureID)
  1928 {
  1929     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1930     SDL_Renderer *renderer;
  1931 
  1932     if (!texture) {
  1933         return;
  1934     }
  1935     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1936         return;
  1937     }
  1938     renderer = texture->renderer;
  1939     if (!renderer->UnlockTexture) {
  1940         return;
  1941     }
  1942     renderer->UnlockTexture(renderer, texture);
  1943 }
  1944 
  1945 void
  1946 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1947                  const SDL_Rect * rects)
  1948 {
  1949     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1950     SDL_Renderer *renderer;
  1951 
  1952     if (!texture) {
  1953         return;
  1954     }
  1955     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1956         return;
  1957     }
  1958     renderer = texture->renderer;
  1959     if (!renderer->DirtyTexture) {
  1960         return;
  1961     }
  1962     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1963 }
  1964 
  1965 int
  1966 SDL_RenderFill(Uint8 r, Uint8 g, Uint8 b, Uint8 a, const SDL_Rect * rect)
  1967 {
  1968     SDL_Renderer *renderer;
  1969     SDL_Window *window;
  1970     SDL_Rect real_rect;
  1971 
  1972     if (!_this) {
  1973         SDL_UninitializedVideo();
  1974         return -1;
  1975     }
  1976     renderer = SDL_CurrentDisplay.current_renderer;
  1977     if (!renderer) {
  1978         return -1;
  1979     }
  1980     if(!renderer->RenderFill) {
  1981         SDL_Unsupported();
  1982         return -1;
  1983     }
  1984     window = SDL_GetWindowFromID(renderer->window);
  1985     real_rect.x = 0;
  1986     real_rect.y = 0;
  1987     real_rect.w = window->w;
  1988     real_rect.h = window->h;
  1989     if (rect) {
  1990         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1991             return 0;
  1992         }
  1993     }
  1994     return renderer->RenderFill(renderer, r, g, b, a, &real_rect);
  1995 }
  1996 
  1997 int
  1998 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1999                const SDL_Rect * dstrect)
  2000 {
  2001     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2002     SDL_Renderer *renderer;
  2003     SDL_Window *window;
  2004     SDL_Rect real_srcrect;
  2005     SDL_Rect real_dstrect;
  2006 
  2007     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  2008         return -1;
  2009     }
  2010     renderer = SDL_CurrentDisplay.current_renderer;
  2011     if (!renderer) {
  2012         return -1;
  2013     }
  2014     if (!renderer->RenderCopy) {
  2015         SDL_Unsupported();
  2016         return -1;
  2017     }
  2018     window = SDL_GetWindowFromID(renderer->window);
  2019     if (srcrect) {
  2020         real_srcrect = *srcrect;
  2021     } else {
  2022         real_srcrect.x = 0;
  2023         real_srcrect.y = 0;
  2024         real_srcrect.w = texture->w;
  2025         real_srcrect.h = texture->h;
  2026     }
  2027     if (dstrect) {
  2028         real_dstrect = *dstrect;
  2029     } else {
  2030         real_dstrect.x = 0;
  2031         real_dstrect.y = 0;
  2032         real_dstrect.w = window->w;
  2033         real_dstrect.h = window->h;
  2034     }
  2035 
  2036     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2037                                 &real_dstrect);
  2038 }
  2039 
  2040 void
  2041 SDL_RenderPresent(void)
  2042 {
  2043     SDL_Renderer *renderer;
  2044 
  2045     if (!_this) {
  2046         SDL_UninitializedVideo();
  2047         return;
  2048     }
  2049     renderer = SDL_CurrentDisplay.current_renderer;
  2050     if (!renderer || !renderer->RenderPresent) {
  2051         return;
  2052     }
  2053     renderer->RenderPresent(renderer);
  2054 }
  2055 
  2056 void
  2057 SDL_DestroyTexture(SDL_TextureID textureID)
  2058 {
  2059     int hash;
  2060     SDL_Texture *prev, *texture;
  2061     SDL_Renderer *renderer;
  2062 
  2063     if (!_this) {
  2064         SDL_UninitializedVideo();
  2065         return;
  2066     }
  2067     /* Look up the texture in the hash table */
  2068     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2069     prev = NULL;
  2070     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2071          prev = texture, texture = texture->next) {
  2072         if (texture->id == textureID) {
  2073             break;
  2074         }
  2075     }
  2076     if (!texture) {
  2077         return;
  2078     }
  2079     /* Unlink the texture from the list */
  2080     if (prev) {
  2081         prev->next = texture->next;
  2082     } else {
  2083         SDL_CurrentDisplay.textures[hash] = texture->next;
  2084     }
  2085 
  2086     /* Free the texture */
  2087     renderer = texture->renderer;
  2088     renderer->DestroyTexture(renderer, texture);
  2089     SDL_free(texture);
  2090 }
  2091 
  2092 void
  2093 SDL_DestroyRenderer(SDL_WindowID windowID)
  2094 {
  2095     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2096     SDL_Renderer *renderer;
  2097     int i;
  2098 
  2099     if (!window) {
  2100         return;
  2101     }
  2102     renderer = window->renderer;
  2103     if (!renderer) {
  2104         return;
  2105     }
  2106     /* Free existing textures for this renderer */
  2107     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2108         SDL_Texture *texture;
  2109         SDL_Texture *prev = NULL;
  2110         SDL_Texture *next;
  2111         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2112              texture = next) {
  2113             next = texture->next;
  2114             if (texture->renderer == renderer) {
  2115                 if (prev) {
  2116                     prev->next = next;
  2117                 } else {
  2118                     SDL_CurrentDisplay.textures[i] = next;
  2119                 }
  2120                 renderer->DestroyTexture(renderer, texture);
  2121                 SDL_free(texture);
  2122             } else {
  2123                 prev = texture;
  2124             }
  2125         }
  2126     }
  2127 
  2128     /* Free the renderer instance */
  2129     renderer->DestroyRenderer(renderer);
  2130 
  2131     /* Clear references */
  2132     window->renderer = NULL;
  2133     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2134         SDL_CurrentDisplay.current_renderer = NULL;
  2135     }
  2136 }
  2137 
  2138 void
  2139 SDL_VideoQuit(void)
  2140 {
  2141     int i, j;
  2142 
  2143     if (!_this) {
  2144         return;
  2145     }
  2146     /* Halt event processing before doing anything else */
  2147     SDL_StopEventLoop();
  2148 
  2149     /* Clean up the system video */
  2150     for (i = _this->num_displays; i--;) {
  2151         SDL_VideoDisplay *display = &_this->displays[i];
  2152         for (j = display->num_windows; j--;) {
  2153             SDL_DestroyWindow(display->windows[i].id);
  2154         }
  2155         if (display->windows) {
  2156             SDL_free(display->windows);
  2157             display->windows = NULL;
  2158         }
  2159         display->num_windows = 0;
  2160         if (display->render_drivers) {
  2161             SDL_free(display->render_drivers);
  2162             display->render_drivers = NULL;
  2163         }
  2164         display->num_render_drivers = 0;
  2165     }
  2166     _this->VideoQuit(_this);
  2167 
  2168     for (i = _this->num_displays; i--;) {
  2169         SDL_VideoDisplay *display = &_this->displays[i];
  2170         for (j = display->num_display_modes; j--;) {
  2171             if (display->display_modes[j].driverdata) {
  2172                 SDL_free(display->display_modes[j].driverdata);
  2173                 display->display_modes[j].driverdata = NULL;
  2174             }
  2175         }
  2176         if (display->display_modes) {
  2177             SDL_free(display->display_modes);
  2178             display->display_modes = NULL;
  2179         }
  2180         if (display->desktop_mode.driverdata) {
  2181             SDL_free(display->desktop_mode.driverdata);
  2182             display->desktop_mode.driverdata = NULL;
  2183         }
  2184         if (display->palette) {
  2185             SDL_FreePalette(display->palette);
  2186             display->palette = NULL;
  2187         }
  2188         if (display->gamma) {
  2189             SDL_free(display->gamma);
  2190             display->gamma = NULL;
  2191         }
  2192         if (display->driverdata) {
  2193             SDL_free(display->driverdata);
  2194             display->driverdata = NULL;
  2195         }
  2196     }
  2197     if (_this->displays) {
  2198         SDL_free(_this->displays);
  2199         _this->displays = NULL;
  2200     }
  2201     _this->free(_this);
  2202     _this = NULL;
  2203 }
  2204 
  2205 int
  2206 SDL_GL_LoadLibrary(const char *path)
  2207 {
  2208     int retval;
  2209 
  2210     if (!_this) {
  2211         SDL_UninitializedVideo();
  2212         return -1;
  2213     }
  2214     if (_this->GL_LoadLibrary) {
  2215         retval = _this->GL_LoadLibrary(_this, path);
  2216     } else {
  2217         SDL_SetError("No dynamic GL support in video driver");
  2218         retval = -1;
  2219     }
  2220     return (retval);
  2221 }
  2222 
  2223 void *
  2224 SDL_GL_GetProcAddress(const char *proc)
  2225 {
  2226     void *func;
  2227 
  2228     if (!_this) {
  2229         SDL_UninitializedVideo();
  2230         return NULL;
  2231     }
  2232     func = NULL;
  2233     if (_this->GL_GetProcAddress) {
  2234         if (_this->gl_config.driver_loaded) {
  2235             func = _this->GL_GetProcAddress(_this, proc);
  2236         } else {
  2237             SDL_SetError("No GL driver has been loaded");
  2238         }
  2239     } else {
  2240         SDL_SetError("No dynamic GL support in video driver");
  2241     }
  2242     return func;
  2243 }
  2244 
  2245 SDL_bool
  2246 SDL_GL_ExtensionSupported(const char *extension)
  2247 {
  2248 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2249     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2250     const char *extensions;
  2251     const char *start;
  2252     const char *where, *terminator;
  2253 
  2254     /* Extension names should not have spaces. */
  2255     where = SDL_strchr(extension, ' ');
  2256     if (where || *extension == '\0') {
  2257         return SDL_FALSE;
  2258     }
  2259     /* See if there's an environment variable override */
  2260     start = SDL_getenv(extension);
  2261     if (start && *start == '0') {
  2262         return SDL_FALSE;
  2263     }
  2264     /* Lookup the available extensions */
  2265     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2266     if (glGetStringFunc) {
  2267         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2268     } else {
  2269         extensions = NULL;
  2270     }
  2271     if (!extensions) {
  2272         return SDL_FALSE;
  2273     }
  2274     /*
  2275      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2276      * extensions string. Don't be fooled by sub-strings, etc.
  2277      */
  2278 
  2279     start = extensions;
  2280 
  2281     for (;;) {
  2282         where = SDL_strstr(start, extension);
  2283         if (!where)
  2284             break;
  2285 
  2286         terminator = where + SDL_strlen(extension);
  2287         if (where == start || *(where - 1) == ' ')
  2288             if (*terminator == ' ' || *terminator == '\0')
  2289                 return SDL_TRUE;
  2290 
  2291         start = terminator;
  2292     }
  2293     return SDL_FALSE;
  2294 #else
  2295     return SDL_FALSE;
  2296 #endif
  2297 }
  2298 
  2299 int
  2300 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2301 {
  2302 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2303     int retval;
  2304 
  2305     if (!_this) {
  2306         SDL_UninitializedVideo();
  2307         return -1;
  2308     }
  2309     retval = 0;
  2310     switch (attr) {
  2311     case SDL_GL_RED_SIZE:
  2312         _this->gl_config.red_size = value;
  2313         break;
  2314     case SDL_GL_GREEN_SIZE:
  2315         _this->gl_config.green_size = value;
  2316         break;
  2317     case SDL_GL_BLUE_SIZE:
  2318         _this->gl_config.blue_size = value;
  2319         break;
  2320     case SDL_GL_ALPHA_SIZE:
  2321         _this->gl_config.alpha_size = value;
  2322         break;
  2323     case SDL_GL_DOUBLEBUFFER:
  2324         _this->gl_config.double_buffer = value;
  2325         break;
  2326     case SDL_GL_BUFFER_SIZE:
  2327         _this->gl_config.buffer_size = value;
  2328         break;
  2329     case SDL_GL_DEPTH_SIZE:
  2330         _this->gl_config.depth_size = value;
  2331         break;
  2332     case SDL_GL_STENCIL_SIZE:
  2333         _this->gl_config.stencil_size = value;
  2334         break;
  2335     case SDL_GL_ACCUM_RED_SIZE:
  2336         _this->gl_config.accum_red_size = value;
  2337         break;
  2338     case SDL_GL_ACCUM_GREEN_SIZE:
  2339         _this->gl_config.accum_green_size = value;
  2340         break;
  2341     case SDL_GL_ACCUM_BLUE_SIZE:
  2342         _this->gl_config.accum_blue_size = value;
  2343         break;
  2344     case SDL_GL_ACCUM_ALPHA_SIZE:
  2345         _this->gl_config.accum_alpha_size = value;
  2346         break;
  2347     case SDL_GL_STEREO:
  2348         _this->gl_config.stereo = value;
  2349         break;
  2350     case SDL_GL_MULTISAMPLEBUFFERS:
  2351         _this->gl_config.multisamplebuffers = value;
  2352         break;
  2353     case SDL_GL_MULTISAMPLESAMPLES:
  2354         _this->gl_config.multisamplesamples = value;
  2355         break;
  2356     case SDL_GL_ACCELERATED_VISUAL:
  2357         _this->gl_config.accelerated = value;
  2358         break;
  2359     case SDL_GL_RETAINED_BACKING:
  2360         _this->gl_config.retained_backing = value;
  2361         break;
  2362     default:
  2363         SDL_SetError("Unknown OpenGL attribute");
  2364         retval = -1;
  2365         break;
  2366     }
  2367     return retval;
  2368 #else
  2369     SDL_Unsupported();
  2370     return -1;
  2371 #endif /* SDL_VIDEO_OPENGL */
  2372 }
  2373 
  2374 int
  2375 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2376 {
  2377 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2378     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2379     GLenum attrib = 0;
  2380 
  2381     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2382     if (!glGetIntegervFunc) {
  2383         return -1;
  2384     }
  2385     switch (attr) {
  2386     case SDL_GL_RETAINED_BACKING:
  2387         *value = _this->gl_config.retained_backing;
  2388         return 0;
  2389     case SDL_GL_RED_SIZE:
  2390         attrib = GL_RED_BITS;
  2391         break;
  2392     case SDL_GL_BLUE_SIZE:
  2393         attrib = GL_BLUE_BITS;
  2394         break;
  2395     case SDL_GL_GREEN_SIZE:
  2396         attrib = GL_GREEN_BITS;
  2397         break;
  2398     case SDL_GL_ALPHA_SIZE:
  2399         attrib = GL_ALPHA_BITS;
  2400         break;
  2401     case SDL_GL_DOUBLEBUFFER:
  2402 #ifndef SDL_VIDEO_OPENGL_ES
  2403         attrib = GL_DOUBLEBUFFER;
  2404         break;
  2405 #else
  2406         /*
  2407          * I believe double buffering is the only option in OpenGL ES
  2408          * -- in any case, GL_DOUBLEBUFFER doesn't exist
  2409          */
  2410         *value = 1;
  2411         return 0;
  2412 #endif
  2413     case SDL_GL_DEPTH_SIZE:
  2414         attrib = GL_DEPTH_BITS;
  2415         break;
  2416     case SDL_GL_STENCIL_SIZE:
  2417         attrib = GL_STENCIL_BITS;
  2418         break;
  2419 #ifndef SDL_VIDEO_OPENGL_ES
  2420     case SDL_GL_ACCUM_RED_SIZE:
  2421         attrib = GL_ACCUM_RED_BITS;
  2422         break;
  2423     case SDL_GL_ACCUM_GREEN_SIZE:
  2424         attrib = GL_ACCUM_GREEN_BITS;
  2425         break;
  2426     case SDL_GL_ACCUM_BLUE_SIZE:
  2427         attrib = GL_ACCUM_BLUE_BITS;
  2428         break;
  2429     case SDL_GL_ACCUM_ALPHA_SIZE:
  2430         attrib = GL_ACCUM_ALPHA_BITS;
  2431         break;
  2432     case SDL_GL_STEREO:
  2433         attrib = GL_STEREO;
  2434         break;
  2435 #else
  2436     case SDL_GL_ACCUM_RED_SIZE:
  2437     case SDL_GL_ACCUM_GREEN_SIZE:
  2438     case SDL_GL_ACCUM_BLUE_SIZE:
  2439     case SDL_GL_ACCUM_ALPHA_SIZE:
  2440     case SDL_GL_STEREO:
  2441         /* none of these are supported in OpenGL ES */
  2442         *value = 0;
  2443         return 0;
  2444 #endif
  2445     case SDL_GL_MULTISAMPLEBUFFERS:
  2446 #ifndef SDL_VIDEO_OPENGL_ES
  2447         attrib = GL_SAMPLE_BUFFERS_ARB;
  2448 #else
  2449         attrib = GL_SAMPLE_BUFFERS;
  2450 #endif
  2451         break;
  2452     case SDL_GL_MULTISAMPLESAMPLES:
  2453 #ifndef SDL_VIDEO_OPENGL_ES
  2454         attrib = GL_SAMPLES_ARB;
  2455 #else
  2456         attrib = GL_SAMPLES;
  2457 #endif
  2458         break;
  2459     case SDL_GL_BUFFER_SIZE:
  2460         {
  2461             GLint bits = 0;
  2462             GLint component;
  2463 
  2464             /*
  2465              * there doesn't seem to be a single flag in OpenGL
  2466              * for this!
  2467              */
  2468             glGetIntegervFunc(GL_RED_BITS, &component);
  2469             bits += component;
  2470             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2471             bits += component;
  2472             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2473             bits += component;
  2474             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2475             bits += component;
  2476 
  2477             *value = bits;
  2478             return 0;
  2479         }
  2480     case SDL_GL_ACCELERATED_VISUAL:
  2481         {
  2482             /* FIXME: How do we get this information? */
  2483             *value = (_this->gl_config.accelerated != 0);
  2484             return 0;
  2485         }
  2486     default:
  2487         SDL_SetError("Unknown OpenGL attribute");
  2488         return -1;
  2489     }
  2490 
  2491     glGetIntegervFunc(attrib, (GLint *) value);
  2492     return 0;
  2493 #else
  2494     SDL_Unsupported();
  2495     return -1;
  2496 #endif /* SDL_VIDEO_OPENGL */
  2497 }
  2498 
  2499 SDL_GLContext
  2500 SDL_GL_CreateContext(SDL_WindowID windowID)
  2501 {
  2502     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2503 
  2504     if (!window) {
  2505         return NULL;
  2506     }
  2507     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2508         SDL_SetError("The specified window isn't an OpenGL window");
  2509         return NULL;
  2510     }
  2511     return _this->GL_CreateContext(_this, window);
  2512 }
  2513 
  2514 int
  2515 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2516 {
  2517     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2518 
  2519     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2520         SDL_SetError("The specified window isn't an OpenGL window");
  2521         return -1;
  2522     }
  2523     if (!context) {
  2524         window = NULL;
  2525     }
  2526     return _this->GL_MakeCurrent(_this, window, context);
  2527 }
  2528 
  2529 int
  2530 SDL_GL_SetSwapInterval(int interval)
  2531 {
  2532     if (!_this) {
  2533         SDL_UninitializedVideo();
  2534         return -1;
  2535     }
  2536     if (_this->GL_SetSwapInterval) {
  2537         return _this->GL_SetSwapInterval(_this, interval);
  2538     } else {
  2539         SDL_SetError("Setting the swap interval is not supported");
  2540         return -1;
  2541     }
  2542 }
  2543 
  2544 int
  2545 SDL_GL_GetSwapInterval(void)
  2546 {
  2547     if (!_this) {
  2548         SDL_UninitializedVideo();
  2549         return -1;
  2550     }
  2551     if (_this->GL_GetSwapInterval) {
  2552         return _this->GL_GetSwapInterval(_this);
  2553     } else {
  2554         SDL_SetError("Getting the swap interval is not supported");
  2555         return -1;
  2556     }
  2557 }
  2558 
  2559 void
  2560 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2561 {
  2562     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2563 
  2564     if (!window) {
  2565         return;
  2566     }
  2567     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2568         SDL_SetError("The specified window isn't an OpenGL window");
  2569         return;
  2570     }
  2571     _this->GL_SwapWindow(_this, window);
  2572 }
  2573 
  2574 void
  2575 SDL_GL_DeleteContext(SDL_GLContext context)
  2576 {
  2577     if (!_this || !context) {
  2578         return;
  2579     }
  2580     _this->GL_MakeCurrent(_this, NULL, NULL);
  2581     _this->GL_DeleteContext(_this, context);
  2582 }
  2583 
  2584 #if 0                           // FIXME
  2585 /*
  2586  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  2587  * & 2 for alpha channel.
  2588  */
  2589 static void
  2590 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2591 {
  2592     int x, y;
  2593     Uint32 colorkey;
  2594 #define SET_MASKBIT(icon, x, y, mask) \
  2595 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2596 
  2597     colorkey = icon->format->colorkey;
  2598     switch (icon->format->BytesPerPixel) {
  2599     case 1:
  2600         {
  2601             Uint8 *pixels;
  2602             for (y = 0; y < icon->h; ++y) {
  2603                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2604                 for (x = 0; x < icon->w; ++x) {
  2605                     if (*pixels++ == colorkey) {
  2606                         SET_MASKBIT(icon, x, y, mask);
  2607                     }
  2608                 }
  2609             }
  2610         }
  2611         break;
  2612 
  2613     case 2:
  2614         {
  2615             Uint16 *pixels;
  2616             for (y = 0; y < icon->h; ++y) {
  2617                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2618                 for (x = 0; x < icon->w; ++x) {
  2619                     if ((flags & 1) && *pixels == colorkey) {
  2620                         SET_MASKBIT(icon, x, y, mask);
  2621                     } else if ((flags & 2)
  2622                                && (*pixels & icon->format->Amask) == 0) {
  2623                         SET_MASKBIT(icon, x, y, mask);
  2624                     }
  2625                     pixels++;
  2626                 }
  2627             }
  2628         }
  2629         break;
  2630 
  2631     case 4:
  2632         {
  2633             Uint32 *pixels;
  2634             for (y = 0; y < icon->h; ++y) {
  2635                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2636                 for (x = 0; x < icon->w; ++x) {
  2637                     if ((flags & 1) && *pixels == colorkey) {
  2638                         SET_MASKBIT(icon, x, y, mask);
  2639                     } else if ((flags & 2)
  2640                                && (*pixels & icon->format->Amask) == 0) {
  2641                         SET_MASKBIT(icon, x, y, mask);
  2642                     }
  2643                     pixels++;
  2644                 }
  2645             }
  2646         }
  2647         break;
  2648     }
  2649 }
  2650 
  2651 /*
  2652  * Sets the window manager icon for the display window.
  2653  */
  2654 void
  2655 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2656 {
  2657     if (icon && _this->SetIcon) {
  2658         /* Generate a mask if necessary, and create the icon! */
  2659         if (mask == NULL) {
  2660             int mask_len = icon->h * (icon->w + 7) / 8;
  2661             int flags = 0;
  2662             mask = (Uint8 *) SDL_malloc(mask_len);
  2663             if (mask == NULL) {
  2664                 return;
  2665             }
  2666             SDL_memset(mask, ~0, mask_len);
  2667             if (icon->flags & SDL_SRCCOLORKEY)
  2668                 flags |= 1;
  2669             if (icon->flags & SDL_SRCALPHA)
  2670                 flags |= 2;
  2671             if (flags) {
  2672                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2673             }
  2674             _this->SetIcon(_this, icon, mask);
  2675             SDL_free(mask);
  2676         } else {
  2677             _this->SetIcon(_this, icon, mask);
  2678         }
  2679     }
  2680 }
  2681 #endif
  2682 
  2683 SDL_bool
  2684 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2685 {
  2686     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2687 
  2688     if (!window || !_this->GetWindowWMInfo) {
  2689         return SDL_FALSE;
  2690     }
  2691     return (_this->GetWindowWMInfo(_this, window, info));
  2692 }
  2693 
  2694 /* vi: set ts=4 sw=4 expandtab: */