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