src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 22 Jan 2010 15:55:20 +0000
changeset 3692 ad85eb21a559
parent 3686 d87db9cf6a31
child 3693 d3f330fc2009
permissions -rw-r--r--
Fixed infinite loop at shutdown - update texture 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     display->windows = window;
   907 
   908     if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
   909         SDL_DestroyWindow(window);
   910         return NULL;
   911     }
   912 
   913     if (title) {
   914         SDL_SetWindowTitle(window, title);
   915     }
   916     if (flags & SDL_WINDOW_MAXIMIZED) {
   917         SDL_MaximizeWindow(window);
   918     }
   919     if (flags & SDL_WINDOW_MINIMIZED) {
   920         SDL_MinimizeWindow(window);
   921     }
   922     if (flags & SDL_WINDOW_SHOWN) {
   923         SDL_ShowWindow(window);
   924     }
   925     SDL_UpdateWindowGrab(window);
   926 
   927     return window;
   928 }
   929 
   930 SDL_Window *
   931 SDL_CreateWindowFrom(const void *data)
   932 {
   933     SDL_VideoDisplay *display;
   934     SDL_Window *window;
   935 
   936     if (!_this) {
   937         SDL_UninitializedVideo();
   938         return NULL;
   939     }
   940     display = SDL_CurrentDisplay;
   941     window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
   942     window->id = _this->next_object_id++;
   943     window->flags = SDL_WINDOW_FOREIGN;
   944     window->display = display;
   945     window->next = display->windows;
   946     display->windows = window;
   947 
   948     if (!_this->CreateWindowFrom ||
   949         _this->CreateWindowFrom(_this, window, data) < 0) {
   950         SDL_DestroyWindow(window);
   951         return NULL;
   952     }
   953     return window;
   954 }
   955 
   956 int
   957 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
   958 {
   959     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   960                                   SDL_WINDOW_OPENGL |
   961                                   SDL_WINDOW_BORDERLESS |
   962                                   SDL_WINDOW_RESIZABLE |
   963                                   SDL_WINDOW_INPUT_GRABBED |
   964                                   SDL_WINDOW_FOREIGN);
   965     char *title = window->title;
   966 
   967     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   968         SDL_SetError("No OpenGL support in video driver");
   969         return -1;
   970     }
   971     if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
   972         if (flags & SDL_WINDOW_OPENGL) {
   973             SDL_GL_LoadLibrary(NULL);
   974         } else {
   975             SDL_GL_UnloadLibrary();
   976         }
   977     }
   978 
   979     if (window->flags & SDL_WINDOW_FOREIGN) {
   980         /* Can't destroy and re-create foreign windows, hrm */
   981         flags |= SDL_WINDOW_FOREIGN;
   982     } else {
   983         flags &= ~SDL_WINDOW_FOREIGN;
   984     }
   985 
   986     if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
   987         _this->DestroyWindow(_this, window);
   988     }
   989 
   990     window->title = NULL;
   991     window->flags = (flags & allowed_flags);
   992 
   993     if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
   994         if (_this->CreateWindow(_this, window) < 0) {
   995             if (flags & SDL_WINDOW_OPENGL) {
   996                 SDL_GL_UnloadLibrary();
   997             }
   998             return -1;
   999         }
  1000     }
  1001 
  1002     if (title) {
  1003         SDL_SetWindowTitle(window, title);
  1004         SDL_free(title);
  1005     }
  1006     if (flags & SDL_WINDOW_MAXIMIZED) {
  1007         SDL_MaximizeWindow(window);
  1008     }
  1009     if (flags & SDL_WINDOW_MINIMIZED) {
  1010         SDL_MinimizeWindow(window);
  1011     }
  1012     if (flags & SDL_WINDOW_SHOWN) {
  1013         SDL_ShowWindow(window);
  1014     }
  1015     SDL_UpdateWindowGrab(window);
  1016 
  1017     return 0;
  1018 }
  1019 
  1020 static __inline__ SDL_Renderer *
  1021 SDL_GetCurrentRenderer(SDL_bool create)
  1022 {
  1023     if (!_this) {
  1024         SDL_UninitializedVideo();
  1025         return NULL;
  1026     }
  1027     if (!SDL_CurrentRenderer) {
  1028         if (!create) {
  1029             SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1030             return NULL;
  1031         }
  1032         if (SDL_CreateRenderer(0, -1, 0) < 0) {
  1033             return NULL;
  1034         }
  1035     }
  1036     return SDL_CurrentRenderer;
  1037 }
  1038 
  1039 Uint32
  1040 SDL_GetWindowID(SDL_Window * window)
  1041 {
  1042     if (!window) {
  1043         return 0;
  1044     }
  1045     return window->id;
  1046 }
  1047 
  1048 SDL_Window *
  1049 SDL_GetWindowFromID(Uint32 id)
  1050 {
  1051     SDL_Window *window;
  1052     int i;
  1053 
  1054     if (!_this) {
  1055         return NULL;
  1056     }
  1057     /* FIXME: Should we keep a separate hash table for these? */
  1058     for (i = _this->num_displays; i--;) {
  1059         SDL_VideoDisplay *display = &_this->displays[i];
  1060         for (window = display->windows; window; window = window->next) {
  1061             if (window->id == id) {
  1062                 return window;
  1063             }
  1064         }
  1065     }
  1066     return NULL;
  1067 }
  1068 
  1069 Uint32
  1070 SDL_GetWindowFlags(SDL_Window * window)
  1071 {
  1072     if (!window) {
  1073         return 0;
  1074     }
  1075     return window->flags;
  1076 }
  1077 
  1078 void
  1079 SDL_SetWindowTitle(SDL_Window * window, const char *title)
  1080 {
  1081     if (!window || title == window->title) {
  1082         return;
  1083     }
  1084     if (window->title) {
  1085         SDL_free(window->title);
  1086     }
  1087     if (title) {
  1088         window->title = SDL_strdup(title);
  1089     } else {
  1090         window->title = NULL;
  1091     }
  1092 
  1093     if (_this->SetWindowTitle) {
  1094         _this->SetWindowTitle(_this, window);
  1095     }
  1096 }
  1097 
  1098 const char *
  1099 SDL_GetWindowTitle(SDL_Window * window)
  1100 {
  1101     if (!window) {
  1102         return NULL;
  1103     }
  1104     return window->title;
  1105 }
  1106 
  1107 void
  1108 SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
  1109 {
  1110     if (!window) {
  1111         return;
  1112     }
  1113     if (_this->SetWindowIcon) {
  1114         _this->SetWindowIcon(_this, window, icon);
  1115     }
  1116 }
  1117 
  1118 void
  1119 SDL_SetWindowData(SDL_Window * window, void *userdata)
  1120 {
  1121     if (!window) {
  1122         return;
  1123     }
  1124     window->userdata = userdata;
  1125 }
  1126 
  1127 void *
  1128 SDL_GetWindowData(SDL_Window * window)
  1129 {
  1130     if (!window) {
  1131         return NULL;
  1132     }
  1133     return window->userdata;
  1134 }
  1135 
  1136 void
  1137 SDL_SetWindowPosition(SDL_Window * window, int x, int y)
  1138 {
  1139     if (!window) {
  1140         return;
  1141     }
  1142     if (x != SDL_WINDOWPOS_UNDEFINED) {
  1143         window->x = x;
  1144     }
  1145     if (y != SDL_WINDOWPOS_UNDEFINED) {
  1146         window->y = y;
  1147     }
  1148     if (_this->SetWindowPosition) {
  1149         _this->SetWindowPosition(_this, window);
  1150     }
  1151     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
  1152 }
  1153 
  1154 void
  1155 SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
  1156 {
  1157     if (!window) {
  1158         return;
  1159     }
  1160     if (x) {
  1161         *x = window->x;
  1162     }
  1163     if (y) {
  1164         *y = window->y;
  1165     }
  1166 }
  1167 
  1168 void
  1169 SDL_SetWindowSize(SDL_Window * window, int w, int h)
  1170 {
  1171     if (!window) {
  1172         return;
  1173     }
  1174     window->w = w;
  1175     window->h = h;
  1176 
  1177     if (_this->SetWindowSize) {
  1178         _this->SetWindowSize(_this, window);
  1179     }
  1180     SDL_OnWindowResized(window);
  1181 }
  1182 
  1183 void
  1184 SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
  1185 {
  1186     if (window) {
  1187         if (w) {
  1188             *w = window->w;
  1189         }
  1190         if (h) {
  1191             *h = window->h;
  1192         }
  1193     } else {
  1194         if (w) {
  1195             *w = 0;
  1196         }
  1197         if (h) {
  1198             *h = 0;
  1199         }
  1200     }
  1201 }
  1202 
  1203 void
  1204 SDL_ShowWindow(SDL_Window * window)
  1205 {
  1206     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1207         return;
  1208     }
  1209 
  1210     if (_this->ShowWindow) {
  1211         _this->ShowWindow(_this, window);
  1212     }
  1213     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1214 }
  1215 
  1216 void
  1217 SDL_HideWindow(SDL_Window * window)
  1218 {
  1219     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1220         return;
  1221     }
  1222 
  1223     if (_this->HideWindow) {
  1224         _this->HideWindow(_this, window);
  1225     }
  1226     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1227 }
  1228 
  1229 void
  1230 SDL_RaiseWindow(SDL_Window * window)
  1231 {
  1232     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1233         return;
  1234     }
  1235     if (_this->RaiseWindow) {
  1236         _this->RaiseWindow(_this, window);
  1237     } else {
  1238         /* FIXME: What we really want is a way to request focus */
  1239         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  1240     }
  1241 }
  1242 
  1243 void
  1244 SDL_MaximizeWindow(SDL_Window * window)
  1245 {
  1246     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1247         return;
  1248     }
  1249 
  1250     if (_this->MaximizeWindow) {
  1251         _this->MaximizeWindow(_this, window);
  1252     }
  1253     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1254 }
  1255 
  1256 void
  1257 SDL_MinimizeWindow(SDL_Window * window)
  1258 {
  1259     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1260         return;
  1261     }
  1262 
  1263     if (_this->MinimizeWindow) {
  1264         _this->MinimizeWindow(_this, window);
  1265     }
  1266     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1267 }
  1268 
  1269 void
  1270 SDL_RestoreWindow(SDL_Window * window)
  1271 {
  1272     if (!window
  1273         || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1274         return;
  1275     }
  1276 
  1277     if (_this->RestoreWindow) {
  1278         _this->RestoreWindow(_this, window);
  1279     }
  1280     SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1281 }
  1282 
  1283 int
  1284 SDL_SetWindowFullscreen(SDL_Window * window, int fullscreen)
  1285 {
  1286     if (!window) {
  1287         return -1;
  1288     }
  1289     if (fullscreen) {
  1290         fullscreen = SDL_WINDOW_FULLSCREEN;
  1291     }
  1292     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1293         return 0;
  1294     }
  1295     if (fullscreen) {
  1296         window->flags |= SDL_WINDOW_FULLSCREEN;
  1297 
  1298         SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1299     } else {
  1300         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1301 
  1302         SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1303     }
  1304     return 0;
  1305 }
  1306 
  1307 void
  1308 SDL_SetWindowGrab(SDL_Window * window, int mode)
  1309 {
  1310     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1311         return;
  1312     }
  1313     if (mode) {
  1314         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1315     } else {
  1316         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1317     }
  1318     SDL_UpdateWindowGrab(window);
  1319 }
  1320 
  1321 static void
  1322 SDL_UpdateWindowGrab(SDL_Window * window)
  1323 {
  1324     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1325         _this->SetWindowGrab(_this, window);
  1326     }
  1327 }
  1328 
  1329 int
  1330 SDL_GetWindowGrab(SDL_Window * window)
  1331 {
  1332     if (!window) {
  1333         return 0;
  1334     }
  1335     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1336 }
  1337 
  1338 void
  1339 SDL_OnWindowShown(SDL_Window * window)
  1340 {
  1341     SDL_RaiseWindow(window);
  1342     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1343 }
  1344 
  1345 void
  1346 SDL_OnWindowHidden(SDL_Window * window)
  1347 {
  1348     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1349 }
  1350 
  1351 void
  1352 SDL_OnWindowResized(SDL_Window * window)
  1353 {
  1354     SDL_Renderer *renderer = window->renderer;
  1355 
  1356     if (renderer && renderer->DisplayModeChanged) {
  1357         renderer->DisplayModeChanged(renderer);
  1358     }
  1359 }
  1360 
  1361 void
  1362 SDL_OnWindowMinimized(SDL_Window * window)
  1363 {
  1364     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1365 }
  1366 
  1367 void
  1368 SDL_OnWindowRestored(SDL_Window * window)
  1369 {
  1370     SDL_RaiseWindow(window);
  1371     SDL_UpdateFullscreenMode(window, SDL_TRUE);
  1372 }
  1373 
  1374 void
  1375 SDL_OnWindowFocusGained(SDL_Window * window)
  1376 {
  1377     SDL_VideoDisplay *display = window->display;
  1378 
  1379     if (display->gamma && _this->SetDisplayGammaRamp) {
  1380         _this->SetDisplayGammaRamp(_this, display, display->gamma);
  1381     }
  1382     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1383         && _this->SetWindowGrab) {
  1384         _this->SetWindowGrab(_this, window);
  1385     }
  1386 }
  1387 
  1388 void
  1389 SDL_OnWindowFocusLost(SDL_Window * window)
  1390 {
  1391     SDL_VideoDisplay *display = window->display;
  1392 
  1393     /* If we're fullscreen on a single-head system and lose focus, minimize */
  1394     if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
  1395         _this->num_displays == 1) {
  1396         SDL_MinimizeWindow(window);
  1397     }
  1398 
  1399     if (display->gamma && _this->SetDisplayGammaRamp) {
  1400         _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
  1401     }
  1402     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
  1403         && _this->SetWindowGrab) {
  1404         _this->SetWindowGrab(_this, window);
  1405     }
  1406 }
  1407 
  1408 SDL_Window *
  1409 SDL_GetFocusWindow(void)
  1410 {
  1411     SDL_VideoDisplay *display;
  1412     SDL_Window *window;
  1413 
  1414     if (!_this) {
  1415         return NULL;
  1416     }
  1417     display = SDL_CurrentDisplay;
  1418     for (window = display->windows; window; window = window->next) {
  1419         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1420             return window;
  1421         }
  1422     }
  1423     return NULL;
  1424 }
  1425 
  1426 void
  1427 SDL_DestroyWindow(SDL_Window * window)
  1428 {
  1429     SDL_VideoDisplay *display;
  1430 
  1431     if (!_this || !window || !window->id) {
  1432         SDL_SetError("Invalid window");
  1433         return;
  1434     }
  1435 
  1436     if (window->title) {
  1437         SDL_free(window->title);
  1438     }
  1439     if (window->renderer) {
  1440         SDL_DestroyRenderer(window);
  1441     }
  1442 
  1443     /* Restore video mode, etc. */
  1444     SDL_UpdateFullscreenMode(window, SDL_FALSE);
  1445 
  1446     if (_this->DestroyWindow) {
  1447         _this->DestroyWindow(_this, window);
  1448     }
  1449     if (window->flags & SDL_WINDOW_OPENGL) {
  1450         SDL_GL_UnloadLibrary();
  1451     }
  1452 
  1453     /* Unlink the window from the list */
  1454     display = window->display;
  1455     if (window->prev) {
  1456         window->prev->next = window->next;
  1457     } else {
  1458         display->windows = window->next;
  1459     }
  1460 
  1461     /* Clear the ID so we know it was destroyed */
  1462     window->id = 0;
  1463 
  1464     SDL_free(window);
  1465 }
  1466 
  1467 void
  1468 SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
  1469 {
  1470     SDL_RenderDriver *render_drivers;
  1471 
  1472     render_drivers =
  1473         SDL_realloc(display->render_drivers,
  1474                     (display->num_render_drivers +
  1475                      1) * sizeof(*render_drivers));
  1476     if (render_drivers) {
  1477         render_drivers[display->num_render_drivers] = *driver;
  1478         display->render_drivers = render_drivers;
  1479         display->num_render_drivers++;
  1480     }
  1481 }
  1482 
  1483 int
  1484 SDL_GetNumRenderDrivers(void)
  1485 {
  1486     if (_this) {
  1487         return SDL_CurrentDisplay->num_render_drivers;
  1488     }
  1489     return 0;
  1490 }
  1491 
  1492 int
  1493 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1494 {
  1495     if (!_this) {
  1496         SDL_UninitializedVideo();
  1497         return -1;
  1498     }
  1499     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1500         SDL_SetError("index must be in the range of 0 - %d",
  1501                      SDL_GetNumRenderDrivers() - 1);
  1502         return -1;
  1503     }
  1504     *info = SDL_CurrentDisplay->render_drivers[index].info;
  1505     return 0;
  1506 }
  1507 
  1508 int
  1509 SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  1510 {
  1511     if (!window) {
  1512         SDL_SetError("Invalid window");
  1513         return -1;
  1514     }
  1515 
  1516     /* Free any existing renderer */
  1517     SDL_DestroyRenderer(window);
  1518 
  1519     if (index < 0) {
  1520         char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1521         int n = SDL_GetNumRenderDrivers();
  1522 
  1523 #if SDL_VIDEO_RENDER_OGL
  1524         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1525             override = "opengl";
  1526         }
  1527 #endif /* SDL_VIDEO_RENDER_OGL */
  1528 #if SDL_VIDEO_RENDER_OGL_ES
  1529         if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
  1530             override = "opengl_es";
  1531         }
  1532 #endif /* SDL_VIDEO_RENDER_OGL_ES */
  1533         if (override) {
  1534             for (index = 0; index < n; ++index) {
  1535                 SDL_RenderDriver *driver =
  1536                     &SDL_CurrentDisplay->render_drivers[index];
  1537 
  1538                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1539                     /* Create a new renderer instance */
  1540                     window->renderer = driver->CreateRenderer(window, flags);
  1541                     break;
  1542                 }
  1543             }
  1544         } else {
  1545             for (index = 0; index < n; ++index) {
  1546                 SDL_RenderDriver *driver =
  1547                     &SDL_CurrentDisplay->render_drivers[index];
  1548 
  1549                 if ((driver->info.flags & flags) == flags) {
  1550                     /* Create a new renderer instance */
  1551                     window->renderer = driver->CreateRenderer(window, flags);
  1552                     if (window->renderer) {
  1553                         /* Yay, we got one! */
  1554                         break;
  1555                     }
  1556                 }
  1557             }
  1558         }
  1559         if (index == n) {
  1560             SDL_SetError("Couldn't find matching render driver");
  1561             return -1;
  1562         }
  1563     } else {
  1564         if (index >= SDL_GetNumRenderDrivers()) {
  1565             SDL_SetError("index must be -1 or in the range of 0 - %d",
  1566                          SDL_GetNumRenderDrivers() - 1);
  1567             return -1;
  1568         }
  1569         /* Create a new renderer instance */
  1570         window->renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags);
  1571     }
  1572 
  1573     if (window->renderer == NULL) {
  1574         /* Assuming renderer set its error */
  1575         return -1;
  1576     }
  1577 
  1578     SDL_SelectRenderer(window);
  1579 
  1580     return 0;
  1581 }
  1582 
  1583 int
  1584 SDL_SelectRenderer(SDL_Window * window)
  1585 {
  1586     SDL_Renderer *renderer;
  1587 
  1588     if (!window) {
  1589         SDL_SetError("Invalid window");
  1590         return -1;
  1591     }
  1592     renderer = window->renderer;
  1593     if (!renderer) {
  1594         SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
  1595         return -1;
  1596     }
  1597     if (renderer->ActivateRenderer) {
  1598         if (renderer->ActivateRenderer(renderer) < 0) {
  1599             return -1;
  1600         }
  1601     }
  1602     SDL_CurrentDisplay->current_renderer = renderer;
  1603     return 0;
  1604 }
  1605 
  1606 int
  1607 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1608 {
  1609     SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
  1610     if (!renderer) {
  1611         return -1;
  1612     }
  1613     *info = renderer->info;
  1614     return 0;
  1615 }
  1616 
  1617 SDL_Texture *
  1618 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1619 {
  1620     SDL_Renderer *renderer;
  1621     SDL_Texture *texture;
  1622 
  1623     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1624     if (!renderer) {
  1625         return 0;
  1626     }
  1627     if (!renderer->CreateTexture) {
  1628         SDL_Unsupported();
  1629         return 0;
  1630     }
  1631     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1632     if (!texture) {
  1633         SDL_OutOfMemory();
  1634         return 0;
  1635     }
  1636     texture->format = format;
  1637     texture->access = access;
  1638     texture->w = w;
  1639     texture->h = h;
  1640     texture->r = 255;
  1641     texture->g = 255;
  1642     texture->b = 255;
  1643     texture->a = 255;
  1644     texture->renderer = renderer;
  1645     texture->next = renderer->textures;
  1646     if (renderer->textures) {
  1647         renderer->textures->prev = texture;
  1648     }
  1649     renderer->textures = texture;
  1650 
  1651     if (renderer->CreateTexture(renderer, texture) < 0) {
  1652         SDL_DestroyTexture(texture);
  1653         return 0;
  1654     }
  1655     return texture;
  1656 }
  1657 
  1658 SDL_Texture *
  1659 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1660 {
  1661     SDL_Texture *texture;
  1662     Uint32 requested_format = format;
  1663     SDL_PixelFormat *fmt;
  1664     SDL_Renderer *renderer;
  1665     int bpp;
  1666     Uint32 Rmask, Gmask, Bmask, Amask;
  1667 
  1668     if (!surface) {
  1669         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1670         return 0;
  1671     }
  1672     fmt = surface->format;
  1673 
  1674     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1675     if (!renderer) {
  1676         return 0;
  1677     }
  1678 
  1679     if (format) {
  1680         if (!SDL_PixelFormatEnumToMasks
  1681             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1682             SDL_SetError("Unknown pixel format");
  1683             return 0;
  1684         }
  1685     } else {
  1686         if (surface->format->Amask
  1687             || !(surface->map->info.flags &
  1688                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1689             Uint32 it;
  1690             int pfmt;
  1691 
  1692             /* Pixel formats, sorted by best first */
  1693             static const Uint32 sdl_pformats[] = {
  1694                 SDL_PIXELFORMAT_ARGB8888,
  1695                 SDL_PIXELFORMAT_RGBA8888,
  1696                 SDL_PIXELFORMAT_ABGR8888,
  1697                 SDL_PIXELFORMAT_BGRA8888,
  1698                 SDL_PIXELFORMAT_RGB888,
  1699                 SDL_PIXELFORMAT_BGR888,
  1700                 SDL_PIXELFORMAT_RGB24,
  1701                 SDL_PIXELFORMAT_BGR24,
  1702                 SDL_PIXELFORMAT_RGB565,
  1703                 SDL_PIXELFORMAT_BGR565,
  1704                 SDL_PIXELFORMAT_ARGB1555,
  1705                 SDL_PIXELFORMAT_ABGR1555,
  1706                 SDL_PIXELFORMAT_RGB555,
  1707                 SDL_PIXELFORMAT_BGR555,
  1708                 SDL_PIXELFORMAT_ARGB4444,
  1709                 SDL_PIXELFORMAT_ABGR4444,
  1710                 SDL_PIXELFORMAT_RGB444,
  1711                 SDL_PIXELFORMAT_ARGB2101010,
  1712                 SDL_PIXELFORMAT_INDEX8,
  1713                 SDL_PIXELFORMAT_INDEX4LSB,
  1714                 SDL_PIXELFORMAT_INDEX4MSB,
  1715                 SDL_PIXELFORMAT_RGB332,
  1716                 SDL_PIXELFORMAT_INDEX1LSB,
  1717                 SDL_PIXELFORMAT_INDEX1MSB,
  1718                 SDL_PIXELFORMAT_UNKNOWN
  1719             };
  1720 
  1721             bpp = fmt->BitsPerPixel;
  1722             Rmask = fmt->Rmask;
  1723             Gmask = fmt->Gmask;
  1724             Bmask = fmt->Bmask;
  1725             Amask = fmt->Amask;
  1726 
  1727             format =
  1728                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1729             if (!format) {
  1730                 SDL_SetError("Unknown pixel format");
  1731                 return 0;
  1732             }
  1733 
  1734             /* Search requested format in the supported texture */
  1735             /* formats by current renderer                      */
  1736             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1737                 if (renderer->info.texture_formats[it] == format) {
  1738                     break;
  1739                 }
  1740             }
  1741 
  1742             /* If requested format can't be found, search any best */
  1743             /* format which renderer provides                      */
  1744             if (it == renderer->info.num_texture_formats) {
  1745                 pfmt = 0;
  1746                 for (;;) {
  1747                     if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1748                         break;
  1749                     }
  1750 
  1751                     for (it = 0; it < renderer->info.num_texture_formats;
  1752                          it++) {
  1753                         if (renderer->info.texture_formats[it] ==
  1754                             sdl_pformats[pfmt]) {
  1755                             break;
  1756                         }
  1757                     }
  1758 
  1759                     if (it != renderer->info.num_texture_formats) {
  1760                         /* The best format has been found */
  1761                         break;
  1762                     }
  1763                     pfmt++;
  1764                 }
  1765 
  1766                 /* If any format can't be found, then return an error */
  1767                 if (it == renderer->info.num_texture_formats) {
  1768                     SDL_SetError
  1769                         ("Any of the supported pixel formats can't be found");
  1770                     return 0;
  1771                 }
  1772 
  1773                 /* Convert found pixel format back to color masks */
  1774                 if (SDL_PixelFormatEnumToMasks
  1775                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1776                      &Bmask, &Amask) != SDL_TRUE) {
  1777                     SDL_SetError("Unknown pixel format");
  1778                     return 0;
  1779                 }
  1780             }
  1781         } else {
  1782             /* Need a format with alpha */
  1783             Uint32 it;
  1784             int apfmt;
  1785 
  1786             /* Pixel formats with alpha, sorted by best first */
  1787             static const Uint32 sdl_alpha_pformats[] = {
  1788                 SDL_PIXELFORMAT_ARGB8888,
  1789                 SDL_PIXELFORMAT_RGBA8888,
  1790                 SDL_PIXELFORMAT_ABGR8888,
  1791                 SDL_PIXELFORMAT_BGRA8888,
  1792                 SDL_PIXELFORMAT_ARGB1555,
  1793                 SDL_PIXELFORMAT_ABGR1555,
  1794                 SDL_PIXELFORMAT_ARGB4444,
  1795                 SDL_PIXELFORMAT_ABGR4444,
  1796                 SDL_PIXELFORMAT_ARGB2101010,
  1797                 SDL_PIXELFORMAT_UNKNOWN
  1798             };
  1799 
  1800             if (surface->format->Amask) {
  1801                 /* If surface already has alpha, then try an original */
  1802                 /* surface format first                               */
  1803                 bpp = fmt->BitsPerPixel;
  1804                 Rmask = fmt->Rmask;
  1805                 Gmask = fmt->Gmask;
  1806                 Bmask = fmt->Bmask;
  1807                 Amask = fmt->Amask;
  1808             } else {
  1809                 bpp = 32;
  1810                 Rmask = 0x00FF0000;
  1811                 Gmask = 0x0000FF00;
  1812                 Bmask = 0x000000FF;
  1813                 Amask = 0xFF000000;
  1814             }
  1815 
  1816             format =
  1817                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1818             if (!format) {
  1819                 SDL_SetError("Unknown pixel format");
  1820                 return 0;
  1821             }
  1822 
  1823             /* Search this format in the supported texture formats */
  1824             /* by current renderer                                 */
  1825             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1826                 if (renderer->info.texture_formats[it] == format) {
  1827                     break;
  1828                 }
  1829             }
  1830 
  1831             /* If this format can't be found, search any best       */
  1832             /* compatible format with alpha which renderer provides */
  1833             if (it == renderer->info.num_texture_formats) {
  1834                 apfmt = 0;
  1835                 for (;;) {
  1836                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1837                         break;
  1838                     }
  1839 
  1840                     for (it = 0; it < renderer->info.num_texture_formats;
  1841                          it++) {
  1842                         if (renderer->info.texture_formats[it] ==
  1843                             sdl_alpha_pformats[apfmt]) {
  1844                             break;
  1845                         }
  1846                     }
  1847 
  1848                     if (it != renderer->info.num_texture_formats) {
  1849                         /* Compatible format has been found */
  1850                         break;
  1851                     }
  1852                     apfmt++;
  1853                 }
  1854 
  1855                 /* If compatible format can't be found, then return an error */
  1856                 if (it == renderer->info.num_texture_formats) {
  1857                     SDL_SetError("Compatible pixel format can't be found");
  1858                     return 0;
  1859                 }
  1860 
  1861                 /* Convert found pixel format back to color masks */
  1862                 if (SDL_PixelFormatEnumToMasks
  1863                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1864                      &Bmask, &Amask) != SDL_TRUE) {
  1865                     SDL_SetError("Unknown pixel format");
  1866                     return 0;
  1867                 }
  1868             }
  1869         }
  1870 
  1871         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1872         if (!format) {
  1873             SDL_SetError("Unknown pixel format");
  1874             return 0;
  1875         }
  1876     }
  1877 
  1878     texture =
  1879         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1880                           surface->h);
  1881     if (!texture && !requested_format) {
  1882         SDL_DisplayMode desktop_mode;
  1883         SDL_GetDesktopDisplayMode(&desktop_mode);
  1884         format = desktop_mode.format;
  1885         texture =
  1886             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1887                               surface->h);
  1888     }
  1889     if (!texture) {
  1890         return 0;
  1891     }
  1892     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1893         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1894         if (SDL_MUSTLOCK(surface)) {
  1895             SDL_LockSurface(surface);
  1896             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1897                               surface->pitch);
  1898             SDL_UnlockSurface(surface);
  1899         } else {
  1900             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1901                               surface->pitch);
  1902         }
  1903     } else {
  1904         SDL_PixelFormat dst_fmt;
  1905         SDL_Surface *dst = NULL;
  1906 
  1907         /* Set up a destination surface for the texture update */
  1908         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  1909         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1910             dst_fmt.palette =
  1911                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1912             if (dst_fmt.palette) {
  1913                 /*
  1914                  * FIXME: Should we try to copy
  1915                  * fmt->palette?
  1916                  */
  1917                 SDL_DitherColors(dst_fmt.palette->colors,
  1918                                  SDL_BITSPERPIXEL(format));
  1919             }
  1920         }
  1921         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  1922         if (dst) {
  1923             SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
  1924             SDL_FreeSurface(dst);
  1925         }
  1926         if (dst_fmt.palette) {
  1927             SDL_FreePalette(dst_fmt.palette);
  1928         }
  1929         if (!dst) {
  1930             SDL_DestroyTexture(texture);
  1931             return 0;
  1932         }
  1933     }
  1934 
  1935     {
  1936         Uint8 r, g, b, a;
  1937         int blendMode;
  1938         int scaleMode;
  1939 
  1940         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1941         SDL_SetTextureColorMod(texture, r, g, b);
  1942 
  1943         SDL_GetSurfaceAlphaMod(surface, &a);
  1944         SDL_SetTextureAlphaMod(texture, a);
  1945 
  1946         SDL_GetSurfaceBlendMode(surface, &blendMode);
  1947         SDL_SetTextureBlendMode(texture, blendMode);
  1948 
  1949         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  1950         SDL_SetTextureScaleMode(texture, scaleMode);
  1951     }
  1952 
  1953     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1954         SDL_SetTexturePalette(texture, fmt->palette->colors, 0,
  1955                               fmt->palette->ncolors);
  1956     }
  1957     return texture;
  1958 }
  1959 
  1960 int
  1961 SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  1962                  int *w, int *h)
  1963 {
  1964     if (!texture) {
  1965         return -1;
  1966     }
  1967     if (format) {
  1968         *format = texture->format;
  1969     }
  1970     if (access) {
  1971         *access = texture->access;
  1972     }
  1973     if (w) {
  1974         *w = texture->w;
  1975     }
  1976     if (h) {
  1977         *h = texture->h;
  1978     }
  1979     return 0;
  1980 }
  1981 
  1982 int
  1983 SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
  1984 {
  1985     SDL_Renderer *renderer;
  1986 
  1987     if (!texture) {
  1988         return -1;
  1989     }
  1990     renderer = texture->renderer;
  1991     if (!renderer->QueryTexturePixels) {
  1992         SDL_Unsupported();
  1993         return -1;
  1994     }
  1995     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1996 }
  1997 
  1998 int
  1999 SDL_SetTexturePalette(SDL_Texture * texture, const SDL_Color * colors,
  2000                       int firstcolor, int ncolors)
  2001 {
  2002     SDL_Renderer *renderer;
  2003 
  2004     if (!texture) {
  2005         return -1;
  2006     }
  2007     renderer = texture->renderer;
  2008     if (!renderer->SetTexturePalette) {
  2009         SDL_Unsupported();
  2010         return -1;
  2011     }
  2012     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  2013                                        ncolors);
  2014 }
  2015 
  2016 int
  2017 SDL_GetTexturePalette(SDL_Texture * texture, SDL_Color * colors,
  2018                       int firstcolor, int ncolors)
  2019 {
  2020     SDL_Renderer *renderer;
  2021 
  2022     if (!texture) {
  2023         return -1;
  2024     }
  2025     renderer = texture->renderer;
  2026     if (!renderer->GetTexturePalette) {
  2027         SDL_Unsupported();
  2028         return -1;
  2029     }
  2030     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  2031                                        ncolors);
  2032 }
  2033 
  2034 int
  2035 SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  2036 {
  2037     SDL_Renderer *renderer;
  2038 
  2039     if (!texture) {
  2040         return -1;
  2041     }
  2042     renderer = texture->renderer;
  2043     if (!renderer->SetTextureColorMod) {
  2044         SDL_Unsupported();
  2045         return -1;
  2046     }
  2047     if (r < 255 || g < 255 || b < 255) {
  2048         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  2049     } else {
  2050         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  2051     }
  2052     texture->r = r;
  2053     texture->g = g;
  2054     texture->b = b;
  2055     return renderer->SetTextureColorMod(renderer, texture);
  2056 }
  2057 
  2058 int
  2059 SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  2060                        Uint8 * b)
  2061 {
  2062     SDL_Renderer *renderer;
  2063 
  2064     if (!texture) {
  2065         return -1;
  2066     }
  2067     renderer = texture->renderer;
  2068     if (r) {
  2069         *r = texture->r;
  2070     }
  2071     if (g) {
  2072         *g = texture->g;
  2073     }
  2074     if (b) {
  2075         *b = texture->b;
  2076     }
  2077     return 0;
  2078 }
  2079 
  2080 int
  2081 SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  2082 {
  2083     SDL_Renderer *renderer;
  2084 
  2085     if (!texture) {
  2086         return -1;
  2087     }
  2088     renderer = texture->renderer;
  2089     if (!renderer->SetTextureAlphaMod) {
  2090         SDL_Unsupported();
  2091         return -1;
  2092     }
  2093     if (alpha < 255) {
  2094         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  2095     } else {
  2096         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  2097     }
  2098     texture->a = alpha;
  2099     return renderer->SetTextureAlphaMod(renderer, texture);
  2100 }
  2101 
  2102 int
  2103 SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  2104 {
  2105     if (!texture) {
  2106         return -1;
  2107     }
  2108     if (alpha) {
  2109         *alpha = texture->a;
  2110     }
  2111     return 0;
  2112 }
  2113 
  2114 int
  2115 SDL_SetTextureBlendMode(SDL_Texture * texture, int blendMode)
  2116 {
  2117     SDL_Renderer *renderer;
  2118 
  2119     if (!texture) {
  2120         return -1;
  2121     }
  2122     renderer = texture->renderer;
  2123     if (!renderer->SetTextureBlendMode) {
  2124         SDL_Unsupported();
  2125         return -1;
  2126     }
  2127     texture->blendMode = blendMode;
  2128     return renderer->SetTextureBlendMode(renderer, texture);
  2129 }
  2130 
  2131 int
  2132 SDL_GetTextureBlendMode(SDL_Texture * texture, int *blendMode)
  2133 {
  2134     if (!texture) {
  2135         return -1;
  2136     }
  2137     if (blendMode) {
  2138         *blendMode = texture->blendMode;
  2139     }
  2140     return 0;
  2141 }
  2142 
  2143 int
  2144 SDL_SetTextureScaleMode(SDL_Texture * texture, int scaleMode)
  2145 {
  2146     SDL_Renderer *renderer;
  2147 
  2148     if (!texture) {
  2149         return -1;
  2150     }
  2151     renderer = texture->renderer;
  2152     if (!renderer->SetTextureScaleMode) {
  2153         SDL_Unsupported();
  2154         return -1;
  2155     }
  2156     texture->scaleMode = scaleMode;
  2157     return renderer->SetTextureScaleMode(renderer, texture);
  2158 }
  2159 
  2160 int
  2161 SDL_GetTextureScaleMode(SDL_Texture * texture, int *scaleMode)
  2162 {
  2163     if (!texture) {
  2164         return -1;
  2165     }
  2166     if (scaleMode) {
  2167         *scaleMode = texture->scaleMode;
  2168     }
  2169     return 0;
  2170 }
  2171 
  2172 int
  2173 SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  2174                   const void *pixels, int pitch)
  2175 {
  2176     SDL_Renderer *renderer;
  2177     SDL_Rect full_rect;
  2178 
  2179     if (!texture) {
  2180         return -1;
  2181     }
  2182     renderer = texture->renderer;
  2183     if (!renderer->UpdateTexture) {
  2184         SDL_Unsupported();
  2185         return -1;
  2186     }
  2187     if (!rect) {
  2188         full_rect.x = 0;
  2189         full_rect.y = 0;
  2190         full_rect.w = texture->w;
  2191         full_rect.h = texture->h;
  2192         rect = &full_rect;
  2193     }
  2194     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2195 }
  2196 
  2197 int
  2198 SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
  2199                 void **pixels, int *pitch)
  2200 {
  2201     SDL_Renderer *renderer;
  2202     SDL_Rect full_rect;
  2203 
  2204     if (!texture) {
  2205         return -1;
  2206     }
  2207     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2208         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2209         return -1;
  2210     }
  2211     renderer = texture->renderer;
  2212     if (!renderer->LockTexture) {
  2213         SDL_Unsupported();
  2214         return -1;
  2215     }
  2216     if (!rect) {
  2217         full_rect.x = 0;
  2218         full_rect.y = 0;
  2219         full_rect.w = texture->w;
  2220         full_rect.h = texture->h;
  2221         rect = &full_rect;
  2222     }
  2223     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2224                                  pitch);
  2225 }
  2226 
  2227 void
  2228 SDL_UnlockTexture(SDL_Texture * texture)
  2229 {
  2230     SDL_Renderer *renderer;
  2231 
  2232     if (!texture) {
  2233         return;
  2234     }
  2235     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2236         return;
  2237     }
  2238     renderer = texture->renderer;
  2239     if (!renderer->UnlockTexture) {
  2240         return;
  2241     }
  2242     renderer->UnlockTexture(renderer, texture);
  2243 }
  2244 
  2245 void
  2246 SDL_DirtyTexture(SDL_Texture * texture, int numrects,
  2247                  const SDL_Rect * rects)
  2248 {
  2249     SDL_Renderer *renderer;
  2250 
  2251     if (!texture) {
  2252         return;
  2253     }
  2254     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2255         return;
  2256     }
  2257     renderer = texture->renderer;
  2258     if (!renderer->DirtyTexture) {
  2259         return;
  2260     }
  2261     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2262 }
  2263 
  2264 int
  2265 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2266 {
  2267     SDL_Renderer *renderer;
  2268 
  2269     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2270     if (!renderer) {
  2271         return -1;
  2272     }
  2273     renderer->r = r;
  2274     renderer->g = g;
  2275     renderer->b = b;
  2276     renderer->a = a;
  2277     if (renderer->SetDrawColor) {
  2278         return renderer->SetDrawColor(renderer);
  2279     } else {
  2280         return 0;
  2281     }
  2282 }
  2283 
  2284 int
  2285 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2286 {
  2287     SDL_Renderer *renderer;
  2288 
  2289     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2290     if (!renderer) {
  2291         return -1;
  2292     }
  2293     if (r) {
  2294         *r = renderer->r;
  2295     }
  2296     if (g) {
  2297         *g = renderer->g;
  2298     }
  2299     if (b) {
  2300         *b = renderer->b;
  2301     }
  2302     if (a) {
  2303         *a = renderer->a;
  2304     }
  2305     return 0;
  2306 }
  2307 
  2308 int
  2309 SDL_SetRenderDrawBlendMode(int blendMode)
  2310 {
  2311     SDL_Renderer *renderer;
  2312 
  2313     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2314     if (!renderer) {
  2315         return -1;
  2316     }
  2317     renderer->blendMode = blendMode;
  2318     if (renderer->SetDrawBlendMode) {
  2319         return renderer->SetDrawBlendMode(renderer);
  2320     } else {
  2321         return 0;
  2322     }
  2323 }
  2324 
  2325 int
  2326 SDL_GetRenderDrawBlendMode(int *blendMode)
  2327 {
  2328     SDL_Renderer *renderer;
  2329 
  2330     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2331     if (!renderer) {
  2332         return -1;
  2333     }
  2334     *blendMode = renderer->blendMode;
  2335     return 0;
  2336 }
  2337 
  2338 int
  2339 SDL_RenderClear()
  2340 {
  2341     SDL_Renderer *renderer;
  2342 
  2343     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2344     if (!renderer) {
  2345         return -1;
  2346     }
  2347     if (!renderer->RenderClear) {
  2348         int blendMode = renderer->blendMode;
  2349         int status;
  2350 
  2351         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2352             SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  2353         }
  2354 
  2355         status = SDL_RenderFillRect(NULL);
  2356 
  2357         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2358             SDL_SetRenderDrawBlendMode(blendMode);
  2359         }
  2360         return status;
  2361     }
  2362     return renderer->RenderClear(renderer);
  2363 }
  2364 
  2365 int
  2366 SDL_RenderDrawPoint(int x, int y)
  2367 {
  2368     SDL_Point point;
  2369 
  2370     point.x = x;
  2371     point.y = y;
  2372     return SDL_RenderDrawPoints(&point, 1);
  2373 }
  2374 
  2375 int
  2376 SDL_RenderDrawPoints(const SDL_Point * points, int count)
  2377 {
  2378     SDL_Renderer *renderer;
  2379 
  2380     if (!points) {
  2381         SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  2382         return -1;
  2383     }
  2384 
  2385     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2386     if (!renderer) {
  2387         return -1;
  2388     }
  2389     if (!renderer->RenderDrawPoints) {
  2390         SDL_Unsupported();
  2391         return -1;
  2392     }
  2393     if (count < 1) {
  2394         return 0;
  2395     }
  2396     return renderer->RenderDrawPoints(renderer, points, count);
  2397 }
  2398 
  2399 int
  2400 SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
  2401 {
  2402     SDL_Point points[2];
  2403 
  2404     points[0].x = x1;
  2405     points[0].y = y1;
  2406     points[1].x = x2;
  2407     points[1].y = y2;
  2408     return SDL_RenderDrawLines(points, 2);
  2409 }
  2410 
  2411 int
  2412 SDL_RenderDrawLines(const SDL_Point * points, int count)
  2413 {
  2414     SDL_Renderer *renderer;
  2415 
  2416     if (!points) {
  2417         SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  2418         return -1;
  2419     }
  2420 
  2421     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2422     if (!renderer) {
  2423         return -1;
  2424     }
  2425     if (!renderer->RenderDrawLines) {
  2426         SDL_Unsupported();
  2427         return -1;
  2428     }
  2429     if (count < 2) {
  2430         return 0;
  2431     }
  2432     return renderer->RenderDrawLines(renderer, points, count);
  2433 }
  2434 
  2435 int
  2436 SDL_RenderDrawRect(const SDL_Rect * rect)
  2437 {
  2438     return SDL_RenderDrawRects(&rect, 1);
  2439 }
  2440 
  2441 int
  2442 SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
  2443 {
  2444     SDL_Renderer *renderer;
  2445     int i;
  2446 
  2447     if (!rects) {
  2448         SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  2449         return -1;
  2450     }
  2451 
  2452     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2453     if (!renderer) {
  2454         return -1;
  2455     }
  2456     if (!renderer->RenderDrawRects) {
  2457         SDL_Unsupported();
  2458         return -1;
  2459     }
  2460     if (count < 1) {
  2461         return 0;
  2462     }
  2463     /* Check for NULL rect, which means fill entire window */
  2464     for (i = 0; i < count; ++i) {
  2465         if (rects[i] == NULL) {
  2466             SDL_Window *window = renderer->window;
  2467             SDL_Rect full_rect;
  2468             const SDL_Rect *rect;
  2469 
  2470             full_rect.x = 0;
  2471             full_rect.y = 0;
  2472             full_rect.w = window->w;
  2473             full_rect.h = window->h;
  2474             rect = &full_rect;
  2475             return renderer->RenderDrawRects(renderer, &rect, 1);
  2476         }
  2477     }
  2478     return renderer->RenderDrawRects(renderer, rects, count);
  2479 }
  2480 
  2481 int
  2482 SDL_RenderFillRect(const SDL_Rect * rect)
  2483 {
  2484     return SDL_RenderFillRects(&rect, 1);
  2485 }
  2486 
  2487 int
  2488 SDL_RenderFillRects(const SDL_Rect ** rects, int count)
  2489 {
  2490     SDL_Renderer *renderer;
  2491     int i;
  2492 
  2493     if (!rects) {
  2494         SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  2495         return -1;
  2496     }
  2497 
  2498     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2499     if (!renderer) {
  2500         return -1;
  2501     }
  2502     if (!renderer->RenderFillRects) {
  2503         SDL_Unsupported();
  2504         return -1;
  2505     }
  2506     if (count < 1) {
  2507         return 0;
  2508     }
  2509     /* Check for NULL rect, which means fill entire window */
  2510     for (i = 0; i < count; ++i) {
  2511         if (rects[i] == NULL) {
  2512             SDL_Window *window = renderer->window;
  2513             SDL_Rect full_rect;
  2514             const SDL_Rect *rect;
  2515 
  2516             full_rect.x = 0;
  2517             full_rect.y = 0;
  2518             full_rect.w = window->w;
  2519             full_rect.h = window->h;
  2520             rect = &full_rect;
  2521             return renderer->RenderFillRects(renderer, &rect, 1);
  2522         }
  2523     }
  2524     return renderer->RenderFillRects(renderer, rects, count);
  2525 }
  2526 
  2527 int
  2528 SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
  2529                const SDL_Rect * dstrect)
  2530 {
  2531     SDL_Renderer *renderer;
  2532     SDL_Window *window;
  2533     SDL_Rect real_srcrect;
  2534     SDL_Rect real_dstrect;
  2535 
  2536     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2537     if (!renderer) {
  2538         return -1;
  2539     }
  2540     if (!texture) {
  2541         SDL_SetError("Texture not found");
  2542         return -1;
  2543     }
  2544     if (texture->renderer != renderer) {
  2545         SDL_SetError("Texture was not created with this renderer");
  2546         return -1;
  2547     }
  2548     if (!renderer->RenderCopy) {
  2549         SDL_Unsupported();
  2550         return -1;
  2551     }
  2552     window = renderer->window;
  2553 
  2554     real_srcrect.x = 0;
  2555     real_srcrect.y = 0;
  2556     real_srcrect.w = texture->w;
  2557     real_srcrect.h = texture->h;
  2558     if (srcrect) {
  2559         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2560             return 0;
  2561         }
  2562     }
  2563 
  2564     real_dstrect.x = 0;
  2565     real_dstrect.y = 0;
  2566     real_dstrect.w = window->w;
  2567     real_dstrect.h = window->h;
  2568     if (dstrect) {
  2569         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2570             return 0;
  2571         }
  2572         /* Clip srcrect by the same amount as dstrect was clipped */
  2573         if (dstrect->w != real_dstrect.w) {
  2574             int deltax = (real_dstrect.x - dstrect->x);
  2575             int deltaw = (real_dstrect.w - dstrect->w);
  2576             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2577             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2578         }
  2579         if (dstrect->h != real_dstrect.h) {
  2580             int deltay = (real_dstrect.y - dstrect->y);
  2581             int deltah = (real_dstrect.h - dstrect->h);
  2582             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2583             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2584         }
  2585     }
  2586 
  2587     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2588                                 &real_dstrect);
  2589 }
  2590 
  2591 int
  2592 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2593                      void * pixels, int pitch)
  2594 {
  2595     SDL_Renderer *renderer;
  2596     SDL_Window *window;
  2597     SDL_Rect real_rect;
  2598 
  2599     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2600     if (!renderer) {
  2601         return -1;
  2602     }
  2603     if (!renderer->RenderReadPixels) {
  2604         SDL_Unsupported();
  2605         return -1;
  2606     }
  2607     window = renderer->window;
  2608 
  2609     if (!format) {
  2610         format = window->display->current_mode.format;
  2611     }
  2612 
  2613     real_rect.x = 0;
  2614     real_rect.y = 0;
  2615     real_rect.w = window->w;
  2616     real_rect.h = window->h;
  2617     if (rect) {
  2618         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2619             return 0;
  2620         }
  2621         if (real_rect.y > rect->y) {
  2622             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2623         }
  2624         if (real_rect.x > rect->x) {
  2625             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2626             int bpp = SDL_BYTESPERPIXEL(format);
  2627             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2628         }
  2629     }
  2630 
  2631     return renderer->RenderReadPixels(renderer, &real_rect,
  2632                                       format, pixels, pitch);
  2633 }
  2634 
  2635 int
  2636 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2637                       const void * pixels, int pitch)
  2638 {
  2639     SDL_Renderer *renderer;
  2640     SDL_Window *window;
  2641     SDL_Rect real_rect;
  2642 
  2643     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2644     if (!renderer) {
  2645         return -1;
  2646     }
  2647     if (!renderer->RenderWritePixels) {
  2648         SDL_Unsupported();
  2649         return -1;
  2650     }
  2651     window = renderer->window;
  2652 
  2653     if (!format) {
  2654         format = window->display->current_mode.format;
  2655     }
  2656 
  2657     real_rect.x = 0;
  2658     real_rect.y = 0;
  2659     real_rect.w = window->w;
  2660     real_rect.h = window->h;
  2661     if (rect) {
  2662         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2663             return 0;
  2664         }
  2665         if (real_rect.y > rect->y) {
  2666             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2667         }
  2668         if (real_rect.x > rect->x) {
  2669             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2670             int bpp = SDL_BYTESPERPIXEL(format);
  2671             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2672         }
  2673     }
  2674 
  2675     return renderer->RenderWritePixels(renderer, &real_rect,
  2676                                        format, pixels, pitch);
  2677 }
  2678 
  2679 void
  2680 SDL_RenderPresent(void)
  2681 {
  2682     SDL_Renderer *renderer;
  2683 
  2684     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2685     if (!renderer || !renderer->RenderPresent) {
  2686         return;
  2687     }
  2688     renderer->RenderPresent(renderer);
  2689 }
  2690 
  2691 void
  2692 SDL_DestroyTexture(SDL_Texture * texture)
  2693 {
  2694     SDL_Renderer *renderer;
  2695 
  2696     if (!texture || !texture->renderer) {
  2697         SDL_SetError("Invalid texture");
  2698         return;
  2699     }
  2700 
  2701     renderer = texture->renderer;
  2702     if (texture->next) {
  2703         texture->next->prev = texture->prev;
  2704     }
  2705     if (texture->prev) {
  2706         texture->prev->next = texture->next;
  2707     } else {
  2708         renderer->textures = texture->next;
  2709     }
  2710     texture->renderer = NULL;
  2711 
  2712     renderer->DestroyTexture(renderer, texture);
  2713     SDL_free(texture);
  2714 }
  2715 
  2716 void
  2717 SDL_DestroyRenderer(SDL_Window * window)
  2718 {
  2719     SDL_Renderer *renderer;
  2720 
  2721     if (!window) {
  2722         return;
  2723     }
  2724     renderer = window->renderer;
  2725     if (!renderer) {
  2726         return;
  2727     }
  2728 
  2729     /* Free existing textures for this renderer */
  2730     while (renderer->textures) {
  2731         SDL_DestroyTexture(renderer->textures);
  2732     }
  2733 
  2734     /* Free the renderer instance */
  2735     renderer->DestroyRenderer(renderer);
  2736 
  2737     /* Clear references */
  2738     window->renderer = NULL;
  2739     if (SDL_CurrentDisplay->current_renderer == renderer) {
  2740         SDL_CurrentDisplay->current_renderer = NULL;
  2741     }
  2742 }
  2743 
  2744 SDL_bool
  2745 SDL_IsScreenSaverEnabled()
  2746 {
  2747     if (!_this) {
  2748         return SDL_TRUE;
  2749     }
  2750     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2751 }
  2752 
  2753 void
  2754 SDL_EnableScreenSaver()
  2755 {
  2756     if (!_this) {
  2757         return;
  2758     }
  2759     if (!_this->suspend_screensaver) {
  2760         return;
  2761     }
  2762     _this->suspend_screensaver = SDL_FALSE;
  2763     if (_this->SuspendScreenSaver) {
  2764         _this->SuspendScreenSaver(_this);
  2765     }
  2766 }
  2767 
  2768 void
  2769 SDL_DisableScreenSaver()
  2770 {
  2771     if (!_this) {
  2772         return;
  2773     }
  2774     if (_this->suspend_screensaver) {
  2775         return;
  2776     }
  2777     _this->suspend_screensaver = SDL_TRUE;
  2778     if (_this->SuspendScreenSaver) {
  2779         _this->SuspendScreenSaver(_this);
  2780     }
  2781 }
  2782 
  2783 void
  2784 SDL_VideoQuit(void)
  2785 {
  2786     int i, j;
  2787 
  2788     if (!_this) {
  2789         return;
  2790     }
  2791     /* Halt event processing before doing anything else */
  2792     SDL_StopEventLoop();
  2793     SDL_EnableScreenSaver();
  2794 
  2795     /* Clean up the system video */
  2796     for (i = _this->num_displays; i--;) {
  2797         SDL_VideoDisplay *display = &_this->displays[i];
  2798         while (display->windows) {
  2799             SDL_DestroyWindow(display->windows);
  2800         }
  2801         if (display->render_drivers) {
  2802             SDL_free(display->render_drivers);
  2803             display->render_drivers = NULL;
  2804         }
  2805         display->num_render_drivers = 0;
  2806     }
  2807     _this->VideoQuit(_this);
  2808 
  2809     for (i = _this->num_displays; i--;) {
  2810         SDL_VideoDisplay *display = &_this->displays[i];
  2811         for (j = display->num_display_modes; j--;) {
  2812             if (display->display_modes[j].driverdata) {
  2813                 SDL_free(display->display_modes[j].driverdata);
  2814                 display->display_modes[j].driverdata = NULL;
  2815             }
  2816         }
  2817         if (display->display_modes) {
  2818             SDL_free(display->display_modes);
  2819             display->display_modes = NULL;
  2820         }
  2821         if (display->desktop_mode.driverdata) {
  2822             SDL_free(display->desktop_mode.driverdata);
  2823             display->desktop_mode.driverdata = NULL;
  2824         }
  2825         if (display->palette) {
  2826             SDL_FreePalette(display->palette);
  2827             display->palette = NULL;
  2828         }
  2829         if (display->gamma) {
  2830             SDL_free(display->gamma);
  2831             display->gamma = NULL;
  2832         }
  2833         if (display->driverdata) {
  2834             SDL_free(display->driverdata);
  2835             display->driverdata = NULL;
  2836         }
  2837     }
  2838     if (_this->displays) {
  2839         SDL_free(_this->displays);
  2840         _this->displays = NULL;
  2841     }
  2842     _this->free(_this);
  2843     _this = NULL;
  2844 }
  2845 
  2846 int
  2847 SDL_GL_LoadLibrary(const char *path)
  2848 {
  2849     int retval;
  2850 
  2851     if (!_this) {
  2852         SDL_UninitializedVideo();
  2853         return -1;
  2854     }
  2855     if (_this->gl_config.driver_loaded) {
  2856         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2857             SDL_SetError("OpenGL library already loaded");
  2858             return -1;
  2859         }
  2860         retval = 0;
  2861     } else {
  2862         if (!_this->GL_LoadLibrary) {
  2863             SDL_SetError("No dynamic GL support in video driver");
  2864             return -1;
  2865         }
  2866         retval = _this->GL_LoadLibrary(_this, path);
  2867     }
  2868     if (retval == 0) {
  2869         ++_this->gl_config.driver_loaded;
  2870     }
  2871     return (retval);
  2872 }
  2873 
  2874 void *
  2875 SDL_GL_GetProcAddress(const char *proc)
  2876 {
  2877     void *func;
  2878 
  2879     if (!_this) {
  2880         SDL_UninitializedVideo();
  2881         return NULL;
  2882     }
  2883     func = NULL;
  2884     if (_this->GL_GetProcAddress) {
  2885         if (_this->gl_config.driver_loaded) {
  2886             func = _this->GL_GetProcAddress(_this, proc);
  2887         } else {
  2888             SDL_SetError("No GL driver has been loaded");
  2889         }
  2890     } else {
  2891         SDL_SetError("No dynamic GL support in video driver");
  2892     }
  2893     return func;
  2894 }
  2895 
  2896 void
  2897 SDL_GL_UnloadLibrary(void)
  2898 {
  2899     if (!_this) {
  2900         SDL_UninitializedVideo();
  2901         return;
  2902     }
  2903     if (_this->gl_config.driver_loaded > 0) {
  2904         if (--_this->gl_config.driver_loaded > 0) {
  2905             return;
  2906         }
  2907         if (_this->GL_UnloadLibrary) {
  2908             _this->GL_UnloadLibrary(_this);
  2909         }
  2910     }
  2911 }
  2912 
  2913 SDL_bool
  2914 SDL_GL_ExtensionSupported(const char *extension)
  2915 {
  2916 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2917     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2918     const char *extensions;
  2919     const char *start;
  2920     const char *where, *terminator;
  2921 
  2922     /* Extension names should not have spaces. */
  2923     where = SDL_strchr(extension, ' ');
  2924     if (where || *extension == '\0') {
  2925         return SDL_FALSE;
  2926     }
  2927     /* See if there's an environment variable override */
  2928     start = SDL_getenv(extension);
  2929     if (start && *start == '0') {
  2930         return SDL_FALSE;
  2931     }
  2932     /* Lookup the available extensions */
  2933     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2934     if (glGetStringFunc) {
  2935         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2936     } else {
  2937         extensions = NULL;
  2938     }
  2939     if (!extensions) {
  2940         return SDL_FALSE;
  2941     }
  2942     /*
  2943      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2944      * extensions string. Don't be fooled by sub-strings, etc.
  2945      */
  2946 
  2947     start = extensions;
  2948 
  2949     for (;;) {
  2950         where = SDL_strstr(start, extension);
  2951         if (!where)
  2952             break;
  2953 
  2954         terminator = where + SDL_strlen(extension);
  2955         if (where == start || *(where - 1) == ' ')
  2956             if (*terminator == ' ' || *terminator == '\0')
  2957                 return SDL_TRUE;
  2958 
  2959         start = terminator;
  2960     }
  2961     return SDL_FALSE;
  2962 #else
  2963     return SDL_FALSE;
  2964 #endif
  2965 }
  2966 
  2967 int
  2968 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2969 {
  2970 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2971     int retval;
  2972 
  2973     if (!_this) {
  2974         SDL_UninitializedVideo();
  2975         return -1;
  2976     }
  2977     retval = 0;
  2978     switch (attr) {
  2979     case SDL_GL_RED_SIZE:
  2980         _this->gl_config.red_size = value;
  2981         break;
  2982     case SDL_GL_GREEN_SIZE:
  2983         _this->gl_config.green_size = value;
  2984         break;
  2985     case SDL_GL_BLUE_SIZE:
  2986         _this->gl_config.blue_size = value;
  2987         break;
  2988     case SDL_GL_ALPHA_SIZE:
  2989         _this->gl_config.alpha_size = value;
  2990         break;
  2991     case SDL_GL_DOUBLEBUFFER:
  2992         _this->gl_config.double_buffer = value;
  2993         break;
  2994     case SDL_GL_BUFFER_SIZE:
  2995         _this->gl_config.buffer_size = value;
  2996         break;
  2997     case SDL_GL_DEPTH_SIZE:
  2998         _this->gl_config.depth_size = value;
  2999         break;
  3000     case SDL_GL_STENCIL_SIZE:
  3001         _this->gl_config.stencil_size = value;
  3002         break;
  3003     case SDL_GL_ACCUM_RED_SIZE:
  3004         _this->gl_config.accum_red_size = value;
  3005         break;
  3006     case SDL_GL_ACCUM_GREEN_SIZE:
  3007         _this->gl_config.accum_green_size = value;
  3008         break;
  3009     case SDL_GL_ACCUM_BLUE_SIZE:
  3010         _this->gl_config.accum_blue_size = value;
  3011         break;
  3012     case SDL_GL_ACCUM_ALPHA_SIZE:
  3013         _this->gl_config.accum_alpha_size = value;
  3014         break;
  3015     case SDL_GL_STEREO:
  3016         _this->gl_config.stereo = value;
  3017         break;
  3018     case SDL_GL_MULTISAMPLEBUFFERS:
  3019         _this->gl_config.multisamplebuffers = value;
  3020         break;
  3021     case SDL_GL_MULTISAMPLESAMPLES:
  3022         _this->gl_config.multisamplesamples = value;
  3023         break;
  3024     case SDL_GL_ACCELERATED_VISUAL:
  3025         _this->gl_config.accelerated = value;
  3026         break;
  3027     case SDL_GL_RETAINED_BACKING:
  3028         _this->gl_config.retained_backing = value;
  3029         break;
  3030     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3031         _this->gl_config.major_version = value;
  3032         break;
  3033     case SDL_GL_CONTEXT_MINOR_VERSION:
  3034         _this->gl_config.minor_version = value;
  3035         break;
  3036     default:
  3037         SDL_SetError("Unknown OpenGL attribute");
  3038         retval = -1;
  3039         break;
  3040     }
  3041     return retval;
  3042 #else
  3043     SDL_Unsupported();
  3044     return -1;
  3045 #endif /* SDL_VIDEO_OPENGL */
  3046 }
  3047 
  3048 int
  3049 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3050 {
  3051 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3052     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3053     GLenum(APIENTRY * glGetErrorFunc) (void);
  3054     GLenum attrib = 0;
  3055     GLenum error = 0;
  3056 
  3057     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3058     if (!glGetIntegervFunc) {
  3059         return -1;
  3060     }
  3061 
  3062     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3063     if (!glGetErrorFunc) {
  3064         return -1;
  3065     }
  3066 
  3067     /* Clear value in any case */
  3068     *value = 0;
  3069 
  3070     switch (attr) {
  3071     case SDL_GL_RETAINED_BACKING:
  3072         *value = _this->gl_config.retained_backing;
  3073         return 0;
  3074     case SDL_GL_RED_SIZE:
  3075         attrib = GL_RED_BITS;
  3076         break;
  3077     case SDL_GL_BLUE_SIZE:
  3078         attrib = GL_BLUE_BITS;
  3079         break;
  3080     case SDL_GL_GREEN_SIZE:
  3081         attrib = GL_GREEN_BITS;
  3082         break;
  3083     case SDL_GL_ALPHA_SIZE:
  3084         attrib = GL_ALPHA_BITS;
  3085         break;
  3086     case SDL_GL_DOUBLEBUFFER:
  3087 #ifndef SDL_VIDEO_OPENGL_ES
  3088         attrib = GL_DOUBLEBUFFER;
  3089         break;
  3090 #else
  3091         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3092         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3093         /* SDL driver must set proper value after initialization              */
  3094         *value = _this->gl_config.double_buffer;
  3095         return 0;
  3096 #endif
  3097     case SDL_GL_DEPTH_SIZE:
  3098         attrib = GL_DEPTH_BITS;
  3099         break;
  3100     case SDL_GL_STENCIL_SIZE:
  3101         attrib = GL_STENCIL_BITS;
  3102         break;
  3103 #ifndef SDL_VIDEO_OPENGL_ES
  3104     case SDL_GL_ACCUM_RED_SIZE:
  3105         attrib = GL_ACCUM_RED_BITS;
  3106         break;
  3107     case SDL_GL_ACCUM_GREEN_SIZE:
  3108         attrib = GL_ACCUM_GREEN_BITS;
  3109         break;
  3110     case SDL_GL_ACCUM_BLUE_SIZE:
  3111         attrib = GL_ACCUM_BLUE_BITS;
  3112         break;
  3113     case SDL_GL_ACCUM_ALPHA_SIZE:
  3114         attrib = GL_ACCUM_ALPHA_BITS;
  3115         break;
  3116     case SDL_GL_STEREO:
  3117         attrib = GL_STEREO;
  3118         break;
  3119 #else
  3120     case SDL_GL_ACCUM_RED_SIZE:
  3121     case SDL_GL_ACCUM_GREEN_SIZE:
  3122     case SDL_GL_ACCUM_BLUE_SIZE:
  3123     case SDL_GL_ACCUM_ALPHA_SIZE:
  3124     case SDL_GL_STEREO:
  3125         /* none of these are supported in OpenGL ES */
  3126         *value = 0;
  3127         return 0;
  3128 #endif
  3129     case SDL_GL_MULTISAMPLEBUFFERS:
  3130 #ifndef SDL_VIDEO_OPENGL_ES
  3131         attrib = GL_SAMPLE_BUFFERS_ARB;
  3132 #else
  3133         attrib = GL_SAMPLE_BUFFERS;
  3134 #endif
  3135         break;
  3136     case SDL_GL_MULTISAMPLESAMPLES:
  3137 #ifndef SDL_VIDEO_OPENGL_ES
  3138         attrib = GL_SAMPLES_ARB;
  3139 #else
  3140         attrib = GL_SAMPLES;
  3141 #endif
  3142         break;
  3143     case SDL_GL_BUFFER_SIZE:
  3144         {
  3145             GLint bits = 0;
  3146             GLint component;
  3147 
  3148             /*
  3149              * there doesn't seem to be a single flag in OpenGL
  3150              * for this!
  3151              */
  3152             glGetIntegervFunc(GL_RED_BITS, &component);
  3153             bits += component;
  3154             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3155             bits += component;
  3156             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3157             bits += component;
  3158             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3159             bits += component;
  3160 
  3161             *value = bits;
  3162             return 0;
  3163         }
  3164     case SDL_GL_ACCELERATED_VISUAL:
  3165         {
  3166             /* FIXME: How do we get this information? */
  3167             *value = (_this->gl_config.accelerated != 0);
  3168             return 0;
  3169         }
  3170     default:
  3171         SDL_SetError("Unknown OpenGL attribute");
  3172         return -1;
  3173     }
  3174 
  3175     glGetIntegervFunc(attrib, (GLint *) value);
  3176     error = glGetErrorFunc();
  3177     if (error != GL_NO_ERROR) {
  3178         switch (error) {
  3179         case GL_INVALID_ENUM:
  3180             {
  3181                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3182             }
  3183             break;
  3184         case GL_INVALID_VALUE:
  3185             {
  3186                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3187             }
  3188             break;
  3189         default:
  3190             {
  3191                 SDL_SetError("OpenGL error: %08X", error);
  3192             }
  3193             break;
  3194         }
  3195         return -1;
  3196     }
  3197     return 0;
  3198 #else
  3199     SDL_Unsupported();
  3200     return -1;
  3201 #endif /* SDL_VIDEO_OPENGL */
  3202 }
  3203 
  3204 SDL_GLContext
  3205 SDL_GL_CreateContext(SDL_Window * window)
  3206 {
  3207     if (!window) {
  3208         return NULL;
  3209     }
  3210     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3211         SDL_SetError("The specified window isn't an OpenGL window");
  3212         return NULL;
  3213     }
  3214     return _this->GL_CreateContext(_this, window);
  3215 }
  3216 
  3217 int
  3218 SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
  3219 {
  3220     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3221         SDL_SetError("The specified window isn't an OpenGL window");
  3222         return -1;
  3223     }
  3224     if (!context) {
  3225         window = NULL;
  3226     }
  3227     return _this->GL_MakeCurrent(_this, window, context);
  3228 }
  3229 
  3230 int
  3231 SDL_GL_SetSwapInterval(int interval)
  3232 {
  3233     if (!_this) {
  3234         SDL_UninitializedVideo();
  3235         return -1;
  3236     }
  3237     if (_this->GL_SetSwapInterval) {
  3238         return _this->GL_SetSwapInterval(_this, interval);
  3239     } else {
  3240         SDL_SetError("Setting the swap interval is not supported");
  3241         return -1;
  3242     }
  3243 }
  3244 
  3245 int
  3246 SDL_GL_GetSwapInterval(void)
  3247 {
  3248     if (!_this) {
  3249         SDL_UninitializedVideo();
  3250         return -1;
  3251     }
  3252     if (_this->GL_GetSwapInterval) {
  3253         return _this->GL_GetSwapInterval(_this);
  3254     } else {
  3255         SDL_SetError("Getting the swap interval is not supported");
  3256         return -1;
  3257     }
  3258 }
  3259 
  3260 void
  3261 SDL_GL_SwapWindow(SDL_Window * window)
  3262 {
  3263     if (!window) {
  3264         return;
  3265     }
  3266     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3267         SDL_SetError("The specified window isn't an OpenGL window");
  3268         return;
  3269     }
  3270     _this->GL_SwapWindow(_this, window);
  3271 }
  3272 
  3273 void
  3274 SDL_GL_DeleteContext(SDL_GLContext context)
  3275 {
  3276     if (!_this || !context) {
  3277         return;
  3278     }
  3279     _this->GL_MakeCurrent(_this, NULL, NULL);
  3280     _this->GL_DeleteContext(_this, context);
  3281 }
  3282 
  3283 #if 0                           // FIXME
  3284 /*
  3285  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3286  * & 2 for alpha channel.
  3287  */
  3288 static void
  3289 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3290 {
  3291     int x, y;
  3292     Uint32 colorkey;
  3293 #define SET_MASKBIT(icon, x, y, mask) \
  3294 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3295 
  3296     colorkey = icon->format->colorkey;
  3297     switch (icon->format->BytesPerPixel) {
  3298     case 1:
  3299         {
  3300             Uint8 *pixels;
  3301             for (y = 0; y < icon->h; ++y) {
  3302                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3303                 for (x = 0; x < icon->w; ++x) {
  3304                     if (*pixels++ == colorkey) {
  3305                         SET_MASKBIT(icon, x, y, mask);
  3306                     }
  3307                 }
  3308             }
  3309         }
  3310         break;
  3311 
  3312     case 2:
  3313         {
  3314             Uint16 *pixels;
  3315             for (y = 0; y < icon->h; ++y) {
  3316                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3317                 for (x = 0; x < icon->w; ++x) {
  3318                     if ((flags & 1) && *pixels == colorkey) {
  3319                         SET_MASKBIT(icon, x, y, mask);
  3320                     } else if ((flags & 2)
  3321                                && (*pixels & icon->format->Amask) == 0) {
  3322                         SET_MASKBIT(icon, x, y, mask);
  3323                     }
  3324                     pixels++;
  3325                 }
  3326             }
  3327         }
  3328         break;
  3329 
  3330     case 4:
  3331         {
  3332             Uint32 *pixels;
  3333             for (y = 0; y < icon->h; ++y) {
  3334                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3335                 for (x = 0; x < icon->w; ++x) {
  3336                     if ((flags & 1) && *pixels == colorkey) {
  3337                         SET_MASKBIT(icon, x, y, mask);
  3338                     } else if ((flags & 2)
  3339                                && (*pixels & icon->format->Amask) == 0) {
  3340                         SET_MASKBIT(icon, x, y, mask);
  3341                     }
  3342                     pixels++;
  3343                 }
  3344             }
  3345         }
  3346         break;
  3347     }
  3348 }
  3349 
  3350 /*
  3351  * Sets the window manager icon for the display window.
  3352  */
  3353 void
  3354 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3355 {
  3356     if (icon && _this->SetIcon) {
  3357         /* Generate a mask if necessary, and create the icon! */
  3358         if (mask == NULL) {
  3359             int mask_len = icon->h * (icon->w + 7) / 8;
  3360             int flags = 0;
  3361             mask = (Uint8 *) SDL_malloc(mask_len);
  3362             if (mask == NULL) {
  3363                 return;
  3364             }
  3365             SDL_memset(mask, ~0, mask_len);
  3366             if (icon->flags & SDL_SRCCOLORKEY)
  3367                 flags |= 1;
  3368             if (icon->flags & SDL_SRCALPHA)
  3369                 flags |= 2;
  3370             if (flags) {
  3371                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3372             }
  3373             _this->SetIcon(_this, icon, mask);
  3374             SDL_free(mask);
  3375         } else {
  3376             _this->SetIcon(_this, icon, mask);
  3377         }
  3378     }
  3379 }
  3380 #endif
  3381 
  3382 SDL_bool
  3383 SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
  3384 {
  3385     if (!window || !_this->GetWindowWMInfo) {
  3386         return SDL_FALSE;
  3387     }
  3388     return (_this->GetWindowWMInfo(_this, window, info));
  3389 }
  3390 
  3391 void
  3392 SDL_StartTextInput(void)
  3393 {
  3394     if (_this && _this->StartTextInput) {
  3395         _this->StartTextInput(_this);
  3396     }
  3397     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3398     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3399 }
  3400 
  3401 void
  3402 SDL_StopTextInput(void)
  3403 {
  3404     if (_this && _this->StopTextInput) {
  3405         _this->StopTextInput(_this);
  3406     }
  3407     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3408     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3409 }
  3410 
  3411 void
  3412 SDL_SetTextInputRect(SDL_Rect *rect)
  3413 {
  3414     if (_this && _this->SetTextInputRect) {
  3415         _this->SetTextInputRect(_this, rect);
  3416     }
  3417 }
  3418 
  3419 /* vi: set ts=4 sw=4 expandtab: */