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