src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 14 Jun 2006 06:26:35 +0000
branchSDL-1.3
changeset 1679 153477a6cc31
parent 1678 90bf530ced8e
child 1680 9488fca10677
permissions -rw-r--r--
More compile fixes
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_sysvideo.h"
    28 #include "SDL_blit.h"
    29 #include "SDL_pixels_c.h"
    30 #include "SDL_renderer_sw.h"
    31 #include "../events/SDL_sysevents.h"
    32 #include "../events/SDL_events_c.h"
    33 
    34 /* Available video drivers */
    35 static VideoBootStrap *bootstrap[] = {
    36 #if SDL_VIDEO_DRIVER_QUARTZ
    37     &QZ_bootstrap,
    38 #endif
    39 #if SDL_VIDEO_DRIVER_X11
    40     &X11_bootstrap,
    41 #endif
    42 #if SDL_VIDEO_DRIVER_DGA
    43     &DGA_bootstrap,
    44 #endif
    45 #if SDL_VIDEO_DRIVER_NANOX
    46     &NX_bootstrap,
    47 #endif
    48 #if SDL_VIDEO_DRIVER_IPOD
    49     &iPod_bootstrap,
    50 #endif
    51 #if SDL_VIDEO_DRIVER_QTOPIA
    52     &Qtopia_bootstrap,
    53 #endif
    54 #if SDL_VIDEO_DRIVER_WSCONS
    55     &WSCONS_bootstrap,
    56 #endif
    57 #if SDL_VIDEO_DRIVER_FBCON
    58     &FBCON_bootstrap,
    59 #endif
    60 #if SDL_VIDEO_DRIVER_DIRECTFB
    61     &DirectFB_bootstrap,
    62 #endif
    63 #if SDL_VIDEO_DRIVER_PS2GS
    64     &PS2GS_bootstrap,
    65 #endif
    66 #if SDL_VIDEO_DRIVER_GGI
    67     &GGI_bootstrap,
    68 #endif
    69 #if SDL_VIDEO_DRIVER_VGL
    70     &VGL_bootstrap,
    71 #endif
    72 #if SDL_VIDEO_DRIVER_SVGALIB
    73     &SVGALIB_bootstrap,
    74 #endif
    75 #if SDL_VIDEO_DRIVER_GAPI
    76     &GAPI_bootstrap,
    77 #endif
    78 #if SDL_VIDEO_DRIVER_WINDIB
    79     &WINDIB_bootstrap,
    80 #endif
    81 #if SDL_VIDEO_DRIVER_DDRAW
    82     &DIRECTX_bootstrap,
    83 #endif
    84 #if SDL_VIDEO_DRIVER_BWINDOW
    85     &BWINDOW_bootstrap,
    86 #endif
    87 #if SDL_VIDEO_DRIVER_TOOLBOX
    88     &TOOLBOX_bootstrap,
    89 #endif
    90 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
    91     &DSp_bootstrap,
    92 #endif
    93 #if SDL_VIDEO_DRIVER_CYBERGRAPHICS
    94     &CGX_bootstrap,
    95 #endif
    96 #if SDL_VIDEO_DRIVER_PHOTON
    97     &ph_bootstrap,
    98 #endif
    99 #if SDL_VIDEO_DRIVER_EPOC
   100     &EPOC_bootstrap,
   101 #endif
   102 #if SDL_VIDEO_DRIVER_XBIOS
   103     &XBIOS_bootstrap,
   104 #endif
   105 #if SDL_VIDEO_DRIVER_GEM
   106     &GEM_bootstrap,
   107 #endif
   108 #if SDL_VIDEO_DRIVER_PICOGUI
   109     &PG_bootstrap,
   110 #endif
   111 #if SDL_VIDEO_DRIVER_DC
   112     &DC_bootstrap,
   113 #endif
   114 #if SDL_VIDEO_DRIVER_RISCOS
   115     &RISCOS_bootstrap,
   116 #endif
   117 #if SDL_VIDEO_DRIVER_OS2FS
   118     &OS2FSLib_bootstrap,
   119 #endif
   120 #if SDL_VIDEO_DRIVER_AALIB
   121     &AALIB_bootstrap,
   122 #endif
   123 #if SDL_VIDEO_DRIVER_DUMMY
   124     &DUMMY_bootstrap,
   125 #endif
   126 #if SDL_VIDEO_DRIVER_GLSDL
   127     &glSDL_bootstrap,
   128 #endif
   129     NULL
   130 };
   131 
   132 static SDL_VideoDevice *_this = NULL;
   133 
   134 /* Various local functions */
   135 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   136 void SDL_VideoQuit(void);
   137 
   138 static int
   139 cmpmodes(const void *A, const void *B)
   140 {
   141     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   142     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   143 
   144     if (a.w != b.w) {
   145         return b.w - a.w;
   146     }
   147     if (a.h != b.h) {
   148         return b.h - a.h;
   149     }
   150     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   151         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   152     }
   153     if (a.refresh_rate != b.refresh_rate) {
   154         return b.refresh_rate - a.refresh_rate;
   155     }
   156     return 0;
   157 }
   158 
   159 int
   160 SDL_GetNumVideoDrivers(void)
   161 {
   162     return SDL_arraysize(bootstrap) - 1;
   163 }
   164 
   165 const char *
   166 SDL_GetVideoDriver(int index)
   167 {
   168     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   169         return bootstrap[index]->name;
   170     }
   171     return NULL;
   172 }
   173 
   174 /*
   175  * Initialize the video and event subsystems -- determine native pixel format
   176  */
   177 int
   178 SDL_VideoInit(const char *driver_name, Uint32 flags)
   179 {
   180     SDL_VideoDevice *video;
   181     int index;
   182     int i;
   183     int bpp;
   184     Uint32 Rmask, Gmask, Bmask, Amask;
   185 
   186     /* Toggle the event thread flags, based on OS requirements */
   187 #if defined(MUST_THREAD_EVENTS)
   188     flags |= SDL_INIT_EVENTTHREAD;
   189 #elif defined(CANT_THREAD_EVENTS)
   190     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   191         SDL_SetError("OS doesn't support threaded events");
   192         return -1;
   193     }
   194 #endif
   195 
   196     /* Check to make sure we don't overwrite '_this' */
   197     if (_this != NULL) {
   198         SDL_VideoQuit();
   199     }
   200 
   201     /* Select the proper video driver */
   202     index = 0;
   203     video = NULL;
   204     if (driver_name != NULL) {
   205         for (i = 0; bootstrap[i]; ++i) {
   206             if (SDL_strncmp(bootstrap[i]->name, driver_name,
   207                             SDL_strlen(bootstrap[i]->name)) == 0) {
   208                 if (bootstrap[i]->available()) {
   209                     video = bootstrap[i]->create(index);
   210                 }
   211                 break;
   212             }
   213         }
   214     } else {
   215         for (i = 0; bootstrap[i]; ++i) {
   216             if (bootstrap[i]->available()) {
   217                 video = bootstrap[i]->create(index);
   218                 if (video != NULL) {
   219                     break;
   220                 }
   221             }
   222         }
   223     }
   224     if (video == NULL) {
   225         if (driver_name) {
   226             SDL_SetError("%s not available", driver_name);
   227         } else {
   228             SDL_SetError("No available video device");
   229         }
   230         return -1;
   231     }
   232     _this = video;
   233     _this->name = bootstrap[i]->name;
   234     _this->next_object_id = 1;
   235 
   236 
   237     /* Set some very sane GL defaults */
   238     _this->gl_config.driver_loaded = 0;
   239     _this->gl_config.dll_handle = NULL;
   240     _this->gl_config.red_size = 3;
   241     _this->gl_config.green_size = 3;
   242     _this->gl_config.blue_size = 2;
   243     _this->gl_config.alpha_size = 0;
   244     _this->gl_config.buffer_size = 0;
   245     _this->gl_config.depth_size = 16;
   246     _this->gl_config.stencil_size = 0;
   247     _this->gl_config.double_buffer = 1;
   248     _this->gl_config.accum_red_size = 0;
   249     _this->gl_config.accum_green_size = 0;
   250     _this->gl_config.accum_blue_size = 0;
   251     _this->gl_config.accum_alpha_size = 0;
   252     _this->gl_config.stereo = 0;
   253     _this->gl_config.multisamplebuffers = 0;
   254     _this->gl_config.multisamplesamples = 0;
   255     _this->gl_config.accelerated = -1;  /* not known, don't set */
   256     _this->gl_config.swap_control = -1; /* not known, don't set */
   257 
   258     /* Initialize the video subsystem */
   259     if (_this->VideoInit(_this) < 0) {
   260         SDL_VideoQuit();
   261         return -1;
   262     }
   263 
   264     /* Make sure some displays were added */
   265     if (_this->num_displays == 0) {
   266         SDL_SetError("The video driver did not add any displays");
   267         SDL_VideoQuit();
   268         return (-1);
   269     }
   270 
   271     /* Sort the video modes */
   272     for (i = 0; i < _this->num_displays; ++i) {
   273         SDL_qsort(_this->displays[i].display_modes,
   274                   _this->displays[i].num_display_modes,
   275                   sizeof(SDL_DisplayMode), cmpmodes);
   276     }
   277 
   278     /* The software renderer is always available */
   279     for (i = 0; i < _this->num_displays; ++i) {
   280         if (_this->displays[i].num_render_drivers > 0) {
   281             SDL_AddRenderDriver(i, &SDL_SW_RenderDriver);
   282         }
   283     }
   284 
   285     /* Start the event loop */
   286     if (SDL_StartEventLoop(flags) < 0) {
   287         SDL_VideoQuit();
   288         return -1;
   289     }
   290 
   291     /* We're ready to go! */
   292     return 0;
   293 }
   294 
   295 const char *
   296 SDL_GetCurrentVideoDriver()
   297 {
   298     if (!_this) {
   299         return NULL;
   300     }
   301     return _this->name;
   302 }
   303 
   304 SDL_VideoDevice *
   305 SDL_GetVideoDevice()
   306 {
   307     return _this;
   308 }
   309 
   310 int
   311 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   312 {
   313     SDL_VideoDisplay display;
   314 
   315     SDL_zero(display);
   316     if (desktop_mode) {
   317         display.desktop_mode = *desktop_mode;
   318     }
   319     display.current_mode = display.desktop_mode;
   320 
   321     return SDL_AddVideoDisplay(&display);
   322 }
   323 
   324 int
   325 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   326 {
   327     SDL_VideoDisplay *displays;
   328     int index = -1;
   329 
   330     displays =
   331         SDL_realloc(_this->displays,
   332                     (_this->num_displays + 1) * sizeof(*displays));
   333     if (displays) {
   334         index = _this->num_displays++;
   335         displays[index] = *display;
   336         _this->displays = displays;
   337     } else {
   338         SDL_OutOfMemory();
   339     }
   340     return index;
   341 }
   342 
   343 int
   344 SDL_GetNumVideoDisplays(void)
   345 {
   346     if (!_this) {
   347         return 0;
   348     }
   349     return _this->num_displays;
   350 }
   351 
   352 int
   353 SDL_SelectVideoDisplay(int index)
   354 {
   355     if (!_this) {
   356         SDL_SetError("Video subsystem has not been initialized");
   357         return (-1);
   358     }
   359     if (index >= 0) {
   360         if (index >= _this->num_displays) {
   361             SDL_SetError("index must be in the range 0 - %d",
   362                          _this->num_displays - 1);
   363             return -1;
   364         }
   365         _this->current_display = index;
   366     }
   367     return _this->current_display;
   368 }
   369 
   370 void
   371 SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
   372 {
   373     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   374     SDL_DisplayMode *modes;
   375     int i, nmodes;
   376 
   377     /* Make sure we don't already have the mode in the list */
   378     modes = display->display_modes;
   379     nmodes = display->num_display_modes;
   380     for (i = 0; i < nmodes; ++i) {
   381         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   382             return;
   383         }
   384     }
   385 
   386     /* Go ahead and add the new mode */
   387     modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
   388     if (modes) {
   389         display->display_modes = modes;
   390         modes[nmodes] = *mode;
   391         display->num_display_modes++;
   392     }
   393 }
   394 
   395 int
   396 SDL_GetNumDisplayModes()
   397 {
   398     if (_this) {
   399         return SDL_CurrentDisplay.num_display_modes;
   400     }
   401     return 0;
   402 }
   403 
   404 const SDL_DisplayMode *
   405 SDL_GetDisplayMode(int index)
   406 {
   407     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   408         SDL_SetError("index must be in the range of 0 - %d",
   409                      SDL_GetNumDisplayModes() - 1);
   410         return NULL;
   411     }
   412     return &SDL_CurrentDisplay.display_modes[index];
   413 }
   414 
   415 const SDL_DisplayMode *
   416 SDL_GetDesktopDisplayMode(void)
   417 {
   418     if (_this) {
   419         return &SDL_CurrentDisplay.desktop_mode;
   420     }
   421     return NULL;
   422 }
   423 
   424 const SDL_DisplayMode *
   425 SDL_GetCurrentDisplayMode(void)
   426 {
   427     if (_this) {
   428         return &SDL_CurrentDisplay.current_mode;
   429     }
   430     return NULL;
   431 }
   432 
   433 SDL_DisplayMode *
   434 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   435                           SDL_DisplayMode * closest)
   436 {
   437     Uint32 target_format;
   438     int target_refresh_rate;
   439     int i;
   440     SDL_DisplayMode *current, *match;
   441 
   442     if (!_this || !mode || !closest) {
   443         return NULL;
   444     }
   445 
   446     /* Default to the desktop format */
   447     if (mode->format) {
   448         target_format = mode->format;
   449     } else {
   450         target_format = SDL_CurrentDisplay.desktop_mode.format;
   451     }
   452 
   453     /* Default to the desktop refresh rate */
   454     if (mode->refresh_rate) {
   455         target_refresh_rate = mode->refresh_rate;
   456     } else {
   457         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   458     }
   459 
   460     match = NULL;
   461     for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
   462         current = &SDL_CurrentDisplay.display_modes[i];
   463 
   464         if ((current->w && current->h) &&
   465             (current->w < mode->w || current->h < mode->h)) {
   466             /* Out of sorted modes large enough here */
   467             break;
   468         }
   469         if (!match || current->w < match->w || current->h < match->h) {
   470             match = current;
   471             continue;
   472         }
   473         if (current->format != match->format) {
   474             /* Sorted highest depth to lowest */
   475             if (current->format == target_format ||
   476                 (SDL_BITSPERPIXEL(current->format) >=
   477                  SDL_BITSPERPIXEL(target_format)
   478                  && SDL_PIXELTYPE(current->format) ==
   479                  SDL_PIXELTYPE(target_format))) {
   480                 match = current;
   481             }
   482             continue;
   483         }
   484         if (current->refresh_rate != match->refresh_rate) {
   485             /* Sorted highest refresh to lowest */
   486             if (current->refresh_rate >= target_refresh_rate) {
   487                 match = current;
   488             }
   489         }
   490     }
   491     if (match) {
   492         if (match->format) {
   493             closest->format = match->format;
   494         } else {
   495             closest->format = mode->format;
   496         }
   497         if (match->w && match->h) {
   498             closest->w = match->w;
   499             closest->h = match->h;
   500         } else {
   501             closest->w = mode->w;
   502             closest->h = mode->h;
   503         }
   504         if (match->refresh_rate) {
   505             closest->refresh_rate = match->refresh_rate;
   506         } else {
   507             closest->refresh_rate = mode->refresh_rate;
   508         }
   509         return closest;
   510     }
   511     return NULL;
   512 }
   513 
   514 int
   515 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   516 {
   517     SDL_VideoDisplay *display;
   518     SDL_DisplayMode display_mode;
   519     int i;
   520 
   521     if (!_this) {
   522         SDL_SetError("Video subsystem has not been initialized");
   523         return -1;
   524     }
   525 
   526     /* Make sure there's an actual display mode to set */
   527     if (!mode) {
   528         SDL_SetError("No mode passed to SDL_SetDisplayMode");
   529         return -1;
   530     }
   531     display = &SDL_CurrentDisplay;
   532     display_mode = *mode;
   533 
   534     /* Default to the current mode */
   535     if (!display_mode.format) {
   536         display_mode.format = display->current_mode.format;
   537     }
   538     if (!display_mode.w) {
   539         display_mode.w = display->current_mode.w;
   540     }
   541     if (!display_mode.h) {
   542         display_mode.h = display->current_mode.h;
   543     }
   544     if (!display_mode.refresh_rate) {
   545         display_mode.refresh_rate = display->current_mode.refresh_rate;
   546     }
   547 
   548     /* Get a good video mode, the closest one possible */
   549     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   550         SDL_SetError("No video mode large enough for %dx%d",
   551                      display_mode.w, display_mode.h);
   552         return -1;
   553     }
   554 
   555     /* See if there's anything left to do */
   556     if (SDL_memcmp
   557         (&display_mode, SDL_GetCurrentDisplayMode(),
   558          sizeof(display_mode)) == 0) {
   559         return 0;
   560     }
   561 
   562     return _this->SetDisplayMode(_this, &display_mode);
   563 }
   564 
   565 SDL_WindowID
   566 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   567 {
   568     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   569                                   SDL_WINDOW_BORDERLESS |
   570                                   SDL_WINDOW_SHOWN |
   571                                   SDL_WINDOW_OPENGL |
   572                                   SDL_WINDOW_RESIZABLE |
   573                                   SDL_WINDOW_MAXIMIZED |
   574                                   SDL_WINDOW_MINIMIZED |
   575                                   SDL_WINDOW_INPUT_GRABBED);
   576     SDL_Window window;
   577     int num_windows;
   578     SDL_Window *windows;
   579 
   580     if (!_this) {
   581         SDL_SetError("Video subsystem has not been initialized");
   582         return 0;
   583     }
   584 
   585     SDL_zero(window);
   586     window.id = _this->next_object_id++;
   587     window.title = title ? SDL_strdup(title) : NULL;
   588     window.x = x;
   589     window.y = y;
   590     window.w = w;
   591     window.h = h;
   592     window.flags = (flags & allowed_flags);
   593     window.display = &SDL_CurrentDisplay;
   594 
   595     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   596         if (window.title) {
   597             SDL_free(window.title);
   598         }
   599         return 0;
   600     }
   601 
   602     num_windows = SDL_CurrentDisplay.num_windows;
   603     windows =
   604         SDL_realloc(SDL_CurrentDisplay.windows,
   605                     (num_windows + 1) * sizeof(*windows));
   606     if (!windows) {
   607         if (_this->DestroyWindow) {
   608             _this->DestroyWindow(_this, &window);
   609         }
   610         if (window.title) {
   611             SDL_free(window.title);
   612         }
   613         return 0;
   614     }
   615     windows[num_windows] = window;
   616     SDL_CurrentDisplay.windows = windows;
   617     SDL_CurrentDisplay.num_windows++;
   618 
   619     return window.id;
   620 }
   621 
   622 SDL_WindowID
   623 SDL_CreateWindowFrom(void *data)
   624 {
   625     SDL_Window window;
   626     int num_windows;
   627     SDL_Window *windows;
   628 
   629     if (!_this) {
   630         SDL_SetError("Video subsystem has not been initialized");
   631         return (0);
   632     }
   633 
   634     SDL_zero(window);
   635     window.id = _this->next_object_id++;
   636     window.display = &SDL_CurrentDisplay;
   637 
   638     if (!_this->CreateWindowFrom ||
   639         _this->CreateWindowFrom(_this, &window, data) < 0) {
   640         return 0;
   641     }
   642 
   643     num_windows = SDL_CurrentDisplay.num_windows;
   644     windows =
   645         SDL_realloc(SDL_CurrentDisplay.windows,
   646                     (num_windows + 1) * sizeof(*windows));
   647     if (!windows) {
   648         if (_this->DestroyWindow) {
   649             _this->DestroyWindow(_this, &window);
   650         }
   651         if (window.title) {
   652             SDL_free(window.title);
   653         }
   654         return 0;
   655     }
   656     windows[num_windows] = window;
   657     SDL_CurrentDisplay.windows = windows;
   658     SDL_CurrentDisplay.num_windows++;
   659 
   660     return window.id;
   661 }
   662 
   663 static __inline__ SDL_Window *
   664 SDL_GetWindowFromID(SDL_WindowID windowID)
   665 {
   666     int i, j;
   667 
   668     if (!_this) {
   669         return NULL;
   670     }
   671 
   672     for (i = 0; i < _this->num_displays; ++i) {
   673         SDL_VideoDisplay *display = &_this->displays[i];
   674         for (j = 0; j < display->num_windows; ++j) {
   675             SDL_Window *window = &display->windows[j];
   676             if (window->id == windowID) {
   677                 return window;
   678             }
   679         }
   680     }
   681     return NULL;
   682 }
   683 
   684 Uint32
   685 SDL_GetWindowFlags(SDL_WindowID windowID)
   686 {
   687     SDL_Window *window = SDL_GetWindowFromID(windowID);
   688 
   689     if (!window) {
   690         return 0;
   691     }
   692     return window->flags;
   693 }
   694 
   695 void
   696 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   697 {
   698     SDL_Window *window = SDL_GetWindowFromID(windowID);
   699 
   700     if (!window) {
   701         return;
   702     }
   703     if (window->title) {
   704         SDL_free(window->title);
   705     }
   706     window->title = SDL_strdup(title);
   707 
   708     if (_this->SetWindowTitle) {
   709         _this->SetWindowTitle(_this, window);
   710     }
   711 }
   712 
   713 const char *
   714 SDL_GetWindowTitle(SDL_WindowID windowID)
   715 {
   716     SDL_Window *window = SDL_GetWindowFromID(windowID);
   717 
   718     if (!window) {
   719         return NULL;
   720     }
   721     return window->title;
   722 }
   723 
   724 void
   725 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   726 {
   727     SDL_Window *window = SDL_GetWindowFromID(windowID);
   728 
   729     if (!window) {
   730         return;
   731     }
   732     window->userdata = userdata;
   733 }
   734 
   735 void *
   736 SDL_GetWindowData(SDL_WindowID windowID)
   737 {
   738     SDL_Window *window = SDL_GetWindowFromID(windowID);
   739 
   740     if (!window) {
   741         return NULL;
   742     }
   743     return window->userdata;
   744 }
   745 
   746 void
   747 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   748 {
   749     SDL_Window *window = SDL_GetWindowFromID(windowID);
   750 
   751     if (!window) {
   752         return;
   753     }
   754 
   755     window->x = x;
   756     window->y = y;
   757 
   758     if (_this->SetWindowPosition) {
   759         _this->SetWindowPosition(_this, window);
   760     }
   761 }
   762 
   763 void
   764 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   765 {
   766     SDL_Window *window = SDL_GetWindowFromID(windowID);
   767 
   768     if (!window) {
   769         return;
   770     }
   771     if (x) {
   772         *x = window->x;
   773     }
   774     if (y) {
   775         *y = window->y;
   776     }
   777 }
   778 
   779 void
   780 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   781 {
   782     SDL_Window *window = SDL_GetWindowFromID(windowID);
   783 
   784     if (!window) {
   785         return;
   786     }
   787 
   788     window->w = w;
   789     window->h = h;
   790 
   791     if (_this->SetWindowSize) {
   792         _this->SetWindowSize(_this, window);
   793     }
   794 }
   795 
   796 void
   797 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   798 {
   799     SDL_Window *window = SDL_GetWindowFromID(windowID);
   800 
   801     if (!window) {
   802         return;
   803     }
   804     if (w) {
   805         *w = window->w;
   806     }
   807     if (h) {
   808         *h = window->h;
   809     }
   810 }
   811 
   812 void
   813 SDL_ShowWindow(SDL_WindowID windowID)
   814 {
   815     SDL_Window *window = SDL_GetWindowFromID(windowID);
   816 
   817     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
   818         return;
   819     }
   820 
   821     window->flags |= SDL_WINDOW_SHOWN;
   822 
   823     if (_this->ShowWindow) {
   824         _this->ShowWindow(_this, window);
   825     }
   826 }
   827 
   828 void
   829 SDL_HideWindow(SDL_WindowID windowID)
   830 {
   831     SDL_Window *window = SDL_GetWindowFromID(windowID);
   832 
   833     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
   834         return;
   835     }
   836 
   837     window->flags &= ~SDL_WINDOW_SHOWN;
   838 
   839     if (_this->HideWindow) {
   840         _this->HideWindow(_this, window);
   841     }
   842 }
   843 
   844 void
   845 SDL_RaiseWindow(SDL_WindowID windowID)
   846 {
   847     SDL_Window *window = SDL_GetWindowFromID(windowID);
   848 
   849     if (!window) {
   850         return;
   851     }
   852 
   853     if (_this->RaiseWindow) {
   854         _this->RaiseWindow(_this, window);
   855     }
   856 }
   857 
   858 void
   859 SDL_MaximizeWindow(SDL_WindowID windowID)
   860 {
   861     SDL_Window *window = SDL_GetWindowFromID(windowID);
   862 
   863     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
   864         return;
   865     }
   866 
   867     window->flags |= SDL_WINDOW_MAXIMIZED;
   868 
   869     if (_this->MaximizeWindow) {
   870         _this->MaximizeWindow(_this, window);
   871     }
   872 }
   873 
   874 void
   875 SDL_MinimizeWindow(SDL_WindowID windowID)
   876 {
   877     SDL_Window *window = SDL_GetWindowFromID(windowID);
   878 
   879     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
   880         return;
   881     }
   882 
   883     window->flags |= SDL_WINDOW_MINIMIZED;
   884 
   885     if (_this->MinimizeWindow) {
   886         _this->MinimizeWindow(_this, window);
   887     }
   888 }
   889 
   890 void
   891 SDL_RestoreWindow(SDL_WindowID windowID)
   892 {
   893     SDL_Window *window = SDL_GetWindowFromID(windowID);
   894 
   895     if (!window
   896         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
   897         return;
   898     }
   899 
   900     window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
   901 
   902     if (_this->RestoreWindow) {
   903         _this->RestoreWindow(_this, window);
   904     }
   905 }
   906 
   907 void
   908 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
   909 {
   910     SDL_Window *window = SDL_GetWindowFromID(windowID);
   911 
   912     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
   913         return;
   914     }
   915 
   916     if (mode) {
   917         window->flags |= SDL_WINDOW_INPUT_GRABBED;
   918     } else {
   919         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   920     }
   921 
   922     if (_this->SetWindowGrab) {
   923         _this->SetWindowGrab(_this, window);
   924     }
   925 }
   926 
   927 int
   928 SDL_GetWindowGrab(SDL_WindowID windowID)
   929 {
   930     SDL_Window *window = SDL_GetWindowFromID(windowID);
   931 
   932     if (!window) {
   933         return 0;
   934     }
   935 
   936     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
   937 }
   938 
   939 void
   940 SDL_DestroyWindow(SDL_WindowID windowID)
   941 {
   942     int i, j;
   943 
   944     if (!_this) {
   945         return;
   946     }
   947 
   948     for (i = 0; i < _this->num_displays; ++i) {
   949         SDL_VideoDisplay *display = &_this->displays[i];
   950         for (j = 0; j < display->num_windows; ++j) {
   951             SDL_Window *window = &display->windows[j];
   952             if (window->id != windowID) {
   953                 continue;
   954             }
   955             if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   956                 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   957                 _this->SetWindowGrab(_this, window);
   958             }
   959             if (window->renderer) {
   960                 SDL_DestroyRenderer(window->id);
   961             }
   962             if (_this->DestroyWindow) {
   963                 _this->DestroyWindow(_this, window);
   964             }
   965             if (window->title) {
   966                 SDL_free(window->title);
   967             }
   968             if (window->gamma) {
   969                 SDL_free(window->gamma);
   970             }
   971             if (j != display->num_windows - 1) {
   972                 SDL_memcpy(&display->windows[i],
   973                            &display->windows[i + 1],
   974                            (display->num_windows - i - 1) * sizeof(*window));
   975             }
   976             --display->num_windows;
   977             return;
   978         }
   979     }
   980 }
   981 
   982 void
   983 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
   984 {
   985     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   986     SDL_RenderDriver *render_drivers;
   987 
   988     render_drivers =
   989         SDL_realloc(display->render_drivers,
   990                     (display->num_render_drivers +
   991                      1) * sizeof(*render_drivers));
   992     if (render_drivers) {
   993         render_drivers[display->num_render_drivers] = *driver;
   994         display->render_drivers = render_drivers;
   995         display->num_render_drivers++;
   996     }
   997 }
   998 
   999 int
  1000 SDL_GetNumRenderers(void)
  1001 {
  1002     if (_this) {
  1003         return SDL_CurrentDisplay.num_render_drivers;
  1004     }
  1005     return 0;
  1006 }
  1007 
  1008 int
  1009 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1010 {
  1011     if (index < 0 || index >= SDL_GetNumRenderers()) {
  1012         SDL_SetError("index must be in the range of 0 - %d",
  1013                      SDL_GetNumRenderers() - 1);
  1014         return -1;
  1015     }
  1016     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1017     return 0;
  1018 }
  1019 
  1020 int
  1021 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1022 {
  1023     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1024 
  1025     if (!window) {
  1026         return 0;
  1027     }
  1028 
  1029     if (index < 0) {
  1030         int n = SDL_GetNumRenderers();
  1031         for (index = 0; index < n; ++index) {
  1032             if ((SDL_CurrentDisplay.render_drivers[index].info.
  1033                  flags & flags) == flags) {
  1034                 break;
  1035             }
  1036         }
  1037         if (index == n) {
  1038             SDL_SetError("Couldn't find matching render driver");
  1039             return -1;
  1040         }
  1041     }
  1042 
  1043     if (index >= SDL_GetNumRenderers()) {
  1044         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1045                      SDL_GetNumRenderers() - 1);
  1046         return -1;
  1047     }
  1048 
  1049     /* Free any existing renderer */
  1050     SDL_DestroyRenderer(windowID);
  1051 
  1052     /* Create a new renderer instance */
  1053     window->renderer =
  1054         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1055                                                                 flags);
  1056     if (!window->renderer) {
  1057         return -1;
  1058     }
  1059     SDL_CurrentDisplay.current_renderer = window->renderer;
  1060 
  1061     return 0;
  1062 }
  1063 
  1064 int
  1065 SDL_SelectRenderer(SDL_WindowID windowID)
  1066 {
  1067     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1068 
  1069     if (!window || !window->renderer) {
  1070         return -1;
  1071     }
  1072     SDL_CurrentDisplay.current_renderer = window->renderer;
  1073     return 0;
  1074 }
  1075 
  1076 SDL_TextureID
  1077 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1078 {
  1079     SDL_Renderer *renderer;
  1080     SDL_Texture *texture;
  1081 
  1082     if (!_this) {
  1083         return 0;
  1084     }
  1085 
  1086     renderer = SDL_CurrentDisplay.current_renderer;
  1087     if (!renderer || !renderer->CreateTexture) {
  1088         return 0;
  1089     }
  1090 
  1091     texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
  1092     if (!texture) {
  1093         SDL_OutOfMemory();
  1094         return 0;
  1095     }
  1096 
  1097     SDL_zerop(texture);
  1098     texture->id = _this->next_object_id++;
  1099     texture->format = format;
  1100     texture->access = access;
  1101     texture->w = w;
  1102     texture->h = h;
  1103     texture->renderer = renderer;
  1104 
  1105     if (renderer->CreateTexture(renderer, texture) < 0) {
  1106         SDL_free(texture);
  1107         return 0;
  1108     }
  1109 }
  1110 
  1111 SDL_TextureID
  1112 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1113 {
  1114     SDL_TextureID textureID;
  1115     Uint32 surface_flags = surface->flags;
  1116     SDL_PixelFormat *fmt = surface->format;
  1117     Uint32 colorkey;
  1118     Uint8 alpha;
  1119     SDL_Rect bounds;
  1120     SDL_Surface dst;
  1121     int bpp;
  1122     Uint32 Rmask, Gmask, Bmask, Amask;
  1123 
  1124     if (!surface) {
  1125         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1126         return 0;
  1127     }
  1128 
  1129     if (format) {
  1130         if (!SDL_PixelFormatEnumToMasks
  1131             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1132             SDL_SetError("Unknown pixel format");
  1133             return 0;
  1134         }
  1135     } else {
  1136         bpp = fmt->BitsPerPixel;
  1137         Rmask = fmt->Rmask;
  1138         Gmask = fmt->Gmask;
  1139         Bmask = fmt->Bmask;
  1140         Amask = fmt->Amask;
  1141         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1142         if (!format) {
  1143             SDL_SetError("Unknown pixel format");
  1144             return 0;
  1145         }
  1146     }
  1147 
  1148     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1149     if (!textureID) {
  1150         return 0;
  1151     }
  1152 
  1153     /* Set up a destination surface for the texture update */
  1154     SDL_zero(dst);
  1155     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1156     if (!dst.format) {
  1157         SDL_DestroyTexture(textureID);
  1158         return 0;
  1159     }
  1160     dst.w = surface->w;
  1161     dst.h = surface->h;
  1162     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1163         dst.flags |= SDL_PREALLOC;
  1164     } else {
  1165         dst.pitch = SDL_CalculatePitch(&dst);
  1166         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1167         if (!dst.pixels) {
  1168             SDL_DestroyTexture(textureID);
  1169             SDL_FreeFormat(dst.format);
  1170             SDL_OutOfMemory();
  1171             return 0;
  1172         }
  1173     }
  1174 
  1175     /* Copy the palette if any */
  1176     if (fmt->palette && dst.format->palette) {
  1177         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1178                               fmt->palette->ncolors);
  1179 
  1180         SDL_memcpy(dst.format->palette->colors,
  1181                    fmt->palette->colors,
  1182                    fmt->palette->ncolors * sizeof(SDL_Color));
  1183         dst.format->palette->ncolors = fmt->palette->ncolors;
  1184     }
  1185 
  1186     /* Make the texture transparent if the surface has colorkey */
  1187     if (surface_flags & SDL_SRCCOLORKEY) {
  1188         int row;
  1189         int length = dst.w * dst.format->BytesPerPixel;
  1190         Uint8 *p = (Uint8 *) dst.pixels;
  1191         for (row = 0; row < dst.h; ++row) {
  1192             SDL_memset(p, 0, length);
  1193             p += dst.pitch;
  1194         }
  1195     }
  1196 
  1197     /* Copy over the alpha channel */
  1198     if (surface_flags & SDL_SRCALPHA) {
  1199         if (fmt->Amask) {
  1200             surface->flags &= ~SDL_SRCALPHA;
  1201         } else {
  1202             /* FIXME: Need to make sure the texture has an alpha channel
  1203              *        and copy 'alpha' into the texture alpha channel.
  1204              */
  1205             alpha = surface->format->alpha;
  1206             SDL_SetAlpha(surface, 0, 0);
  1207         }
  1208     }
  1209 
  1210     /* Copy over the image data */
  1211     bounds.x = 0;
  1212     bounds.y = 0;
  1213     bounds.w = surface->w;
  1214     bounds.h = surface->h;
  1215     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1216 
  1217     /* Clean up the original surface */
  1218     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1219         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
  1220         SDL_SetColorKey(surface, cflags, colorkey);
  1221     }
  1222     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1223         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1224         if (fmt->Amask) {
  1225             surface->flags |= SDL_SRCALPHA;
  1226         } else {
  1227             SDL_SetAlpha(surface, aflags, alpha);
  1228         }
  1229     }
  1230 
  1231     /* Update the texture */
  1232     if (dst.flags & SDL_PREALLOC) {
  1233         SDL_UnlockTexture(textureID);
  1234     } else {
  1235         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1236         SDL_free(dst.pixels);
  1237     }
  1238     SDL_FreeFormat(dst.format);
  1239 
  1240     return textureID;
  1241 }
  1242 
  1243 static __inline__ SDL_Texture *
  1244 SDL_GetTextureFromID(SDL_TextureID textureID)
  1245 {
  1246     int hash;
  1247     SDL_Texture *texture;
  1248 
  1249     if (!_this) {
  1250         return NULL;
  1251     }
  1252 
  1253     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1254     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1255          texture = texture->next) {
  1256         if (texture->id == textureID) {
  1257             return texture;
  1258         }
  1259     }
  1260     return NULL;
  1261 }
  1262 
  1263 int
  1264 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1265                  int *w, int *h)
  1266 {
  1267     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1268 
  1269     if (!texture) {
  1270         return -1;
  1271     }
  1272 
  1273     if (format) {
  1274         *format = texture->format;
  1275     }
  1276     if (access) {
  1277         *access = texture->access;
  1278     }
  1279     if (w) {
  1280         *w = texture->w;
  1281     }
  1282     if (h) {
  1283         *h = texture->h;
  1284     }
  1285     return 0;
  1286 }
  1287 
  1288 int
  1289 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1290 {
  1291     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1292     SDL_Renderer *renderer;
  1293 
  1294     if (!texture) {
  1295         return -1;
  1296     }
  1297 
  1298     renderer = texture->renderer;
  1299     if (!renderer->QueryTexturePixels) {
  1300         return -1;
  1301     }
  1302     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1303 }
  1304 
  1305 int
  1306 SDL_UpdateTexture(SDL_TextureID textureID, SDL_Rect * rect,
  1307                   const void *pixels, int pitch)
  1308 {
  1309     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1310     SDL_Renderer *renderer;
  1311 
  1312     if (!texture) {
  1313         return -1;
  1314     }
  1315 
  1316     renderer = texture->renderer;
  1317     if (!renderer->UpdateTexture) {
  1318         return -1;
  1319     }
  1320     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1321 }
  1322 
  1323 int
  1324 SDL_LockTexture(SDL_TextureID textureID, SDL_Rect * rect, int markDirty,
  1325                 void **pixels, int *pitch)
  1326 {
  1327     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1328     SDL_Renderer *renderer;
  1329 
  1330     if (!texture) {
  1331         return -1;
  1332     }
  1333 
  1334     renderer = texture->renderer;
  1335     if (!renderer->LockTexture) {
  1336         return -1;
  1337     }
  1338     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1339                                  pitch);
  1340 }
  1341 
  1342 void
  1343 SDL_UnlockTexture(SDL_TextureID textureID)
  1344 {
  1345     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1346     SDL_Renderer *renderer;
  1347 
  1348     if (!texture) {
  1349         return;
  1350     }
  1351 
  1352     renderer = texture->renderer;
  1353     if (!renderer->UnlockTexture) {
  1354         return;
  1355     }
  1356     return renderer->UnlockTexture(renderer, texture);
  1357 }
  1358 
  1359 void
  1360 SDL_DirtyTexture(SDL_TextureID textureID, int numrects, SDL_Rect * rects)
  1361 {
  1362     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1363     SDL_Renderer *renderer;
  1364 
  1365     if (!texture) {
  1366         return;
  1367     }
  1368 
  1369     renderer = texture->renderer;
  1370     if (!renderer->DirtyTexture) {
  1371         return;
  1372     }
  1373     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1374 }
  1375 
  1376 void
  1377 SDL_SelectRenderTexture(SDL_TextureID textureID)
  1378 {
  1379     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1380     SDL_Renderer *renderer;
  1381 
  1382     if (!texture || texture->access != SDL_TextureAccess_Render) {
  1383         return;
  1384     }
  1385     renderer = texture->renderer;
  1386     if (!renderer->SelectRenderTexture) {
  1387         return;
  1388     }
  1389     renderer->SelectRenderTexture(renderer, texture);
  1390 }
  1391 
  1392 int
  1393 SDL_RenderFill(SDL_Rect * rect, Uint32 color)
  1394 {
  1395     SDL_Renderer *renderer;
  1396 
  1397     if (!_this) {
  1398         return -1;
  1399     }
  1400 
  1401     renderer = SDL_CurrentDisplay.current_renderer;
  1402     if (!renderer || !renderer->RenderFill) {
  1403         return -1;
  1404     }
  1405 
  1406     renderer->RenderFill(renderer, rect, color);
  1407 }
  1408 
  1409 int
  1410 SDL_RenderCopy(SDL_TextureID textureID, SDL_Rect * srcrect,
  1411                SDL_Rect * dstrect, int blendMode, int scaleMode)
  1412 {
  1413     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1414     SDL_Renderer *renderer;
  1415 
  1416     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1417         return;
  1418     }
  1419 
  1420     renderer = SDL_CurrentDisplay.current_renderer;
  1421     if (!renderer || !renderer->RenderCopy) {
  1422         return -1;
  1423     }
  1424 
  1425     return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
  1426                                 blendMode, scaleMode);
  1427 }
  1428 
  1429 int
  1430 SDL_RenderReadPixels(SDL_Rect * rect, void *pixels, int pitch)
  1431 {
  1432     SDL_Renderer *renderer;
  1433 
  1434     if (!_this) {
  1435         return -1;
  1436     }
  1437 
  1438     renderer = SDL_CurrentDisplay.current_renderer;
  1439     if (!renderer || !renderer->RenderReadPixels) {
  1440         return -1;
  1441     }
  1442 
  1443     return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
  1444 }
  1445 
  1446 int
  1447 SDL_RenderWritePixels(SDL_Rect * rect, const void *pixels, int pitch)
  1448 {
  1449     SDL_Renderer *renderer;
  1450 
  1451     if (!_this) {
  1452         return -1;
  1453     }
  1454 
  1455     renderer = SDL_CurrentDisplay.current_renderer;
  1456     if (!renderer || !renderer->RenderWritePixels) {
  1457         return -1;
  1458     }
  1459 
  1460     return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
  1461 }
  1462 
  1463 void
  1464 SDL_RenderPresent(void)
  1465 {
  1466     SDL_Renderer *renderer;
  1467 
  1468     if (!_this) {
  1469         return;
  1470     }
  1471 
  1472     renderer = SDL_CurrentDisplay.current_renderer;
  1473     if (!renderer || !renderer->RenderPresent) {
  1474         return;
  1475     }
  1476 
  1477     renderer->RenderPresent(renderer);
  1478 }
  1479 
  1480 void
  1481 SDL_DestroyTexture(SDL_TextureID textureID)
  1482 {
  1483     int hash;
  1484     SDL_Texture *prev, *texture;
  1485     SDL_Renderer *renderer;
  1486 
  1487     if (!_this) {
  1488         return;
  1489     }
  1490 
  1491     /* Look up the texture in the hash table */
  1492     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1493     prev = NULL;
  1494     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1495          prev = texture, texture = texture->next) {
  1496         if (texture->id == textureID) {
  1497             break;
  1498         }
  1499     }
  1500     if (!texture) {
  1501         return;
  1502     }
  1503 
  1504     /* Unlink the texture from the list */
  1505     if (prev) {
  1506         prev->next = texture->next;
  1507     } else {
  1508         SDL_CurrentDisplay.textures[hash] = texture->next;
  1509     }
  1510 
  1511     /* Free the texture */
  1512     renderer = texture->renderer;
  1513     renderer->DestroyTexture(renderer, texture);
  1514     SDL_free(texture);
  1515 }
  1516 
  1517 void
  1518 SDL_DestroyRenderer(SDL_WindowID windowID)
  1519 {
  1520     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1521     SDL_Renderer *renderer;
  1522     int i;
  1523 
  1524     if (!window) {
  1525         return;
  1526     }
  1527 
  1528     renderer = window->renderer;
  1529     if (!renderer) {
  1530         return;
  1531     }
  1532 
  1533     /* Free existing textures for this renderer */
  1534     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1535         SDL_Texture *texture;
  1536         SDL_Texture *prev = NULL;
  1537         SDL_Texture *next;
  1538         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1539              texture = next) {
  1540             next = texture->next;
  1541             if (texture->renderer == renderer) {
  1542                 if (prev) {
  1543                     prev->next = next;
  1544                 } else {
  1545                     SDL_CurrentDisplay.textures[i] = next;
  1546                 }
  1547                 renderer->DestroyTexture(renderer, texture);
  1548                 SDL_free(texture);
  1549             } else {
  1550                 prev = texture;
  1551             }
  1552         }
  1553     }
  1554 
  1555     /* Free the renderer instance */
  1556     renderer->DestroyRenderer(renderer);
  1557 
  1558     /* Clear references */
  1559     window->renderer = NULL;
  1560     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1561         SDL_CurrentDisplay.current_renderer = NULL;
  1562     }
  1563 }
  1564 
  1565 void
  1566 SDL_VideoQuit(void)
  1567 {
  1568     int i, j;
  1569 
  1570     if (!_this) {
  1571         return;
  1572     }
  1573 
  1574     /* Halt event processing before doing anything else */
  1575     SDL_StopEventLoop();
  1576 
  1577     /* Clean up the system video */
  1578     for (i = _this->num_displays; i--;) {
  1579         SDL_VideoDisplay *display = &_this->displays[i];
  1580         for (j = display->num_windows; j--;) {
  1581             SDL_DestroyWindow(display->windows[i].id);
  1582         }
  1583         if (display->windows) {
  1584             SDL_free(display->windows);
  1585             display->windows = NULL;
  1586         }
  1587     }
  1588     _this->VideoQuit(_this);
  1589     if (_this->displays) {
  1590         SDL_free(_this->displays);
  1591     }
  1592     _this->free(_this);
  1593     _this = NULL;
  1594 }
  1595 
  1596 /* Load the GL driver library */
  1597 int
  1598 SDL_GL_LoadLibrary(const char *path)
  1599 {
  1600     int retval;
  1601 
  1602     retval = -1;
  1603     if (_this == NULL) {
  1604         SDL_SetError("Video subsystem has not been initialized");
  1605     } else {
  1606         if (_this->GL_LoadLibrary) {
  1607             retval = _this->GL_LoadLibrary(_this, path);
  1608         } else {
  1609             SDL_SetError("No dynamic GL support in video driver");
  1610         }
  1611     }
  1612     return (retval);
  1613 }
  1614 
  1615 void *
  1616 SDL_GL_GetProcAddress(const char *proc)
  1617 {
  1618     void *func;
  1619 
  1620     func = NULL;
  1621     if (_this->GL_GetProcAddress) {
  1622         if (_this->gl_config.driver_loaded) {
  1623             func = _this->GL_GetProcAddress(_this, proc);
  1624         } else {
  1625             SDL_SetError("No GL driver has been loaded");
  1626         }
  1627     } else {
  1628         SDL_SetError("No dynamic GL support in video driver");
  1629     }
  1630     return func;
  1631 }
  1632 
  1633 /* Set the specified GL attribute for setting up a GL video mode */
  1634 int
  1635 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  1636 {
  1637     int retval;
  1638 
  1639     retval = 0;
  1640     switch (attr) {
  1641     case SDL_GL_RED_SIZE:
  1642         _this->gl_config.red_size = value;
  1643         break;
  1644     case SDL_GL_GREEN_SIZE:
  1645         _this->gl_config.green_size = value;
  1646         break;
  1647     case SDL_GL_BLUE_SIZE:
  1648         _this->gl_config.blue_size = value;
  1649         break;
  1650     case SDL_GL_ALPHA_SIZE:
  1651         _this->gl_config.alpha_size = value;
  1652         break;
  1653     case SDL_GL_DOUBLEBUFFER:
  1654         _this->gl_config.double_buffer = value;
  1655         break;
  1656     case SDL_GL_BUFFER_SIZE:
  1657         _this->gl_config.buffer_size = value;
  1658         break;
  1659     case SDL_GL_DEPTH_SIZE:
  1660         _this->gl_config.depth_size = value;
  1661         break;
  1662     case SDL_GL_STENCIL_SIZE:
  1663         _this->gl_config.stencil_size = value;
  1664         break;
  1665     case SDL_GL_ACCUM_RED_SIZE:
  1666         _this->gl_config.accum_red_size = value;
  1667         break;
  1668     case SDL_GL_ACCUM_GREEN_SIZE:
  1669         _this->gl_config.accum_green_size = value;
  1670         break;
  1671     case SDL_GL_ACCUM_BLUE_SIZE:
  1672         _this->gl_config.accum_blue_size = value;
  1673         break;
  1674     case SDL_GL_ACCUM_ALPHA_SIZE:
  1675         _this->gl_config.accum_alpha_size = value;
  1676         break;
  1677     case SDL_GL_STEREO:
  1678         _this->gl_config.stereo = value;
  1679         break;
  1680     case SDL_GL_MULTISAMPLEBUFFERS:
  1681         _this->gl_config.multisamplebuffers = value;
  1682         break;
  1683     case SDL_GL_MULTISAMPLESAMPLES:
  1684         _this->gl_config.multisamplesamples = value;
  1685         break;
  1686     case SDL_GL_ACCELERATED_VISUAL:
  1687         _this->gl_config.accelerated = value;
  1688         break;
  1689     case SDL_GL_SWAP_CONTROL:
  1690         _this->gl_config.swap_control = value;
  1691         break;
  1692     default:
  1693         SDL_SetError("Unknown OpenGL attribute");
  1694         retval = -1;
  1695         break;
  1696     }
  1697     return (retval);
  1698 }
  1699 
  1700 /* Retrieve an attribute value from the windowing system. */
  1701 int
  1702 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  1703 {
  1704     int retval = -1;
  1705 
  1706     if (_this->GL_GetAttribute) {
  1707         retval = _this->GL_GetAttribute(_this, attr, value);
  1708     } else {
  1709         *value = 0;
  1710         SDL_SetError("GL_GetAttribute not supported");
  1711     }
  1712     return retval;
  1713 }
  1714 
  1715 /* Perform a GL buffer swap on the current GL context */
  1716 void
  1717 SDL_GL_SwapBuffers(void)
  1718 {
  1719     // FIXME: Track the current window context - do we provide N contexts, and match them to M windows, or is there a one-to-one mapping?
  1720     _this->GL_SwapBuffers(_this);
  1721 }
  1722 
  1723 #if 0                           // FIXME
  1724 /* Utility function used by SDL_WM_SetIcon();
  1725  * flags & 1 for color key, flags & 2 for alpha channel. */
  1726 static void
  1727 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  1728 {
  1729     int x, y;
  1730     Uint32 colorkey;
  1731 #define SET_MASKBIT(icon, x, y, mask) \
  1732 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  1733 
  1734     colorkey = icon->format->colorkey;
  1735     switch (icon->format->BytesPerPixel) {
  1736     case 1:
  1737         {
  1738             Uint8 *pixels;
  1739             for (y = 0; y < icon->h; ++y) {
  1740                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  1741                 for (x = 0; x < icon->w; ++x) {
  1742                     if (*pixels++ == colorkey) {
  1743                         SET_MASKBIT(icon, x, y, mask);
  1744                     }
  1745                 }
  1746             }
  1747         }
  1748         break;
  1749 
  1750     case 2:
  1751         {
  1752             Uint16 *pixels;
  1753             for (y = 0; y < icon->h; ++y) {
  1754                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  1755                 for (x = 0; x < icon->w; ++x) {
  1756                     if ((flags & 1) && *pixels == colorkey) {
  1757                         SET_MASKBIT(icon, x, y, mask);
  1758                     } else if ((flags & 2)
  1759                                && (*pixels & icon->format->Amask) == 0) {
  1760                         SET_MASKBIT(icon, x, y, mask);
  1761                     }
  1762                     pixels++;
  1763                 }
  1764             }
  1765         }
  1766         break;
  1767 
  1768     case 4:
  1769         {
  1770             Uint32 *pixels;
  1771             for (y = 0; y < icon->h; ++y) {
  1772                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  1773                 for (x = 0; x < icon->w; ++x) {
  1774                     if ((flags & 1) && *pixels == colorkey) {
  1775                         SET_MASKBIT(icon, x, y, mask);
  1776                     } else if ((flags & 2)
  1777                                && (*pixels & icon->format->Amask) == 0) {
  1778                         SET_MASKBIT(icon, x, y, mask);
  1779                     }
  1780                     pixels++;
  1781                 }
  1782             }
  1783         }
  1784         break;
  1785     }
  1786 }
  1787 
  1788 /*
  1789  * Sets the window manager icon for the display window.
  1790  */
  1791 void
  1792 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  1793 {
  1794     if (icon && _this->SetIcon) {
  1795         /* Generate a mask if necessary, and create the icon! */
  1796         if (mask == NULL) {
  1797             int mask_len = icon->h * (icon->w + 7) / 8;
  1798             int flags = 0;
  1799             mask = (Uint8 *) SDL_malloc(mask_len);
  1800             if (mask == NULL) {
  1801                 return;
  1802             }
  1803             SDL_memset(mask, ~0, mask_len);
  1804             if (icon->flags & SDL_SRCCOLORKEY)
  1805                 flags |= 1;
  1806             if (icon->flags & SDL_SRCALPHA)
  1807                 flags |= 2;
  1808             if (flags) {
  1809                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1810             }
  1811             _this->SetIcon(_this, icon, mask);
  1812             SDL_free(mask);
  1813         } else {
  1814             _this->SetIcon(_this, icon, mask);
  1815         }
  1816     }
  1817 }
  1818 #endif
  1819 
  1820 SDL_bool
  1821 SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
  1822 {
  1823     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1824 
  1825     if (!window || !_this->GetWindowWMInfo) {
  1826         return SDL_FALSE;
  1827     }
  1828     return (_this->GetWindowWMInfo(_this, window, info));
  1829 }
  1830 
  1831 /* vi: set ts=4 sw=4 expandtab: */