src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 15 Jun 2006 07:07:07 +0000
branchSDL-1.3
changeset 1681 80a5e6a4e1e2
parent 1680 9488fca10677
child 1682 7ae8018b2e5d
permissions -rw-r--r--
Working on paletted display and texture support (two different issues)
     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     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   563         display->palette.ncolors =
   564             (1 << SDL_BITSPERPIXEL(display_mode.format));
   565         display->palette.colors =
   566             (SDL_Color *) SDL_realloc(display->palette.colors,
   567                                       display->palette.ncolors *
   568                                       sizeof(*display->palette.colors));
   569         if (!display->palette.colors) {
   570             SDL_OutOfMemory();
   571             return -1;
   572         }
   573         SDL_memset(display->palette.colors, 0xff,
   574                    display->palette.ncolors *
   575                    sizeof(*display->palette.colors));
   576     } else {
   577         if (display->palette.colors) {
   578             SDL_free(display->palette.colors);
   579         }
   580         display->palette.colors = NULL;
   581         display->palette.ncolors = 0;
   582     }
   583 
   584     return _this->SetDisplayMode(_this, &display_mode);
   585 }
   586 
   587 int
   588 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   589 {
   590     SDL_Palette *palette;
   591 
   592     if (!_this) {
   593         SDL_SetError("Video subsystem has not been initialized");
   594         return -1;
   595     }
   596 
   597     palette = &SDL_CurrentDisplay.palette;
   598     if (!palette->ncolors) {
   599         SDL_SetError("Display mode does not have a palette");
   600         return -1;
   601     }
   602 
   603     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   604         SDL_SetError("Palette indices are out of range");
   605         return -1;
   606     }
   607 
   608     SDL_memcpy(&palette->colors[firstcolor], colors,
   609                ncolors * sizeof(*colors));
   610 
   611     if (_this->SetDisplayPalette) {
   612         return _this->SetDisplayPalette(_this, palette);
   613     } else {
   614         return 0;
   615     }
   616 }
   617 
   618 int
   619 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   620 {
   621     SDL_Palette *palette;
   622 
   623     if (!_this) {
   624         SDL_SetError("Video subsystem has not been initialized");
   625         return -1;
   626     }
   627 
   628     palette = &SDL_CurrentDisplay.palette;
   629     if (!palette->ncolors) {
   630         SDL_SetError("Display mode does not have a palette");
   631         return -1;
   632     }
   633 
   634     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   635         SDL_SetError("Palette indices are out of range");
   636         return -1;
   637     }
   638 
   639     SDL_memcpy(colors, &palette->colors[firstcolor],
   640                ncolors * sizeof(*colors));
   641     return 0;
   642 }
   643 
   644 SDL_WindowID
   645 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   646 {
   647     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   648                                   SDL_WINDOW_BORDERLESS |
   649                                   SDL_WINDOW_SHOWN |
   650                                   SDL_WINDOW_OPENGL |
   651                                   SDL_WINDOW_RESIZABLE |
   652                                   SDL_WINDOW_MAXIMIZED |
   653                                   SDL_WINDOW_MINIMIZED |
   654                                   SDL_WINDOW_INPUT_GRABBED);
   655     SDL_Window window;
   656     int num_windows;
   657     SDL_Window *windows;
   658 
   659     if (!_this) {
   660         SDL_SetError("Video subsystem has not been initialized");
   661         return 0;
   662     }
   663 
   664     SDL_zero(window);
   665     window.id = _this->next_object_id++;
   666     window.title = title ? SDL_strdup(title) : NULL;
   667     window.x = x;
   668     window.y = y;
   669     window.w = w;
   670     window.h = h;
   671     window.flags = (flags & allowed_flags);
   672     window.display = &SDL_CurrentDisplay;
   673 
   674     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   675         if (window.title) {
   676             SDL_free(window.title);
   677         }
   678         return 0;
   679     }
   680 
   681     num_windows = SDL_CurrentDisplay.num_windows;
   682     windows =
   683         SDL_realloc(SDL_CurrentDisplay.windows,
   684                     (num_windows + 1) * sizeof(*windows));
   685     if (!windows) {
   686         if (_this->DestroyWindow) {
   687             _this->DestroyWindow(_this, &window);
   688         }
   689         if (window.title) {
   690             SDL_free(window.title);
   691         }
   692         return 0;
   693     }
   694     windows[num_windows] = window;
   695     SDL_CurrentDisplay.windows = windows;
   696     SDL_CurrentDisplay.num_windows++;
   697 
   698     return window.id;
   699 }
   700 
   701 SDL_WindowID
   702 SDL_CreateWindowFrom(const void *data)
   703 {
   704     SDL_Window window;
   705     int num_windows;
   706     SDL_Window *windows;
   707 
   708     if (!_this) {
   709         SDL_SetError("Video subsystem has not been initialized");
   710         return (0);
   711     }
   712 
   713     SDL_zero(window);
   714     window.id = _this->next_object_id++;
   715     window.display = &SDL_CurrentDisplay;
   716 
   717     if (!_this->CreateWindowFrom ||
   718         _this->CreateWindowFrom(_this, &window, data) < 0) {
   719         return 0;
   720     }
   721 
   722     num_windows = SDL_CurrentDisplay.num_windows;
   723     windows =
   724         SDL_realloc(SDL_CurrentDisplay.windows,
   725                     (num_windows + 1) * sizeof(*windows));
   726     if (!windows) {
   727         if (_this->DestroyWindow) {
   728             _this->DestroyWindow(_this, &window);
   729         }
   730         if (window.title) {
   731             SDL_free(window.title);
   732         }
   733         return 0;
   734     }
   735     windows[num_windows] = window;
   736     SDL_CurrentDisplay.windows = windows;
   737     SDL_CurrentDisplay.num_windows++;
   738 
   739     return window.id;
   740 }
   741 
   742 static __inline__ SDL_Window *
   743 SDL_GetWindowFromID(SDL_WindowID windowID)
   744 {
   745     int i, j;
   746 
   747     if (!_this) {
   748         return NULL;
   749     }
   750 
   751     for (i = 0; i < _this->num_displays; ++i) {
   752         SDL_VideoDisplay *display = &_this->displays[i];
   753         for (j = 0; j < display->num_windows; ++j) {
   754             SDL_Window *window = &display->windows[j];
   755             if (window->id == windowID) {
   756                 return window;
   757             }
   758         }
   759     }
   760     return NULL;
   761 }
   762 
   763 Uint32
   764 SDL_GetWindowFlags(SDL_WindowID windowID)
   765 {
   766     SDL_Window *window = SDL_GetWindowFromID(windowID);
   767 
   768     if (!window) {
   769         return 0;
   770     }
   771     return window->flags;
   772 }
   773 
   774 void
   775 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   776 {
   777     SDL_Window *window = SDL_GetWindowFromID(windowID);
   778 
   779     if (!window) {
   780         return;
   781     }
   782     if (window->title) {
   783         SDL_free(window->title);
   784     }
   785     window->title = SDL_strdup(title);
   786 
   787     if (_this->SetWindowTitle) {
   788         _this->SetWindowTitle(_this, window);
   789     }
   790 }
   791 
   792 const char *
   793 SDL_GetWindowTitle(SDL_WindowID windowID)
   794 {
   795     SDL_Window *window = SDL_GetWindowFromID(windowID);
   796 
   797     if (!window) {
   798         return NULL;
   799     }
   800     return window->title;
   801 }
   802 
   803 void
   804 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   805 {
   806     SDL_Window *window = SDL_GetWindowFromID(windowID);
   807 
   808     if (!window) {
   809         return;
   810     }
   811     window->userdata = userdata;
   812 }
   813 
   814 void *
   815 SDL_GetWindowData(SDL_WindowID windowID)
   816 {
   817     SDL_Window *window = SDL_GetWindowFromID(windowID);
   818 
   819     if (!window) {
   820         return NULL;
   821     }
   822     return window->userdata;
   823 }
   824 
   825 void
   826 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   827 {
   828     SDL_Window *window = SDL_GetWindowFromID(windowID);
   829 
   830     if (!window) {
   831         return;
   832     }
   833 
   834     window->x = x;
   835     window->y = y;
   836 
   837     if (_this->SetWindowPosition) {
   838         _this->SetWindowPosition(_this, window);
   839     }
   840 }
   841 
   842 void
   843 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   844 {
   845     SDL_Window *window = SDL_GetWindowFromID(windowID);
   846 
   847     if (!window) {
   848         return;
   849     }
   850     if (x) {
   851         *x = window->x;
   852     }
   853     if (y) {
   854         *y = window->y;
   855     }
   856 }
   857 
   858 void
   859 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   860 {
   861     SDL_Window *window = SDL_GetWindowFromID(windowID);
   862 
   863     if (!window) {
   864         return;
   865     }
   866 
   867     window->w = w;
   868     window->h = h;
   869 
   870     if (_this->SetWindowSize) {
   871         _this->SetWindowSize(_this, window);
   872     }
   873 }
   874 
   875 void
   876 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   877 {
   878     SDL_Window *window = SDL_GetWindowFromID(windowID);
   879 
   880     if (!window) {
   881         return;
   882     }
   883     if (w) {
   884         *w = window->w;
   885     }
   886     if (h) {
   887         *h = window->h;
   888     }
   889 }
   890 
   891 void
   892 SDL_ShowWindow(SDL_WindowID windowID)
   893 {
   894     SDL_Window *window = SDL_GetWindowFromID(windowID);
   895 
   896     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
   897         return;
   898     }
   899 
   900     window->flags |= SDL_WINDOW_SHOWN;
   901 
   902     if (_this->ShowWindow) {
   903         _this->ShowWindow(_this, window);
   904     }
   905 }
   906 
   907 void
   908 SDL_HideWindow(SDL_WindowID windowID)
   909 {
   910     SDL_Window *window = SDL_GetWindowFromID(windowID);
   911 
   912     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
   913         return;
   914     }
   915 
   916     window->flags &= ~SDL_WINDOW_SHOWN;
   917 
   918     if (_this->HideWindow) {
   919         _this->HideWindow(_this, window);
   920     }
   921 }
   922 
   923 void
   924 SDL_RaiseWindow(SDL_WindowID windowID)
   925 {
   926     SDL_Window *window = SDL_GetWindowFromID(windowID);
   927 
   928     if (!window) {
   929         return;
   930     }
   931 
   932     if (_this->RaiseWindow) {
   933         _this->RaiseWindow(_this, window);
   934     }
   935 }
   936 
   937 void
   938 SDL_MaximizeWindow(SDL_WindowID windowID)
   939 {
   940     SDL_Window *window = SDL_GetWindowFromID(windowID);
   941 
   942     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
   943         return;
   944     }
   945 
   946     window->flags |= SDL_WINDOW_MAXIMIZED;
   947 
   948     if (_this->MaximizeWindow) {
   949         _this->MaximizeWindow(_this, window);
   950     }
   951 }
   952 
   953 void
   954 SDL_MinimizeWindow(SDL_WindowID windowID)
   955 {
   956     SDL_Window *window = SDL_GetWindowFromID(windowID);
   957 
   958     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
   959         return;
   960     }
   961 
   962     window->flags |= SDL_WINDOW_MINIMIZED;
   963 
   964     if (_this->MinimizeWindow) {
   965         _this->MinimizeWindow(_this, window);
   966     }
   967 }
   968 
   969 void
   970 SDL_RestoreWindow(SDL_WindowID windowID)
   971 {
   972     SDL_Window *window = SDL_GetWindowFromID(windowID);
   973 
   974     if (!window
   975         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
   976         return;
   977     }
   978 
   979     window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
   980 
   981     if (_this->RestoreWindow) {
   982         _this->RestoreWindow(_this, window);
   983     }
   984 }
   985 
   986 void
   987 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
   988 {
   989     SDL_Window *window = SDL_GetWindowFromID(windowID);
   990 
   991     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
   992         return;
   993     }
   994 
   995     if (mode) {
   996         window->flags |= SDL_WINDOW_INPUT_GRABBED;
   997     } else {
   998         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
   999     }
  1000 
  1001     if (_this->SetWindowGrab) {
  1002         _this->SetWindowGrab(_this, window);
  1003     }
  1004 }
  1005 
  1006 int
  1007 SDL_GetWindowGrab(SDL_WindowID windowID)
  1008 {
  1009     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1010 
  1011     if (!window) {
  1012         return 0;
  1013     }
  1014 
  1015     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1016 }
  1017 
  1018 void
  1019 SDL_DestroyWindow(SDL_WindowID windowID)
  1020 {
  1021     int i, j;
  1022 
  1023     if (!_this) {
  1024         return;
  1025     }
  1026 
  1027     for (i = 0; i < _this->num_displays; ++i) {
  1028         SDL_VideoDisplay *display = &_this->displays[i];
  1029         for (j = 0; j < display->num_windows; ++j) {
  1030             SDL_Window *window = &display->windows[j];
  1031             if (window->id != windowID) {
  1032                 continue;
  1033             }
  1034             if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  1035                 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1036                 _this->SetWindowGrab(_this, window);
  1037             }
  1038             if (window->renderer) {
  1039                 SDL_DestroyRenderer(window->id);
  1040             }
  1041             if (_this->DestroyWindow) {
  1042                 _this->DestroyWindow(_this, window);
  1043             }
  1044             if (window->title) {
  1045                 SDL_free(window->title);
  1046             }
  1047             if (window->gamma) {
  1048                 SDL_free(window->gamma);
  1049             }
  1050             if (j != display->num_windows - 1) {
  1051                 SDL_memcpy(&display->windows[i],
  1052                            &display->windows[i + 1],
  1053                            (display->num_windows - i - 1) * sizeof(*window));
  1054             }
  1055             --display->num_windows;
  1056             return;
  1057         }
  1058     }
  1059 }
  1060 
  1061 void
  1062 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1063 {
  1064     SDL_VideoDisplay *display = &_this->displays[displayIndex];
  1065     SDL_RenderDriver *render_drivers;
  1066 
  1067     render_drivers =
  1068         SDL_realloc(display->render_drivers,
  1069                     (display->num_render_drivers +
  1070                      1) * sizeof(*render_drivers));
  1071     if (render_drivers) {
  1072         render_drivers[display->num_render_drivers] = *driver;
  1073         display->render_drivers = render_drivers;
  1074         display->num_render_drivers++;
  1075     }
  1076 }
  1077 
  1078 int
  1079 SDL_GetNumRenderers(void)
  1080 {
  1081     if (_this) {
  1082         return SDL_CurrentDisplay.num_render_drivers;
  1083     }
  1084     return 0;
  1085 }
  1086 
  1087 int
  1088 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1089 {
  1090     if (index < 0 || index >= SDL_GetNumRenderers()) {
  1091         SDL_SetError("index must be in the range of 0 - %d",
  1092                      SDL_GetNumRenderers() - 1);
  1093         return -1;
  1094     }
  1095     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1096     return 0;
  1097 }
  1098 
  1099 int
  1100 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1101 {
  1102     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1103 
  1104     if (!window) {
  1105         return 0;
  1106     }
  1107 
  1108     if (index < 0) {
  1109         int n = SDL_GetNumRenderers();
  1110         for (index = 0; index < n; ++index) {
  1111             SDL_RenderDriver *driver =
  1112                 &SDL_CurrentDisplay.render_drivers[index];
  1113 
  1114             /* Skip minimal drivers in automatic scans */
  1115             if (!(flags & SDL_Renderer_Minimal)
  1116                 && (driver->info.flags & SDL_Renderer_Minimal)) {
  1117                 continue;
  1118             }
  1119             if ((driver->info.flags & flags) == flags) {
  1120                 break;
  1121             }
  1122         }
  1123         if (index == n) {
  1124             SDL_SetError("Couldn't find matching render driver");
  1125             return -1;
  1126         }
  1127     }
  1128 
  1129     if (index >= SDL_GetNumRenderers()) {
  1130         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1131                      SDL_GetNumRenderers() - 1);
  1132         return -1;
  1133     }
  1134 
  1135     /* Free any existing renderer */
  1136     SDL_DestroyRenderer(windowID);
  1137 
  1138     /* Create a new renderer instance */
  1139     window->renderer =
  1140         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1141                                                                 flags);
  1142     if (!window->renderer) {
  1143         return -1;
  1144     }
  1145     SDL_CurrentDisplay.current_renderer = window->renderer;
  1146 
  1147     return 0;
  1148 }
  1149 
  1150 int
  1151 SDL_SelectRenderer(SDL_WindowID windowID)
  1152 {
  1153     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1154 
  1155     if (!window || !window->renderer) {
  1156         return -1;
  1157     }
  1158     SDL_CurrentDisplay.current_renderer = window->renderer;
  1159     return 0;
  1160 }
  1161 
  1162 SDL_TextureID
  1163 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1164 {
  1165     int hash;
  1166     SDL_Renderer *renderer;
  1167     SDL_Texture *texture;
  1168 
  1169     if (!_this) {
  1170         return 0;
  1171     }
  1172 
  1173     renderer = SDL_CurrentDisplay.current_renderer;
  1174     if (!renderer || !renderer->CreateTexture) {
  1175         return 0;
  1176     }
  1177 
  1178     texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
  1179     if (!texture) {
  1180         SDL_OutOfMemory();
  1181         return 0;
  1182     }
  1183 
  1184     SDL_zerop(texture);
  1185     texture->id = _this->next_object_id++;
  1186     texture->format = format;
  1187     texture->access = access;
  1188     texture->w = w;
  1189     texture->h = h;
  1190     texture->renderer = renderer;
  1191 
  1192     if (renderer->CreateTexture(renderer, texture) < 0) {
  1193         SDL_free(texture);
  1194         return 0;
  1195     }
  1196 
  1197     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1198     texture->next = SDL_CurrentDisplay.textures[hash];
  1199     SDL_CurrentDisplay.textures[hash] = texture;
  1200 
  1201     return texture->id;
  1202 }
  1203 
  1204 SDL_TextureID
  1205 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1206 {
  1207     SDL_TextureID textureID;
  1208     Uint32 surface_flags = surface->flags;
  1209     SDL_PixelFormat *fmt = surface->format;
  1210     Uint32 colorkey;
  1211     Uint8 alpha;
  1212     SDL_Rect bounds;
  1213     SDL_Surface dst;
  1214     int bpp;
  1215     Uint32 Rmask, Gmask, Bmask, Amask;
  1216 
  1217     if (!surface) {
  1218         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1219         return 0;
  1220     }
  1221 
  1222     if (format) {
  1223         if (!SDL_PixelFormatEnumToMasks
  1224             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1225             SDL_SetError("Unknown pixel format");
  1226             return 0;
  1227         }
  1228     } else {
  1229         bpp = fmt->BitsPerPixel;
  1230         Rmask = fmt->Rmask;
  1231         Gmask = fmt->Gmask;
  1232         Bmask = fmt->Bmask;
  1233         Amask = fmt->Amask;
  1234         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1235         if (!format) {
  1236             SDL_SetError("Unknown pixel format");
  1237             return 0;
  1238         }
  1239     }
  1240 
  1241     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1242     if (!textureID) {
  1243         return 0;
  1244     }
  1245 
  1246     /* Set up a destination surface for the texture update */
  1247     SDL_zero(dst);
  1248     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1249     if (!dst.format) {
  1250         SDL_DestroyTexture(textureID);
  1251         return 0;
  1252     }
  1253     dst.w = surface->w;
  1254     dst.h = surface->h;
  1255     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1256         dst.flags |= SDL_PREALLOC;
  1257     } else {
  1258         dst.pitch = SDL_CalculatePitch(&dst);
  1259         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1260         if (!dst.pixels) {
  1261             SDL_DestroyTexture(textureID);
  1262             SDL_FreeFormat(dst.format);
  1263             SDL_OutOfMemory();
  1264             return 0;
  1265         }
  1266     }
  1267 
  1268     /* Copy the palette if any */
  1269     if (fmt->palette && dst.format->palette) {
  1270         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1271                               fmt->palette->ncolors);
  1272 
  1273         SDL_memcpy(dst.format->palette->colors,
  1274                    fmt->palette->colors,
  1275                    fmt->palette->ncolors * sizeof(SDL_Color));
  1276         dst.format->palette->ncolors = fmt->palette->ncolors;
  1277     }
  1278 
  1279     /* Make the texture transparent if the surface has colorkey */
  1280     if (surface_flags & SDL_SRCCOLORKEY) {
  1281         int row;
  1282         int length = dst.w * dst.format->BytesPerPixel;
  1283         Uint8 *p = (Uint8 *) dst.pixels;
  1284         for (row = 0; row < dst.h; ++row) {
  1285             SDL_memset(p, 0, length);
  1286             p += dst.pitch;
  1287         }
  1288     }
  1289 
  1290     /* Copy over the alpha channel */
  1291     if (surface_flags & SDL_SRCALPHA) {
  1292         if (fmt->Amask) {
  1293             surface->flags &= ~SDL_SRCALPHA;
  1294         } else {
  1295             /* FIXME: Need to make sure the texture has an alpha channel
  1296              *        and copy 'alpha' into the texture alpha channel.
  1297              */
  1298             alpha = surface->format->alpha;
  1299             SDL_SetAlpha(surface, 0, 0);
  1300         }
  1301     }
  1302 
  1303     /* Copy over the image data */
  1304     bounds.x = 0;
  1305     bounds.y = 0;
  1306     bounds.w = surface->w;
  1307     bounds.h = surface->h;
  1308     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1309 
  1310     /* Clean up the original surface */
  1311     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1312         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
  1313         SDL_SetColorKey(surface, cflags, colorkey);
  1314     }
  1315     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1316         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1317         if (fmt->Amask) {
  1318             surface->flags |= SDL_SRCALPHA;
  1319         } else {
  1320             SDL_SetAlpha(surface, aflags, alpha);
  1321         }
  1322     }
  1323 
  1324     /* Update the texture */
  1325     if (dst.flags & SDL_PREALLOC) {
  1326         SDL_UnlockTexture(textureID);
  1327     } else {
  1328         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1329         SDL_free(dst.pixels);
  1330     }
  1331     SDL_FreeFormat(dst.format);
  1332 
  1333     return textureID;
  1334 }
  1335 
  1336 static __inline__ SDL_Texture *
  1337 SDL_GetTextureFromID(SDL_TextureID textureID)
  1338 {
  1339     int hash;
  1340     SDL_Texture *texture;
  1341 
  1342     if (!_this) {
  1343         return NULL;
  1344     }
  1345 
  1346     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1347     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1348          texture = texture->next) {
  1349         if (texture->id == textureID) {
  1350             return texture;
  1351         }
  1352     }
  1353     return NULL;
  1354 }
  1355 
  1356 int
  1357 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1358                  int *w, int *h)
  1359 {
  1360     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1361 
  1362     if (!texture) {
  1363         return -1;
  1364     }
  1365 
  1366     if (format) {
  1367         *format = texture->format;
  1368     }
  1369     if (access) {
  1370         *access = texture->access;
  1371     }
  1372     if (w) {
  1373         *w = texture->w;
  1374     }
  1375     if (h) {
  1376         *h = texture->h;
  1377     }
  1378     return 0;
  1379 }
  1380 
  1381 int
  1382 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1383 {
  1384     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1385     SDL_Renderer *renderer;
  1386 
  1387     if (!texture) {
  1388         return -1;
  1389     }
  1390 
  1391     renderer = texture->renderer;
  1392     if (!renderer->QueryTexturePixels) {
  1393         return -1;
  1394     }
  1395     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1396 }
  1397 
  1398 int
  1399 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1400                       int firstcolor, int ncolors)
  1401 {
  1402     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1403     SDL_Renderer *renderer;
  1404 
  1405     if (!texture) {
  1406         return -1;
  1407     }
  1408 
  1409     renderer = texture->renderer;
  1410     if (!renderer->SetTexturePalette) {
  1411         return -1;
  1412     }
  1413     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1414                                        ncolors);
  1415 }
  1416 
  1417 int
  1418 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1419                       int firstcolor, int ncolors)
  1420 {
  1421     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1422     SDL_Renderer *renderer;
  1423 
  1424     if (!texture) {
  1425         return -1;
  1426     }
  1427 
  1428     renderer = texture->renderer;
  1429     if (!renderer->GetTexturePalette) {
  1430         return -1;
  1431     }
  1432     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1433                                        ncolors);
  1434 }
  1435 
  1436 int
  1437 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1438                   const void *pixels, int pitch)
  1439 {
  1440     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1441     SDL_Renderer *renderer;
  1442 
  1443     if (!texture) {
  1444         return -1;
  1445     }
  1446 
  1447     renderer = texture->renderer;
  1448     if (!renderer->UpdateTexture) {
  1449         return -1;
  1450     }
  1451     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1452 }
  1453 
  1454 int
  1455 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1456                 void **pixels, int *pitch)
  1457 {
  1458     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1459     SDL_Renderer *renderer;
  1460 
  1461     if (!texture) {
  1462         return -1;
  1463     }
  1464 
  1465     renderer = texture->renderer;
  1466     if (!renderer->LockTexture) {
  1467         return -1;
  1468     }
  1469     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1470                                  pitch);
  1471 }
  1472 
  1473 void
  1474 SDL_UnlockTexture(SDL_TextureID textureID)
  1475 {
  1476     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1477     SDL_Renderer *renderer;
  1478 
  1479     if (!texture) {
  1480         return;
  1481     }
  1482 
  1483     renderer = texture->renderer;
  1484     if (!renderer->UnlockTexture) {
  1485         return;
  1486     }
  1487     return renderer->UnlockTexture(renderer, texture);
  1488 }
  1489 
  1490 void
  1491 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1492                  const SDL_Rect * rects)
  1493 {
  1494     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1495     SDL_Renderer *renderer;
  1496 
  1497     if (!texture) {
  1498         return;
  1499     }
  1500 
  1501     renderer = texture->renderer;
  1502     if (!renderer->DirtyTexture) {
  1503         return;
  1504     }
  1505     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1506 }
  1507 
  1508 void
  1509 SDL_SelectRenderTexture(SDL_TextureID textureID)
  1510 {
  1511     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1512     SDL_Renderer *renderer;
  1513 
  1514     if (!texture || texture->access != SDL_TextureAccess_Render) {
  1515         return;
  1516     }
  1517     renderer = texture->renderer;
  1518     if (!renderer->SelectRenderTexture) {
  1519         return;
  1520     }
  1521     renderer->SelectRenderTexture(renderer, texture);
  1522 }
  1523 
  1524 int
  1525 SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
  1526 {
  1527     SDL_Renderer *renderer;
  1528 
  1529     if (!_this) {
  1530         return -1;
  1531     }
  1532 
  1533     renderer = SDL_CurrentDisplay.current_renderer;
  1534     if (!renderer || !renderer->RenderFill) {
  1535         return -1;
  1536     }
  1537 
  1538     renderer->RenderFill(renderer, rect, color);
  1539 }
  1540 
  1541 int
  1542 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1543                const SDL_Rect * dstrect, int blendMode, int scaleMode)
  1544 {
  1545     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1546     SDL_Renderer *renderer;
  1547 
  1548     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1549         return;
  1550     }
  1551 
  1552     renderer = SDL_CurrentDisplay.current_renderer;
  1553     if (!renderer || !renderer->RenderCopy) {
  1554         return -1;
  1555     }
  1556 
  1557     return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
  1558                                 blendMode, scaleMode);
  1559 }
  1560 
  1561 int
  1562 SDL_RenderReadPixels(const SDL_Rect * rect, void *pixels, int pitch)
  1563 {
  1564     SDL_Renderer *renderer;
  1565 
  1566     if (!_this) {
  1567         return -1;
  1568     }
  1569 
  1570     renderer = SDL_CurrentDisplay.current_renderer;
  1571     if (!renderer || !renderer->RenderReadPixels) {
  1572         return -1;
  1573     }
  1574 
  1575     return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
  1576 }
  1577 
  1578 int
  1579 SDL_RenderWritePixels(const SDL_Rect * rect, const void *pixels, int pitch)
  1580 {
  1581     SDL_Renderer *renderer;
  1582 
  1583     if (!_this) {
  1584         return -1;
  1585     }
  1586 
  1587     renderer = SDL_CurrentDisplay.current_renderer;
  1588     if (!renderer || !renderer->RenderWritePixels) {
  1589         return -1;
  1590     }
  1591 
  1592     return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
  1593 }
  1594 
  1595 void
  1596 SDL_RenderPresent(void)
  1597 {
  1598     SDL_Renderer *renderer;
  1599 
  1600     if (!_this) {
  1601         return;
  1602     }
  1603 
  1604     renderer = SDL_CurrentDisplay.current_renderer;
  1605     if (!renderer || !renderer->RenderPresent) {
  1606         return;
  1607     }
  1608 
  1609     renderer->RenderPresent(renderer);
  1610 }
  1611 
  1612 void
  1613 SDL_DestroyTexture(SDL_TextureID textureID)
  1614 {
  1615     int hash;
  1616     SDL_Texture *prev, *texture;
  1617     SDL_Renderer *renderer;
  1618 
  1619     if (!_this) {
  1620         return;
  1621     }
  1622 
  1623     /* Look up the texture in the hash table */
  1624     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1625     prev = NULL;
  1626     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1627          prev = texture, texture = texture->next) {
  1628         if (texture->id == textureID) {
  1629             break;
  1630         }
  1631     }
  1632     if (!texture) {
  1633         return;
  1634     }
  1635 
  1636     /* Unlink the texture from the list */
  1637     if (prev) {
  1638         prev->next = texture->next;
  1639     } else {
  1640         SDL_CurrentDisplay.textures[hash] = texture->next;
  1641     }
  1642 
  1643     /* Free the texture */
  1644     renderer = texture->renderer;
  1645     renderer->DestroyTexture(renderer, texture);
  1646     SDL_free(texture);
  1647 }
  1648 
  1649 void
  1650 SDL_DestroyRenderer(SDL_WindowID windowID)
  1651 {
  1652     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1653     SDL_Renderer *renderer;
  1654     int i;
  1655 
  1656     if (!window) {
  1657         return;
  1658     }
  1659 
  1660     renderer = window->renderer;
  1661     if (!renderer) {
  1662         return;
  1663     }
  1664 
  1665     /* Free existing textures for this renderer */
  1666     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1667         SDL_Texture *texture;
  1668         SDL_Texture *prev = NULL;
  1669         SDL_Texture *next;
  1670         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1671              texture = next) {
  1672             next = texture->next;
  1673             if (texture->renderer == renderer) {
  1674                 if (prev) {
  1675                     prev->next = next;
  1676                 } else {
  1677                     SDL_CurrentDisplay.textures[i] = next;
  1678                 }
  1679                 renderer->DestroyTexture(renderer, texture);
  1680                 SDL_free(texture);
  1681             } else {
  1682                 prev = texture;
  1683             }
  1684         }
  1685     }
  1686 
  1687     /* Free the renderer instance */
  1688     renderer->DestroyRenderer(renderer);
  1689 
  1690     /* Clear references */
  1691     window->renderer = NULL;
  1692     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1693         SDL_CurrentDisplay.current_renderer = NULL;
  1694     }
  1695 }
  1696 
  1697 void
  1698 SDL_VideoQuit(void)
  1699 {
  1700     int i, j;
  1701 
  1702     if (!_this) {
  1703         return;
  1704     }
  1705 
  1706     /* Halt event processing before doing anything else */
  1707     SDL_StopEventLoop();
  1708 
  1709     /* Clean up the system video */
  1710     for (i = _this->num_displays; i--;) {
  1711         SDL_VideoDisplay *display = &_this->displays[i];
  1712         for (j = display->num_windows; j--;) {
  1713             SDL_DestroyWindow(display->windows[i].id);
  1714         }
  1715         if (display->windows) {
  1716             SDL_free(display->windows);
  1717             display->windows = NULL;
  1718         }
  1719         if (display->palette.colors) {
  1720             SDL_free(display->palette.colors);
  1721             display->palette.colors = NULL;
  1722             display->palette.ncolors = 0;
  1723         }
  1724     }
  1725     _this->VideoQuit(_this);
  1726     if (_this->displays) {
  1727         SDL_free(_this->displays);
  1728     }
  1729     _this->free(_this);
  1730     _this = NULL;
  1731 }
  1732 
  1733 /* Load the GL driver library */
  1734 int
  1735 SDL_GL_LoadLibrary(const char *path)
  1736 {
  1737     int retval;
  1738 
  1739     retval = -1;
  1740     if (_this == NULL) {
  1741         SDL_SetError("Video subsystem has not been initialized");
  1742     } else {
  1743         if (_this->GL_LoadLibrary) {
  1744             retval = _this->GL_LoadLibrary(_this, path);
  1745         } else {
  1746             SDL_SetError("No dynamic GL support in video driver");
  1747         }
  1748     }
  1749     return (retval);
  1750 }
  1751 
  1752 void *
  1753 SDL_GL_GetProcAddress(const char *proc)
  1754 {
  1755     void *func;
  1756 
  1757     func = NULL;
  1758     if (_this->GL_GetProcAddress) {
  1759         if (_this->gl_config.driver_loaded) {
  1760             func = _this->GL_GetProcAddress(_this, proc);
  1761         } else {
  1762             SDL_SetError("No GL driver has been loaded");
  1763         }
  1764     } else {
  1765         SDL_SetError("No dynamic GL support in video driver");
  1766     }
  1767     return func;
  1768 }
  1769 
  1770 /* Set the specified GL attribute for setting up a GL video mode */
  1771 int
  1772 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  1773 {
  1774     int retval;
  1775 
  1776     retval = 0;
  1777     switch (attr) {
  1778     case SDL_GL_RED_SIZE:
  1779         _this->gl_config.red_size = value;
  1780         break;
  1781     case SDL_GL_GREEN_SIZE:
  1782         _this->gl_config.green_size = value;
  1783         break;
  1784     case SDL_GL_BLUE_SIZE:
  1785         _this->gl_config.blue_size = value;
  1786         break;
  1787     case SDL_GL_ALPHA_SIZE:
  1788         _this->gl_config.alpha_size = value;
  1789         break;
  1790     case SDL_GL_DOUBLEBUFFER:
  1791         _this->gl_config.double_buffer = value;
  1792         break;
  1793     case SDL_GL_BUFFER_SIZE:
  1794         _this->gl_config.buffer_size = value;
  1795         break;
  1796     case SDL_GL_DEPTH_SIZE:
  1797         _this->gl_config.depth_size = value;
  1798         break;
  1799     case SDL_GL_STENCIL_SIZE:
  1800         _this->gl_config.stencil_size = value;
  1801         break;
  1802     case SDL_GL_ACCUM_RED_SIZE:
  1803         _this->gl_config.accum_red_size = value;
  1804         break;
  1805     case SDL_GL_ACCUM_GREEN_SIZE:
  1806         _this->gl_config.accum_green_size = value;
  1807         break;
  1808     case SDL_GL_ACCUM_BLUE_SIZE:
  1809         _this->gl_config.accum_blue_size = value;
  1810         break;
  1811     case SDL_GL_ACCUM_ALPHA_SIZE:
  1812         _this->gl_config.accum_alpha_size = value;
  1813         break;
  1814     case SDL_GL_STEREO:
  1815         _this->gl_config.stereo = value;
  1816         break;
  1817     case SDL_GL_MULTISAMPLEBUFFERS:
  1818         _this->gl_config.multisamplebuffers = value;
  1819         break;
  1820     case SDL_GL_MULTISAMPLESAMPLES:
  1821         _this->gl_config.multisamplesamples = value;
  1822         break;
  1823     case SDL_GL_ACCELERATED_VISUAL:
  1824         _this->gl_config.accelerated = value;
  1825         break;
  1826     case SDL_GL_SWAP_CONTROL:
  1827         _this->gl_config.swap_control = value;
  1828         break;
  1829     default:
  1830         SDL_SetError("Unknown OpenGL attribute");
  1831         retval = -1;
  1832         break;
  1833     }
  1834     return (retval);
  1835 }
  1836 
  1837 /* Retrieve an attribute value from the windowing system. */
  1838 int
  1839 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  1840 {
  1841     int retval = -1;
  1842 
  1843     if (_this->GL_GetAttribute) {
  1844         retval = _this->GL_GetAttribute(_this, attr, value);
  1845     } else {
  1846         *value = 0;
  1847         SDL_SetError("GL_GetAttribute not supported");
  1848     }
  1849     return retval;
  1850 }
  1851 
  1852 /* Perform a GL buffer swap on the current GL context */
  1853 void
  1854 SDL_GL_SwapBuffers(void)
  1855 {
  1856     // 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?
  1857     _this->GL_SwapBuffers(_this);
  1858 }
  1859 
  1860 #if 0                           // FIXME
  1861 /* Utility function used by SDL_WM_SetIcon();
  1862  * flags & 1 for color key, flags & 2 for alpha channel. */
  1863 static void
  1864 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  1865 {
  1866     int x, y;
  1867     Uint32 colorkey;
  1868 #define SET_MASKBIT(icon, x, y, mask) \
  1869 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  1870 
  1871     colorkey = icon->format->colorkey;
  1872     switch (icon->format->BytesPerPixel) {
  1873     case 1:
  1874         {
  1875             Uint8 *pixels;
  1876             for (y = 0; y < icon->h; ++y) {
  1877                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  1878                 for (x = 0; x < icon->w; ++x) {
  1879                     if (*pixels++ == colorkey) {
  1880                         SET_MASKBIT(icon, x, y, mask);
  1881                     }
  1882                 }
  1883             }
  1884         }
  1885         break;
  1886 
  1887     case 2:
  1888         {
  1889             Uint16 *pixels;
  1890             for (y = 0; y < icon->h; ++y) {
  1891                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  1892                 for (x = 0; x < icon->w; ++x) {
  1893                     if ((flags & 1) && *pixels == colorkey) {
  1894                         SET_MASKBIT(icon, x, y, mask);
  1895                     } else if ((flags & 2)
  1896                                && (*pixels & icon->format->Amask) == 0) {
  1897                         SET_MASKBIT(icon, x, y, mask);
  1898                     }
  1899                     pixels++;
  1900                 }
  1901             }
  1902         }
  1903         break;
  1904 
  1905     case 4:
  1906         {
  1907             Uint32 *pixels;
  1908             for (y = 0; y < icon->h; ++y) {
  1909                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  1910                 for (x = 0; x < icon->w; ++x) {
  1911                     if ((flags & 1) && *pixels == colorkey) {
  1912                         SET_MASKBIT(icon, x, y, mask);
  1913                     } else if ((flags & 2)
  1914                                && (*pixels & icon->format->Amask) == 0) {
  1915                         SET_MASKBIT(icon, x, y, mask);
  1916                     }
  1917                     pixels++;
  1918                 }
  1919             }
  1920         }
  1921         break;
  1922     }
  1923 }
  1924 
  1925 /*
  1926  * Sets the window manager icon for the display window.
  1927  */
  1928 void
  1929 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  1930 {
  1931     if (icon && _this->SetIcon) {
  1932         /* Generate a mask if necessary, and create the icon! */
  1933         if (mask == NULL) {
  1934             int mask_len = icon->h * (icon->w + 7) / 8;
  1935             int flags = 0;
  1936             mask = (Uint8 *) SDL_malloc(mask_len);
  1937             if (mask == NULL) {
  1938                 return;
  1939             }
  1940             SDL_memset(mask, ~0, mask_len);
  1941             if (icon->flags & SDL_SRCCOLORKEY)
  1942                 flags |= 1;
  1943             if (icon->flags & SDL_SRCALPHA)
  1944                 flags |= 2;
  1945             if (flags) {
  1946                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1947             }
  1948             _this->SetIcon(_this, icon, mask);
  1949             SDL_free(mask);
  1950         } else {
  1951             _this->SetIcon(_this, icon, mask);
  1952         }
  1953     }
  1954 }
  1955 #endif
  1956 
  1957 SDL_bool
  1958 SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
  1959 {
  1960     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1961 
  1962     if (!window || !_this->GetWindowWMInfo) {
  1963         return SDL_FALSE;
  1964     }
  1965     return (_this->GetWindowWMInfo(_this, window, info));
  1966 }
  1967 
  1968 /* vi: set ts=4 sw=4 expandtab: */