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