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