src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 22 Jan 2010 16:17:11 +0000
changeset 3693 d3f330fc2009
parent 3692 ad85eb21a559
child 3694 b0a707f589a6
permissions -rw-r--r--
update window prev links
     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_video.h"
    28 #include "SDL_sysvideo.h"
    29 #include "SDL_blit.h"
    30 #include "SDL_pixels_c.h"
    31 #include "SDL_renderer_gl.h"
    32 #include "SDL_renderer_gles.h"
    33 #include "SDL_renderer_sw.h"
    34 #include "../events/SDL_sysevents.h"
    35 #include "../events/SDL_events_c.h"
    36 
    37 #if SDL_VIDEO_OPENGL_ES
    38 #include "SDL_opengles.h"
    39 #endif /* SDL_VIDEO_OPENGL_ES */
    40 
    41 #if SDL_VIDEO_OPENGL
    42 #include "SDL_opengl.h"
    43 
    44 /* On Windows, windows.h defines CreateWindow */
    45 #ifdef CreateWindow
    46 #undef CreateWindow
    47 #endif
    48 #endif /* SDL_VIDEO_OPENGL */
    49 
    50 /* Available video drivers */
    51 static VideoBootStrap *bootstrap[] = {
    52 #if SDL_VIDEO_DRIVER_COCOA
    53     &COCOA_bootstrap,
    54 #endif
    55 #if SDL_VIDEO_DRIVER_X11
    56     &X11_bootstrap,
    57 #endif
    58 #if SDL_VIDEO_DRIVER_FBCON
    59     &FBCON_bootstrap,
    60 #endif
    61 #if SDL_VIDEO_DRIVER_DIRECTFB
    62     &DirectFB_bootstrap,
    63 #endif
    64 #if SDL_VIDEO_DRIVER_PS3
    65     &PS3_bootstrap,
    66 #endif
    67 #if SDL_VIDEO_DRIVER_SVGALIB
    68     &SVGALIB_bootstrap,
    69 #endif
    70 #if SDL_VIDEO_DRIVER_GAPI
    71     &GAPI_bootstrap,
    72 #endif
    73 #if SDL_VIDEO_DRIVER_WIN32
    74     &WIN32_bootstrap,
    75 #endif
    76 #if SDL_VIDEO_DRIVER_BWINDOW
    77     &BWINDOW_bootstrap,
    78 #endif
    79 #if SDL_VIDEO_DRIVER_PHOTON
    80     &photon_bootstrap,
    81 #endif
    82 #if SDL_VIDEO_DRIVER_QNXGF
    83     &qnxgf_bootstrap,
    84 #endif
    85 #if SDL_VIDEO_DRIVER_EPOC
    86     &EPOC_bootstrap,
    87 #endif
    88 #if SDL_VIDEO_DRIVER_RISCOS
    89     &RISCOS_bootstrap,
    90 #endif
    91 #if SDL_VIDEO_DRIVER_NDS
    92     &NDS_bootstrap,
    93 #endif
    94 #if SDL_VIDEO_DRIVER_UIKIT
    95     &UIKIT_bootstrap,
    96 #endif
    97 #if SDL_VIDEO_DRIVER_DUMMY
    98     &DUMMY_bootstrap,
    99 #endif
   100 #if SDL_VIDEO_DRIVER_PANDORA
   101     &PND_bootstrap,
   102 #endif
   103     NULL
   104 };
   105 
   106 static SDL_VideoDevice *_this = NULL;
   107 
   108 /* Various local functions */
   109 static void SDL_UpdateWindowGrab(SDL_Window * window);
   110 
   111 static int
   112 cmpmodes(const void *A, const void *B)
   113 {
   114     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   115     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   116 
   117     if (a.w != b.w) {
   118         return b.w - a.w;
   119     }
   120     if (a.h != b.h) {
   121         return b.h - a.h;
   122     }
   123     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   124         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   125     }
   126     if (SDL_PIXELLAYOUT(a.format) != SDL_PIXELLAYOUT(b.format)) {
   127         return SDL_PIXELLAYOUT(b.format) - SDL_PIXELLAYOUT(a.format);
   128     }
   129     if (a.refresh_rate != b.refresh_rate) {
   130         return b.refresh_rate - a.refresh_rate;
   131     }
   132     return 0;
   133 }
   134 
   135 static void
   136 SDL_UninitializedVideo()
   137 {
   138     SDL_SetError("Video subsystem has not been initialized");
   139 }
   140 
   141 int
   142 SDL_GetNumVideoDrivers(void)
   143 {
   144     return SDL_arraysize(bootstrap) - 1;
   145 }
   146 
   147 const char *
   148 SDL_GetVideoDriver(int index)
   149 {
   150     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   151         return bootstrap[index]->name;
   152     }
   153     return NULL;
   154 }
   155 
   156 /*
   157  * Initialize the video and event subsystems -- determine native pixel format
   158  */
   159 int
   160 SDL_VideoInit(const char *driver_name, Uint32 flags)
   161 {
   162     SDL_VideoDevice *video;
   163     int index;
   164     int i;
   165 
   166     /* Toggle the event thread flags, based on OS requirements */
   167 #if defined(MUST_THREAD_EVENTS)
   168     flags |= SDL_INIT_EVENTTHREAD;
   169 #elif defined(CANT_THREAD_EVENTS)
   170     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   171         SDL_SetError("OS doesn't support threaded events");
   172         return -1;
   173     }
   174 #endif
   175 
   176     /* Start the event loop */
   177     if (SDL_StartEventLoop(flags) < 0) {
   178         return -1;
   179     }
   180     /* Check to make sure we don't overwrite '_this' */
   181     if (_this != NULL) {
   182         SDL_VideoQuit();
   183     }
   184     /* Select the proper video driver */
   185     index = 0;
   186     video = NULL;
   187     if (driver_name == NULL) {
   188         driver_name = SDL_getenv("SDL_VIDEODRIVER");
   189     }
   190     if (driver_name != NULL) {
   191         for (i = 0; bootstrap[i]; ++i) {
   192             if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
   193                 video = bootstrap[i]->create(index);
   194                 break;
   195             }
   196         }
   197     } else {
   198         for (i = 0; bootstrap[i]; ++i) {
   199             if (bootstrap[i]->available()) {
   200                 video = bootstrap[i]->create(index);
   201                 if (video != NULL) {
   202                     break;
   203                 }
   204             }
   205         }
   206     }
   207     if (video == NULL) {
   208         if (driver_name) {
   209             SDL_SetError("%s not available", driver_name);
   210         } else {
   211             SDL_SetError("No available video device");
   212         }
   213         return -1;
   214     }
   215     _this = video;
   216     _this->name = bootstrap[i]->name;
   217     _this->next_object_id = 1;
   218 
   219 
   220     /* Set some very sane GL defaults */
   221     _this->gl_config.driver_loaded = 0;
   222     _this->gl_config.dll_handle = NULL;
   223     _this->gl_config.red_size = 3;
   224     _this->gl_config.green_size = 3;
   225     _this->gl_config.blue_size = 2;
   226     _this->gl_config.alpha_size = 0;
   227     _this->gl_config.buffer_size = 0;
   228     _this->gl_config.depth_size = 16;
   229     _this->gl_config.stencil_size = 0;
   230     _this->gl_config.double_buffer = 1;
   231     _this->gl_config.accum_red_size = 0;
   232     _this->gl_config.accum_green_size = 0;
   233     _this->gl_config.accum_blue_size = 0;
   234     _this->gl_config.accum_alpha_size = 0;
   235     _this->gl_config.stereo = 0;
   236     _this->gl_config.multisamplebuffers = 0;
   237     _this->gl_config.multisamplesamples = 0;
   238     _this->gl_config.retained_backing = 1;
   239     _this->gl_config.accelerated = -1;  /* accelerated or not, both are fine */
   240     _this->gl_config.major_version = 2;
   241     _this->gl_config.minor_version = 1;
   242 
   243     /* Initialize the video subsystem */
   244     if (_this->VideoInit(_this) < 0) {
   245         SDL_VideoQuit();
   246         return -1;
   247     }
   248     /* Make sure some displays were added */
   249     if (_this->num_displays == 0) {
   250         SDL_SetError("The video driver did not add any displays");
   251         SDL_VideoQuit();
   252         return (-1);
   253     }
   254     /* The software renderer is always available */
   255     for (i = 0; i < _this->num_displays; ++i) {
   256         SDL_VideoDisplay *display = &_this->displays[i];
   257         if (_this->GL_CreateContext) {
   258 #if SDL_VIDEO_RENDER_OGL
   259             SDL_AddRenderDriver(display, &GL_RenderDriver);
   260 #endif
   261 #if SDL_VIDEO_RENDER_OGL_ES
   262             SDL_AddRenderDriver(display, &GL_ES_RenderDriver);
   263 #endif
   264         }
   265         if (display->num_render_drivers > 0) {
   266             SDL_AddRenderDriver(display, &SW_RenderDriver);
   267         }
   268     }
   269 
   270     /* We're ready to go! */
   271     return 0;
   272 }
   273 
   274 const char *
   275 SDL_GetCurrentVideoDriver()
   276 {
   277     if (!_this) {
   278         SDL_UninitializedVideo();
   279         return NULL;
   280     }
   281     return _this->name;
   282 }
   283 
   284 SDL_VideoDevice *
   285 SDL_GetVideoDevice()
   286 {
   287     return _this;
   288 }
   289 
   290 int
   291 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   292 {
   293     SDL_VideoDisplay display;
   294 
   295     SDL_zero(display);
   296     if (desktop_mode) {
   297         display.desktop_mode = *desktop_mode;
   298     }
   299     display.current_mode = display.desktop_mode;
   300 
   301     return SDL_AddVideoDisplay(&display);
   302 }
   303 
   304 int
   305 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   306 {
   307     SDL_VideoDisplay *displays;
   308     int index = -1;
   309 
   310     displays =
   311         SDL_realloc(_this->displays,
   312                     (_this->num_displays + 1) * sizeof(*displays));
   313     if (displays) {
   314         index = _this->num_displays++;
   315         displays[index] = *display;
   316         displays[index].device = _this;
   317         _this->displays = displays;
   318     } else {
   319         SDL_OutOfMemory();
   320     }
   321     return index;
   322 }
   323 
   324 int
   325 SDL_GetNumVideoDisplays(void)
   326 {
   327     if (!_this) {
   328         SDL_UninitializedVideo();
   329         return 0;
   330     }
   331     return _this->num_displays;
   332 }
   333 
   334 int
   335 SDL_GetDisplayBounds(int index, SDL_Rect * rect)
   336 {
   337     if (!_this) {
   338         SDL_UninitializedVideo();
   339         return -1;
   340     }
   341     if (index < 0 || index >= _this->num_displays) {
   342         SDL_SetError("index must be in the range 0 - %d",
   343                      _this->num_displays - 1);
   344         return -1;
   345     }
   346     if (rect) {
   347         SDL_VideoDisplay *display = &_this->displays[index];
   348 
   349         if (_this->GetDisplayBounds) {
   350             if (_this->GetDisplayBounds(_this, display, rect) < 0) {
   351                 return -1;
   352             }
   353         } else {
   354             /* Assume that the displays are left to right */
   355             if (index == 0) {
   356                 rect->x = 0;
   357                 rect->y = 0;
   358             } else {
   359                 SDL_GetDisplayBounds(index-1, rect);
   360                 rect->x += rect->w;
   361             }
   362             rect->w = display->desktop_mode.w;
   363             rect->h = display->desktop_mode.h;
   364         }
   365     }
   366     return 0;
   367 }
   368 
   369 int
   370 SDL_SelectVideoDisplay(int index)
   371 {
   372     if (!_this) {
   373         SDL_UninitializedVideo();
   374         return (-1);
   375     }
   376     if (index < 0 || index >= _this->num_displays) {
   377         SDL_SetError("index must be in the range 0 - %d",
   378                      _this->num_displays - 1);
   379         return -1;
   380     }
   381     _this->current_display = index;
   382     return 0;
   383 }
   384 
   385 int
   386 SDL_GetCurrentVideoDisplay(void)
   387 {
   388     if (!_this) {
   389         SDL_UninitializedVideo();
   390         return (-1);
   391     }
   392     return _this->current_display;
   393 }
   394 
   395 SDL_bool
   396 SDL_AddDisplayMode(SDL_VideoDisplay * display,  const SDL_DisplayMode * mode)
   397 {
   398     SDL_DisplayMode *modes;
   399     int i, nmodes;
   400 
   401     /* Make sure we don't already have the mode in the list */
   402     modes = display->display_modes;
   403     nmodes = display->num_display_modes;
   404     for (i = nmodes; i--;) {
   405         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   406             return SDL_FALSE;
   407         }
   408     }
   409 
   410     /* Go ahead and add the new mode */
   411     if (nmodes == display->max_display_modes) {
   412         modes =
   413             SDL_realloc(modes,
   414                         (display->max_display_modes + 32) * sizeof(*modes));
   415         if (!modes) {
   416             return SDL_FALSE;
   417         }
   418         display->display_modes = modes;
   419         display->max_display_modes += 32;
   420     }
   421     modes[nmodes] = *mode;
   422     display->num_display_modes++;
   423 
   424     /* Re-sort video modes */
   425     SDL_qsort(display->display_modes, display->num_display_modes,
   426               sizeof(SDL_DisplayMode), cmpmodes);
   427 
   428     return SDL_TRUE;
   429 }
   430 
   431 int
   432 SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
   433 {
   434     if (!display->num_display_modes && _this->GetDisplayModes) {
   435         _this->GetDisplayModes(_this, display);
   436         SDL_qsort(display->display_modes, display->num_display_modes,
   437                   sizeof(SDL_DisplayMode), cmpmodes);
   438     }
   439     return display->num_display_modes;
   440 }
   441 
   442 int
   443 SDL_GetNumDisplayModes()
   444 {
   445     if (_this) {
   446         return SDL_GetNumDisplayModesForDisplay(SDL_CurrentDisplay);
   447     }
   448     return 0;
   449 }
   450 
   451 int
   452 SDL_GetDisplayModeForDisplay(SDL_VideoDisplay * display, int index, SDL_DisplayMode * mode)
   453 {
   454     if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
   455         SDL_SetError("index must be in the range of 0 - %d",
   456                      SDL_GetNumDisplayModesForDisplay(display) - 1);
   457         return -1;
   458     }
   459     if (mode) {
   460         *mode = display->display_modes[index];
   461     }
   462     return 0;
   463 }
   464 
   465 int
   466 SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
   467 {
   468     return SDL_GetDisplayModeForDisplay(SDL_CurrentDisplay, index, mode);
   469 }
   470 
   471 int
   472 SDL_GetDesktopDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   473 {
   474     if (mode) {
   475         *mode = display->desktop_mode;
   476     }
   477     return 0;
   478 }
   479 
   480 int
   481 SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
   482 {
   483     if (!_this) {
   484         SDL_UninitializedVideo();
   485         return -1;
   486     }
   487     return SDL_GetDesktopDisplayModeForDisplay(SDL_CurrentDisplay, mode);
   488 }
   489 
   490 int
   491 SDL_GetCurrentDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   492 {
   493     if (mode) {
   494         *mode = display->current_mode;
   495     }
   496     return 0;
   497 }
   498 
   499 int
   500 SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
   501 {
   502     if (!_this) {
   503         SDL_UninitializedVideo();
   504         return -1;
   505     }
   506     return SDL_GetCurrentDisplayModeForDisplay(SDL_CurrentDisplay, mode);
   507 }
   508 
   509 SDL_DisplayMode *
   510 SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
   511                                     const SDL_DisplayMode * mode,
   512                                     SDL_DisplayMode * closest)
   513 {
   514     Uint32 target_format;
   515     int target_refresh_rate;
   516     int i;
   517     SDL_DisplayMode *current, *match;
   518 
   519     if (!mode || !closest) {
   520         SDL_SetError("Missing desired mode or closest mode parameter");
   521         return NULL;
   522     }
   523 
   524     /* Default to the desktop format */
   525     if (mode->format) {
   526         target_format = mode->format;
   527     } else {
   528         target_format = display->desktop_mode.format;
   529     }
   530 
   531     /* Default to the desktop refresh rate */
   532     if (mode->refresh_rate) {
   533         target_refresh_rate = mode->refresh_rate;
   534     } else {
   535         target_refresh_rate = display->desktop_mode.refresh_rate;
   536     }
   537 
   538     match = NULL;
   539     for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
   540         current = &display->display_modes[i];
   541 
   542         if (current->w && (current->w < mode->w)) {
   543             /* Out of sorted modes large enough here */
   544             break;
   545         }
   546         if (current->h && (current->h < mode->h)) {
   547             if (current->w && (current->w == mode->w)) {
   548                 /* Out of sorted modes large enough here */
   549                 break;
   550             }
   551             /* Wider, but not tall enough, due to a different
   552                aspect ratio. This mode must be skipped, but closer
   553                modes may still follow. */
   554             continue;
   555         }
   556         if (!match || current->w < match->w || current->h < match->h) {
   557             match = current;
   558             continue;
   559         }
   560         if (current->format != match->format) {
   561             /* Sorted highest depth to lowest */
   562             if (current->format == target_format ||
   563                 (SDL_BITSPERPIXEL(current->format) >=
   564                  SDL_BITSPERPIXEL(target_format)
   565                  && SDL_PIXELTYPE(current->format) ==
   566                  SDL_PIXELTYPE(target_format))) {
   567                 match = current;
   568             }
   569             continue;
   570         }
   571         if (current->refresh_rate != match->refresh_rate) {
   572             /* Sorted highest refresh to lowest */
   573             if (current->refresh_rate >= target_refresh_rate) {
   574                 match = current;
   575             }
   576         }
   577     }
   578     if (match) {
   579         if (match->format) {
   580             closest->format = match->format;
   581         } else {
   582             closest->format = mode->format;
   583         }
   584         if (match->w && match->h) {
   585             closest->w = match->w;
   586             closest->h = match->h;
   587         } else {
   588             closest->w = mode->w;
   589             closest->h = mode->h;
   590         }
   591         if (match->refresh_rate) {
   592             closest->refresh_rate = match->refresh_rate;
   593         } else {
   594             closest->refresh_rate = mode->refresh_rate;
   595         }
   596         closest->driverdata = match->driverdata;
   597 
   598         /*
   599          * Pick some reasonable defaults if the app and driver don't
   600          * care
   601          */
   602         if (!closest->format) {
   603             closest->format = SDL_PIXELFORMAT_RGB888;
   604         }
   605         if (!closest->w) {
   606             closest->w = 640;
   607         }
   608         if (!closest->h) {
   609             closest->h = 480;
   610         }
   611         return closest;
   612     }
   613     return NULL;
   614 }
   615 
   616 SDL_DisplayMode *
   617 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   618                           SDL_DisplayMode * closest)
   619 {
   620     if (!_this) {
   621         SDL_UninitializedVideo();
   622         return NULL;
   623     }
   624     return SDL_GetClosestDisplayModeForDisplay(SDL_CurrentDisplay, mode, closest);
   625 }
   626 
   627 int
   628 SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
   629 {
   630     SDL_DisplayMode display_mode;
   631     SDL_DisplayMode current_mode;
   632     int ncolors;
   633 
   634     if (mode) {
   635         display_mode = *mode;
   636 
   637         /* Default to the current mode */
   638         if (!display_mode.format) {
   639             display_mode.format = display->current_mode.format;
   640         }
   641         if (!display_mode.w) {
   642             display_mode.w = display->current_mode.w;
   643         }
   644         if (!display_mode.h) {
   645             display_mode.h = display->current_mode.h;
   646         }
   647         if (!display_mode.refresh_rate) {
   648             display_mode.refresh_rate = display->current_mode.refresh_rate;
   649         }
   650 
   651         /* Get a good video mode, the closest one possible */
   652         if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
   653             SDL_SetError("No video mode large enough for %dx%d",
   654                          display_mode.w, display_mode.h);
   655             return -1;
   656         }
   657     } else {
   658         display_mode = display->desktop_mode;
   659     }
   660 
   661     /* See if there's anything left to do */
   662     SDL_GetCurrentDisplayModeForDisplay(display, &current_mode);
   663     if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
   664         return 0;
   665     }
   666 
   667     /* Actually change the display mode */
   668     if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
   669         return -1;
   670     }
   671     display->current_mode = display_mode;
   672 
   673     /* Set up a palette, if necessary */
   674     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   675         ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   676     } else {
   677         ncolors = 0;
   678     }
   679     if ((!ncolors && display->palette) || (ncolors && !display->palette)
   680         || (ncolors && ncolors != display->palette->ncolors)) {
   681         if (display->palette) {
   682             SDL_FreePalette(display->palette);
   683             display->palette = NULL;
   684         }
   685         if (ncolors) {
   686             display->palette = SDL_AllocPalette(ncolors);
   687             if (!display->palette) {
   688                 return -1;
   689             }
   690             SDL_DitherColors(display->palette->colors,
   691                              SDL_BITSPERPIXEL(display_mode.format));
   692         }
   693     }
   694 
   695     return 0;
   696 }
   697 
   698 int
   699 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   700 {
   701     if (!_this) {
   702         SDL_UninitializedVideo();
   703         return -1;
   704     }
   705     return SDL_SetDisplayModeForDisplay(SDL_CurrentDisplay, mode);
   706 }
   707 
   708 int
   709 SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
   710 {
   711     if (!window) {
   712         return -1;
   713     }
   714 
   715     if (mode) {
   716         window->fullscreen_mode = *mode;
   717     } else {
   718         SDL_zero(window->fullscreen_mode);
   719     }
   720     return 0;
   721 }
   722 
   723 int
   724 SDL_GetWindowDisplayMode(SDL_Window * window, SDL_DisplayMode * mode)
   725 {
   726     SDL_DisplayMode fullscreen_mode;
   727 
   728     if (!window) {
   729         return -1;
   730     }
   731 
   732     fullscreen_mode = window->fullscreen_mode;
   733     if (!fullscreen_mode.w) {
   734         fullscreen_mode.w = window->w;
   735     }
   736     if (!fullscreen_mode.h) {
   737         fullscreen_mode.h = window->h;
   738     }
   739 
   740     if (!SDL_GetClosestDisplayModeForDisplay(window->display,
   741                                              &fullscreen_mode,
   742                                              &fullscreen_mode)) {
   743         SDL_SetError("Couldn't find display mode match");
   744         return -1;
   745     }
   746 
   747     if (mode) {
   748         *mode = fullscreen_mode;
   749     }
   750     return 0;
   751 }
   752 
   753 static void
   754 SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool attempt)
   755 {
   756     SDL_VideoDisplay *display = window->display;
   757     int i;
   758 
   759     /* See if we're already processing a window */
   760     if (display->updating_fullscreen) {
   761         return;
   762     }
   763 
   764     display->updating_fullscreen = SDL_TRUE;
   765 
   766     /* See if we even want to do anything here */
   767     if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
   768         (window->flags & SDL_WINDOW_SHOWN)) {
   769         if (attempt) {
   770             /* We just gained some state, try to gain all states */
   771             if (window->flags & SDL_WINDOW_MINIMIZED) {
   772                 SDL_RestoreWindow(window);
   773             } else {
   774                 SDL_RaiseWindow(window);
   775             }
   776         } else {
   777             /* We just lost some state, try to release all states */
   778             SDL_MinimizeWindow(window);
   779         }
   780     }
   781 
   782     if (FULLSCREEN_VISIBLE(window)) {
   783         /* Hide any other fullscreen windows */
   784         for (i = 0; i < display->num_windows; ++i) {
   785             SDL_Window *other = &display->windows[i];
   786             if (other != window && FULLSCREEN_VISIBLE(other)) {
   787                 SDL_MinimizeWindow(other);
   788             }
   789         }
   790     }
   791 
   792     display->updating_fullscreen = SDL_FALSE;
   793 
   794     /* See if there are any fullscreen windows */
   795     for (i = 0; i < display->num_windows; ++i) {
   796         window = &display->windows[i];
   797         if (FULLSCREEN_VISIBLE(window)) {
   798             SDL_DisplayMode fullscreen_mode;
   799             if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
   800                 SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
   801                 display->fullscreen_window = window;
   802                 return;
   803             }
   804         }
   805     }
   806 
   807     /* Nope, restore the desktop mode */
   808     SDL_SetDisplayModeForDisplay(display, NULL);
   809     display->fullscreen_window = NULL;
   810 }
   811 
   812 int
   813 SDL_SetPaletteForDisplay(SDL_VideoDisplay * display, const SDL_Color * colors, int firstcolor, int ncolors)
   814 {
   815     SDL_Palette *palette;
   816     int status = 0;
   817 
   818     palette = display->palette;
   819     if (!palette) {
   820         SDL_SetError("Display mode does not have a palette");
   821         return -1;
   822     }
   823     status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
   824 
   825     if (_this->SetDisplayPalette) {
   826         if (_this->SetDisplayPalette(_this, display, palette) < 0) {
   827             status = -1;
   828         }
   829     }
   830     return status;
   831 }
   832 
   833 int
   834 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   835 {
   836     if (!_this) {
   837         SDL_UninitializedVideo();
   838         return -1;
   839     }
   840     return SDL_SetPaletteForDisplay(SDL_CurrentDisplay, colors, firstcolor, ncolors);
   841 }
   842 
   843 int
   844 SDL_GetPaletteForDisplay(SDL_VideoDisplay * display, SDL_Color * colors, int firstcolor, int ncolors)
   845 {
   846     SDL_Palette *palette;
   847 
   848     palette = display->palette;
   849     if (!palette || !palette->ncolors) {
   850         SDL_SetError("Display mode does not have a palette");
   851         return -1;
   852     }
   853     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   854         SDL_SetError("Palette indices are out of range");
   855         return -1;
   856     }
   857     SDL_memcpy(colors, &palette->colors[firstcolor],
   858                ncolors * sizeof(*colors));
   859     return 0;
   860 }
   861 
   862 int
   863 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   864 {
   865     if (!_this) {
   866         SDL_UninitializedVideo();
   867         return -1;
   868     }
   869     return SDL_GetPaletteForDisplay(SDL_CurrentDisplay, colors, firstcolor, ncolors);
   870 }
   871 
   872 SDL_Window *
   873 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   874 {
   875     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   876                                   SDL_WINDOW_OPENGL |
   877                                   SDL_WINDOW_BORDERLESS |
   878                                   SDL_WINDOW_RESIZABLE |
   879                                   SDL_WINDOW_INPUT_GRABBED);
   880     SDL_VideoDisplay *display;
   881     SDL_Window *window;
   882 
   883     if (!_this) {
   884         /* Initialize the video system if needed */
   885         if (SDL_VideoInit(NULL, 0) < 0) {
   886             return NULL;
   887         }
   888     }
   889     if (flags & SDL_WINDOW_OPENGL) {
   890         if (!_this->GL_CreateContext) {
   891             SDL_SetError("No OpenGL support in video driver");
   892             return NULL;
   893         }
   894         SDL_GL_LoadLibrary(NULL);
   895     }
   896     display = SDL_CurrentDisplay;
   897     window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
   898     window->id = _this->next_object_id++;
   899     window->x = x;
   900     window->y = y;
   901     window->w = w;
   902     window->h = h;
   903     window->flags = (flags & allowed_flags);
   904     window->display = display;
   905     window->next = display->windows;
   906     if (display->windows) {
   907         display->windows->prev = window;
   908     }
   909     display->windows = window;
   910 
   911     if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
   912         SDL_DestroyWindow(window);
   913         return NULL;
   914     }
   915 
   916     if (title) {
   917         SDL_SetWindowTitle(window, title);
   918     }
   919     if (flags & SDL_WINDOW_MAXIMIZED) {
   920         SDL_MaximizeWindow(window);
   921     }
   922     if (flags & SDL_WINDOW_MINIMIZED) {
   923         SDL_MinimizeWindow(window);
   924     }
   925     if (flags & SDL_WINDOW_SHOWN) {
   926         SDL_ShowWindow(window);
   927     }
   928     SDL_UpdateWindowGrab(window);
   929 
   930     return window;
   931 }
   932 
   933 SDL_Window *
   934 SDL_CreateWindowFrom(const void *data)
   935 {
   936     SDL_VideoDisplay *display;
   937     SDL_Window *window;
   938 
   939     if (!_this) {
   940         SDL_UninitializedVideo();
   941         return NULL;
   942     }
   943     display = SDL_CurrentDisplay;
   944     window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
   945     window->id = _this->next_object_id++;
   946     window->flags = SDL_WINDOW_FOREIGN;
   947     window->display = display;
   948     window->next = display->windows;
   949     if (display->windows) {
   950         display->windows->prev = window;
   951     }
   952     display->windows = window;
   953 
   954     if (!_this->CreateWindowFrom ||
   955         _this->CreateWindowFrom(_this, window, data) < 0) {
   956         SDL_DestroyWindow(window);
   957         return NULL;
   958     }
   959     return window;
   960 }
   961 
   962 int
   963 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
   964 {
   965     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   966                                   SDL_WINDOW_OPENGL |
   967                                   SDL_WINDOW_BORDERLESS |
   968                                   SDL_WINDOW_RESIZABLE |
   969                                   SDL_WINDOW_INPUT_GRABBED |
   970                                   SDL_WINDOW_FOREIGN);
   971     char *title = window->title;
   972 
   973     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   974         SDL_SetError("No OpenGL support in video driver");
   975         return -1;
   976     }
   977     if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
   978         if (flags & SDL_WINDOW_OPENGL) {
   979             SDL_GL_LoadLibrary(NULL);
   980         } else {
   981             SDL_GL_UnloadLibrary();
   982         }
   983     }
   984 
   985     if (window->flags & SDL_WINDOW_FOREIGN) {
   986         /* Can't destroy and re-create foreign windows, hrm */
   987         flags |= SDL_WINDOW_FOREIGN;
   988     } else {
   989         flags &= ~SDL_WINDOW_FOREIGN;
   990     }
   991 
   992     if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
   993         _this->DestroyWindow(_this, window);
   994     }
   995 
   996     window->title = NULL;
   997     window->flags = (flags & allowed_flags);
   998 
   999     if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
  1000         if (_this->CreateWindow(_this, window) < 0) {
  1001             if (flags & SDL_WINDOW_OPENGL) {
  1002                 SDL_GL_UnloadLibrary();
  1003             }
  1004             return -1;
  1005         }
  1006     }
  1007 
  1008     if (title) {
  1009         SDL_SetWindowTitle(window, title);
  1010         SDL_free(title);
  1011     }
  1012     if (flags & SDL_WINDOW_MAXIMIZED) {
  1013         SDL_MaximizeWindow(window);
  1014     }
  1015     if (flags & SDL_WINDOW_MINIMIZED) {
  1016         SDL_MinimizeWindow(window);
  1017     }
  1018     if (flags & SDL_WINDOW_SHOWN) {
  1019         SDL_ShowWindow(window);
  1020     }
  1021     SDL_UpdateWindowGrab(window);
  1022 
  1023     return 0;
  1024 }
  1025 
  1026 static __inline__ SDL_Renderer *
  1027 SDL_GetCurrentRenderer(SDL_bool create)
  1028 {
  1029     if (!_this) {
  1030         SDL_UninitializedVideo();
  1031         return NULL;
  1032     }
  1033     if (!SDL_CurrentRenderer) {
  1034         if (!create) {
  1035             SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1036             return NULL;
  1037         }
  1038         if (SDL_CreateRenderer(0, -1, 0) < 0) {
  1039             return NULL;
  1040         }
  1041     }
  1042     return SDL_CurrentRenderer;
  1043 }
  1044 
  1045 Uint32
  1046 SDL_GetWindowID(SDL_Window * window)
  1047 {
  1048     if (!window) {
  1049         return 0;
  1050     }
  1051     return window->id;
  1052 }
  1053 
  1054 SDL_Window *
  1055 SDL_GetWindowFromID(Uint32 id)
  1056 {
  1057     SDL_Window *window;
  1058     int i;
  1059 
  1060     if (!_this) {
  1061         return NULL;
  1062     }
  1063     /* FIXME: Should we keep a separate hash table for these? */
  1064     for (i = _this->num_displays; i--;) {
  1065         SDL_VideoDisplay *display = &_this->displays[i];
  1066         for (window = display->windows; window; window = window->next) {
  1067             if (window->id == id) {
  1068                 return window;
  1069             }
  1070         }
  1071     }
  1072     return NULL;
  1073 }
  1074 
  1075 Uint32
  1076 SDL_GetWindowFlags(SDL_Window * window)
  1077 {
  1078     if (!window) {
  1079         return 0;
  1080     }
  1081     return window->flags;
  1082 }
  1083 
  1084 void
  1085 SDL_SetWindowTitle(SDL_Window * window, const char *title)
  1086 {
  1087     if (!window || title == window->title) {
  1088         return;
  1089     }
  1090     if (window->title) {
  1091         SDL_free(window->title);
  1092     }
  1093     if (title) {
  1094         window->title = SDL_strdup(title);
  1095     } else {
  1096         window->title = NULL;
  1097     }
  1098 
  1099     if (_this->SetWindowTitle) {
  1100         _this->SetWindowTitle(_this, window);
  1101     }
  1102 }
  1103 
  1104 const char *
  1105 SDL_GetWindowTitle(SDL_Window * window)
  1106 {
  1107     if (!window) {
  1108         return NULL;
  1109     }
  1110     return window->title;
  1111 }
  1112 
  1113 void
  1114 SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
  1115 {
  1116     if (!window) {
  1117         return;
  1118     }
  1119     if (_this->SetWindowIcon) {
  1120         _this->SetWindowIcon(_this, window, icon);
  1121     }
  1122 }
  1123 
  1124 void
  1125 SDL_SetWindowData(SDL_Window * window, void *userdata)
  1126 {
  1127     if (!window) {
  1128         return;
  1129     }
  1130     window->userdata = userdata;
  1131 }
  1132 
  1133 void *
  1134 SDL_GetWindowData(SDL_Window * window)
  1135 {
  1136     if (!window) {
  1137         return NULL;
  1138     }
  1139     return window->userdata;
  1140 }
  1141 
  1142 void
  1143 SDL_SetWindowPosition(SDL_Window * window, int x, int y)
  1144 {
  1145     if (!window) {
  1146         return;
  1147     }
  1148     if (x != SDL_WINDOWPOS_UNDEFINED) {
  1149         window->x = x;
  1150     }
  1151     if (y != SDL_WINDOWPOS_UNDEFINED) {
  1152         window->y = y;
  1153     }
  1154     if (_this->SetWindowPosition) {
  1155         _this->SetWindowPosition(_this, window);
  1156     }
  1157     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
  1158 }
  1159 
  1160 void
  1161 SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
  1162 {
  1163     if (!window) {
  1164         return;
  1165     }
  1166     if (x) {
  1167         *x = window->x;
  1168     }
  1169     if (y) {
  1170         *y = window->y;
  1171     }
  1172 }
  1173 
  1174 void
  1175 SDL_SetWindowSize(SDL_Window * window, int w, int h)
  1176 {
  1177     if (!window) {
  1178         return;
  1179     }
  1180     window->w = w;
  1181     window->h = h;
  1182 
  1183     if (_this->SetWindowSize) {
  1184         _this->SetWindowSize(_this, window);
  1185     }
  1186     SDL_OnWindowResized(window);
  1187 }
  1188 
  1189 void
  1190 SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
  1191 {
  1192     if (window) {
  1193         if (w) {
  1194             *w = window->w;
  1195         }
  1196         if (h) {
  1197             *h = window->h;
  1198         }
  1199     } else {
  1200         if (w) {
  1201             *w = 0;
  1202         }
  1203         if (h) {
  1204             *h = 0;
  1205         }
  1206     }
  1207 }
  1208 
  1209 void
  1210 SDL_ShowWindow(SDL_Window * window)
  1211 {
  1212     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1213         return;
  1214     }
  1215 
  1216     if (_this->ShowWindow) {
  1217         _this->ShowWindow(_this, window);
  1218     }
  1219     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1220 }
  1221 
  1222 void
  1223 SDL_HideWindow(SDL_Window * window)
  1224 {
  1225     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1226         return;
  1227     }
  1228 
  1229     if (_this->HideWindow) {
  1230         _this->HideWindow(_this, window);
  1231     }
  1232     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1233 }
  1234 
  1235 void
  1236 SDL_RaiseWindow(SDL_Window * window)
  1237 {
  1238     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1239         return;
  1240     }
  1241     if (_this->RaiseWindow) {
  1242         _this->RaiseWindow(_this, window);
  1243     } else {
  1244         /* FIXME: What we really want is a way to request focus */
  1245         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  1246     }
  1247 }
  1248 
  1249 void
  1250 SDL_MaximizeWindow(SDL_Window * window)
  1251 {
  1252     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1253         return;
  1254     }
  1255 
  1256     if (_this->MaximizeWindow) {
  1257         _this->MaximizeWindow(_this, window);
  1258     }
  1259     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1260 }
  1261 
  1262 void
  1263 SDL_MinimizeWindow(SDL_Window * window)
  1264 {
  1265     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1266         return;
  1267     }
  1268 
  1269     if (_this->MinimizeWindow) {
  1270         _this->MinimizeWindow(_this, window);
  1271     }
  1272     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1273 }
  1274 
  1275 void
  1276 SDL_RestoreWindow(SDL_Window * window)
  1277 {
  1278     if (!window
  1279         || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1280         return;
  1281     }
  1282 
  1283     if (_this->RestoreWindow) {
  1284         _this->RestoreWindow(_this, window);
  1285     }
  1286     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1287 }
  1288 
  1289 int
  1290 SDL_SetWindowFullscreen(SDL_Window * window, int fullscreen)
  1291 {
  1292     if (!window) {
  1293         return -1;
  1294     }
  1295     if (fullscreen) {
  1296         fullscreen = SDL_WINDOW_FULLSCREEN;
  1297     }
  1298     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1299         return 0;
  1300     }
  1301     if (fullscreen) {
  1302         window->flags |= SDL_WINDOW_FULLSCREEN;
  1303 
  1304         SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1305     } else {
  1306         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1307 
  1308         SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1309     }
  1310     return 0;
  1311 }
  1312 
  1313 void
  1314 SDL_SetWindowGrab(SDL_Window * window, int mode)
  1315 {
  1316     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1317         return;
  1318     }
  1319     if (mode) {
  1320         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1321     } else {
  1322         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1323     }
  1324     SDL_UpdateWindowGrab(window);
  1325 }
  1326 
  1327 static void
  1328 SDL_UpdateWindowGrab(SDL_Window * window)
  1329 {
  1330     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1331         _this->SetWindowGrab(_this, window);
  1332     }
  1333 }
  1334 
  1335 int
  1336 SDL_GetWindowGrab(SDL_Window * window)
  1337 {
  1338     if (!window) {
  1339         return 0;
  1340     }
  1341     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1342 }
  1343 
  1344 void
  1345 SDL_OnWindowShown(SDL_Window * window)
  1346 {
  1347     SDL_RaiseWindow(window);
  1348     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1349 }
  1350 
  1351 void
  1352 SDL_OnWindowHidden(SDL_Window * window)
  1353 {
  1354     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1355 }
  1356 
  1357 void
  1358 SDL_OnWindowResized(SDL_Window * window)
  1359 {
  1360     SDL_Renderer *renderer = window->renderer;
  1361 
  1362     if (renderer && renderer->DisplayModeChanged) {
  1363         renderer->DisplayModeChanged(renderer);
  1364     }
  1365 }
  1366 
  1367 void
  1368 SDL_OnWindowMinimized(SDL_Window * window)
  1369 {
  1370     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1371 }
  1372 
  1373 void
  1374 SDL_OnWindowRestored(SDL_Window * window)
  1375 {
  1376     SDL_RaiseWindow(window);
  1377     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1378 }
  1379 
  1380 void
  1381 SDL_OnWindowFocusGained(SDL_Window * window)
  1382 {
  1383     SDL_VideoDisplay *display = window->display;
  1384 
  1385     if (display->gamma && _this->SetDisplayGammaRamp) {
  1386         _this->SetDisplayGammaRamp(_this, display, display->gamma);
  1387     }
  1388     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1389         && _this->SetWindowGrab) {
  1390         _this->SetWindowGrab(_this, window);
  1391     }
  1392 }
  1393 
  1394 void
  1395 SDL_OnWindowFocusLost(SDL_Window * window)
  1396 {
  1397     SDL_VideoDisplay *display = window->display;
  1398 
  1399     /* If we're fullscreen on a single-head system and lose focus, minimize */
  1400     if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
  1401         _this->num_displays == 1) {
  1402         SDL_MinimizeWindow(window);
  1403     }
  1404 
  1405     if (display->gamma && _this->SetDisplayGammaRamp) {
  1406         _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
  1407     }
  1408     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1409         && _this->SetWindowGrab) {
  1410         _this->SetWindowGrab(_this, window);
  1411     }
  1412 }
  1413 
  1414 SDL_Window *
  1415 SDL_GetFocusWindow(void)
  1416 {
  1417     SDL_VideoDisplay *display;
  1418     SDL_Window *window;
  1419 
  1420     if (!_this) {
  1421         return NULL;
  1422     }
  1423     display = SDL_CurrentDisplay;
  1424     for (window = display->windows; window; window = window->next) {
  1425         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1426             return window;
  1427         }
  1428     }
  1429     return NULL;
  1430 }
  1431 
  1432 void
  1433 SDL_DestroyWindow(SDL_Window * window)
  1434 {
  1435     SDL_VideoDisplay *display;
  1436 
  1437     if (!_this || !window || !window->id) {
  1438         SDL_SetError("Invalid window");
  1439         return;
  1440     }
  1441 
  1442     if (window->title) {
  1443         SDL_free(window->title);
  1444     }
  1445     if (window->renderer) {
  1446         SDL_DestroyRenderer(window);
  1447     }
  1448 
  1449     /* Restore video mode, etc. */
  1450     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1451 
  1452     if (_this->DestroyWindow) {
  1453         _this->DestroyWindow(_this, window);
  1454     }
  1455     if (window->flags & SDL_WINDOW_OPENGL) {
  1456         SDL_GL_UnloadLibrary();
  1457     }
  1458 
  1459     /* Unlink the window from the list */
  1460     display = window->display;
  1461     if (window->next) {
  1462         window->next->prev = window->prev;
  1463     }
  1464     if (window->prev) {
  1465         window->prev->next = window->next;
  1466     } else {
  1467         display->windows = window->next;
  1468     }
  1469 
  1470     /* Clear the ID so we know it was destroyed */
  1471     window->id = 0;
  1472 
  1473     SDL_free(window);
  1474 }
  1475 
  1476 void
  1477 SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
  1478 {
  1479     SDL_RenderDriver *render_drivers;
  1480 
  1481     render_drivers =
  1482         SDL_realloc(display->render_drivers,
  1483                     (display->num_render_drivers +
  1484                      1) * sizeof(*render_drivers));
  1485     if (render_drivers) {
  1486         render_drivers[display->num_render_drivers] = *driver;
  1487         display->render_drivers = render_drivers;
  1488         display->num_render_drivers++;
  1489     }
  1490 }
  1491 
  1492 int
  1493 SDL_GetNumRenderDrivers(void)
  1494 {
  1495     if (_this) {
  1496         return SDL_CurrentDisplay->num_render_drivers;
  1497     }
  1498     return 0;
  1499 }
  1500 
  1501 int
  1502 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1503 {
  1504     if (!_this) {
  1505         SDL_UninitializedVideo();
  1506         return -1;
  1507     }
  1508     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1509         SDL_SetError("index must be in the range of 0 - %d",
  1510                      SDL_GetNumRenderDrivers() - 1);
  1511         return -1;
  1512     }
  1513     *info = SDL_CurrentDisplay->render_drivers[index].info;
  1514     return 0;
  1515 }
  1516 
  1517 int
  1518 SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  1519 {
  1520     if (!window) {
  1521         SDL_SetError("Invalid window");
  1522         return -1;
  1523     }
  1524 
  1525     /* Free any existing renderer */
  1526     SDL_DestroyRenderer(window);
  1527 
  1528     if (index < 0) {
  1529         char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1530         int n = SDL_GetNumRenderDrivers();
  1531 
  1532 #if SDL_VIDEO_RENDER_OGL
  1533         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1534             override = "opengl";
  1535         }
  1536 #endif /* SDL_VIDEO_RENDER_OGL */
  1537 #if SDL_VIDEO_RENDER_OGL_ES
  1538         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1539             override = "opengl_es";
  1540         }
  1541 #endif /* SDL_VIDEO_RENDER_OGL_ES */
  1542         if (override) {
  1543             for (index = 0; index < n; ++index) {
  1544                 SDL_RenderDriver *driver =
  1545                     &SDL_CurrentDisplay->render_drivers[index];
  1546 
  1547                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1548                     /* Create a new renderer instance */
  1549                     window->renderer = driver->CreateRenderer(window, flags);
  1550                     break;
  1551                 }
  1552             }
  1553         } else {
  1554             for (index = 0; index < n; ++index) {
  1555                 SDL_RenderDriver *driver =
  1556                     &SDL_CurrentDisplay->render_drivers[index];
  1557 
  1558                 if ((driver->info.flags & flags) == flags) {
  1559                     /* Create a new renderer instance */
  1560                     window->renderer = driver->CreateRenderer(window, flags);
  1561                     if (window->renderer) {
  1562                         /* Yay, we got one! */
  1563                         break;
  1564                     }
  1565                 }
  1566             }
  1567         }
  1568         if (index == n) {
  1569             SDL_SetError("Couldn't find matching render driver");
  1570             return -1;
  1571         }
  1572     } else {
  1573         if (index >= SDL_GetNumRenderDrivers()) {
  1574             SDL_SetError("index must be -1 or in the range of 0 - %d",
  1575                          SDL_GetNumRenderDrivers() - 1);
  1576             return -1;
  1577         }
  1578         /* Create a new renderer instance */
  1579         window->renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags);
  1580     }
  1581 
  1582     if (window->renderer == NULL) {
  1583         /* Assuming renderer set its error */
  1584         return -1;
  1585     }
  1586 
  1587     SDL_SelectRenderer(window);
  1588 
  1589     return 0;
  1590 }
  1591 
  1592 int
  1593 SDL_SelectRenderer(SDL_Window * window)
  1594 {
  1595     SDL_Renderer *renderer;
  1596 
  1597     if (!window) {
  1598         SDL_SetError("Invalid window");
  1599         return -1;
  1600     }
  1601     renderer = window->renderer;
  1602     if (!renderer) {
  1603         SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1604         return -1;
  1605     }
  1606     if (renderer->ActivateRenderer) {
  1607         if (renderer->ActivateRenderer(renderer) < 0) {
  1608             return -1;
  1609         }
  1610     }
  1611     SDL_CurrentDisplay->current_renderer = renderer;
  1612     return 0;
  1613 }
  1614 
  1615 int
  1616 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1617 {
  1618     SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
  1619     if (!renderer) {
  1620         return -1;
  1621     }
  1622     *info = renderer->info;
  1623     return 0;
  1624 }
  1625 
  1626 SDL_Texture *
  1627 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1628 {
  1629     SDL_Renderer *renderer;
  1630     SDL_Texture *texture;
  1631 
  1632     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1633     if (!renderer) {
  1634         return 0;
  1635     }
  1636     if (!renderer->CreateTexture) {
  1637         SDL_Unsupported();
  1638         return 0;
  1639     }
  1640     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1641     if (!texture) {
  1642         SDL_OutOfMemory();
  1643         return 0;
  1644     }
  1645     texture->format = format;
  1646     texture->access = access;
  1647     texture->w = w;
  1648     texture->h = h;
  1649     texture->r = 255;
  1650     texture->g = 255;
  1651     texture->b = 255;
  1652     texture->a = 255;
  1653     texture->renderer = renderer;
  1654     texture->next = renderer->textures;
  1655     if (renderer->textures) {
  1656         renderer->textures->prev = texture;
  1657     }
  1658     renderer->textures = texture;
  1659 
  1660     if (renderer->CreateTexture(renderer, texture) < 0) {
  1661         SDL_DestroyTexture(texture);
  1662         return 0;
  1663     }
  1664     return texture;
  1665 }
  1666 
  1667 SDL_Texture *
  1668 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1669 {
  1670     SDL_Texture *texture;
  1671     Uint32 requested_format = format;
  1672     SDL_PixelFormat *fmt;
  1673     SDL_Renderer *renderer;
  1674     int bpp;
  1675     Uint32 Rmask, Gmask, Bmask, Amask;
  1676 
  1677     if (!surface) {
  1678         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1679         return 0;
  1680     }
  1681     fmt = surface->format;
  1682 
  1683     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1684     if (!renderer) {
  1685         return 0;
  1686     }
  1687 
  1688     if (format) {
  1689         if (!SDL_PixelFormatEnumToMasks
  1690             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1691             SDL_SetError("Unknown pixel format");
  1692             return 0;
  1693         }
  1694     } else {
  1695         if (surface->format->Amask
  1696             || !(surface->map->info.flags &
  1697                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1698             Uint32 it;
  1699             int pfmt;
  1700 
  1701             /* Pixel formats, sorted by best first */
  1702             static const Uint32 sdl_pformats[] = {
  1703                 SDL_PIXELFORMAT_ARGB8888,
  1704                 SDL_PIXELFORMAT_RGBA8888,
  1705                 SDL_PIXELFORMAT_ABGR8888,
  1706                 SDL_PIXELFORMAT_BGRA8888,
  1707                 SDL_PIXELFORMAT_RGB888,
  1708                 SDL_PIXELFORMAT_BGR888,
  1709                 SDL_PIXELFORMAT_RGB24,
  1710                 SDL_PIXELFORMAT_BGR24,
  1711                 SDL_PIXELFORMAT_RGB565,
  1712                 SDL_PIXELFORMAT_BGR565,
  1713                 SDL_PIXELFORMAT_ARGB1555,
  1714                 SDL_PIXELFORMAT_ABGR1555,
  1715                 SDL_PIXELFORMAT_RGB555,
  1716                 SDL_PIXELFORMAT_BGR555,
  1717                 SDL_PIXELFORMAT_ARGB4444,
  1718                 SDL_PIXELFORMAT_ABGR4444,
  1719                 SDL_PIXELFORMAT_RGB444,
  1720                 SDL_PIXELFORMAT_ARGB2101010,
  1721                 SDL_PIXELFORMAT_INDEX8,
  1722                 SDL_PIXELFORMAT_INDEX4LSB,
  1723                 SDL_PIXELFORMAT_INDEX4MSB,
  1724                 SDL_PIXELFORMAT_RGB332,
  1725                 SDL_PIXELFORMAT_INDEX1LSB,
  1726                 SDL_PIXELFORMAT_INDEX1MSB,
  1727                 SDL_PIXELFORMAT_UNKNOWN
  1728             };
  1729 
  1730             bpp = fmt->BitsPerPixel;
  1731             Rmask = fmt->Rmask;
  1732             Gmask = fmt->Gmask;
  1733             Bmask = fmt->Bmask;
  1734             Amask = fmt->Amask;
  1735 
  1736             format =
  1737                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1738             if (!format) {
  1739                 SDL_SetError("Unknown pixel format");
  1740                 return 0;
  1741             }
  1742 
  1743             /* Search requested format in the supported texture */
  1744             /* formats by current renderer                      */
  1745             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1746                 if (renderer->info.texture_formats[it] == format) {
  1747                     break;
  1748                 }
  1749             }
  1750 
  1751             /* If requested format can't be found, search any best */
  1752             /* format which renderer provides                      */
  1753             if (it == renderer->info.num_texture_formats) {
  1754                 pfmt = 0;
  1755                 for (;;) {
  1756                     if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1757                         break;
  1758                     }
  1759 
  1760                     for (it = 0; it < renderer->info.num_texture_formats;
  1761                          it++) {
  1762                         if (renderer->info.texture_formats[it] ==
  1763                             sdl_pformats[pfmt]) {
  1764                             break;
  1765                         }
  1766                     }
  1767 
  1768                     if (it != renderer->info.num_texture_formats) {
  1769                         /* The best format has been found */
  1770                         break;
  1771                     }
  1772                     pfmt++;
  1773                 }
  1774 
  1775                 /* If any format can't be found, then return an error */
  1776                 if (it == renderer->info.num_texture_formats) {
  1777                     SDL_SetError
  1778                         ("Any of the supported pixel formats can't be found");
  1779                     return 0;
  1780                 }
  1781 
  1782                 /* Convert found pixel format back to color masks */
  1783                 if (SDL_PixelFormatEnumToMasks
  1784                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1785                      &Bmask, &Amask) != SDL_TRUE) {
  1786                     SDL_SetError("Unknown pixel format");
  1787                     return 0;
  1788                 }
  1789             }
  1790         } else {
  1791             /* Need a format with alpha */
  1792             Uint32 it;
  1793             int apfmt;
  1794 
  1795             /* Pixel formats with alpha, sorted by best first */
  1796             static const Uint32 sdl_alpha_pformats[] = {
  1797                 SDL_PIXELFORMAT_ARGB8888,
  1798                 SDL_PIXELFORMAT_RGBA8888,
  1799                 SDL_PIXELFORMAT_ABGR8888,
  1800                 SDL_PIXELFORMAT_BGRA8888,
  1801                 SDL_PIXELFORMAT_ARGB1555,
  1802                 SDL_PIXELFORMAT_ABGR1555,
  1803                 SDL_PIXELFORMAT_ARGB4444,
  1804                 SDL_PIXELFORMAT_ABGR4444,
  1805                 SDL_PIXELFORMAT_ARGB2101010,
  1806                 SDL_PIXELFORMAT_UNKNOWN
  1807             };
  1808 
  1809             if (surface->format->Amask) {
  1810                 /* If surface already has alpha, then try an original */
  1811                 /* surface format first                               */
  1812                 bpp = fmt->BitsPerPixel;
  1813                 Rmask = fmt->Rmask;
  1814                 Gmask = fmt->Gmask;
  1815                 Bmask = fmt->Bmask;
  1816                 Amask = fmt->Amask;
  1817             } else {
  1818                 bpp = 32;
  1819                 Rmask = 0x00FF0000;
  1820                 Gmask = 0x0000FF00;
  1821                 Bmask = 0x000000FF;
  1822                 Amask = 0xFF000000;
  1823             }
  1824 
  1825             format =
  1826                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1827             if (!format) {
  1828                 SDL_SetError("Unknown pixel format");
  1829                 return 0;
  1830             }
  1831 
  1832             /* Search this format in the supported texture formats */
  1833             /* by current renderer                                 */
  1834             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1835                 if (renderer->info.texture_formats[it] == format) {
  1836                     break;
  1837                 }
  1838             }
  1839 
  1840             /* If this format can't be found, search any best       */
  1841             /* compatible format with alpha which renderer provides */
  1842             if (it == renderer->info.num_texture_formats) {
  1843                 apfmt = 0;
  1844                 for (;;) {
  1845                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1846                         break;
  1847                     }
  1848 
  1849                     for (it = 0; it < renderer->info.num_texture_formats;
  1850                          it++) {
  1851                         if (renderer->info.texture_formats[it] ==
  1852                             sdl_alpha_pformats[apfmt]) {
  1853                             break;
  1854                         }
  1855                     }
  1856 
  1857                     if (it != renderer->info.num_texture_formats) {
  1858                         /* Compatible format has been found */
  1859                         break;
  1860                     }
  1861                     apfmt++;
  1862                 }
  1863 
  1864                 /* If compatible format can't be found, then return an error */
  1865                 if (it == renderer->info.num_texture_formats) {
  1866                     SDL_SetError("Compatible pixel format can't be found");
  1867                     return 0;
  1868                 }
  1869 
  1870                 /* Convert found pixel format back to color masks */
  1871                 if (SDL_PixelFormatEnumToMasks
  1872                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1873                      &Bmask, &Amask) != SDL_TRUE) {
  1874                     SDL_SetError("Unknown pixel format");
  1875                     return 0;
  1876                 }
  1877             }
  1878         }
  1879 
  1880         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1881         if (!format) {
  1882             SDL_SetError("Unknown pixel format");
  1883             return 0;
  1884         }
  1885     }
  1886 
  1887     texture =
  1888         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1889                           surface->h);
  1890     if (!texture && !requested_format) {
  1891         SDL_DisplayMode desktop_mode;
  1892         SDL_GetDesktopDisplayMode(&desktop_mode);
  1893         format = desktop_mode.format;
  1894         texture =
  1895             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1896                               surface->h);
  1897     }
  1898     if (!texture) {
  1899         return 0;
  1900     }
  1901     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1902         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1903         if (SDL_MUSTLOCK(surface)) {
  1904             SDL_LockSurface(surface);
  1905             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1906                               surface->pitch);
  1907             SDL_UnlockSurface(surface);
  1908         } else {
  1909             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1910                               surface->pitch);
  1911         }
  1912     } else {
  1913         SDL_PixelFormat dst_fmt;
  1914         SDL_Surface *dst = NULL;
  1915 
  1916         /* Set up a destination surface for the texture update */
  1917         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  1918         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1919             dst_fmt.palette =
  1920                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1921             if (dst_fmt.palette) {
  1922                 /*
  1923                  * FIXME: Should we try to copy
  1924                  * fmt->palette?
  1925                  */
  1926                 SDL_DitherColors(dst_fmt.palette->colors,
  1927                                  SDL_BITSPERPIXEL(format));
  1928             }
  1929         }
  1930         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  1931         if (dst) {
  1932             SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
  1933             SDL_FreeSurface(dst);
  1934         }
  1935         if (dst_fmt.palette) {
  1936             SDL_FreePalette(dst_fmt.palette);
  1937         }
  1938         if (!dst) {
  1939             SDL_DestroyTexture(texture);
  1940             return 0;
  1941         }
  1942     }
  1943 
  1944     {
  1945         Uint8 r, g, b, a;
  1946         int blendMode;
  1947         int scaleMode;
  1948 
  1949         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1950         SDL_SetTextureColorMod(texture, r, g, b);
  1951 
  1952         SDL_GetSurfaceAlphaMod(surface, &a);
  1953         SDL_SetTextureAlphaMod(texture, a);
  1954 
  1955         SDL_GetSurfaceBlendMode(surface, &blendMode);
  1956         SDL_SetTextureBlendMode(texture, blendMode);
  1957 
  1958         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  1959         SDL_SetTextureScaleMode(texture, scaleMode);
  1960     }
  1961 
  1962     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1963         SDL_SetTexturePalette(texture, fmt->palette->colors, 0,
  1964                               fmt->palette->ncolors);
  1965     }
  1966     return texture;
  1967 }
  1968 
  1969 int
  1970 SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  1971                  int *w, int *h)
  1972 {
  1973     if (!texture) {
  1974         return -1;
  1975     }
  1976     if (format) {
  1977         *format = texture->format;
  1978     }
  1979     if (access) {
  1980         *access = texture->access;
  1981     }
  1982     if (w) {
  1983         *w = texture->w;
  1984     }
  1985     if (h) {
  1986         *h = texture->h;
  1987     }
  1988     return 0;
  1989 }
  1990 
  1991 int
  1992 SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
  1993 {
  1994     SDL_Renderer *renderer;
  1995 
  1996     if (!texture) {
  1997         return -1;
  1998     }
  1999     renderer = texture->renderer;
  2000     if (!renderer->QueryTexturePixels) {
  2001         SDL_Unsupported();
  2002         return -1;
  2003     }
  2004     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  2005 }
  2006 
  2007 int
  2008 SDL_SetTexturePalette(SDL_Texture * texture, const SDL_Color * colors,
  2009                       int firstcolor, int ncolors)
  2010 {
  2011     SDL_Renderer *renderer;
  2012 
  2013     if (!texture) {
  2014         return -1;
  2015     }
  2016     renderer = texture->renderer;
  2017     if (!renderer->SetTexturePalette) {
  2018         SDL_Unsupported();
  2019         return -1;
  2020     }
  2021     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  2022                                        ncolors);
  2023 }
  2024 
  2025 int
  2026 SDL_GetTexturePalette(SDL_Texture * texture, SDL_Color * colors,
  2027                       int firstcolor, int ncolors)
  2028 {
  2029     SDL_Renderer *renderer;
  2030 
  2031     if (!texture) {
  2032         return -1;
  2033     }
  2034     renderer = texture->renderer;
  2035     if (!renderer->GetTexturePalette) {
  2036         SDL_Unsupported();
  2037         return -1;
  2038     }
  2039     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  2040                                        ncolors);
  2041 }
  2042 
  2043 int
  2044 SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  2045 {
  2046     SDL_Renderer *renderer;
  2047 
  2048     if (!texture) {
  2049         return -1;
  2050     }
  2051     renderer = texture->renderer;
  2052     if (!renderer->SetTextureColorMod) {
  2053         SDL_Unsupported();
  2054         return -1;
  2055     }
  2056     if (r < 255 || g < 255 || b < 255) {
  2057         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  2058     } else {
  2059         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  2060     }
  2061     texture->r = r;
  2062     texture->g = g;
  2063     texture->b = b;
  2064     return renderer->SetTextureColorMod(renderer, texture);
  2065 }
  2066 
  2067 int
  2068 SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  2069                        Uint8 * b)
  2070 {
  2071     SDL_Renderer *renderer;
  2072 
  2073     if (!texture) {
  2074         return -1;
  2075     }
  2076     renderer = texture->renderer;
  2077     if (r) {
  2078         *r = texture->r;
  2079     }
  2080     if (g) {
  2081         *g = texture->g;
  2082     }
  2083     if (b) {
  2084         *b = texture->b;
  2085     }
  2086     return 0;
  2087 }
  2088 
  2089 int
  2090 SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  2091 {
  2092     SDL_Renderer *renderer;
  2093 
  2094     if (!texture) {
  2095         return -1;
  2096     }
  2097     renderer = texture->renderer;
  2098     if (!renderer->SetTextureAlphaMod) {
  2099         SDL_Unsupported();
  2100         return -1;
  2101     }
  2102     if (alpha < 255) {
  2103         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  2104     } else {
  2105         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  2106     }
  2107     texture->a = alpha;
  2108     return renderer->SetTextureAlphaMod(renderer, texture);
  2109 }
  2110 
  2111 int
  2112 SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  2113 {
  2114     if (!texture) {
  2115         return -1;
  2116     }
  2117     if (alpha) {
  2118         *alpha = texture->a;
  2119     }
  2120     return 0;
  2121 }
  2122 
  2123 int
  2124 SDL_SetTextureBlendMode(SDL_Texture * texture, int blendMode)
  2125 {
  2126     SDL_Renderer *renderer;
  2127 
  2128     if (!texture) {
  2129         return -1;
  2130     }
  2131     renderer = texture->renderer;
  2132     if (!renderer->SetTextureBlendMode) {
  2133         SDL_Unsupported();
  2134         return -1;
  2135     }
  2136     texture->blendMode = blendMode;
  2137     return renderer->SetTextureBlendMode(renderer, texture);
  2138 }
  2139 
  2140 int
  2141 SDL_GetTextureBlendMode(SDL_Texture * texture, int *blendMode)
  2142 {
  2143     if (!texture) {
  2144         return -1;
  2145     }
  2146     if (blendMode) {
  2147         *blendMode = texture->blendMode;
  2148     }
  2149     return 0;
  2150 }
  2151 
  2152 int
  2153 SDL_SetTextureScaleMode(SDL_Texture * texture, int scaleMode)
  2154 {
  2155     SDL_Renderer *renderer;
  2156 
  2157     if (!texture) {
  2158         return -1;
  2159     }
  2160     renderer = texture->renderer;
  2161     if (!renderer->SetTextureScaleMode) {
  2162         SDL_Unsupported();
  2163         return -1;
  2164     }
  2165     texture->scaleMode = scaleMode;
  2166     return renderer->SetTextureScaleMode(renderer, texture);
  2167 }
  2168 
  2169 int
  2170 SDL_GetTextureScaleMode(SDL_Texture * texture, int *scaleMode)
  2171 {
  2172     if (!texture) {
  2173         return -1;
  2174     }
  2175     if (scaleMode) {
  2176         *scaleMode = texture->scaleMode;
  2177     }
  2178     return 0;
  2179 }
  2180 
  2181 int
  2182 SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  2183                   const void *pixels, int pitch)
  2184 {
  2185     SDL_Renderer *renderer;
  2186     SDL_Rect full_rect;
  2187 
  2188     if (!texture) {
  2189         return -1;
  2190     }
  2191     renderer = texture->renderer;
  2192     if (!renderer->UpdateTexture) {
  2193         SDL_Unsupported();
  2194         return -1;
  2195     }
  2196     if (!rect) {
  2197         full_rect.x = 0;
  2198         full_rect.y = 0;
  2199         full_rect.w = texture->w;
  2200         full_rect.h = texture->h;
  2201         rect = &full_rect;
  2202     }
  2203     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2204 }
  2205 
  2206 int
  2207 SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
  2208                 void **pixels, int *pitch)
  2209 {
  2210     SDL_Renderer *renderer;
  2211     SDL_Rect full_rect;
  2212 
  2213     if (!texture) {
  2214         return -1;
  2215     }
  2216     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2217         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2218         return -1;
  2219     }
  2220     renderer = texture->renderer;
  2221     if (!renderer->LockTexture) {
  2222         SDL_Unsupported();
  2223         return -1;
  2224     }
  2225     if (!rect) {
  2226         full_rect.x = 0;
  2227         full_rect.y = 0;
  2228         full_rect.w = texture->w;
  2229         full_rect.h = texture->h;
  2230         rect = &full_rect;
  2231     }
  2232     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2233                                  pitch);
  2234 }
  2235 
  2236 void
  2237 SDL_UnlockTexture(SDL_Texture * texture)
  2238 {
  2239     SDL_Renderer *renderer;
  2240 
  2241     if (!texture) {
  2242         return;
  2243     }
  2244     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2245         return;
  2246     }
  2247     renderer = texture->renderer;
  2248     if (!renderer->UnlockTexture) {
  2249         return;
  2250     }
  2251     renderer->UnlockTexture(renderer, texture);
  2252 }
  2253 
  2254 void
  2255 SDL_DirtyTexture(SDL_Texture * texture, int numrects,
  2256                  const SDL_Rect * rects)
  2257 {
  2258     SDL_Renderer *renderer;
  2259 
  2260     if (!texture) {
  2261         return;
  2262     }
  2263     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2264         return;
  2265     }
  2266     renderer = texture->renderer;
  2267     if (!renderer->DirtyTexture) {
  2268         return;
  2269     }
  2270     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2271 }
  2272 
  2273 int
  2274 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2275 {
  2276     SDL_Renderer *renderer;
  2277 
  2278     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2279     if (!renderer) {
  2280         return -1;
  2281     }
  2282     renderer->r = r;
  2283     renderer->g = g;
  2284     renderer->b = b;
  2285     renderer->a = a;
  2286     if (renderer->SetDrawColor) {
  2287         return renderer->SetDrawColor(renderer);
  2288     } else {
  2289         return 0;
  2290     }
  2291 }
  2292 
  2293 int
  2294 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2295 {
  2296     SDL_Renderer *renderer;
  2297 
  2298     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2299     if (!renderer) {
  2300         return -1;
  2301     }
  2302     if (r) {
  2303         *r = renderer->r;
  2304     }
  2305     if (g) {
  2306         *g = renderer->g;
  2307     }
  2308     if (b) {
  2309         *b = renderer->b;
  2310     }
  2311     if (a) {
  2312         *a = renderer->a;
  2313     }
  2314     return 0;
  2315 }
  2316 
  2317 int
  2318 SDL_SetRenderDrawBlendMode(int blendMode)
  2319 {
  2320     SDL_Renderer *renderer;
  2321 
  2322     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2323     if (!renderer) {
  2324         return -1;
  2325     }
  2326     renderer->blendMode = blendMode;
  2327     if (renderer->SetDrawBlendMode) {
  2328         return renderer->SetDrawBlendMode(renderer);
  2329     } else {
  2330         return 0;
  2331     }
  2332 }
  2333 
  2334 int
  2335 SDL_GetRenderDrawBlendMode(int *blendMode)
  2336 {
  2337     SDL_Renderer *renderer;
  2338 
  2339     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2340     if (!renderer) {
  2341         return -1;
  2342     }
  2343     *blendMode = renderer->blendMode;
  2344     return 0;
  2345 }
  2346 
  2347 int
  2348 SDL_RenderClear()
  2349 {
  2350     SDL_Renderer *renderer;
  2351 
  2352     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2353     if (!renderer) {
  2354         return -1;
  2355     }
  2356     if (!renderer->RenderClear) {
  2357         int blendMode = renderer->blendMode;
  2358         int status;
  2359 
  2360         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2361             SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  2362         }
  2363 
  2364         status = SDL_RenderFillRect(NULL);
  2365 
  2366         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2367             SDL_SetRenderDrawBlendMode(blendMode);
  2368         }
  2369         return status;
  2370     }
  2371     return renderer->RenderClear(renderer);
  2372 }
  2373 
  2374 int
  2375 SDL_RenderDrawPoint(int x, int y)
  2376 {
  2377     SDL_Point point;
  2378 
  2379     point.x = x;
  2380     point.y = y;
  2381     return SDL_RenderDrawPoints(&point, 1);
  2382 }
  2383 
  2384 int
  2385 SDL_RenderDrawPoints(const SDL_Point * points, int count)
  2386 {
  2387     SDL_Renderer *renderer;
  2388 
  2389     if (!points) {
  2390         SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  2391         return -1;
  2392     }
  2393 
  2394     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2395     if (!renderer) {
  2396         return -1;
  2397     }
  2398     if (!renderer->RenderDrawPoints) {
  2399         SDL_Unsupported();
  2400         return -1;
  2401     }
  2402     if (count < 1) {
  2403         return 0;
  2404     }
  2405     return renderer->RenderDrawPoints(renderer, points, count);
  2406 }
  2407 
  2408 int
  2409 SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
  2410 {
  2411     SDL_Point points[2];
  2412 
  2413     points[0].x = x1;
  2414     points[0].y = y1;
  2415     points[1].x = x2;
  2416     points[1].y = y2;
  2417     return SDL_RenderDrawLines(points, 2);
  2418 }
  2419 
  2420 int
  2421 SDL_RenderDrawLines(const SDL_Point * points, int count)
  2422 {
  2423     SDL_Renderer *renderer;
  2424 
  2425     if (!points) {
  2426         SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  2427         return -1;
  2428     }
  2429 
  2430     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2431     if (!renderer) {
  2432         return -1;
  2433     }
  2434     if (!renderer->RenderDrawLines) {
  2435         SDL_Unsupported();
  2436         return -1;
  2437     }
  2438     if (count < 2) {
  2439         return 0;
  2440     }
  2441     return renderer->RenderDrawLines(renderer, points, count);
  2442 }
  2443 
  2444 int
  2445 SDL_RenderDrawRect(const SDL_Rect * rect)
  2446 {
  2447     return SDL_RenderDrawRects(&rect, 1);
  2448 }
  2449 
  2450 int
  2451 SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
  2452 {
  2453     SDL_Renderer *renderer;
  2454     int i;
  2455 
  2456     if (!rects) {
  2457         SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  2458         return -1;
  2459     }
  2460 
  2461     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2462     if (!renderer) {
  2463         return -1;
  2464     }
  2465     if (!renderer->RenderDrawRects) {
  2466         SDL_Unsupported();
  2467         return -1;
  2468     }
  2469     if (count < 1) {
  2470         return 0;
  2471     }
  2472     /* Check for NULL rect, which means fill entire window */
  2473     for (i = 0; i < count; ++i) {
  2474         if (rects[i] == NULL) {
  2475             SDL_Window *window = renderer->window;
  2476             SDL_Rect full_rect;
  2477             const SDL_Rect *rect;
  2478 
  2479             full_rect.x = 0;
  2480             full_rect.y = 0;
  2481             full_rect.w = window->w;
  2482             full_rect.h = window->h;
  2483             rect = &full_rect;
  2484             return renderer->RenderDrawRects(renderer, &rect, 1);
  2485         }
  2486     }
  2487     return renderer->RenderDrawRects(renderer, rects, count);
  2488 }
  2489 
  2490 int
  2491 SDL_RenderFillRect(const SDL_Rect * rect)
  2492 {
  2493     return SDL_RenderFillRects(&rect, 1);
  2494 }
  2495 
  2496 int
  2497 SDL_RenderFillRects(const SDL_Rect ** rects, int count)
  2498 {
  2499     SDL_Renderer *renderer;
  2500     int i;
  2501 
  2502     if (!rects) {
  2503         SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  2504         return -1;
  2505     }
  2506 
  2507     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2508     if (!renderer) {
  2509         return -1;
  2510     }
  2511     if (!renderer->RenderFillRects) {
  2512         SDL_Unsupported();
  2513         return -1;
  2514     }
  2515     if (count < 1) {
  2516         return 0;
  2517     }
  2518     /* Check for NULL rect, which means fill entire window */
  2519     for (i = 0; i < count; ++i) {
  2520         if (rects[i] == NULL) {
  2521             SDL_Window *window = renderer->window;
  2522             SDL_Rect full_rect;
  2523             const SDL_Rect *rect;
  2524 
  2525             full_rect.x = 0;
  2526             full_rect.y = 0;
  2527             full_rect.w = window->w;
  2528             full_rect.h = window->h;
  2529             rect = &full_rect;
  2530             return renderer->RenderFillRects(renderer, &rect, 1);
  2531         }
  2532     }
  2533     return renderer->RenderFillRects(renderer, rects, count);
  2534 }
  2535 
  2536 int
  2537 SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
  2538                const SDL_Rect * dstrect)
  2539 {
  2540     SDL_Renderer *renderer;
  2541     SDL_Window *window;
  2542     SDL_Rect real_srcrect;
  2543     SDL_Rect real_dstrect;
  2544 
  2545     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2546     if (!renderer) {
  2547         return -1;
  2548     }
  2549     if (!texture) {
  2550         SDL_SetError("Texture not found");
  2551         return -1;
  2552     }
  2553     if (texture->renderer != renderer) {
  2554         SDL_SetError("Texture was not created with this renderer");
  2555         return -1;
  2556     }
  2557     if (!renderer->RenderCopy) {
  2558         SDL_Unsupported();
  2559         return -1;
  2560     }
  2561     window = renderer->window;
  2562 
  2563     real_srcrect.x = 0;
  2564     real_srcrect.y = 0;
  2565     real_srcrect.w = texture->w;
  2566     real_srcrect.h = texture->h;
  2567     if (srcrect) {
  2568         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2569             return 0;
  2570         }
  2571     }
  2572 
  2573     real_dstrect.x = 0;
  2574     real_dstrect.y = 0;
  2575     real_dstrect.w = window->w;
  2576     real_dstrect.h = window->h;
  2577     if (dstrect) {
  2578         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2579             return 0;
  2580         }
  2581         /* Clip srcrect by the same amount as dstrect was clipped */
  2582         if (dstrect->w != real_dstrect.w) {
  2583             int deltax = (real_dstrect.x - dstrect->x);
  2584             int deltaw = (real_dstrect.w - dstrect->w);
  2585             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2586             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2587         }
  2588         if (dstrect->h != real_dstrect.h) {
  2589             int deltay = (real_dstrect.y - dstrect->y);
  2590             int deltah = (real_dstrect.h - dstrect->h);
  2591             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2592             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2593         }
  2594     }
  2595 
  2596     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2597                                 &real_dstrect);
  2598 }
  2599 
  2600 int
  2601 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2602                      void * pixels, int pitch)
  2603 {
  2604     SDL_Renderer *renderer;
  2605     SDL_Window *window;
  2606     SDL_Rect real_rect;
  2607 
  2608     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2609     if (!renderer) {
  2610         return -1;
  2611     }
  2612     if (!renderer->RenderReadPixels) {
  2613         SDL_Unsupported();
  2614         return -1;
  2615     }
  2616     window = renderer->window;
  2617 
  2618     if (!format) {
  2619         format = window->display->current_mode.format;
  2620     }
  2621 
  2622     real_rect.x = 0;
  2623     real_rect.y = 0;
  2624     real_rect.w = window->w;
  2625     real_rect.h = window->h;
  2626     if (rect) {
  2627         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2628             return 0;
  2629         }
  2630         if (real_rect.y > rect->y) {
  2631             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2632         }
  2633         if (real_rect.x > rect->x) {
  2634             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2635             int bpp = SDL_BYTESPERPIXEL(format);
  2636             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2637         }
  2638     }
  2639 
  2640     return renderer->RenderReadPixels(renderer, &real_rect,
  2641                                       format, pixels, pitch);
  2642 }
  2643 
  2644 int
  2645 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2646                       const void * pixels, int pitch)
  2647 {
  2648     SDL_Renderer *renderer;
  2649     SDL_Window *window;
  2650     SDL_Rect real_rect;
  2651 
  2652     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2653     if (!renderer) {
  2654         return -1;
  2655     }
  2656     if (!renderer->RenderWritePixels) {
  2657         SDL_Unsupported();
  2658         return -1;
  2659     }
  2660     window = renderer->window;
  2661 
  2662     if (!format) {
  2663         format = window->display->current_mode.format;
  2664     }
  2665 
  2666     real_rect.x = 0;
  2667     real_rect.y = 0;
  2668     real_rect.w = window->w;
  2669     real_rect.h = window->h;
  2670     if (rect) {
  2671         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2672             return 0;
  2673         }
  2674         if (real_rect.y > rect->y) {
  2675             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2676         }
  2677         if (real_rect.x > rect->x) {
  2678             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2679             int bpp = SDL_BYTESPERPIXEL(format);
  2680             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2681         }
  2682     }
  2683 
  2684     return renderer->RenderWritePixels(renderer, &real_rect,
  2685                                        format, pixels, pitch);
  2686 }
  2687 
  2688 void
  2689 SDL_RenderPresent(void)
  2690 {
  2691     SDL_Renderer *renderer;
  2692 
  2693     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2694     if (!renderer || !renderer->RenderPresent) {
  2695         return;
  2696     }
  2697     renderer->RenderPresent(renderer);
  2698 }
  2699 
  2700 void
  2701 SDL_DestroyTexture(SDL_Texture * texture)
  2702 {
  2703     SDL_Renderer *renderer;
  2704 
  2705     if (!texture || !texture->renderer) {
  2706         SDL_SetError("Invalid texture");
  2707         return;
  2708     }
  2709 
  2710     renderer = texture->renderer;
  2711     if (texture->next) {
  2712         texture->next->prev = texture->prev;
  2713     }
  2714     if (texture->prev) {
  2715         texture->prev->next = texture->next;
  2716     } else {
  2717         renderer->textures = texture->next;
  2718     }
  2719     texture->renderer = NULL;
  2720 
  2721     renderer->DestroyTexture(renderer, texture);
  2722     SDL_free(texture);
  2723 }
  2724 
  2725 void
  2726 SDL_DestroyRenderer(SDL_Window * window)
  2727 {
  2728     SDL_Renderer *renderer;
  2729 
  2730     if (!window) {
  2731         return;
  2732     }
  2733     renderer = window->renderer;
  2734     if (!renderer) {
  2735         return;
  2736     }
  2737 
  2738     /* Free existing textures for this renderer */
  2739     while (renderer->textures) {
  2740         SDL_DestroyTexture(renderer->textures);
  2741     }
  2742 
  2743     /* Free the renderer instance */
  2744     renderer->DestroyRenderer(renderer);
  2745 
  2746     /* Clear references */
  2747     window->renderer = NULL;
  2748     if (SDL_CurrentDisplay->current_renderer == renderer) {
  2749         SDL_CurrentDisplay->current_renderer = NULL;
  2750     }
  2751 }
  2752 
  2753 SDL_bool
  2754 SDL_IsScreenSaverEnabled()
  2755 {
  2756     if (!_this) {
  2757         return SDL_TRUE;
  2758     }
  2759     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2760 }
  2761 
  2762 void
  2763 SDL_EnableScreenSaver()
  2764 {
  2765     if (!_this) {
  2766         return;
  2767     }
  2768     if (!_this->suspend_screensaver) {
  2769         return;
  2770     }
  2771     _this->suspend_screensaver = SDL_FALSE;
  2772     if (_this->SuspendScreenSaver) {
  2773         _this->SuspendScreenSaver(_this);
  2774     }
  2775 }
  2776 
  2777 void
  2778 SDL_DisableScreenSaver()
  2779 {
  2780     if (!_this) {
  2781         return;
  2782     }
  2783     if (_this->suspend_screensaver) {
  2784         return;
  2785     }
  2786     _this->suspend_screensaver = SDL_TRUE;
  2787     if (_this->SuspendScreenSaver) {
  2788         _this->SuspendScreenSaver(_this);
  2789     }
  2790 }
  2791 
  2792 void
  2793 SDL_VideoQuit(void)
  2794 {
  2795     int i, j;
  2796 
  2797     if (!_this) {
  2798         return;
  2799     }
  2800     /* Halt event processing before doing anything else */
  2801     SDL_StopEventLoop();
  2802     SDL_EnableScreenSaver();
  2803 
  2804     /* Clean up the system video */
  2805     for (i = _this->num_displays; i--;) {
  2806         SDL_VideoDisplay *display = &_this->displays[i];
  2807         while (display->windows) {
  2808             SDL_DestroyWindow(display->windows);
  2809         }
  2810         if (display->render_drivers) {
  2811             SDL_free(display->render_drivers);
  2812             display->render_drivers = NULL;
  2813         }
  2814         display->num_render_drivers = 0;
  2815     }
  2816     _this->VideoQuit(_this);
  2817 
  2818     for (i = _this->num_displays; i--;) {
  2819         SDL_VideoDisplay *display = &_this->displays[i];
  2820         for (j = display->num_display_modes; j--;) {
  2821             if (display->display_modes[j].driverdata) {
  2822                 SDL_free(display->display_modes[j].driverdata);
  2823                 display->display_modes[j].driverdata = NULL;
  2824             }
  2825         }
  2826         if (display->display_modes) {
  2827             SDL_free(display->display_modes);
  2828             display->display_modes = NULL;
  2829         }
  2830         if (display->desktop_mode.driverdata) {
  2831             SDL_free(display->desktop_mode.driverdata);
  2832             display->desktop_mode.driverdata = NULL;
  2833         }
  2834         if (display->palette) {
  2835             SDL_FreePalette(display->palette);
  2836             display->palette = NULL;
  2837         }
  2838         if (display->gamma) {
  2839             SDL_free(display->gamma);
  2840             display->gamma = NULL;
  2841         }
  2842         if (display->driverdata) {
  2843             SDL_free(display->driverdata);
  2844             display->driverdata = NULL;
  2845         }
  2846     }
  2847     if (_this->displays) {
  2848         SDL_free(_this->displays);
  2849         _this->displays = NULL;
  2850     }
  2851     _this->free(_this);
  2852     _this = NULL;
  2853 }
  2854 
  2855 int
  2856 SDL_GL_LoadLibrary(const char *path)
  2857 {
  2858     int retval;
  2859 
  2860     if (!_this) {
  2861         SDL_UninitializedVideo();
  2862         return -1;
  2863     }
  2864     if (_this->gl_config.driver_loaded) {
  2865         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2866             SDL_SetError("OpenGL library already loaded");
  2867             return -1;
  2868         }
  2869         retval = 0;
  2870     } else {
  2871         if (!_this->GL_LoadLibrary) {
  2872             SDL_SetError("No dynamic GL support in video driver");
  2873             return -1;
  2874         }
  2875         retval = _this->GL_LoadLibrary(_this, path);
  2876     }
  2877     if (retval == 0) {
  2878         ++_this->gl_config.driver_loaded;
  2879     }
  2880     return (retval);
  2881 }
  2882 
  2883 void *
  2884 SDL_GL_GetProcAddress(const char *proc)
  2885 {
  2886     void *func;
  2887 
  2888     if (!_this) {
  2889         SDL_UninitializedVideo();
  2890         return NULL;
  2891     }
  2892     func = NULL;
  2893     if (_this->GL_GetProcAddress) {
  2894         if (_this->gl_config.driver_loaded) {
  2895             func = _this->GL_GetProcAddress(_this, proc);
  2896         } else {
  2897             SDL_SetError("No GL driver has been loaded");
  2898         }
  2899     } else {
  2900         SDL_SetError("No dynamic GL support in video driver");
  2901     }
  2902     return func;
  2903 }
  2904 
  2905 void
  2906 SDL_GL_UnloadLibrary(void)
  2907 {
  2908     if (!_this) {
  2909         SDL_UninitializedVideo();
  2910         return;
  2911     }
  2912     if (_this->gl_config.driver_loaded > 0) {
  2913         if (--_this->gl_config.driver_loaded > 0) {
  2914             return;
  2915         }
  2916         if (_this->GL_UnloadLibrary) {
  2917             _this->GL_UnloadLibrary(_this);
  2918         }
  2919     }
  2920 }
  2921 
  2922 SDL_bool
  2923 SDL_GL_ExtensionSupported(const char *extension)
  2924 {
  2925 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2926     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2927     const char *extensions;
  2928     const char *start;
  2929     const char *where, *terminator;
  2930 
  2931     /* Extension names should not have spaces. */
  2932     where = SDL_strchr(extension, ' ');
  2933     if (where || *extension == '\0') {
  2934         return SDL_FALSE;
  2935     }
  2936     /* See if there's an environment variable override */
  2937     start = SDL_getenv(extension);
  2938     if (start && *start == '0') {
  2939         return SDL_FALSE;
  2940     }
  2941     /* Lookup the available extensions */
  2942     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2943     if (glGetStringFunc) {
  2944         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2945     } else {
  2946         extensions = NULL;
  2947     }
  2948     if (!extensions) {
  2949         return SDL_FALSE;
  2950     }
  2951     /*
  2952      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2953      * extensions string. Don't be fooled by sub-strings, etc.
  2954      */
  2955 
  2956     start = extensions;
  2957 
  2958     for (;;) {
  2959         where = SDL_strstr(start, extension);
  2960         if (!where)
  2961             break;
  2962 
  2963         terminator = where + SDL_strlen(extension);
  2964         if (where == start || *(where - 1) == ' ')
  2965             if (*terminator == ' ' || *terminator == '\0')
  2966                 return SDL_TRUE;
  2967 
  2968         start = terminator;
  2969     }
  2970     return SDL_FALSE;
  2971 #else
  2972     return SDL_FALSE;
  2973 #endif
  2974 }
  2975 
  2976 int
  2977 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2978 {
  2979 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2980     int retval;
  2981 
  2982     if (!_this) {
  2983         SDL_UninitializedVideo();
  2984         return -1;
  2985     }
  2986     retval = 0;
  2987     switch (attr) {
  2988     case SDL_GL_RED_SIZE:
  2989         _this->gl_config.red_size = value;
  2990         break;
  2991     case SDL_GL_GREEN_SIZE:
  2992         _this->gl_config.green_size = value;
  2993         break;
  2994     case SDL_GL_BLUE_SIZE:
  2995         _this->gl_config.blue_size = value;
  2996         break;
  2997     case SDL_GL_ALPHA_SIZE:
  2998         _this->gl_config.alpha_size = value;
  2999         break;
  3000     case SDL_GL_DOUBLEBUFFER:
  3001         _this->gl_config.double_buffer = value;
  3002         break;
  3003     case SDL_GL_BUFFER_SIZE:
  3004         _this->gl_config.buffer_size = value;
  3005         break;
  3006     case SDL_GL_DEPTH_SIZE:
  3007         _this->gl_config.depth_size = value;
  3008         break;
  3009     case SDL_GL_STENCIL_SIZE:
  3010         _this->gl_config.stencil_size = value;
  3011         break;
  3012     case SDL_GL_ACCUM_RED_SIZE:
  3013         _this->gl_config.accum_red_size = value;
  3014         break;
  3015     case SDL_GL_ACCUM_GREEN_SIZE:
  3016         _this->gl_config.accum_green_size = value;
  3017         break;
  3018     case SDL_GL_ACCUM_BLUE_SIZE:
  3019         _this->gl_config.accum_blue_size = value;
  3020         break;
  3021     case SDL_GL_ACCUM_ALPHA_SIZE:
  3022         _this->gl_config.accum_alpha_size = value;
  3023         break;
  3024     case SDL_GL_STEREO:
  3025         _this->gl_config.stereo = value;
  3026         break;
  3027     case SDL_GL_MULTISAMPLEBUFFERS:
  3028         _this->gl_config.multisamplebuffers = value;
  3029         break;
  3030     case SDL_GL_MULTISAMPLESAMPLES:
  3031         _this->gl_config.multisamplesamples = value;
  3032         break;
  3033     case SDL_GL_ACCELERATED_VISUAL:
  3034         _this->gl_config.accelerated = value;
  3035         break;
  3036     case SDL_GL_RETAINED_BACKING:
  3037         _this->gl_config.retained_backing = value;
  3038         break;
  3039     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3040         _this->gl_config.major_version = value;
  3041         break;
  3042     case SDL_GL_CONTEXT_MINOR_VERSION:
  3043         _this->gl_config.minor_version = value;
  3044         break;
  3045     default:
  3046         SDL_SetError("Unknown OpenGL attribute");
  3047         retval = -1;
  3048         break;
  3049     }
  3050     return retval;
  3051 #else
  3052     SDL_Unsupported();
  3053     return -1;
  3054 #endif /* SDL_VIDEO_OPENGL */
  3055 }
  3056 
  3057 int
  3058 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3059 {
  3060 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3061     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3062     GLenum(APIENTRY * glGetErrorFunc) (void);
  3063     GLenum attrib = 0;
  3064     GLenum error = 0;
  3065 
  3066     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3067     if (!glGetIntegervFunc) {
  3068         return -1;
  3069     }
  3070 
  3071     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3072     if (!glGetErrorFunc) {
  3073         return -1;
  3074     }
  3075 
  3076     /* Clear value in any case */
  3077     *value = 0;
  3078 
  3079     switch (attr) {
  3080     case SDL_GL_RETAINED_BACKING:
  3081         *value = _this->gl_config.retained_backing;
  3082         return 0;
  3083     case SDL_GL_RED_SIZE:
  3084         attrib = GL_RED_BITS;
  3085         break;
  3086     case SDL_GL_BLUE_SIZE:
  3087         attrib = GL_BLUE_BITS;
  3088         break;
  3089     case SDL_GL_GREEN_SIZE:
  3090         attrib = GL_GREEN_BITS;
  3091         break;
  3092     case SDL_GL_ALPHA_SIZE:
  3093         attrib = GL_ALPHA_BITS;
  3094         break;
  3095     case SDL_GL_DOUBLEBUFFER:
  3096 #ifndef SDL_VIDEO_OPENGL_ES
  3097         attrib = GL_DOUBLEBUFFER;
  3098         break;
  3099 #else
  3100         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3101         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3102         /* SDL driver must set proper value after initialization              */
  3103         *value = _this->gl_config.double_buffer;
  3104         return 0;
  3105 #endif
  3106     case SDL_GL_DEPTH_SIZE:
  3107         attrib = GL_DEPTH_BITS;
  3108         break;
  3109     case SDL_GL_STENCIL_SIZE:
  3110         attrib = GL_STENCIL_BITS;
  3111         break;
  3112 #ifndef SDL_VIDEO_OPENGL_ES
  3113     case SDL_GL_ACCUM_RED_SIZE:
  3114         attrib = GL_ACCUM_RED_BITS;
  3115         break;
  3116     case SDL_GL_ACCUM_GREEN_SIZE:
  3117         attrib = GL_ACCUM_GREEN_BITS;
  3118         break;
  3119     case SDL_GL_ACCUM_BLUE_SIZE:
  3120         attrib = GL_ACCUM_BLUE_BITS;
  3121         break;
  3122     case SDL_GL_ACCUM_ALPHA_SIZE:
  3123         attrib = GL_ACCUM_ALPHA_BITS;
  3124         break;
  3125     case SDL_GL_STEREO:
  3126         attrib = GL_STEREO;
  3127         break;
  3128 #else
  3129     case SDL_GL_ACCUM_RED_SIZE:
  3130     case SDL_GL_ACCUM_GREEN_SIZE:
  3131     case SDL_GL_ACCUM_BLUE_SIZE:
  3132     case SDL_GL_ACCUM_ALPHA_SIZE:
  3133     case SDL_GL_STEREO:
  3134         /* none of these are supported in OpenGL ES */
  3135         *value = 0;
  3136         return 0;
  3137 #endif
  3138     case SDL_GL_MULTISAMPLEBUFFERS:
  3139 #ifndef SDL_VIDEO_OPENGL_ES
  3140         attrib = GL_SAMPLE_BUFFERS_ARB;
  3141 #else
  3142         attrib = GL_SAMPLE_BUFFERS;
  3143 #endif
  3144         break;
  3145     case SDL_GL_MULTISAMPLESAMPLES:
  3146 #ifndef SDL_VIDEO_OPENGL_ES
  3147         attrib = GL_SAMPLES_ARB;
  3148 #else
  3149         attrib = GL_SAMPLES;
  3150 #endif
  3151         break;
  3152     case SDL_GL_BUFFER_SIZE:
  3153         {
  3154             GLint bits = 0;
  3155             GLint component;
  3156 
  3157             /*
  3158              * there doesn't seem to be a single flag in OpenGL
  3159              * for this!
  3160              */
  3161             glGetIntegervFunc(GL_RED_BITS, &component);
  3162             bits += component;
  3163             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3164             bits += component;
  3165             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3166             bits += component;
  3167             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3168             bits += component;
  3169 
  3170             *value = bits;
  3171             return 0;
  3172         }
  3173     case SDL_GL_ACCELERATED_VISUAL:
  3174         {
  3175             /* FIXME: How do we get this information? */
  3176             *value = (_this->gl_config.accelerated != 0);
  3177             return 0;
  3178         }
  3179     default:
  3180         SDL_SetError("Unknown OpenGL attribute");
  3181         return -1;
  3182     }
  3183 
  3184     glGetIntegervFunc(attrib, (GLint *) value);
  3185     error = glGetErrorFunc();
  3186     if (error != GL_NO_ERROR) {
  3187         switch (error) {
  3188         case GL_INVALID_ENUM:
  3189             {
  3190                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3191             }
  3192             break;
  3193         case GL_INVALID_VALUE:
  3194             {
  3195                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3196             }
  3197             break;
  3198         default:
  3199             {
  3200                 SDL_SetError("OpenGL error: %08X", error);
  3201             }
  3202             break;
  3203         }
  3204         return -1;
  3205     }
  3206     return 0;
  3207 #else
  3208     SDL_Unsupported();
  3209     return -1;
  3210 #endif /* SDL_VIDEO_OPENGL */
  3211 }
  3212 
  3213 SDL_GLContext
  3214 SDL_GL_CreateContext(SDL_Window * window)
  3215 {
  3216     if (!window) {
  3217         return NULL;
  3218     }
  3219     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3220         SDL_SetError("The specified window isn't an OpenGL window");
  3221         return NULL;
  3222     }
  3223     return _this->GL_CreateContext(_this, window);
  3224 }
  3225 
  3226 int
  3227 SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
  3228 {
  3229     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3230         SDL_SetError("The specified window isn't an OpenGL window");
  3231         return -1;
  3232     }
  3233     if (!context) {
  3234         window = NULL;
  3235     }
  3236     return _this->GL_MakeCurrent(_this, window, context);
  3237 }
  3238 
  3239 int
  3240 SDL_GL_SetSwapInterval(int interval)
  3241 {
  3242     if (!_this) {
  3243         SDL_UninitializedVideo();
  3244         return -1;
  3245     }
  3246     if (_this->GL_SetSwapInterval) {
  3247         return _this->GL_SetSwapInterval(_this, interval);
  3248     } else {
  3249         SDL_SetError("Setting the swap interval is not supported");
  3250         return -1;
  3251     }
  3252 }
  3253 
  3254 int
  3255 SDL_GL_GetSwapInterval(void)
  3256 {
  3257     if (!_this) {
  3258         SDL_UninitializedVideo();
  3259         return -1;
  3260     }
  3261     if (_this->GL_GetSwapInterval) {
  3262         return _this->GL_GetSwapInterval(_this);
  3263     } else {
  3264         SDL_SetError("Getting the swap interval is not supported");
  3265         return -1;
  3266     }
  3267 }
  3268 
  3269 void
  3270 SDL_GL_SwapWindow(SDL_Window * window)
  3271 {
  3272     if (!window) {
  3273         return;
  3274     }
  3275     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3276         SDL_SetError("The specified window isn't an OpenGL window");
  3277         return;
  3278     }
  3279     _this->GL_SwapWindow(_this, window);
  3280 }
  3281 
  3282 void
  3283 SDL_GL_DeleteContext(SDL_GLContext context)
  3284 {
  3285     if (!_this || !context) {
  3286         return;
  3287     }
  3288     _this->GL_MakeCurrent(_this, NULL, NULL);
  3289     _this->GL_DeleteContext(_this, context);
  3290 }
  3291 
  3292 #if 0                           // FIXME
  3293 /*
  3294  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3295  * & 2 for alpha channel.
  3296  */
  3297 static void
  3298 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3299 {
  3300     int x, y;
  3301     Uint32 colorkey;
  3302 #define SET_MASKBIT(icon, x, y, mask) \
  3303 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3304 
  3305     colorkey = icon->format->colorkey;
  3306     switch (icon->format->BytesPerPixel) {
  3307     case 1:
  3308         {
  3309             Uint8 *pixels;
  3310             for (y = 0; y < icon->h; ++y) {
  3311                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3312                 for (x = 0; x < icon->w; ++x) {
  3313                     if (*pixels++ == colorkey) {
  3314                         SET_MASKBIT(icon, x, y, mask);
  3315                     }
  3316                 }
  3317             }
  3318         }
  3319         break;
  3320 
  3321     case 2:
  3322         {
  3323             Uint16 *pixels;
  3324             for (y = 0; y < icon->h; ++y) {
  3325                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3326                 for (x = 0; x < icon->w; ++x) {
  3327                     if ((flags & 1) && *pixels == colorkey) {
  3328                         SET_MASKBIT(icon, x, y, mask);
  3329                     } else if ((flags & 2)
  3330                                && (*pixels & icon->format->Amask) == 0) {
  3331                         SET_MASKBIT(icon, x, y, mask);
  3332                     }
  3333                     pixels++;
  3334                 }
  3335             }
  3336         }
  3337         break;
  3338 
  3339     case 4:
  3340         {
  3341             Uint32 *pixels;
  3342             for (y = 0; y < icon->h; ++y) {
  3343                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3344                 for (x = 0; x < icon->w; ++x) {
  3345                     if ((flags & 1) && *pixels == colorkey) {
  3346                         SET_MASKBIT(icon, x, y, mask);
  3347                     } else if ((flags & 2)
  3348                                && (*pixels & icon->format->Amask) == 0) {
  3349                         SET_MASKBIT(icon, x, y, mask);
  3350                     }
  3351                     pixels++;
  3352                 }
  3353             }
  3354         }
  3355         break;
  3356     }
  3357 }
  3358 
  3359 /*
  3360  * Sets the window manager icon for the display window.
  3361  */
  3362 void
  3363 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3364 {
  3365     if (icon && _this->SetIcon) {
  3366         /* Generate a mask if necessary, and create the icon! */
  3367         if (mask == NULL) {
  3368             int mask_len = icon->h * (icon->w + 7) / 8;
  3369             int flags = 0;
  3370             mask = (Uint8 *) SDL_malloc(mask_len);
  3371             if (mask == NULL) {
  3372                 return;
  3373             }
  3374             SDL_memset(mask, ~0, mask_len);
  3375             if (icon->flags & SDL_SRCCOLORKEY)
  3376                 flags |= 1;
  3377             if (icon->flags & SDL_SRCALPHA)
  3378                 flags |= 2;
  3379             if (flags) {
  3380                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3381             }
  3382             _this->SetIcon(_this, icon, mask);
  3383             SDL_free(mask);
  3384         } else {
  3385             _this->SetIcon(_this, icon, mask);
  3386         }
  3387     }
  3388 }
  3389 #endif
  3390 
  3391 SDL_bool
  3392 SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
  3393 {
  3394     if (!window || !_this->GetWindowWMInfo) {
  3395         return SDL_FALSE;
  3396     }
  3397     return (_this->GetWindowWMInfo(_this, window, info));
  3398 }
  3399 
  3400 void
  3401 SDL_StartTextInput(void)
  3402 {
  3403     if (_this && _this->StartTextInput) {
  3404         _this->StartTextInput(_this);
  3405     }
  3406     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3407     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3408 }
  3409 
  3410 void
  3411 SDL_StopTextInput(void)
  3412 {
  3413     if (_this && _this->StopTextInput) {
  3414         _this->StopTextInput(_this);
  3415     }
  3416     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3417     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3418 }
  3419 
  3420 void
  3421 SDL_SetTextInputRect(SDL_Rect *rect)
  3422 {
  3423     if (_this && _this->SetTextInputRect) {
  3424         _this->SetTextInputRect(_this, rect);
  3425     }
  3426 }
  3427 
  3428 /* vi: set ts=4 sw=4 expandtab: */