src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 07:10:22 +0000
changeset 3686 d87db9cf6a31
parent 3685 64ce267332c6
child 3692 ad85eb21a559
permissions -rw-r--r--
Fixed compile warning
     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     renderer->textures = texture;
  1647 
  1648     if (renderer->CreateTexture(renderer, texture) < 0) {
  1649         SDL_DestroyTexture(texture);
  1650         return 0;
  1651     }
  1652     return texture;
  1653 }
  1654 
  1655 SDL_Texture *
  1656 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1657 {
  1658     SDL_Texture *texture;
  1659     Uint32 requested_format = format;
  1660     SDL_PixelFormat *fmt;
  1661     SDL_Renderer *renderer;
  1662     int bpp;
  1663     Uint32 Rmask, Gmask, Bmask, Amask;
  1664 
  1665     if (!surface) {
  1666         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1667         return 0;
  1668     }
  1669     fmt = surface->format;
  1670 
  1671     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  1672     if (!renderer) {
  1673         return 0;
  1674     }
  1675 
  1676     if (format) {
  1677         if (!SDL_PixelFormatEnumToMasks
  1678             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1679             SDL_SetError("Unknown pixel format");
  1680             return 0;
  1681         }
  1682     } else {
  1683         if (surface->format->Amask
  1684             || !(surface->map->info.flags &
  1685                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1686             Uint32 it;
  1687             int pfmt;
  1688 
  1689             /* Pixel formats, sorted by best first */
  1690             static const Uint32 sdl_pformats[] = {
  1691                 SDL_PIXELFORMAT_ARGB8888,
  1692                 SDL_PIXELFORMAT_RGBA8888,
  1693                 SDL_PIXELFORMAT_ABGR8888,
  1694                 SDL_PIXELFORMAT_BGRA8888,
  1695                 SDL_PIXELFORMAT_RGB888,
  1696                 SDL_PIXELFORMAT_BGR888,
  1697                 SDL_PIXELFORMAT_RGB24,
  1698                 SDL_PIXELFORMAT_BGR24,
  1699                 SDL_PIXELFORMAT_RGB565,
  1700                 SDL_PIXELFORMAT_BGR565,
  1701                 SDL_PIXELFORMAT_ARGB1555,
  1702                 SDL_PIXELFORMAT_ABGR1555,
  1703                 SDL_PIXELFORMAT_RGB555,
  1704                 SDL_PIXELFORMAT_BGR555,
  1705                 SDL_PIXELFORMAT_ARGB4444,
  1706                 SDL_PIXELFORMAT_ABGR4444,
  1707                 SDL_PIXELFORMAT_RGB444,
  1708                 SDL_PIXELFORMAT_ARGB2101010,
  1709                 SDL_PIXELFORMAT_INDEX8,
  1710                 SDL_PIXELFORMAT_INDEX4LSB,
  1711                 SDL_PIXELFORMAT_INDEX4MSB,
  1712                 SDL_PIXELFORMAT_RGB332,
  1713                 SDL_PIXELFORMAT_INDEX1LSB,
  1714                 SDL_PIXELFORMAT_INDEX1MSB,
  1715                 SDL_PIXELFORMAT_UNKNOWN
  1716             };
  1717 
  1718             bpp = fmt->BitsPerPixel;
  1719             Rmask = fmt->Rmask;
  1720             Gmask = fmt->Gmask;
  1721             Bmask = fmt->Bmask;
  1722             Amask = fmt->Amask;
  1723 
  1724             format =
  1725                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1726             if (!format) {
  1727                 SDL_SetError("Unknown pixel format");
  1728                 return 0;
  1729             }
  1730 
  1731             /* Search requested format in the supported texture */
  1732             /* formats by current renderer                      */
  1733             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1734                 if (renderer->info.texture_formats[it] == format) {
  1735                     break;
  1736                 }
  1737             }
  1738 
  1739             /* If requested format can't be found, search any best */
  1740             /* format which renderer provides                      */
  1741             if (it == renderer->info.num_texture_formats) {
  1742                 pfmt = 0;
  1743                 for (;;) {
  1744                     if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1745                         break;
  1746                     }
  1747 
  1748                     for (it = 0; it < renderer->info.num_texture_formats;
  1749                          it++) {
  1750                         if (renderer->info.texture_formats[it] ==
  1751                             sdl_pformats[pfmt]) {
  1752                             break;
  1753                         }
  1754                     }
  1755 
  1756                     if (it != renderer->info.num_texture_formats) {
  1757                         /* The best format has been found */
  1758                         break;
  1759                     }
  1760                     pfmt++;
  1761                 }
  1762 
  1763                 /* If any format can't be found, then return an error */
  1764                 if (it == renderer->info.num_texture_formats) {
  1765                     SDL_SetError
  1766                         ("Any of the supported pixel formats can't be found");
  1767                     return 0;
  1768                 }
  1769 
  1770                 /* Convert found pixel format back to color masks */
  1771                 if (SDL_PixelFormatEnumToMasks
  1772                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1773                      &Bmask, &Amask) != SDL_TRUE) {
  1774                     SDL_SetError("Unknown pixel format");
  1775                     return 0;
  1776                 }
  1777             }
  1778         } else {
  1779             /* Need a format with alpha */
  1780             Uint32 it;
  1781             int apfmt;
  1782 
  1783             /* Pixel formats with alpha, sorted by best first */
  1784             static const Uint32 sdl_alpha_pformats[] = {
  1785                 SDL_PIXELFORMAT_ARGB8888,
  1786                 SDL_PIXELFORMAT_RGBA8888,
  1787                 SDL_PIXELFORMAT_ABGR8888,
  1788                 SDL_PIXELFORMAT_BGRA8888,
  1789                 SDL_PIXELFORMAT_ARGB1555,
  1790                 SDL_PIXELFORMAT_ABGR1555,
  1791                 SDL_PIXELFORMAT_ARGB4444,
  1792                 SDL_PIXELFORMAT_ABGR4444,
  1793                 SDL_PIXELFORMAT_ARGB2101010,
  1794                 SDL_PIXELFORMAT_UNKNOWN
  1795             };
  1796 
  1797             if (surface->format->Amask) {
  1798                 /* If surface already has alpha, then try an original */
  1799                 /* surface format first                               */
  1800                 bpp = fmt->BitsPerPixel;
  1801                 Rmask = fmt->Rmask;
  1802                 Gmask = fmt->Gmask;
  1803                 Bmask = fmt->Bmask;
  1804                 Amask = fmt->Amask;
  1805             } else {
  1806                 bpp = 32;
  1807                 Rmask = 0x00FF0000;
  1808                 Gmask = 0x0000FF00;
  1809                 Bmask = 0x000000FF;
  1810                 Amask = 0xFF000000;
  1811             }
  1812 
  1813             format =
  1814                 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1815             if (!format) {
  1816                 SDL_SetError("Unknown pixel format");
  1817                 return 0;
  1818             }
  1819 
  1820             /* Search this format in the supported texture formats */
  1821             /* by current renderer                                 */
  1822             for (it = 0; it < renderer->info.num_texture_formats; it++) {
  1823                 if (renderer->info.texture_formats[it] == format) {
  1824                     break;
  1825                 }
  1826             }
  1827 
  1828             /* If this format can't be found, search any best       */
  1829             /* compatible format with alpha which renderer provides */
  1830             if (it == renderer->info.num_texture_formats) {
  1831                 apfmt = 0;
  1832                 for (;;) {
  1833                     if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
  1834                         break;
  1835                     }
  1836 
  1837                     for (it = 0; it < renderer->info.num_texture_formats;
  1838                          it++) {
  1839                         if (renderer->info.texture_formats[it] ==
  1840                             sdl_alpha_pformats[apfmt]) {
  1841                             break;
  1842                         }
  1843                     }
  1844 
  1845                     if (it != renderer->info.num_texture_formats) {
  1846                         /* Compatible format has been found */
  1847                         break;
  1848                     }
  1849                     apfmt++;
  1850                 }
  1851 
  1852                 /* If compatible format can't be found, then return an error */
  1853                 if (it == renderer->info.num_texture_formats) {
  1854                     SDL_SetError("Compatible pixel format can't be found");
  1855                     return 0;
  1856                 }
  1857 
  1858                 /* Convert found pixel format back to color masks */
  1859                 if (SDL_PixelFormatEnumToMasks
  1860                     (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
  1861                      &Bmask, &Amask) != SDL_TRUE) {
  1862                     SDL_SetError("Unknown pixel format");
  1863                     return 0;
  1864                 }
  1865             }
  1866         }
  1867 
  1868         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1869         if (!format) {
  1870             SDL_SetError("Unknown pixel format");
  1871             return 0;
  1872         }
  1873     }
  1874 
  1875     texture =
  1876         SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1877                           surface->h);
  1878     if (!texture && !requested_format) {
  1879         SDL_DisplayMode desktop_mode;
  1880         SDL_GetDesktopDisplayMode(&desktop_mode);
  1881         format = desktop_mode.format;
  1882         texture =
  1883             SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1884                               surface->h);
  1885     }
  1886     if (!texture) {
  1887         return 0;
  1888     }
  1889     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1890         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1891         if (SDL_MUSTLOCK(surface)) {
  1892             SDL_LockSurface(surface);
  1893             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1894                               surface->pitch);
  1895             SDL_UnlockSurface(surface);
  1896         } else {
  1897             SDL_UpdateTexture(texture, NULL, surface->pixels,
  1898                               surface->pitch);
  1899         }
  1900     } else {
  1901         SDL_PixelFormat dst_fmt;
  1902         SDL_Surface *dst = NULL;
  1903 
  1904         /* Set up a destination surface for the texture update */
  1905         SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
  1906         if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1907             dst_fmt.palette =
  1908                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1909             if (dst_fmt.palette) {
  1910                 /*
  1911                  * FIXME: Should we try to copy
  1912                  * fmt->palette?
  1913                  */
  1914                 SDL_DitherColors(dst_fmt.palette->colors,
  1915                                  SDL_BITSPERPIXEL(format));
  1916             }
  1917         }
  1918         dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
  1919         if (dst) {
  1920             SDL_UpdateTexture(texture, NULL, dst->pixels, dst->pitch);
  1921             SDL_FreeSurface(dst);
  1922         }
  1923         if (dst_fmt.palette) {
  1924             SDL_FreePalette(dst_fmt.palette);
  1925         }
  1926         if (!dst) {
  1927             SDL_DestroyTexture(texture);
  1928             return 0;
  1929         }
  1930     }
  1931 
  1932     {
  1933         Uint8 r, g, b, a;
  1934         int blendMode;
  1935         int scaleMode;
  1936 
  1937         SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1938         SDL_SetTextureColorMod(texture, r, g, b);
  1939 
  1940         SDL_GetSurfaceAlphaMod(surface, &a);
  1941         SDL_SetTextureAlphaMod(texture, a);
  1942 
  1943         SDL_GetSurfaceBlendMode(surface, &blendMode);
  1944         SDL_SetTextureBlendMode(texture, blendMode);
  1945 
  1946         SDL_GetSurfaceScaleMode(surface, &scaleMode);
  1947         SDL_SetTextureScaleMode(texture, scaleMode);
  1948     }
  1949 
  1950     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1951         SDL_SetTexturePalette(texture, fmt->palette->colors, 0,
  1952                               fmt->palette->ncolors);
  1953     }
  1954     return texture;
  1955 }
  1956 
  1957 int
  1958 SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  1959                  int *w, int *h)
  1960 {
  1961     if (!texture) {
  1962         return -1;
  1963     }
  1964     if (format) {
  1965         *format = texture->format;
  1966     }
  1967     if (access) {
  1968         *access = texture->access;
  1969     }
  1970     if (w) {
  1971         *w = texture->w;
  1972     }
  1973     if (h) {
  1974         *h = texture->h;
  1975     }
  1976     return 0;
  1977 }
  1978 
  1979 int
  1980 SDL_QueryTexturePixels(SDL_Texture * texture, void **pixels, int *pitch)
  1981 {
  1982     SDL_Renderer *renderer;
  1983 
  1984     if (!texture) {
  1985         return -1;
  1986     }
  1987     renderer = texture->renderer;
  1988     if (!renderer->QueryTexturePixels) {
  1989         SDL_Unsupported();
  1990         return -1;
  1991     }
  1992     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1993 }
  1994 
  1995 int
  1996 SDL_SetTexturePalette(SDL_Texture * texture, const SDL_Color * colors,
  1997                       int firstcolor, int ncolors)
  1998 {
  1999     SDL_Renderer *renderer;
  2000 
  2001     if (!texture) {
  2002         return -1;
  2003     }
  2004     renderer = texture->renderer;
  2005     if (!renderer->SetTexturePalette) {
  2006         SDL_Unsupported();
  2007         return -1;
  2008     }
  2009     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  2010                                        ncolors);
  2011 }
  2012 
  2013 int
  2014 SDL_GetTexturePalette(SDL_Texture * texture, SDL_Color * colors,
  2015                       int firstcolor, int ncolors)
  2016 {
  2017     SDL_Renderer *renderer;
  2018 
  2019     if (!texture) {
  2020         return -1;
  2021     }
  2022     renderer = texture->renderer;
  2023     if (!renderer->GetTexturePalette) {
  2024         SDL_Unsupported();
  2025         return -1;
  2026     }
  2027     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  2028                                        ncolors);
  2029 }
  2030 
  2031 int
  2032 SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  2033 {
  2034     SDL_Renderer *renderer;
  2035 
  2036     if (!texture) {
  2037         return -1;
  2038     }
  2039     renderer = texture->renderer;
  2040     if (!renderer->SetTextureColorMod) {
  2041         SDL_Unsupported();
  2042         return -1;
  2043     }
  2044     if (r < 255 || g < 255 || b < 255) {
  2045         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  2046     } else {
  2047         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  2048     }
  2049     texture->r = r;
  2050     texture->g = g;
  2051     texture->b = b;
  2052     return renderer->SetTextureColorMod(renderer, texture);
  2053 }
  2054 
  2055 int
  2056 SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  2057                        Uint8 * b)
  2058 {
  2059     SDL_Renderer *renderer;
  2060 
  2061     if (!texture) {
  2062         return -1;
  2063     }
  2064     renderer = texture->renderer;
  2065     if (r) {
  2066         *r = texture->r;
  2067     }
  2068     if (g) {
  2069         *g = texture->g;
  2070     }
  2071     if (b) {
  2072         *b = texture->b;
  2073     }
  2074     return 0;
  2075 }
  2076 
  2077 int
  2078 SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  2079 {
  2080     SDL_Renderer *renderer;
  2081 
  2082     if (!texture) {
  2083         return -1;
  2084     }
  2085     renderer = texture->renderer;
  2086     if (!renderer->SetTextureAlphaMod) {
  2087         SDL_Unsupported();
  2088         return -1;
  2089     }
  2090     if (alpha < 255) {
  2091         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  2092     } else {
  2093         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  2094     }
  2095     texture->a = alpha;
  2096     return renderer->SetTextureAlphaMod(renderer, texture);
  2097 }
  2098 
  2099 int
  2100 SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  2101 {
  2102     if (!texture) {
  2103         return -1;
  2104     }
  2105     if (alpha) {
  2106         *alpha = texture->a;
  2107     }
  2108     return 0;
  2109 }
  2110 
  2111 int
  2112 SDL_SetTextureBlendMode(SDL_Texture * texture, int blendMode)
  2113 {
  2114     SDL_Renderer *renderer;
  2115 
  2116     if (!texture) {
  2117         return -1;
  2118     }
  2119     renderer = texture->renderer;
  2120     if (!renderer->SetTextureBlendMode) {
  2121         SDL_Unsupported();
  2122         return -1;
  2123     }
  2124     texture->blendMode = blendMode;
  2125     return renderer->SetTextureBlendMode(renderer, texture);
  2126 }
  2127 
  2128 int
  2129 SDL_GetTextureBlendMode(SDL_Texture * texture, int *blendMode)
  2130 {
  2131     if (!texture) {
  2132         return -1;
  2133     }
  2134     if (blendMode) {
  2135         *blendMode = texture->blendMode;
  2136     }
  2137     return 0;
  2138 }
  2139 
  2140 int
  2141 SDL_SetTextureScaleMode(SDL_Texture * texture, int scaleMode)
  2142 {
  2143     SDL_Renderer *renderer;
  2144 
  2145     if (!texture) {
  2146         return -1;
  2147     }
  2148     renderer = texture->renderer;
  2149     if (!renderer->SetTextureScaleMode) {
  2150         SDL_Unsupported();
  2151         return -1;
  2152     }
  2153     texture->scaleMode = scaleMode;
  2154     return renderer->SetTextureScaleMode(renderer, texture);
  2155 }
  2156 
  2157 int
  2158 SDL_GetTextureScaleMode(SDL_Texture * texture, int *scaleMode)
  2159 {
  2160     if (!texture) {
  2161         return -1;
  2162     }
  2163     if (scaleMode) {
  2164         *scaleMode = texture->scaleMode;
  2165     }
  2166     return 0;
  2167 }
  2168 
  2169 int
  2170 SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  2171                   const void *pixels, int pitch)
  2172 {
  2173     SDL_Renderer *renderer;
  2174     SDL_Rect full_rect;
  2175 
  2176     if (!texture) {
  2177         return -1;
  2178     }
  2179     renderer = texture->renderer;
  2180     if (!renderer->UpdateTexture) {
  2181         SDL_Unsupported();
  2182         return -1;
  2183     }
  2184     if (!rect) {
  2185         full_rect.x = 0;
  2186         full_rect.y = 0;
  2187         full_rect.w = texture->w;
  2188         full_rect.h = texture->h;
  2189         rect = &full_rect;
  2190     }
  2191     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  2192 }
  2193 
  2194 int
  2195 SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect, int markDirty,
  2196                 void **pixels, int *pitch)
  2197 {
  2198     SDL_Renderer *renderer;
  2199     SDL_Rect full_rect;
  2200 
  2201     if (!texture) {
  2202         return -1;
  2203     }
  2204     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2205         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  2206         return -1;
  2207     }
  2208     renderer = texture->renderer;
  2209     if (!renderer->LockTexture) {
  2210         SDL_Unsupported();
  2211         return -1;
  2212     }
  2213     if (!rect) {
  2214         full_rect.x = 0;
  2215         full_rect.y = 0;
  2216         full_rect.w = texture->w;
  2217         full_rect.h = texture->h;
  2218         rect = &full_rect;
  2219     }
  2220     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  2221                                  pitch);
  2222 }
  2223 
  2224 void
  2225 SDL_UnlockTexture(SDL_Texture * texture)
  2226 {
  2227     SDL_Renderer *renderer;
  2228 
  2229     if (!texture) {
  2230         return;
  2231     }
  2232     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2233         return;
  2234     }
  2235     renderer = texture->renderer;
  2236     if (!renderer->UnlockTexture) {
  2237         return;
  2238     }
  2239     renderer->UnlockTexture(renderer, texture);
  2240 }
  2241 
  2242 void
  2243 SDL_DirtyTexture(SDL_Texture * texture, int numrects,
  2244                  const SDL_Rect * rects)
  2245 {
  2246     SDL_Renderer *renderer;
  2247 
  2248     if (!texture) {
  2249         return;
  2250     }
  2251     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2252         return;
  2253     }
  2254     renderer = texture->renderer;
  2255     if (!renderer->DirtyTexture) {
  2256         return;
  2257     }
  2258     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2259 }
  2260 
  2261 int
  2262 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2263 {
  2264     SDL_Renderer *renderer;
  2265 
  2266     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2267     if (!renderer) {
  2268         return -1;
  2269     }
  2270     renderer->r = r;
  2271     renderer->g = g;
  2272     renderer->b = b;
  2273     renderer->a = a;
  2274     if (renderer->SetDrawColor) {
  2275         return renderer->SetDrawColor(renderer);
  2276     } else {
  2277         return 0;
  2278     }
  2279 }
  2280 
  2281 int
  2282 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  2283 {
  2284     SDL_Renderer *renderer;
  2285 
  2286     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2287     if (!renderer) {
  2288         return -1;
  2289     }
  2290     if (r) {
  2291         *r = renderer->r;
  2292     }
  2293     if (g) {
  2294         *g = renderer->g;
  2295     }
  2296     if (b) {
  2297         *b = renderer->b;
  2298     }
  2299     if (a) {
  2300         *a = renderer->a;
  2301     }
  2302     return 0;
  2303 }
  2304 
  2305 int
  2306 SDL_SetRenderDrawBlendMode(int blendMode)
  2307 {
  2308     SDL_Renderer *renderer;
  2309 
  2310     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2311     if (!renderer) {
  2312         return -1;
  2313     }
  2314     renderer->blendMode = blendMode;
  2315     if (renderer->SetDrawBlendMode) {
  2316         return renderer->SetDrawBlendMode(renderer);
  2317     } else {
  2318         return 0;
  2319     }
  2320 }
  2321 
  2322 int
  2323 SDL_GetRenderDrawBlendMode(int *blendMode)
  2324 {
  2325     SDL_Renderer *renderer;
  2326 
  2327     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2328     if (!renderer) {
  2329         return -1;
  2330     }
  2331     *blendMode = renderer->blendMode;
  2332     return 0;
  2333 }
  2334 
  2335 int
  2336 SDL_RenderClear()
  2337 {
  2338     SDL_Renderer *renderer;
  2339 
  2340     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2341     if (!renderer) {
  2342         return -1;
  2343     }
  2344     if (!renderer->RenderClear) {
  2345         int blendMode = renderer->blendMode;
  2346         int status;
  2347 
  2348         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2349             SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  2350         }
  2351 
  2352         status = SDL_RenderFillRect(NULL);
  2353 
  2354         if (blendMode >= SDL_BLENDMODE_BLEND) {
  2355             SDL_SetRenderDrawBlendMode(blendMode);
  2356         }
  2357         return status;
  2358     }
  2359     return renderer->RenderClear(renderer);
  2360 }
  2361 
  2362 int
  2363 SDL_RenderDrawPoint(int x, int y)
  2364 {
  2365     SDL_Point point;
  2366 
  2367     point.x = x;
  2368     point.y = y;
  2369     return SDL_RenderDrawPoints(&point, 1);
  2370 }
  2371 
  2372 int
  2373 SDL_RenderDrawPoints(const SDL_Point * points, int count)
  2374 {
  2375     SDL_Renderer *renderer;
  2376 
  2377     if (!points) {
  2378         SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  2379         return -1;
  2380     }
  2381 
  2382     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2383     if (!renderer) {
  2384         return -1;
  2385     }
  2386     if (!renderer->RenderDrawPoints) {
  2387         SDL_Unsupported();
  2388         return -1;
  2389     }
  2390     if (count < 1) {
  2391         return 0;
  2392     }
  2393     return renderer->RenderDrawPoints(renderer, points, count);
  2394 }
  2395 
  2396 int
  2397 SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
  2398 {
  2399     SDL_Point points[2];
  2400 
  2401     points[0].x = x1;
  2402     points[0].y = y1;
  2403     points[1].x = x2;
  2404     points[1].y = y2;
  2405     return SDL_RenderDrawLines(points, 2);
  2406 }
  2407 
  2408 int
  2409 SDL_RenderDrawLines(const SDL_Point * points, int count)
  2410 {
  2411     SDL_Renderer *renderer;
  2412 
  2413     if (!points) {
  2414         SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  2415         return -1;
  2416     }
  2417 
  2418     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2419     if (!renderer) {
  2420         return -1;
  2421     }
  2422     if (!renderer->RenderDrawLines) {
  2423         SDL_Unsupported();
  2424         return -1;
  2425     }
  2426     if (count < 2) {
  2427         return 0;
  2428     }
  2429     return renderer->RenderDrawLines(renderer, points, count);
  2430 }
  2431 
  2432 int
  2433 SDL_RenderDrawRect(const SDL_Rect * rect)
  2434 {
  2435     return SDL_RenderDrawRects(&rect, 1);
  2436 }
  2437 
  2438 int
  2439 SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
  2440 {
  2441     SDL_Renderer *renderer;
  2442     int i;
  2443 
  2444     if (!rects) {
  2445         SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  2446         return -1;
  2447     }
  2448 
  2449     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2450     if (!renderer) {
  2451         return -1;
  2452     }
  2453     if (!renderer->RenderDrawRects) {
  2454         SDL_Unsupported();
  2455         return -1;
  2456     }
  2457     if (count < 1) {
  2458         return 0;
  2459     }
  2460     /* Check for NULL rect, which means fill entire window */
  2461     for (i = 0; i < count; ++i) {
  2462         if (rects[i] == NULL) {
  2463             SDL_Window *window = renderer->window;
  2464             SDL_Rect full_rect;
  2465             const SDL_Rect *rect;
  2466 
  2467             full_rect.x = 0;
  2468             full_rect.y = 0;
  2469             full_rect.w = window->w;
  2470             full_rect.h = window->h;
  2471             rect = &full_rect;
  2472             return renderer->RenderDrawRects(renderer, &rect, 1);
  2473         }
  2474     }
  2475     return renderer->RenderDrawRects(renderer, rects, count);
  2476 }
  2477 
  2478 int
  2479 SDL_RenderFillRect(const SDL_Rect * rect)
  2480 {
  2481     return SDL_RenderFillRects(&rect, 1);
  2482 }
  2483 
  2484 int
  2485 SDL_RenderFillRects(const SDL_Rect ** rects, int count)
  2486 {
  2487     SDL_Renderer *renderer;
  2488     int i;
  2489 
  2490     if (!rects) {
  2491         SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  2492         return -1;
  2493     }
  2494 
  2495     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2496     if (!renderer) {
  2497         return -1;
  2498     }
  2499     if (!renderer->RenderFillRects) {
  2500         SDL_Unsupported();
  2501         return -1;
  2502     }
  2503     if (count < 1) {
  2504         return 0;
  2505     }
  2506     /* Check for NULL rect, which means fill entire window */
  2507     for (i = 0; i < count; ++i) {
  2508         if (rects[i] == NULL) {
  2509             SDL_Window *window = renderer->window;
  2510             SDL_Rect full_rect;
  2511             const SDL_Rect *rect;
  2512 
  2513             full_rect.x = 0;
  2514             full_rect.y = 0;
  2515             full_rect.w = window->w;
  2516             full_rect.h = window->h;
  2517             rect = &full_rect;
  2518             return renderer->RenderFillRects(renderer, &rect, 1);
  2519         }
  2520     }
  2521     return renderer->RenderFillRects(renderer, rects, count);
  2522 }
  2523 
  2524 int
  2525 SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
  2526                const SDL_Rect * dstrect)
  2527 {
  2528     SDL_Renderer *renderer;
  2529     SDL_Window *window;
  2530     SDL_Rect real_srcrect;
  2531     SDL_Rect real_dstrect;
  2532 
  2533     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2534     if (!renderer) {
  2535         return -1;
  2536     }
  2537     if (!texture) {
  2538         SDL_SetError("Texture not found");
  2539         return -1;
  2540     }
  2541     if (texture->renderer != renderer) {
  2542         SDL_SetError("Texture was not created with this renderer");
  2543         return -1;
  2544     }
  2545     if (!renderer->RenderCopy) {
  2546         SDL_Unsupported();
  2547         return -1;
  2548     }
  2549     window = renderer->window;
  2550 
  2551     real_srcrect.x = 0;
  2552     real_srcrect.y = 0;
  2553     real_srcrect.w = texture->w;
  2554     real_srcrect.h = texture->h;
  2555     if (srcrect) {
  2556         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2557             return 0;
  2558         }
  2559     }
  2560 
  2561     real_dstrect.x = 0;
  2562     real_dstrect.y = 0;
  2563     real_dstrect.w = window->w;
  2564     real_dstrect.h = window->h;
  2565     if (dstrect) {
  2566         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  2567             return 0;
  2568         }
  2569         /* Clip srcrect by the same amount as dstrect was clipped */
  2570         if (dstrect->w != real_dstrect.w) {
  2571             int deltax = (real_dstrect.x - dstrect->x);
  2572             int deltaw = (real_dstrect.w - dstrect->w);
  2573             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  2574             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  2575         }
  2576         if (dstrect->h != real_dstrect.h) {
  2577             int deltay = (real_dstrect.y - dstrect->y);
  2578             int deltah = (real_dstrect.h - dstrect->h);
  2579             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  2580             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  2581         }
  2582     }
  2583 
  2584     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2585                                 &real_dstrect);
  2586 }
  2587 
  2588 int
  2589 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
  2590                      void * pixels, int pitch)
  2591 {
  2592     SDL_Renderer *renderer;
  2593     SDL_Window *window;
  2594     SDL_Rect real_rect;
  2595 
  2596     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2597     if (!renderer) {
  2598         return -1;
  2599     }
  2600     if (!renderer->RenderReadPixels) {
  2601         SDL_Unsupported();
  2602         return -1;
  2603     }
  2604     window = renderer->window;
  2605 
  2606     if (!format) {
  2607         format = window->display->current_mode.format;
  2608     }
  2609 
  2610     real_rect.x = 0;
  2611     real_rect.y = 0;
  2612     real_rect.w = window->w;
  2613     real_rect.h = window->h;
  2614     if (rect) {
  2615         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2616             return 0;
  2617         }
  2618         if (real_rect.y > rect->y) {
  2619             pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2620         }
  2621         if (real_rect.x > rect->x) {
  2622             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2623             int bpp = SDL_BYTESPERPIXEL(format);
  2624             pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2625         }
  2626     }
  2627 
  2628     return renderer->RenderReadPixels(renderer, &real_rect,
  2629                                       format, pixels, pitch);
  2630 }
  2631 
  2632 int
  2633 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
  2634                       const void * pixels, int pitch)
  2635 {
  2636     SDL_Renderer *renderer;
  2637     SDL_Window *window;
  2638     SDL_Rect real_rect;
  2639 
  2640     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2641     if (!renderer) {
  2642         return -1;
  2643     }
  2644     if (!renderer->RenderWritePixels) {
  2645         SDL_Unsupported();
  2646         return -1;
  2647     }
  2648     window = renderer->window;
  2649 
  2650     if (!format) {
  2651         format = window->display->current_mode.format;
  2652     }
  2653 
  2654     real_rect.x = 0;
  2655     real_rect.y = 0;
  2656     real_rect.w = window->w;
  2657     real_rect.h = window->h;
  2658     if (rect) {
  2659         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2660             return 0;
  2661         }
  2662         if (real_rect.y > rect->y) {
  2663             pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2664         }
  2665         if (real_rect.x > rect->x) {
  2666             Uint32 format = SDL_CurrentDisplay->current_mode.format;
  2667             int bpp = SDL_BYTESPERPIXEL(format);
  2668             pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2669         }
  2670     }
  2671 
  2672     return renderer->RenderWritePixels(renderer, &real_rect,
  2673                                        format, pixels, pitch);
  2674 }
  2675 
  2676 void
  2677 SDL_RenderPresent(void)
  2678 {
  2679     SDL_Renderer *renderer;
  2680 
  2681     renderer = SDL_GetCurrentRenderer(SDL_TRUE);
  2682     if (!renderer || !renderer->RenderPresent) {
  2683         return;
  2684     }
  2685     renderer->RenderPresent(renderer);
  2686 }
  2687 
  2688 void
  2689 SDL_DestroyTexture(SDL_Texture * texture)
  2690 {
  2691     SDL_Renderer *renderer;
  2692 
  2693     if (!texture || !texture->renderer) {
  2694         SDL_SetError("Invalid texture");
  2695         return;
  2696     }
  2697 
  2698     renderer = texture->renderer;
  2699     if (texture->prev) {
  2700         texture->prev->next = texture->next;
  2701     } else {
  2702         renderer->textures = texture->next;
  2703     }
  2704     texture->renderer = NULL;
  2705 
  2706     renderer->DestroyTexture(renderer, texture);
  2707     SDL_free(texture);
  2708 }
  2709 
  2710 void
  2711 SDL_DestroyRenderer(SDL_Window * window)
  2712 {
  2713     SDL_Renderer *renderer;
  2714 
  2715     if (!window) {
  2716         return;
  2717     }
  2718     renderer = window->renderer;
  2719     if (!renderer) {
  2720         return;
  2721     }
  2722 
  2723     /* Free existing textures for this renderer */
  2724     while (renderer->textures) {
  2725         SDL_DestroyTexture(renderer->textures);
  2726     }
  2727 
  2728     /* Free the renderer instance */
  2729     renderer->DestroyRenderer(renderer);
  2730 
  2731     /* Clear references */
  2732     window->renderer = NULL;
  2733     if (SDL_CurrentDisplay->current_renderer == renderer) {
  2734         SDL_CurrentDisplay->current_renderer = NULL;
  2735     }
  2736 }
  2737 
  2738 SDL_bool
  2739 SDL_IsScreenSaverEnabled()
  2740 {
  2741     if (!_this) {
  2742         return SDL_TRUE;
  2743     }
  2744     return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
  2745 }
  2746 
  2747 void
  2748 SDL_EnableScreenSaver()
  2749 {
  2750     if (!_this) {
  2751         return;
  2752     }
  2753     if (!_this->suspend_screensaver) {
  2754         return;
  2755     }
  2756     _this->suspend_screensaver = SDL_FALSE;
  2757     if (_this->SuspendScreenSaver) {
  2758         _this->SuspendScreenSaver(_this);
  2759     }
  2760 }
  2761 
  2762 void
  2763 SDL_DisableScreenSaver()
  2764 {
  2765     if (!_this) {
  2766         return;
  2767     }
  2768     if (_this->suspend_screensaver) {
  2769         return;
  2770     }
  2771     _this->suspend_screensaver = SDL_TRUE;
  2772     if (_this->SuspendScreenSaver) {
  2773         _this->SuspendScreenSaver(_this);
  2774     }
  2775 }
  2776 
  2777 void
  2778 SDL_VideoQuit(void)
  2779 {
  2780     int i, j;
  2781 
  2782     if (!_this) {
  2783         return;
  2784     }
  2785     /* Halt event processing before doing anything else */
  2786     SDL_StopEventLoop();
  2787     SDL_EnableScreenSaver();
  2788 
  2789     /* Clean up the system video */
  2790     for (i = _this->num_displays; i--;) {
  2791         SDL_VideoDisplay *display = &_this->displays[i];
  2792         while (display->windows) {
  2793             SDL_DestroyWindow(display->windows);
  2794         }
  2795         if (display->render_drivers) {
  2796             SDL_free(display->render_drivers);
  2797             display->render_drivers = NULL;
  2798         }
  2799         display->num_render_drivers = 0;
  2800     }
  2801     _this->VideoQuit(_this);
  2802 
  2803     for (i = _this->num_displays; i--;) {
  2804         SDL_VideoDisplay *display = &_this->displays[i];
  2805         for (j = display->num_display_modes; j--;) {
  2806             if (display->display_modes[j].driverdata) {
  2807                 SDL_free(display->display_modes[j].driverdata);
  2808                 display->display_modes[j].driverdata = NULL;
  2809             }
  2810         }
  2811         if (display->display_modes) {
  2812             SDL_free(display->display_modes);
  2813             display->display_modes = NULL;
  2814         }
  2815         if (display->desktop_mode.driverdata) {
  2816             SDL_free(display->desktop_mode.driverdata);
  2817             display->desktop_mode.driverdata = NULL;
  2818         }
  2819         if (display->palette) {
  2820             SDL_FreePalette(display->palette);
  2821             display->palette = NULL;
  2822         }
  2823         if (display->gamma) {
  2824             SDL_free(display->gamma);
  2825             display->gamma = NULL;
  2826         }
  2827         if (display->driverdata) {
  2828             SDL_free(display->driverdata);
  2829             display->driverdata = NULL;
  2830         }
  2831     }
  2832     if (_this->displays) {
  2833         SDL_free(_this->displays);
  2834         _this->displays = NULL;
  2835     }
  2836     _this->free(_this);
  2837     _this = NULL;
  2838 }
  2839 
  2840 int
  2841 SDL_GL_LoadLibrary(const char *path)
  2842 {
  2843     int retval;
  2844 
  2845     if (!_this) {
  2846         SDL_UninitializedVideo();
  2847         return -1;
  2848     }
  2849     if (_this->gl_config.driver_loaded) {
  2850         if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
  2851             SDL_SetError("OpenGL library already loaded");
  2852             return -1;
  2853         }
  2854         retval = 0;
  2855     } else {
  2856         if (!_this->GL_LoadLibrary) {
  2857             SDL_SetError("No dynamic GL support in video driver");
  2858             return -1;
  2859         }
  2860         retval = _this->GL_LoadLibrary(_this, path);
  2861     }
  2862     if (retval == 0) {
  2863         ++_this->gl_config.driver_loaded;
  2864     }
  2865     return (retval);
  2866 }
  2867 
  2868 void *
  2869 SDL_GL_GetProcAddress(const char *proc)
  2870 {
  2871     void *func;
  2872 
  2873     if (!_this) {
  2874         SDL_UninitializedVideo();
  2875         return NULL;
  2876     }
  2877     func = NULL;
  2878     if (_this->GL_GetProcAddress) {
  2879         if (_this->gl_config.driver_loaded) {
  2880             func = _this->GL_GetProcAddress(_this, proc);
  2881         } else {
  2882             SDL_SetError("No GL driver has been loaded");
  2883         }
  2884     } else {
  2885         SDL_SetError("No dynamic GL support in video driver");
  2886     }
  2887     return func;
  2888 }
  2889 
  2890 void
  2891 SDL_GL_UnloadLibrary(void)
  2892 {
  2893     if (!_this) {
  2894         SDL_UninitializedVideo();
  2895         return;
  2896     }
  2897     if (_this->gl_config.driver_loaded > 0) {
  2898         if (--_this->gl_config.driver_loaded > 0) {
  2899             return;
  2900         }
  2901         if (_this->GL_UnloadLibrary) {
  2902             _this->GL_UnloadLibrary(_this);
  2903         }
  2904     }
  2905 }
  2906 
  2907 SDL_bool
  2908 SDL_GL_ExtensionSupported(const char *extension)
  2909 {
  2910 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2911     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2912     const char *extensions;
  2913     const char *start;
  2914     const char *where, *terminator;
  2915 
  2916     /* Extension names should not have spaces. */
  2917     where = SDL_strchr(extension, ' ');
  2918     if (where || *extension == '\0') {
  2919         return SDL_FALSE;
  2920     }
  2921     /* See if there's an environment variable override */
  2922     start = SDL_getenv(extension);
  2923     if (start && *start == '0') {
  2924         return SDL_FALSE;
  2925     }
  2926     /* Lookup the available extensions */
  2927     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2928     if (glGetStringFunc) {
  2929         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2930     } else {
  2931         extensions = NULL;
  2932     }
  2933     if (!extensions) {
  2934         return SDL_FALSE;
  2935     }
  2936     /*
  2937      * It takes a bit of care to be fool-proof about parsing the OpenGL
  2938      * extensions string. Don't be fooled by sub-strings, etc.
  2939      */
  2940 
  2941     start = extensions;
  2942 
  2943     for (;;) {
  2944         where = SDL_strstr(start, extension);
  2945         if (!where)
  2946             break;
  2947 
  2948         terminator = where + SDL_strlen(extension);
  2949         if (where == start || *(where - 1) == ' ')
  2950             if (*terminator == ' ' || *terminator == '\0')
  2951                 return SDL_TRUE;
  2952 
  2953         start = terminator;
  2954     }
  2955     return SDL_FALSE;
  2956 #else
  2957     return SDL_FALSE;
  2958 #endif
  2959 }
  2960 
  2961 int
  2962 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2963 {
  2964 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2965     int retval;
  2966 
  2967     if (!_this) {
  2968         SDL_UninitializedVideo();
  2969         return -1;
  2970     }
  2971     retval = 0;
  2972     switch (attr) {
  2973     case SDL_GL_RED_SIZE:
  2974         _this->gl_config.red_size = value;
  2975         break;
  2976     case SDL_GL_GREEN_SIZE:
  2977         _this->gl_config.green_size = value;
  2978         break;
  2979     case SDL_GL_BLUE_SIZE:
  2980         _this->gl_config.blue_size = value;
  2981         break;
  2982     case SDL_GL_ALPHA_SIZE:
  2983         _this->gl_config.alpha_size = value;
  2984         break;
  2985     case SDL_GL_DOUBLEBUFFER:
  2986         _this->gl_config.double_buffer = value;
  2987         break;
  2988     case SDL_GL_BUFFER_SIZE:
  2989         _this->gl_config.buffer_size = value;
  2990         break;
  2991     case SDL_GL_DEPTH_SIZE:
  2992         _this->gl_config.depth_size = value;
  2993         break;
  2994     case SDL_GL_STENCIL_SIZE:
  2995         _this->gl_config.stencil_size = value;
  2996         break;
  2997     case SDL_GL_ACCUM_RED_SIZE:
  2998         _this->gl_config.accum_red_size = value;
  2999         break;
  3000     case SDL_GL_ACCUM_GREEN_SIZE:
  3001         _this->gl_config.accum_green_size = value;
  3002         break;
  3003     case SDL_GL_ACCUM_BLUE_SIZE:
  3004         _this->gl_config.accum_blue_size = value;
  3005         break;
  3006     case SDL_GL_ACCUM_ALPHA_SIZE:
  3007         _this->gl_config.accum_alpha_size = value;
  3008         break;
  3009     case SDL_GL_STEREO:
  3010         _this->gl_config.stereo = value;
  3011         break;
  3012     case SDL_GL_MULTISAMPLEBUFFERS:
  3013         _this->gl_config.multisamplebuffers = value;
  3014         break;
  3015     case SDL_GL_MULTISAMPLESAMPLES:
  3016         _this->gl_config.multisamplesamples = value;
  3017         break;
  3018     case SDL_GL_ACCELERATED_VISUAL:
  3019         _this->gl_config.accelerated = value;
  3020         break;
  3021     case SDL_GL_RETAINED_BACKING:
  3022         _this->gl_config.retained_backing = value;
  3023         break;
  3024     case SDL_GL_CONTEXT_MAJOR_VERSION:
  3025         _this->gl_config.major_version = value;
  3026         break;
  3027     case SDL_GL_CONTEXT_MINOR_VERSION:
  3028         _this->gl_config.minor_version = value;
  3029         break;
  3030     default:
  3031         SDL_SetError("Unknown OpenGL attribute");
  3032         retval = -1;
  3033         break;
  3034     }
  3035     return retval;
  3036 #else
  3037     SDL_Unsupported();
  3038     return -1;
  3039 #endif /* SDL_VIDEO_OPENGL */
  3040 }
  3041 
  3042 int
  3043 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  3044 {
  3045 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  3046     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  3047     GLenum(APIENTRY * glGetErrorFunc) (void);
  3048     GLenum attrib = 0;
  3049     GLenum error = 0;
  3050 
  3051     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  3052     if (!glGetIntegervFunc) {
  3053         return -1;
  3054     }
  3055 
  3056     glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
  3057     if (!glGetErrorFunc) {
  3058         return -1;
  3059     }
  3060 
  3061     /* Clear value in any case */
  3062     *value = 0;
  3063 
  3064     switch (attr) {
  3065     case SDL_GL_RETAINED_BACKING:
  3066         *value = _this->gl_config.retained_backing;
  3067         return 0;
  3068     case SDL_GL_RED_SIZE:
  3069         attrib = GL_RED_BITS;
  3070         break;
  3071     case SDL_GL_BLUE_SIZE:
  3072         attrib = GL_BLUE_BITS;
  3073         break;
  3074     case SDL_GL_GREEN_SIZE:
  3075         attrib = GL_GREEN_BITS;
  3076         break;
  3077     case SDL_GL_ALPHA_SIZE:
  3078         attrib = GL_ALPHA_BITS;
  3079         break;
  3080     case SDL_GL_DOUBLEBUFFER:
  3081 #ifndef SDL_VIDEO_OPENGL_ES
  3082         attrib = GL_DOUBLEBUFFER;
  3083         break;
  3084 #else
  3085         /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
  3086         /* parameter which switches double buffer to single buffer. OpenGL ES */
  3087         /* SDL driver must set proper value after initialization              */
  3088         *value = _this->gl_config.double_buffer;
  3089         return 0;
  3090 #endif
  3091     case SDL_GL_DEPTH_SIZE:
  3092         attrib = GL_DEPTH_BITS;
  3093         break;
  3094     case SDL_GL_STENCIL_SIZE:
  3095         attrib = GL_STENCIL_BITS;
  3096         break;
  3097 #ifndef SDL_VIDEO_OPENGL_ES
  3098     case SDL_GL_ACCUM_RED_SIZE:
  3099         attrib = GL_ACCUM_RED_BITS;
  3100         break;
  3101     case SDL_GL_ACCUM_GREEN_SIZE:
  3102         attrib = GL_ACCUM_GREEN_BITS;
  3103         break;
  3104     case SDL_GL_ACCUM_BLUE_SIZE:
  3105         attrib = GL_ACCUM_BLUE_BITS;
  3106         break;
  3107     case SDL_GL_ACCUM_ALPHA_SIZE:
  3108         attrib = GL_ACCUM_ALPHA_BITS;
  3109         break;
  3110     case SDL_GL_STEREO:
  3111         attrib = GL_STEREO;
  3112         break;
  3113 #else
  3114     case SDL_GL_ACCUM_RED_SIZE:
  3115     case SDL_GL_ACCUM_GREEN_SIZE:
  3116     case SDL_GL_ACCUM_BLUE_SIZE:
  3117     case SDL_GL_ACCUM_ALPHA_SIZE:
  3118     case SDL_GL_STEREO:
  3119         /* none of these are supported in OpenGL ES */
  3120         *value = 0;
  3121         return 0;
  3122 #endif
  3123     case SDL_GL_MULTISAMPLEBUFFERS:
  3124 #ifndef SDL_VIDEO_OPENGL_ES
  3125         attrib = GL_SAMPLE_BUFFERS_ARB;
  3126 #else
  3127         attrib = GL_SAMPLE_BUFFERS;
  3128 #endif
  3129         break;
  3130     case SDL_GL_MULTISAMPLESAMPLES:
  3131 #ifndef SDL_VIDEO_OPENGL_ES
  3132         attrib = GL_SAMPLES_ARB;
  3133 #else
  3134         attrib = GL_SAMPLES;
  3135 #endif
  3136         break;
  3137     case SDL_GL_BUFFER_SIZE:
  3138         {
  3139             GLint bits = 0;
  3140             GLint component;
  3141 
  3142             /*
  3143              * there doesn't seem to be a single flag in OpenGL
  3144              * for this!
  3145              */
  3146             glGetIntegervFunc(GL_RED_BITS, &component);
  3147             bits += component;
  3148             glGetIntegervFunc(GL_GREEN_BITS, &component);
  3149             bits += component;
  3150             glGetIntegervFunc(GL_BLUE_BITS, &component);
  3151             bits += component;
  3152             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  3153             bits += component;
  3154 
  3155             *value = bits;
  3156             return 0;
  3157         }
  3158     case SDL_GL_ACCELERATED_VISUAL:
  3159         {
  3160             /* FIXME: How do we get this information? */
  3161             *value = (_this->gl_config.accelerated != 0);
  3162             return 0;
  3163         }
  3164     default:
  3165         SDL_SetError("Unknown OpenGL attribute");
  3166         return -1;
  3167     }
  3168 
  3169     glGetIntegervFunc(attrib, (GLint *) value);
  3170     error = glGetErrorFunc();
  3171     if (error != GL_NO_ERROR) {
  3172         switch (error) {
  3173         case GL_INVALID_ENUM:
  3174             {
  3175                 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
  3176             }
  3177             break;
  3178         case GL_INVALID_VALUE:
  3179             {
  3180                 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
  3181             }
  3182             break;
  3183         default:
  3184             {
  3185                 SDL_SetError("OpenGL error: %08X", error);
  3186             }
  3187             break;
  3188         }
  3189         return -1;
  3190     }
  3191     return 0;
  3192 #else
  3193     SDL_Unsupported();
  3194     return -1;
  3195 #endif /* SDL_VIDEO_OPENGL */
  3196 }
  3197 
  3198 SDL_GLContext
  3199 SDL_GL_CreateContext(SDL_Window * window)
  3200 {
  3201     if (!window) {
  3202         return NULL;
  3203     }
  3204     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3205         SDL_SetError("The specified window isn't an OpenGL window");
  3206         return NULL;
  3207     }
  3208     return _this->GL_CreateContext(_this, window);
  3209 }
  3210 
  3211 int
  3212 SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
  3213 {
  3214     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  3215         SDL_SetError("The specified window isn't an OpenGL window");
  3216         return -1;
  3217     }
  3218     if (!context) {
  3219         window = NULL;
  3220     }
  3221     return _this->GL_MakeCurrent(_this, window, context);
  3222 }
  3223 
  3224 int
  3225 SDL_GL_SetSwapInterval(int interval)
  3226 {
  3227     if (!_this) {
  3228         SDL_UninitializedVideo();
  3229         return -1;
  3230     }
  3231     if (_this->GL_SetSwapInterval) {
  3232         return _this->GL_SetSwapInterval(_this, interval);
  3233     } else {
  3234         SDL_SetError("Setting the swap interval is not supported");
  3235         return -1;
  3236     }
  3237 }
  3238 
  3239 int
  3240 SDL_GL_GetSwapInterval(void)
  3241 {
  3242     if (!_this) {
  3243         SDL_UninitializedVideo();
  3244         return -1;
  3245     }
  3246     if (_this->GL_GetSwapInterval) {
  3247         return _this->GL_GetSwapInterval(_this);
  3248     } else {
  3249         SDL_SetError("Getting the swap interval is not supported");
  3250         return -1;
  3251     }
  3252 }
  3253 
  3254 void
  3255 SDL_GL_SwapWindow(SDL_Window * window)
  3256 {
  3257     if (!window) {
  3258         return;
  3259     }
  3260     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  3261         SDL_SetError("The specified window isn't an OpenGL window");
  3262         return;
  3263     }
  3264     _this->GL_SwapWindow(_this, window);
  3265 }
  3266 
  3267 void
  3268 SDL_GL_DeleteContext(SDL_GLContext context)
  3269 {
  3270     if (!_this || !context) {
  3271         return;
  3272     }
  3273     _this->GL_MakeCurrent(_this, NULL, NULL);
  3274     _this->GL_DeleteContext(_this, context);
  3275 }
  3276 
  3277 #if 0                           // FIXME
  3278 /*
  3279  * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
  3280  * & 2 for alpha channel.
  3281  */
  3282 static void
  3283 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  3284 {
  3285     int x, y;
  3286     Uint32 colorkey;
  3287 #define SET_MASKBIT(icon, x, y, mask) \
  3288 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  3289 
  3290     colorkey = icon->format->colorkey;
  3291     switch (icon->format->BytesPerPixel) {
  3292     case 1:
  3293         {
  3294             Uint8 *pixels;
  3295             for (y = 0; y < icon->h; ++y) {
  3296                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  3297                 for (x = 0; x < icon->w; ++x) {
  3298                     if (*pixels++ == colorkey) {
  3299                         SET_MASKBIT(icon, x, y, mask);
  3300                     }
  3301                 }
  3302             }
  3303         }
  3304         break;
  3305 
  3306     case 2:
  3307         {
  3308             Uint16 *pixels;
  3309             for (y = 0; y < icon->h; ++y) {
  3310                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  3311                 for (x = 0; x < icon->w; ++x) {
  3312                     if ((flags & 1) && *pixels == colorkey) {
  3313                         SET_MASKBIT(icon, x, y, mask);
  3314                     } else if ((flags & 2)
  3315                                && (*pixels & icon->format->Amask) == 0) {
  3316                         SET_MASKBIT(icon, x, y, mask);
  3317                     }
  3318                     pixels++;
  3319                 }
  3320             }
  3321         }
  3322         break;
  3323 
  3324     case 4:
  3325         {
  3326             Uint32 *pixels;
  3327             for (y = 0; y < icon->h; ++y) {
  3328                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  3329                 for (x = 0; x < icon->w; ++x) {
  3330                     if ((flags & 1) && *pixels == colorkey) {
  3331                         SET_MASKBIT(icon, x, y, mask);
  3332                     } else if ((flags & 2)
  3333                                && (*pixels & icon->format->Amask) == 0) {
  3334                         SET_MASKBIT(icon, x, y, mask);
  3335                     }
  3336                     pixels++;
  3337                 }
  3338             }
  3339         }
  3340         break;
  3341     }
  3342 }
  3343 
  3344 /*
  3345  * Sets the window manager icon for the display window.
  3346  */
  3347 void
  3348 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  3349 {
  3350     if (icon && _this->SetIcon) {
  3351         /* Generate a mask if necessary, and create the icon! */
  3352         if (mask == NULL) {
  3353             int mask_len = icon->h * (icon->w + 7) / 8;
  3354             int flags = 0;
  3355             mask = (Uint8 *) SDL_malloc(mask_len);
  3356             if (mask == NULL) {
  3357                 return;
  3358             }
  3359             SDL_memset(mask, ~0, mask_len);
  3360             if (icon->flags & SDL_SRCCOLORKEY)
  3361                 flags |= 1;
  3362             if (icon->flags & SDL_SRCALPHA)
  3363                 flags |= 2;
  3364             if (flags) {
  3365                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  3366             }
  3367             _this->SetIcon(_this, icon, mask);
  3368             SDL_free(mask);
  3369         } else {
  3370             _this->SetIcon(_this, icon, mask);
  3371         }
  3372     }
  3373 }
  3374 #endif
  3375 
  3376 SDL_bool
  3377 SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
  3378 {
  3379     if (!window || !_this->GetWindowWMInfo) {
  3380         return SDL_FALSE;
  3381     }
  3382     return (_this->GetWindowWMInfo(_this, window, info));
  3383 }
  3384 
  3385 void
  3386 SDL_StartTextInput(void)
  3387 {
  3388     if (_this && _this->StartTextInput) {
  3389         _this->StartTextInput(_this);
  3390     }
  3391     SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
  3392     SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
  3393 }
  3394 
  3395 void
  3396 SDL_StopTextInput(void)
  3397 {
  3398     if (_this && _this->StopTextInput) {
  3399         _this->StopTextInput(_this);
  3400     }
  3401     SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
  3402     SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
  3403 }
  3404 
  3405 void
  3406 SDL_SetTextInputRect(SDL_Rect *rect)
  3407 {
  3408     if (_this && _this->SetTextInputRect) {
  3409         _this->SetTextInputRect(_this, rect);
  3410     }
  3411 }
  3412 
  3413 /* vi: set ts=4 sw=4 expandtab: */