src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 12 Jun 2006 09:10:06 +0000
branchSDL-1.3
changeset 1676 e136f3ffdc1b
parent 1672 8e754b82cecc
child 1677 5e4c5e095925
permissions -rw-r--r--
Adding software renderer implementation
     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 void
   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     SDL_AddVideoDisplay(&display);
   322 }
   323 
   324 void
   325 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   326 {
   327     SDL_VideoDisplay *displays;
   328 
   329     displays =
   330         SDL_realloc(_this->displays,
   331                     (_this->num_displays + 1) * sizeof(*displays));
   332     if (displays) {
   333         displays[_this->num_displays] = *display;
   334         _this->displays = displays;
   335         _this->num_displays++;
   336     }
   337 }
   338 
   339 int
   340 SDL_GetNumVideoDisplays(void)
   341 {
   342     if (!_this) {
   343         return 0;
   344     }
   345     return _this->num_displays;
   346 }
   347 
   348 int
   349 SDL_SelectVideoDisplay(int index)
   350 {
   351     if (!_this) {
   352         SDL_SetError("Video subsystem has not been initialized");
   353         return (-1);
   354     }
   355     if (index >= 0) {
   356         if (index >= _this->num_displays) {
   357             SDL_SetError("index must be in the range 0 - %d",
   358                          _this->num_displays - 1);
   359             return -1;
   360         }
   361         _this->current_display = index;
   362     }
   363     return _this->current_display;
   364 }
   365 
   366 void
   367 SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
   368 {
   369     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   370     SDL_DisplayMode *modes;
   371     int i, nmodes;
   372 
   373     /* Make sure we don't already have the mode in the list */
   374     modes = display->display_modes;
   375     nmodes = display->num_display_modes;
   376     for (i = 0; i < nmodes; ++i) {
   377         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   378             return;
   379         }
   380     }
   381 
   382     /* Go ahead and add the new mode */
   383     modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
   384     if (modes) {
   385         display->display_modes = modes;
   386         modes[nmodes] = *mode;
   387         display->num_display_modes++;
   388     }
   389 }
   390 
   391 int
   392 SDL_GetNumDisplayModes()
   393 {
   394     if (_this) {
   395         return SDL_CurrentDisplay.num_display_modes;
   396     }
   397     return 0;
   398 }
   399 
   400 const SDL_DisplayMode *
   401 SDL_GetDisplayMode(int index)
   402 {
   403     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   404         SDL_SetError("index must be in the range of 0 - %d",
   405                      SDL_GetNumDisplayModes() - 1);
   406         return NULL;
   407     }
   408     return &SDL_CurrentDisplay.display_modes[index];
   409 }
   410 
   411 const SDL_DisplayMode *
   412 SDL_GetDesktopDisplayMode(void)
   413 {
   414     if (_this) {
   415         return &SDL_CurrentDisplay.desktop_mode;
   416     }
   417     return NULL;
   418 }
   419 
   420 const SDL_DisplayMode *
   421 SDL_GetCurrentDisplayMode(void)
   422 {
   423     if (_this) {
   424         return &SDL_CurrentDisplay.current_mode;
   425     }
   426     return NULL;
   427 }
   428 
   429 SDL_DisplayMode *
   430 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   431                           SDL_DisplayMode * closest)
   432 {
   433     Uint32 target_format;
   434     int target_refresh_rate;
   435     int i;
   436     SDL_DisplayMode *current, *match;
   437 
   438     if (!_this || !mode || !closest) {
   439         return NULL;
   440     }
   441 
   442     /* Default to the desktop format */
   443     if (mode->format) {
   444         target_format = mode->format;
   445     } else {
   446         target_format = SDL_CurrentDisplay.desktop_mode.format;
   447     }
   448 
   449     /* Default to the desktop refresh rate */
   450     if (mode->refresh_rate) {
   451         target_refresh_rate = mode->refresh_rate;
   452     } else {
   453         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   454     }
   455 
   456     match = NULL;
   457     for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
   458         current = &SDL_CurrentDisplay.display_modes[i];
   459 
   460         if ((current->w && current->h) &&
   461             (current->w < mode->w || current->h < mode->h)) {
   462             /* Out of sorted modes large enough here */
   463             break;
   464         }
   465         if (!match || current->w < match->w || current->h < match->h) {
   466             match = current;
   467             continue;
   468         }
   469         if (current->format != match->format) {
   470             /* Sorted highest depth to lowest */
   471             if (current->format == target_format ||
   472                 (SDL_BITSPERPIXEL(current->format) >=
   473                  SDL_BITSPERPIXEL(target_format)
   474                  && SDL_PIXELTYPE(current->format) ==
   475                  SDL_PIXELTYPE(target_format))) {
   476                 match = current;
   477             }
   478             continue;
   479         }
   480         if (current->refresh_rate != match->refresh_rate) {
   481             /* Sorted highest refresh to lowest */
   482             if (current->refresh_rate >= target_refresh_rate) {
   483                 match = current;
   484             }
   485         }
   486     }
   487     if (match) {
   488         if (match->format) {
   489             closest->format = match->format;
   490         } else {
   491             closest->format = mode->format;
   492         }
   493         if (match->w && match->h) {
   494             closest->w = match->w;
   495             closest->h = match->h;
   496         } else {
   497             closest->w = mode->w;
   498             closest->h = mode->h;
   499         }
   500         if (match->refresh_rate) {
   501             closest->refresh_rate = match->refresh_rate;
   502         } else {
   503             closest->refresh_rate = mode->refresh_rate;
   504         }
   505         return closest;
   506     }
   507     return NULL;
   508 }
   509 
   510 int
   511 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   512 {
   513     SDL_VideoDisplay *display;
   514     SDL_DisplayMode display_mode;
   515     int i;
   516 
   517     if (!_this) {
   518         SDL_SetError("Video subsystem has not been initialized");
   519         return -1;
   520     }
   521 
   522     /* Make sure there's an actual display mode to set */
   523     if (!mode) {
   524         SDL_SetError("No mode passed to SDL_SetDisplayMode");
   525         return -1;
   526     }
   527     display = &SDL_CurrentDisplay;
   528     display_mode = *mode;
   529 
   530     /* Default to the current mode */
   531     if (!display_mode.format) {
   532         display_mode.format = display->current_mode.format;
   533     }
   534     if (!display_mode.w) {
   535         display_mode.w = display->current_mode.w;
   536     }
   537     if (!display_mode.h) {
   538         display_mode.h = display->current_mode.h;
   539     }
   540     if (!display_mode.refresh_rate) {
   541         display_mode.refresh_rate = display->current_mode.refresh_rate;
   542     }
   543 
   544     /* Get a good video mode, the closest one possible */
   545     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   546         SDL_SetError("No video mode large enough for %dx%d",
   547                      display_mode.w, display_mode.h);
   548         return -1;
   549     }
   550 
   551     /* See if there's anything left to do */
   552     if (SDL_memcmp
   553         (&display_mode, SDL_GetCurrentDisplayMode(),
   554          sizeof(display_mode)) == 0) {
   555         return 0;
   556     }
   557 
   558     return _this->SetDisplayMode(_this, &display_mode);
   559 }
   560 
   561 SDL_WindowID
   562 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   563 {
   564     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   565                                   SDL_WINDOW_BORDERLESS |
   566                                   SDL_WINDOW_SHOWN |
   567                                   SDL_WINDOW_OPENGL |
   568                                   SDL_WINDOW_RESIZABLE |
   569                                   SDL_WINDOW_MAXIMIZED |
   570                                   SDL_WINDOW_MINIMIZED |
   571                                   SDL_WINDOW_INPUT_GRABBED);
   572     SDL_Window window;
   573     int num_windows;
   574     SDL_Window *windows;
   575 
   576     if (!_this) {
   577         SDL_SetError("Video subsystem has not been initialized");
   578         return 0;
   579     }
   580 
   581     SDL_zero(window);
   582     window.id = _this->next_object_id++;
   583     window.title = title ? SDL_strdup(title) : NULL;
   584     window.x = x;
   585     window.y = y;
   586     window.w = w;
   587     window.h = h;
   588     window.flags = (flags & allowed_flags);
   589     window.display = &SDL_CurrentDisplay;
   590 
   591     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   592         if (window.title) {
   593             SDL_free(window.title);
   594         }
   595         return 0;
   596     }
   597 
   598     num_windows = SDL_CurrentDisplay.num_windows;
   599     windows =
   600         SDL_realloc(SDL_CurrentDisplay.windows,
   601                     (num_windows + 1) * sizeof(*windows));
   602     if (!windows) {
   603         if (_this->DestroyWindow) {
   604             _this->DestroyWindow(_this, &window);
   605         }
   606         if (window.title) {
   607             SDL_free(window.title);
   608         }
   609         return 0;
   610     }
   611     windows[num_windows] = window;
   612     SDL_CurrentDisplay.windows = windows;
   613     SDL_CurrentDisplay.num_windows++;
   614 
   615     return window.id;
   616 }
   617 
   618 SDL_WindowID
   619 SDL_CreateWindowFrom(void *data)
   620 {
   621     SDL_Window window;
   622     int num_windows;
   623     SDL_Window *windows;
   624 
   625     if (!_this) {
   626         SDL_SetError("Video subsystem has not been initialized");
   627         return (0);
   628     }
   629 
   630     SDL_zero(window);
   631     window.id = _this->next_object_id++;
   632     window.display = &SDL_CurrentDisplay;
   633 
   634     if (!_this->CreateWindowFrom ||
   635         _this->CreateWindowFrom(_this, &window, data) < 0) {
   636         return 0;
   637     }
   638 
   639     num_windows = SDL_CurrentDisplay.num_windows;
   640     windows =
   641         SDL_realloc(SDL_CurrentDisplay.windows,
   642                     (num_windows + 1) * sizeof(*windows));
   643     if (!windows) {
   644         if (_this->DestroyWindow) {
   645             _this->DestroyWindow(_this, &window);
   646         }
   647         if (window.title) {
   648             SDL_free(window.title);
   649         }
   650         return 0;
   651     }
   652     windows[num_windows] = window;
   653     SDL_CurrentDisplay.windows = windows;
   654     SDL_CurrentDisplay.num_windows++;
   655 
   656     return window.id;
   657 }
   658 
   659 static __inline__ SDL_Window *
   660 SDL_GetWindowFromID(SDL_WindowID windowID)
   661 {
   662     int i, j;
   663 
   664     if (!_this) {
   665         return NULL;
   666     }
   667 
   668     for (i = 0; i < _this->num_displays; ++i) {
   669         SDL_VideoDisplay *display = &_this->displays[i];
   670         for (j = 0; j < display->num_windows; ++j) {
   671             SDL_Window *window = &display->windows[j];
   672             if (window->id == windowID) {
   673                 return window;
   674             }
   675         }
   676     }
   677     return NULL;
   678 }
   679 
   680 Uint32
   681 SDL_GetWindowFlags(SDL_WindowID windowID)
   682 {
   683     SDL_Window *window = SDL_GetWindowFromID(windowID);
   684 
   685     if (!window) {
   686         return 0;
   687     }
   688     return window->flags;
   689 }
   690 
   691 void
   692 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   693 {
   694     SDL_Window *window = SDL_GetWindowFromID(windowID);
   695 
   696     if (!window) {
   697         return;
   698     }
   699     if (window->title) {
   700         SDL_free(window->title);
   701     }
   702     window->title = SDL_strdup(title);
   703 
   704     if (_this->SetWindowTitle) {
   705         _this->SetWindowTitle(_this, window);
   706     }
   707 }
   708 
   709 const char *
   710 SDL_GetWindowTitle(SDL_WindowID windowID)
   711 {
   712     SDL_Window *window = SDL_GetWindowFromID(windowID);
   713 
   714     if (!window) {
   715         return NULL;
   716     }
   717     return window->title;
   718 }
   719 
   720 void
   721 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   722 {
   723     SDL_Window *window = SDL_GetWindowFromID(windowID);
   724 
   725     if (!window) {
   726         return;
   727     }
   728     window->userdata = userdata;
   729 }
   730 
   731 void *
   732 SDL_GetWindowData(SDL_WindowID windowID)
   733 {
   734     SDL_Window *window = SDL_GetWindowFromID(windowID);
   735 
   736     if (!window) {
   737         return NULL;
   738     }
   739     return window->userdata;
   740 }
   741 
   742 void
   743 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   744 {
   745     SDL_Window *window = SDL_GetWindowFromID(windowID);
   746 
   747     if (!window) {
   748         return;
   749     }
   750 
   751     window->x = x;
   752     window->y = y;
   753 
   754     if (_this->SetWindowPosition) {
   755         _this->SetWindowPosition(_this, window);
   756     }
   757 }
   758 
   759 void
   760 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   761 {
   762     SDL_Window *window = SDL_GetWindowFromID(windowID);
   763 
   764     if (!window) {
   765         return;
   766     }
   767     if (x) {
   768         *x = window->x;
   769     }
   770     if (y) {
   771         *y = window->y;
   772     }
   773 }
   774 
   775 void
   776 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   777 {
   778     SDL_Window *window = SDL_GetWindowFromID(windowID);
   779 
   780     if (!window) {
   781         return;
   782     }
   783 
   784     window->w = w;
   785     window->h = h;
   786 
   787     if (_this->SetWindowSize) {
   788         _this->SetWindowSize(_this, window);
   789     }
   790 }
   791 
   792 void
   793 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   794 {
   795     SDL_Window *window = SDL_GetWindowFromID(windowID);
   796 
   797     if (!window) {
   798         return;
   799     }
   800     if (w) {
   801         *w = window->w;
   802     }
   803     if (h) {
   804         *h = window->h;
   805     }
   806 }
   807 
   808 void
   809 SDL_ShowWindow(SDL_WindowID windowID)
   810 {
   811     SDL_Window *window = SDL_GetWindowFromID(windowID);
   812 
   813     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
   814         return;
   815     }
   816 
   817     window->flags |= SDL_WINDOW_SHOWN;
   818 
   819     if (_this->ShowWindow) {
   820         _this->ShowWindow(_this, window);
   821     }
   822 }
   823 
   824 void
   825 SDL_HideWindow(SDL_WindowID windowID)
   826 {
   827     SDL_Window *window = SDL_GetWindowFromID(windowID);
   828 
   829     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
   830         return;
   831     }
   832 
   833     window->flags &= ~SDL_WINDOW_SHOWN;
   834 
   835     if (_this->HideWindow) {
   836         _this->HideWindow(_this, window);
   837     }
   838 }
   839 
   840 void
   841 SDL_RaiseWindow(SDL_WindowID windowID)
   842 {
   843     SDL_Window *window = SDL_GetWindowFromID(windowID);
   844 
   845     if (!window) {
   846         return;
   847     }
   848 
   849     if (_this->RaiseWindow) {
   850         _this->RaiseWindow(_this, window);
   851     }
   852 }
   853 
   854 void
   855 SDL_MaximizeWindow(SDL_WindowID windowID)
   856 {
   857     SDL_Window *window = SDL_GetWindowFromID(windowID);
   858 
   859     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
   860         return;
   861     }
   862 
   863     window->flags |= SDL_WINDOW_MAXIMIZED;
   864 
   865     if (_this->MaximizeWindow) {
   866         _this->MaximizeWindow(_this, window);
   867     }
   868 }
   869 
   870 void
   871 SDL_MinimizeWindow(SDL_WindowID windowID)
   872 {
   873     SDL_Window *window = SDL_GetWindowFromID(windowID);
   874 
   875     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
   876         return;
   877     }
   878 
   879     window->flags |= SDL_WINDOW_MINIMIZED;
   880 
   881     if (_this->MinimizeWindow) {
   882         _this->MinimizeWindow(_this, window);
   883     }
   884 }
   885 
   886 void
   887 SDL_RestoreWindow(SDL_WindowID windowID)
   888 {
   889     SDL_Window *window = SDL_GetWindowFromID(windowID);
   890 
   891     if (!window
   892         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
   893         return;
   894     }
   895 
   896     window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
   897 
   898     if (_this->RestoreWindow) {
   899         _this->RestoreWindow(_this, window);
   900     }
   901 }
   902 
   903 void
   904 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
   905 {
   906     SDL_Window *window = SDL_GetWindowFromID(windowID);
   907 
   908     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
   909         return;
   910     }
   911 
   912     if (mode) {
   913         window->flags |= SDL_WINDOW_INPUT_GRABBED;
   914     } else {
   915         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   916     }
   917 
   918     if (_this->SetWindowGrab) {
   919         _this->SetWindowGrab(_this, window);
   920     }
   921 }
   922 
   923 int
   924 SDL_GetWindowGrab(SDL_WindowID windowID)
   925 {
   926     SDL_Window *window = SDL_GetWindowFromID(windowID);
   927 
   928     if (!window) {
   929         return 0;
   930     }
   931 
   932     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
   933 }
   934 
   935 void
   936 SDL_DestroyWindow(SDL_WindowID windowID)
   937 {
   938     int i, j;
   939 
   940     if (!_this) {
   941         return;
   942     }
   943 
   944     for (i = 0; i < _this->num_displays; ++i) {
   945         SDL_VideoDisplay *display = &_this->displays[i];
   946         for (j = 0; j < display->num_windows; ++j) {
   947             SDL_Window *window = &display->windows[j];
   948             if (window->id != windowID) {
   949                 continue;
   950             }
   951             if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   952                 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   953                 _this->SetWindowGrab(_this, window);
   954             }
   955             if (window->renderer) {
   956                 SDL_DestroyRenderer(window->id);
   957             }
   958             if (_this->DestroyWindow) {
   959                 _this->DestroyWindow(_this, window);
   960             }
   961             if (window->title) {
   962                 SDL_free(window->title);
   963             }
   964             if (window->gamma) {
   965                 SDL_free(window->gamma);
   966             }
   967             if (j != display->num_windows - 1) {
   968                 SDL_memcpy(&display->windows[i],
   969                            &display->windows[i + 1],
   970                            (display->num_windows - i - 1) * sizeof(*window));
   971             }
   972             --display->num_windows;
   973             return;
   974         }
   975     }
   976 }
   977 
   978 void
   979 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
   980 {
   981     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   982     SDL_RenderDriver *render_drivers;
   983 
   984     render_drivers =
   985         SDL_realloc(display->render_drivers,
   986                     (display->num_render_drivers +
   987                      1) * sizeof(*render_drivers));
   988     if (render_drivers) {
   989         render_drivers[display->num_render_drivers] = *driver;
   990         display->render_drivers = render_drivers;
   991         display->num_render_drivers++;
   992     }
   993 }
   994 
   995 int
   996 SDL_GetNumRenderers(void)
   997 {
   998     if (_this) {
   999         return SDL_CurrentDisplay.num_render_drivers;
  1000     }
  1001     return 0;
  1002 }
  1003 
  1004 int
  1005 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1006 {
  1007     if (index < 0 || index >= SDL_GetNumRenderers()) {
  1008         SDL_SetError("index must be in the range of 0 - %d",
  1009                      SDL_GetNumRenderers() - 1);
  1010         return -1;
  1011     }
  1012     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1013     return 0;
  1014 }
  1015 
  1016 int
  1017 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1018 {
  1019     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1020 
  1021     if (!window) {
  1022         return 0;
  1023     }
  1024 
  1025     if (index < 0) {
  1026         int n = SDL_GetNumRenderers();
  1027         for (index = 0; index < n; ++index) {
  1028             if ((SDL_CurrentDisplay.render_drivers[index].info.
  1029                  flags & flags) == flags) {
  1030                 break;
  1031             }
  1032         }
  1033         if (index == n) {
  1034             SDL_SetError("Couldn't find matching render driver");
  1035             return -1;
  1036         }
  1037     }
  1038 
  1039     if (index >= SDL_GetNumRenderers()) {
  1040         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1041                      SDL_GetNumRenderers() - 1);
  1042         return -1;
  1043     }
  1044 
  1045     /* Free any existing renderer */
  1046     SDL_DestroyRenderer(windowID);
  1047 
  1048     /* Create a new renderer instance */
  1049     window->renderer =
  1050         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1051                                                                 flags);
  1052     if (!window->renderer) {
  1053         return -1;
  1054     }
  1055     SDL_CurrentDisplay.current_renderer = window->renderer;
  1056 
  1057     return 0;
  1058 }
  1059 
  1060 int
  1061 SDL_SelectRenderer(SDL_WindowID windowID)
  1062 {
  1063     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1064 
  1065     if (!window || !window->renderer) {
  1066         return -1;
  1067     }
  1068     SDL_CurrentDisplay.current_renderer = window->renderer;
  1069     return 0;
  1070 }
  1071 
  1072 SDL_TextureID
  1073 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1074 {
  1075     SDL_Renderer *renderer;
  1076     SDL_Texture *texture;
  1077 
  1078     if (!_this) {
  1079         return 0;
  1080     }
  1081 
  1082     renderer = SDL_CurrentDisplay.current_renderer;
  1083     if (!renderer || !renderer->CreateTexture) {
  1084         return 0;
  1085     }
  1086 
  1087     texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
  1088     if (!texture) {
  1089         SDL_OutOfMemory();
  1090         return 0;
  1091     }
  1092 
  1093     SDL_zerop(texture);
  1094     texture->id = _this->next_object_id++;
  1095     texture->format = format;
  1096     texture->access = access;
  1097     texture->w = w;
  1098     texture->h = h;
  1099     texture->renderer = renderer;
  1100 
  1101     if (renderer->CreateTexture(renderer, texture) < 0) {
  1102         SDL_free(texture);
  1103         return 0;
  1104     }
  1105 }
  1106 
  1107 SDL_TextureID
  1108 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1109 {
  1110     SDL_TextureID textureID;
  1111     Uint32 surface_flags = surface->flags;
  1112     SDL_PixelFormat *fmt = surface->format;
  1113     Uint32 colorkey;
  1114     Uint8 alpha;
  1115     SDL_Rect bounds;
  1116     SDL_Surface dst;
  1117     int bpp;
  1118     Uint32 Rmask, Gmask, Bmask, Amask;
  1119 
  1120     if (!surface) {
  1121         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1122         return 0;
  1123     }
  1124 
  1125     if (format) {
  1126         if (!SDL_PixelFormatEnumToMasks
  1127             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1128             SDL_SetError("Unknown pixel format");
  1129             return 0;
  1130         }
  1131     } else {
  1132         bpp = fmt->BitsPerPixel;
  1133         Rmask = fmt->Rmask;
  1134         Gmask = fmt->Gmask;
  1135         Bmask = fmt->Bmask;
  1136         Amask = fmt->Amask;
  1137         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1138         if (!format) {
  1139             SDL_SetError("Unknown pixel format");
  1140             return 0;
  1141         }
  1142     }
  1143 
  1144     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1145     if (!textureID) {
  1146         return 0;
  1147     }
  1148 
  1149     /* Set up a destination surface for the texture update */
  1150     SDL_zero(dst);
  1151     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1152     if (!dst.format) {
  1153         SDL_DestroyTexture(textureID);
  1154         return 0;
  1155     }
  1156     dst.w = surface->w;
  1157     dst.h = surface->h;
  1158     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1159         dst.flags |= SDL_PREALLOC;
  1160     } else {
  1161         dst.pitch = SDL_CalculatePitch(&dst);
  1162         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1163         if (!dst.pixels) {
  1164             SDL_DestroyTexture(textureID);
  1165             SDL_FreeFormat(dst.format);
  1166             SDL_OutOfMemory();
  1167             return 0;
  1168         }
  1169     }
  1170 
  1171     /* Copy the palette if any */
  1172     if (fmt->palette && dst.format->palette) {
  1173         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1174                               fmt->palette->ncolors);
  1175 
  1176         SDL_memcpy(dst.format->palette->colors,
  1177                    fmt->palette->colors,
  1178                    fmt->palette->ncolors * sizeof(SDL_Color));
  1179         dst.format->palette->ncolors = fmt->palette->ncolors;
  1180     }
  1181 
  1182     /* Make the texture transparent if the surface has colorkey */
  1183     if (surface_flags & SDL_SRCCOLORKEY) {
  1184         int row;
  1185         int length = dst.w * dst.format->BytesPerPixel;
  1186         Uint8 *p = (Uint8 *) dst.pixels;
  1187         for (row = 0; row < dst.h; ++row) {
  1188             SDL_memset(p, 0, length);
  1189             p += dst.pitch;
  1190         }
  1191     }
  1192 
  1193     /* Copy over the alpha channel */
  1194     if (surface_flags & SDL_SRCALPHA) {
  1195         if (fmt->Amask) {
  1196             surface->flags &= ~SDL_SRCALPHA;
  1197         } else {
  1198             /* FIXME: Need to make sure the texture has an alpha channel
  1199              *        and copy 'alpha' into the texture alpha channel.
  1200              */
  1201             alpha = surface->format->alpha;
  1202             SDL_SetAlpha(surface, 0, 0);
  1203         }
  1204     }
  1205 
  1206     /* Copy over the image data */
  1207     bounds.x = 0;
  1208     bounds.y = 0;
  1209     bounds.w = surface->w;
  1210     bounds.h = surface->h;
  1211     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1212 
  1213     /* Clean up the original surface */
  1214     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1215         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
  1216         SDL_SetColorKey(surface, cflags, colorkey);
  1217     }
  1218     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1219         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1220         if (fmt->Amask) {
  1221             surface->flags |= SDL_SRCALPHA;
  1222         } else {
  1223             SDL_SetAlpha(surface, aflags, alpha);
  1224         }
  1225     }
  1226 
  1227     /* Update the texture */
  1228     if (dst.flags & SDL_PREALLOC) {
  1229         SDL_UnlockTexture(textureID);
  1230     } else {
  1231         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1232         SDL_free(dst.pixels);
  1233     }
  1234     SDL_FreeFormat(dst.format);
  1235 
  1236     return textureID;
  1237 }
  1238 
  1239 static __inline__ SDL_Texture *
  1240 SDL_GetTextureFromID(SDL_TextureID textureID)
  1241 {
  1242     int hash;
  1243     SDL_Texture *texture;
  1244 
  1245     if (!_this) {
  1246         return NULL;
  1247     }
  1248 
  1249     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1250     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1251          texture = texture->next) {
  1252         if (texture->id == textureID) {
  1253             return texture;
  1254         }
  1255     }
  1256     return NULL;
  1257 }
  1258 
  1259 int
  1260 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1261                  int *w, int *h)
  1262 {
  1263     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1264 
  1265     if (!texture) {
  1266         return -1;
  1267     }
  1268 
  1269     if (format) {
  1270         *format = texture->format;
  1271     }
  1272     if (access) {
  1273         *access = texture->access;
  1274     }
  1275     if (w) {
  1276         *w = texture->w;
  1277     }
  1278     if (h) {
  1279         *h = texture->h;
  1280     }
  1281     return 0;
  1282 }
  1283 
  1284 int
  1285 SDL_UpdateTexture(SDL_TextureID textureID, SDL_Rect * rect,
  1286                   const void *pixels, int pitch)
  1287 {
  1288     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1289     SDL_Renderer *renderer;
  1290 
  1291     if (!texture) {
  1292         return -1;
  1293     }
  1294 
  1295     renderer = texture->renderer;
  1296     if (!renderer->UpdateTexture) {
  1297         return -1;
  1298     }
  1299     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1300 }
  1301 
  1302 int
  1303 SDL_LockTexture(SDL_TextureID textureID, SDL_Rect * rect, int markDirty,
  1304                 void **pixels, int *pitch)
  1305 {
  1306     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1307     SDL_Renderer *renderer;
  1308 
  1309     if (!texture) {
  1310         return -1;
  1311     }
  1312 
  1313     renderer = texture->renderer;
  1314     if (!renderer->LockTexture) {
  1315         return -1;
  1316     }
  1317     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1318                                  pitch);
  1319 }
  1320 
  1321 void
  1322 SDL_UnlockTexture(SDL_TextureID textureID)
  1323 {
  1324     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1325     SDL_Renderer *renderer;
  1326 
  1327     if (!texture) {
  1328         return;
  1329     }
  1330 
  1331     renderer = texture->renderer;
  1332     if (!renderer->UnlockTexture) {
  1333         return;
  1334     }
  1335     return renderer->UnlockTexture(renderer, texture);
  1336 }
  1337 
  1338 void
  1339 SDL_DirtyTexture(SDL_TextureID textureID, int numrects, SDL_Rect * rects)
  1340 {
  1341     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1342     SDL_Renderer *renderer;
  1343 
  1344     if (!texture) {
  1345         return;
  1346     }
  1347 
  1348     renderer = texture->renderer;
  1349     if (!renderer->DirtyTexture) {
  1350         return;
  1351     }
  1352     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1353 }
  1354 
  1355 void
  1356 SDL_SelectRenderTexture(SDL_TextureID textureID)
  1357 {
  1358     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1359     SDL_Renderer *renderer;
  1360 
  1361     if (!texture || texture->access != SDL_TextureAccess_Render) {
  1362         return;
  1363     }
  1364     renderer = texture->renderer;
  1365     if (!renderer->SelectRenderTexture) {
  1366         return;
  1367     }
  1368     renderer->SelectRenderTexture(renderer, texture);
  1369 }
  1370 
  1371 int
  1372 SDL_RenderFill(SDL_Rect * rect, Uint32 color)
  1373 {
  1374     SDL_Renderer *renderer;
  1375 
  1376     if (!_this) {
  1377         return -1;
  1378     }
  1379 
  1380     renderer = SDL_CurrentDisplay.current_renderer;
  1381     if (!renderer || !renderer->RenderFill) {
  1382         return -1;
  1383     }
  1384 
  1385     renderer->RenderFill(renderer, rect, color);
  1386 }
  1387 
  1388 int
  1389 SDL_RenderCopy(SDL_TextureID textureID, SDL_Rect * srcrect,
  1390                SDL_Rect * dstrect, int blendMode, int scaleMode)
  1391 {
  1392     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1393     SDL_Renderer *renderer;
  1394 
  1395     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1396         return;
  1397     }
  1398 
  1399     renderer = SDL_CurrentDisplay.current_renderer;
  1400     if (!renderer || !renderer->RenderCopy) {
  1401         return -1;
  1402     }
  1403 
  1404     return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
  1405                                 blendMode, scaleMode);
  1406 }
  1407 
  1408 int
  1409 SDL_RenderReadPixels(SDL_Rect * rect, void *pixels, int pitch)
  1410 {
  1411     SDL_Renderer *renderer;
  1412 
  1413     if (!_this) {
  1414         return -1;
  1415     }
  1416 
  1417     renderer = SDL_CurrentDisplay.current_renderer;
  1418     if (!renderer || !renderer->RenderReadPixels) {
  1419         return -1;
  1420     }
  1421 
  1422     return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
  1423 }
  1424 
  1425 int
  1426 SDL_RenderWritePixels(SDL_Rect * rect, const void *pixels, int pitch)
  1427 {
  1428     SDL_Renderer *renderer;
  1429 
  1430     if (!_this) {
  1431         return -1;
  1432     }
  1433 
  1434     renderer = SDL_CurrentDisplay.current_renderer;
  1435     if (!renderer || !renderer->RenderWritePixels) {
  1436         return -1;
  1437     }
  1438 
  1439     return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
  1440 }
  1441 
  1442 void
  1443 SDL_RenderPresent(void)
  1444 {
  1445     SDL_Renderer *renderer;
  1446 
  1447     if (!_this) {
  1448         return;
  1449     }
  1450 
  1451     renderer = SDL_CurrentDisplay.current_renderer;
  1452     if (!renderer || !renderer->RenderPresent) {
  1453         return;
  1454     }
  1455 
  1456     renderer->RenderPresent(renderer);
  1457 }
  1458 
  1459 void
  1460 SDL_DestroyTexture(SDL_TextureID textureID)
  1461 {
  1462     int hash;
  1463     SDL_Texture *prev, *texture;
  1464     SDL_Renderer *renderer;
  1465 
  1466     if (!_this) {
  1467         return;
  1468     }
  1469 
  1470     /* Look up the texture in the hash table */
  1471     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1472     prev = NULL;
  1473     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1474          prev = texture, texture = texture->next) {
  1475         if (texture->id == textureID) {
  1476             break;
  1477         }
  1478     }
  1479     if (!texture) {
  1480         return;
  1481     }
  1482 
  1483     /* Unlink the texture from the list */
  1484     if (prev) {
  1485         prev->next = texture->next;
  1486     } else {
  1487         SDL_CurrentDisplay.textures[hash] = texture->next;
  1488     }
  1489 
  1490     /* Free the texture */
  1491     renderer = texture->renderer;
  1492     renderer->DestroyTexture(renderer, texture);
  1493     SDL_free(texture);
  1494 }
  1495 
  1496 void
  1497 SDL_DestroyRenderer(SDL_WindowID windowID)
  1498 {
  1499     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1500     SDL_Renderer *renderer;
  1501     int i;
  1502 
  1503     if (!window) {
  1504         return;
  1505     }
  1506 
  1507     renderer = window->renderer;
  1508     if (!renderer) {
  1509         return;
  1510     }
  1511 
  1512     /* Free existing textures for this renderer */
  1513     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1514         SDL_Texture *texture;
  1515         SDL_Texture *prev = NULL;
  1516         SDL_Texture *next;
  1517         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1518              texture = next) {
  1519             next = texture->next;
  1520             if (texture->renderer == renderer) {
  1521                 if (prev) {
  1522                     prev->next = next;
  1523                 } else {
  1524                     SDL_CurrentDisplay.textures[i] = next;
  1525                 }
  1526                 renderer->DestroyTexture(renderer, texture);
  1527                 SDL_free(texture);
  1528             } else {
  1529                 prev = texture;
  1530             }
  1531         }
  1532     }
  1533 
  1534     /* Free the renderer instance */
  1535     renderer->DestroyRenderer(renderer);
  1536 
  1537     /* Clear references */
  1538     window->renderer = NULL;
  1539     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1540         SDL_CurrentDisplay.current_renderer = NULL;
  1541     }
  1542 }
  1543 
  1544 void
  1545 SDL_VideoQuit(void)
  1546 {
  1547     int i, j;
  1548 
  1549     if (!_this) {
  1550         return;
  1551     }
  1552 
  1553     /* Halt event processing before doing anything else */
  1554     SDL_StopEventLoop();
  1555 
  1556     /* Clean up allocated window manager items */
  1557     SDL_CursorQuit();
  1558 
  1559     /* Clean up the system video */
  1560     for (i = _this->num_displays; i--;) {
  1561         SDL_VideoDisplay *display = &_this->displays[i];
  1562         for (j = display->num_windows; j--;) {
  1563             SDL_DestroyWindow(display->windows[i].id);
  1564         }
  1565         if (display->windows) {
  1566             SDL_free(display->windows);
  1567             display->windows = NULL;
  1568         }
  1569     }
  1570     _this->VideoQuit(_this);
  1571     if (_this->displays) {
  1572         SDL_free(_this->displays);
  1573     }
  1574     _this->free(_this);
  1575     _this = NULL;
  1576 }
  1577 
  1578 /* Load the GL driver library */
  1579 int
  1580 SDL_GL_LoadLibrary(const char *path)
  1581 {
  1582     int retval;
  1583 
  1584     retval = -1;
  1585     if (_this == NULL) {
  1586         SDL_SetError("Video subsystem has not been initialized");
  1587     } else {
  1588         if (_this->GL_LoadLibrary) {
  1589             retval = _this->GL_LoadLibrary(_this, path);
  1590         } else {
  1591             SDL_SetError("No dynamic GL support in video driver");
  1592         }
  1593     }
  1594     return (retval);
  1595 }
  1596 
  1597 void *
  1598 SDL_GL_GetProcAddress(const char *proc)
  1599 {
  1600     void *func;
  1601 
  1602     func = NULL;
  1603     if (_this->GL_GetProcAddress) {
  1604         if (_this->gl_config.driver_loaded) {
  1605             func = _this->GL_GetProcAddress(_this, proc);
  1606         } else {
  1607             SDL_SetError("No GL driver has been loaded");
  1608         }
  1609     } else {
  1610         SDL_SetError("No dynamic GL support in video driver");
  1611     }
  1612     return func;
  1613 }
  1614 
  1615 /* Set the specified GL attribute for setting up a GL video mode */
  1616 int
  1617 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  1618 {
  1619     int retval;
  1620 
  1621     retval = 0;
  1622     switch (attr) {
  1623     case SDL_GL_RED_SIZE:
  1624         _this->gl_config.red_size = value;
  1625         break;
  1626     case SDL_GL_GREEN_SIZE:
  1627         _this->gl_config.green_size = value;
  1628         break;
  1629     case SDL_GL_BLUE_SIZE:
  1630         _this->gl_config.blue_size = value;
  1631         break;
  1632     case SDL_GL_ALPHA_SIZE:
  1633         _this->gl_config.alpha_size = value;
  1634         break;
  1635     case SDL_GL_DOUBLEBUFFER:
  1636         _this->gl_config.double_buffer = value;
  1637         break;
  1638     case SDL_GL_BUFFER_SIZE:
  1639         _this->gl_config.buffer_size = value;
  1640         break;
  1641     case SDL_GL_DEPTH_SIZE:
  1642         _this->gl_config.depth_size = value;
  1643         break;
  1644     case SDL_GL_STENCIL_SIZE:
  1645         _this->gl_config.stencil_size = value;
  1646         break;
  1647     case SDL_GL_ACCUM_RED_SIZE:
  1648         _this->gl_config.accum_red_size = value;
  1649         break;
  1650     case SDL_GL_ACCUM_GREEN_SIZE:
  1651         _this->gl_config.accum_green_size = value;
  1652         break;
  1653     case SDL_GL_ACCUM_BLUE_SIZE:
  1654         _this->gl_config.accum_blue_size = value;
  1655         break;
  1656     case SDL_GL_ACCUM_ALPHA_SIZE:
  1657         _this->gl_config.accum_alpha_size = value;
  1658         break;
  1659     case SDL_GL_STEREO:
  1660         _this->gl_config.stereo = value;
  1661         break;
  1662     case SDL_GL_MULTISAMPLEBUFFERS:
  1663         _this->gl_config.multisamplebuffers = value;
  1664         break;
  1665     case SDL_GL_MULTISAMPLESAMPLES:
  1666         _this->gl_config.multisamplesamples = value;
  1667         break;
  1668     case SDL_GL_ACCELERATED_VISUAL:
  1669         _this->gl_config.accelerated = value;
  1670         break;
  1671     case SDL_GL_SWAP_CONTROL:
  1672         _this->gl_config.swap_control = value;
  1673         break;
  1674     default:
  1675         SDL_SetError("Unknown OpenGL attribute");
  1676         retval = -1;
  1677         break;
  1678     }
  1679     return (retval);
  1680 }
  1681 
  1682 /* Retrieve an attribute value from the windowing system. */
  1683 int
  1684 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  1685 {
  1686     int retval = -1;
  1687 
  1688     if (_this->GL_GetAttribute) {
  1689         retval = _this->GL_GetAttribute(_this, attr, value);
  1690     } else {
  1691         *value = 0;
  1692         SDL_SetError("GL_GetAttribute not supported");
  1693     }
  1694     return retval;
  1695 }
  1696 
  1697 /* Perform a GL buffer swap on the current GL context */
  1698 void
  1699 SDL_GL_SwapBuffers(void)
  1700 {
  1701     // 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?
  1702     _this->GL_SwapBuffers(_this);
  1703 }
  1704 
  1705 #if 0                           // FIXME
  1706 /* Utility function used by SDL_WM_SetIcon();
  1707  * flags & 1 for color key, flags & 2 for alpha channel. */
  1708 static void
  1709 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  1710 {
  1711     int x, y;
  1712     Uint32 colorkey;
  1713 #define SET_MASKBIT(icon, x, y, mask) \
  1714 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  1715 
  1716     colorkey = icon->format->colorkey;
  1717     switch (icon->format->BytesPerPixel) {
  1718     case 1:
  1719         {
  1720             Uint8 *pixels;
  1721             for (y = 0; y < icon->h; ++y) {
  1722                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  1723                 for (x = 0; x < icon->w; ++x) {
  1724                     if (*pixels++ == colorkey) {
  1725                         SET_MASKBIT(icon, x, y, mask);
  1726                     }
  1727                 }
  1728             }
  1729         }
  1730         break;
  1731 
  1732     case 2:
  1733         {
  1734             Uint16 *pixels;
  1735             for (y = 0; y < icon->h; ++y) {
  1736                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  1737                 for (x = 0; x < icon->w; ++x) {
  1738                     if ((flags & 1) && *pixels == colorkey) {
  1739                         SET_MASKBIT(icon, x, y, mask);
  1740                     } else if ((flags & 2)
  1741                                && (*pixels & icon->format->Amask) == 0) {
  1742                         SET_MASKBIT(icon, x, y, mask);
  1743                     }
  1744                     pixels++;
  1745                 }
  1746             }
  1747         }
  1748         break;
  1749 
  1750     case 4:
  1751         {
  1752             Uint32 *pixels;
  1753             for (y = 0; y < icon->h; ++y) {
  1754                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  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 }
  1769 
  1770 /*
  1771  * Sets the window manager icon for the display window.
  1772  */
  1773 void
  1774 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  1775 {
  1776     if (icon && _this->SetIcon) {
  1777         /* Generate a mask if necessary, and create the icon! */
  1778         if (mask == NULL) {
  1779             int mask_len = icon->h * (icon->w + 7) / 8;
  1780             int flags = 0;
  1781             mask = (Uint8 *) SDL_malloc(mask_len);
  1782             if (mask == NULL) {
  1783                 return;
  1784             }
  1785             SDL_memset(mask, ~0, mask_len);
  1786             if (icon->flags & SDL_SRCCOLORKEY)
  1787                 flags |= 1;
  1788             if (icon->flags & SDL_SRCALPHA)
  1789                 flags |= 2;
  1790             if (flags) {
  1791                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1792             }
  1793             _this->SetIcon(_this, icon, mask);
  1794             SDL_free(mask);
  1795         } else {
  1796             _this->SetIcon(_this, icon, mask);
  1797         }
  1798     }
  1799 }
  1800 #endif
  1801 
  1802 SDL_bool
  1803 SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
  1804 {
  1805     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1806 
  1807     if (!window || !_this->GetWindowWMInfo) {
  1808         return SDL_FALSE;
  1809     }
  1810     return (_this->GetWindowWMInfo(_this, window, info));
  1811 }
  1812 
  1813 /* vi: set ts=4 sw=4 expandtab: */