src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Oct 2006 02:28:41 +0000
changeset 2046 da8332c8f480
parent 1985 8055185ae4ed
child 2062 c9aa6bcb26f3
permissions -rw-r--r--
Replaced strncmp for SDL_VIDEODRIVER test with strcasecmp
     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_gl.h"
    31 #include "SDL_renderer_sw.h"
    32 #include "../events/SDL_sysevents.h"
    33 #include "../events/SDL_events_c.h"
    34 
    35 #if SDL_VIDEO_OPENGL
    36 #include "SDL_opengl.h"
    37 
    38 /* On Windows, windows.h defines CreateWindow */
    39 #ifdef CreateWindow
    40 #undef CreateWindow
    41 #endif
    42 #endif /* SDL_VIDEO_OPENGL */
    43 
    44 /* Available video drivers */
    45 static VideoBootStrap *bootstrap[] = {
    46 #if SDL_VIDEO_DRIVER_COCOA
    47     &COCOA_bootstrap,
    48 #endif
    49 #if SDL_VIDEO_DRIVER_X11
    50     &X11_bootstrap,
    51 #endif
    52 #if SDL_VIDEO_DRIVER_NANOX
    53     &NX_bootstrap,
    54 #endif
    55 #if SDL_VIDEO_DRIVER_IPOD
    56     &iPod_bootstrap,
    57 #endif
    58 #if SDL_VIDEO_DRIVER_WSCONS
    59     &WSCONS_bootstrap,
    60 #endif
    61 #if SDL_VIDEO_DRIVER_FBCON
    62     &FBCON_bootstrap,
    63 #endif
    64 #if SDL_VIDEO_DRIVER_DIRECTFB
    65     &DirectFB_bootstrap,
    66 #endif
    67 #if SDL_VIDEO_DRIVER_PS2GS
    68     &PS2GS_bootstrap,
    69 #endif
    70 #if SDL_VIDEO_DRIVER_VGL
    71     &VGL_bootstrap,
    72 #endif
    73 #if SDL_VIDEO_DRIVER_SVGALIB
    74     &SVGALIB_bootstrap,
    75 #endif
    76 #if SDL_VIDEO_DRIVER_GAPI
    77     &GAPI_bootstrap,
    78 #endif
    79 #if SDL_VIDEO_DRIVER_WIN32
    80     &WIN32_bootstrap,
    81 #endif
    82 #if SDL_VIDEO_DRIVER_BWINDOW
    83     &BWINDOW_bootstrap,
    84 #endif
    85 #if SDL_VIDEO_DRIVER_CYBERGRAPHICS
    86     &CGX_bootstrap,
    87 #endif
    88 #if SDL_VIDEO_DRIVER_PHOTON
    89     &ph_bootstrap,
    90 #endif
    91 #if SDL_VIDEO_DRIVER_EPOC
    92     &EPOC_bootstrap,
    93 #endif
    94 #if SDL_VIDEO_DRIVER_XBIOS
    95     &XBIOS_bootstrap,
    96 #endif
    97 #if SDL_VIDEO_DRIVER_GEM
    98     &GEM_bootstrap,
    99 #endif
   100 #if SDL_VIDEO_DRIVER_DC
   101     &DC_bootstrap,
   102 #endif
   103 #if SDL_VIDEO_DRIVER_RISCOS
   104     &RISCOS_bootstrap,
   105 #endif
   106 #if SDL_VIDEO_DRIVER_OS2FS
   107     &OS2FSLib_bootstrap,
   108 #endif
   109 #if SDL_VIDEO_DRIVER_DUMMY
   110     &DUMMY_bootstrap,
   111 #endif
   112     NULL
   113 };
   114 
   115 static SDL_VideoDevice *_this = NULL;
   116 
   117 /* Various local functions */
   118 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   119 void SDL_VideoQuit(void);
   120 
   121 static int
   122 cmpmodes(const void *A, const void *B)
   123 {
   124     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   125     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   126 
   127     if (a.w != b.w) {
   128         return b.w - a.w;
   129     }
   130     if (a.h != b.h) {
   131         return b.h - a.h;
   132     }
   133     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   134         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   135     }
   136     if (a.refresh_rate != b.refresh_rate) {
   137         return b.refresh_rate - a.refresh_rate;
   138     }
   139     return 0;
   140 }
   141 
   142 static void
   143 SDL_UninitializedVideo()
   144 {
   145     SDL_SetError("Video subsystem has not been initialized");
   146 }
   147 
   148 int
   149 SDL_GetNumVideoDrivers(void)
   150 {
   151     return SDL_arraysize(bootstrap) - 1;
   152 }
   153 
   154 const char *
   155 SDL_GetVideoDriver(int index)
   156 {
   157     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   158         return bootstrap[index]->name;
   159     }
   160     return NULL;
   161 }
   162 
   163 /*
   164  * Initialize the video and event subsystems -- determine native pixel format
   165  */
   166 int
   167 SDL_VideoInit(const char *driver_name, Uint32 flags)
   168 {
   169     SDL_VideoDevice *video;
   170     int index;
   171     int i;
   172 
   173     /* Toggle the event thread flags, based on OS requirements */
   174 #if defined(MUST_THREAD_EVENTS)
   175     flags |= SDL_INIT_EVENTTHREAD;
   176 #elif defined(CANT_THREAD_EVENTS)
   177     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   178         SDL_SetError("OS doesn't support threaded events");
   179         return -1;
   180     }
   181 #endif
   182 
   183     /* Start the event loop */
   184     if (SDL_StartEventLoop(flags) < 0) {
   185         return -1;
   186     }
   187 
   188     /* Check to make sure we don't overwrite '_this' */
   189     if (_this != NULL) {
   190         SDL_VideoQuit();
   191     }
   192 
   193     /* Select the proper video driver */
   194     index = 0;
   195     video = NULL;
   196     if (driver_name == NULL) {
   197         driver_name = SDL_getenv("SDL_VIDEODRIVER");
   198     }
   199     if (driver_name != NULL) {
   200         for (i = 0; bootstrap[i]; ++i) {
   201             if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
   202                 if (bootstrap[i]->available()) {
   203                     video = bootstrap[i]->create(index);
   204                 }
   205                 break;
   206             }
   207         }
   208     } else {
   209         for (i = 0; bootstrap[i]; ++i) {
   210             if (bootstrap[i]->available()) {
   211                 video = bootstrap[i]->create(index);
   212                 if (video != NULL) {
   213                     break;
   214                 }
   215             }
   216         }
   217     }
   218     if (video == NULL) {
   219         if (driver_name) {
   220             SDL_SetError("%s not available", driver_name);
   221         } else {
   222             SDL_SetError("No available video device");
   223         }
   224         return -1;
   225     }
   226     _this = video;
   227     _this->name = bootstrap[i]->name;
   228     _this->next_object_id = 1;
   229 
   230 
   231     /* Set some very sane GL defaults */
   232     _this->gl_config.driver_loaded = 0;
   233     _this->gl_config.dll_handle = NULL;
   234     _this->gl_config.red_size = 3;
   235     _this->gl_config.green_size = 3;
   236     _this->gl_config.blue_size = 2;
   237     _this->gl_config.alpha_size = 0;
   238     _this->gl_config.buffer_size = 0;
   239     _this->gl_config.depth_size = 16;
   240     _this->gl_config.stencil_size = 0;
   241     _this->gl_config.double_buffer = 1;
   242     _this->gl_config.accum_red_size = 0;
   243     _this->gl_config.accum_green_size = 0;
   244     _this->gl_config.accum_blue_size = 0;
   245     _this->gl_config.accum_alpha_size = 0;
   246     _this->gl_config.stereo = 0;
   247     _this->gl_config.multisamplebuffers = 0;
   248     _this->gl_config.multisamplesamples = 0;
   249     _this->gl_config.accelerated = -1;  /* not known, don't set */
   250 
   251     /* Initialize the video subsystem */
   252     if (_this->VideoInit(_this) < 0) {
   253         SDL_VideoQuit();
   254         return -1;
   255     }
   256 
   257     /* Make sure some displays were added */
   258     if (_this->num_displays == 0) {
   259         SDL_SetError("The video driver did not add any displays");
   260         SDL_VideoQuit();
   261         return (-1);
   262     }
   263 
   264     /* The software renderer is always available */
   265     for (i = 0; i < _this->num_displays; ++i) {
   266 #if SDL_VIDEO_RENDER_OGL
   267         SDL_AddRenderDriver(i, &GL_RenderDriver);
   268 #endif
   269         if (_this->displays[i].num_render_drivers > 0) {
   270             SDL_AddRenderDriver(i, &SW_RenderDriver);
   271         }
   272     }
   273 
   274     /* We're ready to go! */
   275     return 0;
   276 }
   277 
   278 const char *
   279 SDL_GetCurrentVideoDriver()
   280 {
   281     if (!_this) {
   282         SDL_UninitializedVideo();
   283         return NULL;
   284     }
   285     return _this->name;
   286 }
   287 
   288 SDL_VideoDevice *
   289 SDL_GetVideoDevice()
   290 {
   291     return _this;
   292 }
   293 
   294 int
   295 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   296 {
   297     SDL_VideoDisplay display;
   298 
   299     SDL_zero(display);
   300     if (desktop_mode) {
   301         display.desktop_mode = *desktop_mode;
   302     }
   303     display.current_mode = display.desktop_mode;
   304 
   305     return SDL_AddVideoDisplay(&display);
   306 }
   307 
   308 int
   309 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   310 {
   311     SDL_VideoDisplay *displays;
   312     int index = -1;
   313 
   314     displays =
   315         SDL_realloc(_this->displays,
   316                     (_this->num_displays + 1) * sizeof(*displays));
   317     if (displays) {
   318         index = _this->num_displays++;
   319         displays[index] = *display;
   320         displays[index].device = _this;
   321         _this->displays = displays;
   322     } else {
   323         SDL_OutOfMemory();
   324     }
   325     return index;
   326 }
   327 
   328 int
   329 SDL_GetNumVideoDisplays(void)
   330 {
   331     if (!_this) {
   332         SDL_UninitializedVideo();
   333         return 0;
   334     }
   335     return _this->num_displays;
   336 }
   337 
   338 int
   339 SDL_SelectVideoDisplay(int index)
   340 {
   341     if (!_this) {
   342         SDL_UninitializedVideo();
   343         return (-1);
   344     }
   345     if (index < 0 || index >= _this->num_displays) {
   346         SDL_SetError("index must be in the range 0 - %d",
   347                      _this->num_displays - 1);
   348         return -1;
   349     }
   350     _this->current_display = index;
   351     return 0;
   352 }
   353 
   354 int
   355 SDL_GetCurrentVideoDisplay(void)
   356 {
   357     if (!_this) {
   358         SDL_UninitializedVideo();
   359         return (-1);
   360     }
   361     return _this->current_display;
   362 }
   363 
   364 SDL_bool
   365 SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
   366 {
   367     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   368     SDL_DisplayMode *modes;
   369     int i, nmodes;
   370 
   371     /* Make sure we don't already have the mode in the list */
   372     modes = display->display_modes;
   373     nmodes = display->num_display_modes;
   374     for (i = nmodes; i--;) {
   375         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   376             return SDL_FALSE;
   377         }
   378     }
   379 
   380     /* Go ahead and add the new mode */
   381     if (nmodes == display->max_display_modes) {
   382         modes =
   383             SDL_realloc(modes,
   384                         (display->max_display_modes + 32) * sizeof(*modes));
   385         if (!modes) {
   386             return SDL_FALSE;
   387         }
   388         display->display_modes = modes;
   389         display->max_display_modes += 32;
   390     }
   391     modes[nmodes] = *mode;
   392     display->num_display_modes++;
   393 
   394     return SDL_TRUE;
   395 }
   396 
   397 int
   398 SDL_GetNumDisplayModes()
   399 {
   400     if (_this) {
   401         SDL_VideoDisplay *display = &SDL_CurrentDisplay;
   402         if (!display->num_display_modes && _this->GetDisplayModes) {
   403             _this->GetDisplayModes(_this);
   404             SDL_qsort(display->display_modes, display->num_display_modes,
   405                       sizeof(SDL_DisplayMode), cmpmodes);
   406         }
   407         return display->num_display_modes;
   408     }
   409     return 0;
   410 }
   411 
   412 int
   413 SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
   414 {
   415     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   416         SDL_SetError("index must be in the range of 0 - %d",
   417                      SDL_GetNumDisplayModes() - 1);
   418         return -1;
   419     }
   420     if (mode) {
   421         *mode = SDL_CurrentDisplay.display_modes[index];
   422     }
   423     return 0;
   424 }
   425 
   426 int
   427 SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
   428 {
   429     if (!_this) {
   430         SDL_UninitializedVideo();
   431         return -1;
   432     }
   433     if (mode) {
   434         *mode = SDL_CurrentDisplay.desktop_mode;
   435     }
   436     return 0;
   437 }
   438 
   439 int
   440 SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
   441 {
   442     if (!_this) {
   443         SDL_UninitializedVideo();
   444         return -1;
   445     }
   446     if (mode) {
   447         *mode = SDL_CurrentDisplay.current_mode;
   448     }
   449     return 0;
   450 }
   451 
   452 SDL_DisplayMode *
   453 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   454                           SDL_DisplayMode * closest)
   455 {
   456     Uint32 target_format;
   457     int target_refresh_rate;
   458     int i;
   459     SDL_DisplayMode *current, *match;
   460 
   461     if (!_this || !mode || !closest) {
   462         return NULL;
   463     }
   464 
   465     /* Default to the desktop format */
   466     if (mode->format) {
   467         target_format = mode->format;
   468     } else {
   469         target_format = SDL_CurrentDisplay.desktop_mode.format;
   470     }
   471 
   472     /* Default to the desktop refresh rate */
   473     if (mode->refresh_rate) {
   474         target_refresh_rate = mode->refresh_rate;
   475     } else {
   476         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   477     }
   478 
   479     match = NULL;
   480     for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
   481         current = &SDL_CurrentDisplay.display_modes[i];
   482 
   483         if ((current->w && current->h) &&
   484             (current->w < mode->w || current->h < mode->h)) {
   485             /* Out of sorted modes large enough here */
   486             break;
   487         }
   488         if (!match || current->w < match->w || current->h < match->h) {
   489             match = current;
   490             continue;
   491         }
   492         if (current->format != match->format) {
   493             /* Sorted highest depth to lowest */
   494             if (current->format == target_format ||
   495                 (SDL_BITSPERPIXEL(current->format) >=
   496                  SDL_BITSPERPIXEL(target_format)
   497                  && SDL_PIXELTYPE(current->format) ==
   498                  SDL_PIXELTYPE(target_format))) {
   499                 match = current;
   500             }
   501             continue;
   502         }
   503         if (current->refresh_rate != match->refresh_rate) {
   504             /* Sorted highest refresh to lowest */
   505             if (current->refresh_rate >= target_refresh_rate) {
   506                 match = current;
   507             }
   508         }
   509     }
   510     if (match) {
   511         if (match->format) {
   512             closest->format = match->format;
   513         } else {
   514             closest->format = mode->format;
   515         }
   516         if (match->w && match->h) {
   517             closest->w = match->w;
   518             closest->h = match->h;
   519         } else {
   520             closest->w = mode->w;
   521             closest->h = mode->h;
   522         }
   523         if (match->refresh_rate) {
   524             closest->refresh_rate = match->refresh_rate;
   525         } else {
   526             closest->refresh_rate = mode->refresh_rate;
   527         }
   528         closest->driverdata = match->driverdata;
   529 
   530         /* Pick some reasonable defaults if the app and driver don't care */
   531         if (!closest->format) {
   532             closest->format = SDL_PIXELFORMAT_RGB888;
   533         }
   534         if (!closest->w) {
   535             closest->w = 640;
   536         }
   537         if (!closest->h) {
   538             closest->h = 480;
   539         }
   540         return closest;
   541     }
   542     return NULL;
   543 }
   544 
   545 int
   546 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   547 {
   548     SDL_VideoDisplay *display;
   549     SDL_DisplayMode display_mode;
   550     SDL_DisplayMode current_mode;
   551     int i, ncolors;
   552 
   553     if (!_this) {
   554         SDL_UninitializedVideo();
   555         return -1;
   556     }
   557 
   558     display = &SDL_CurrentDisplay;
   559     if (!mode) {
   560         mode = &display->desktop_mode;
   561     }
   562     display_mode = *mode;
   563 
   564     /* Default to the current mode */
   565     if (!display_mode.format) {
   566         display_mode.format = display->current_mode.format;
   567     }
   568     if (!display_mode.w) {
   569         display_mode.w = display->current_mode.w;
   570     }
   571     if (!display_mode.h) {
   572         display_mode.h = display->current_mode.h;
   573     }
   574     if (!display_mode.refresh_rate) {
   575         display_mode.refresh_rate = display->current_mode.refresh_rate;
   576     }
   577 
   578     /* Get a good video mode, the closest one possible */
   579     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   580         SDL_SetError("No video mode large enough for %dx%d",
   581                      display_mode.w, display_mode.h);
   582         return -1;
   583     }
   584 
   585     /* See if there's anything left to do */
   586     SDL_GetCurrentDisplayMode(&current_mode);
   587     if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
   588         return 0;
   589     }
   590 
   591     /* Actually change the display mode */
   592     if (_this->SetDisplayMode(_this, &display_mode) < 0) {
   593         return -1;
   594     }
   595     display->current_mode = display_mode;
   596 
   597     /* Set up a palette, if necessary */
   598     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   599         ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   600     } else {
   601         ncolors = 0;
   602     }
   603     if ((!ncolors && display->palette) || (ncolors && !display->palette)
   604         || (ncolors && ncolors != display->palette->ncolors)) {
   605         if (display->palette) {
   606             SDL_FreePalette(display->palette);
   607             display->palette = NULL;
   608         }
   609         if (ncolors) {
   610             display->palette = SDL_AllocPalette(ncolors);
   611             if (!display->palette) {
   612                 return -1;
   613             }
   614             SDL_DitherColors(display->palette->colors,
   615                              SDL_BITSPERPIXEL(display_mode.format));
   616         }
   617     }
   618 
   619     /* Move any fullscreen windows into position */
   620     for (i = 0; i < display->num_windows; ++i) {
   621         SDL_Window *window = &display->windows[i];
   622         if (FULLSCREEN_VISIBLE(window)) {
   623             SDL_SetWindowPosition(window->id, SDL_WINDOWPOS_CENTERED,
   624                                   SDL_WINDOWPOS_CENTERED);
   625         }
   626     }
   627 
   628     return 0;
   629 }
   630 
   631 int
   632 SDL_SetFullscreenDisplayMode(const SDL_DisplayMode * mode)
   633 {
   634     SDL_VideoDisplay *display;
   635     SDL_DisplayMode fullscreen_mode;
   636     int i;
   637 
   638     if (!_this) {
   639         SDL_UninitializedVideo();
   640         return -1;
   641     }
   642 
   643     display = &SDL_CurrentDisplay;
   644     if (!mode) {
   645         mode = &display->desktop_mode;
   646     }
   647 
   648     SDL_GetClosestDisplayMode(mode, &fullscreen_mode);
   649     if (SDL_memcmp
   650         (&fullscreen_mode, &display->fullscreen_mode,
   651          sizeof(fullscreen_mode)) == 0) {
   652         /* Nothing to do... */
   653         return 0;
   654     }
   655     display->fullscreen_mode = fullscreen_mode;
   656 
   657     /* Actually set the mode if we have a fullscreen window visible */
   658     for (i = 0; i < display->num_windows; ++i) {
   659         SDL_Window *window = &display->windows[i];
   660         if (FULLSCREEN_VISIBLE(window)) {
   661             if (SDL_SetDisplayMode(&display->fullscreen_mode) < 0) {
   662                 return -1;
   663             }
   664         }
   665         if (window->flags & SDL_WINDOW_FULLSCREEN) {
   666             SDL_OnWindowResized(window);
   667         }
   668     }
   669     return 0;
   670 }
   671 
   672 int
   673 SDL_GetFullscreenDisplayMode(SDL_DisplayMode * mode)
   674 {
   675     if (!_this) {
   676         SDL_UninitializedVideo();
   677         return -1;
   678     }
   679     if (mode) {
   680         *mode = SDL_CurrentDisplay.fullscreen_mode;
   681     }
   682     return 0;
   683 }
   684 
   685 int
   686 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   687 {
   688     SDL_Palette *palette;
   689     int status = 0;
   690 
   691     if (!_this) {
   692         SDL_UninitializedVideo();
   693         return -1;
   694     }
   695     palette = SDL_CurrentDisplay.palette;
   696     if (!palette) {
   697         SDL_SetError("Display mode does not have a palette");
   698         return -1;
   699     }
   700 
   701     status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
   702 
   703     if (_this->SetDisplayPalette) {
   704         if (_this->SetDisplayPalette(_this, palette) < 0) {
   705             status = -1;
   706         }
   707     }
   708     return status;
   709 }
   710 
   711 int
   712 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   713 {
   714     SDL_Palette *palette;
   715 
   716     if (!_this) {
   717         SDL_UninitializedVideo();
   718         return -1;
   719     }
   720 
   721     palette = SDL_CurrentDisplay.palette;
   722     if (!palette->ncolors) {
   723         SDL_SetError("Display mode does not have a palette");
   724         return -1;
   725     }
   726 
   727     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   728         SDL_SetError("Palette indices are out of range");
   729         return -1;
   730     }
   731 
   732     SDL_memcpy(colors, &palette->colors[firstcolor],
   733                ncolors * sizeof(*colors));
   734     return 0;
   735 }
   736 
   737 SDL_WindowID
   738 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   739 {
   740     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   741                                   SDL_WINDOW_OPENGL |
   742                                   SDL_WINDOW_BORDERLESS |
   743                                   SDL_WINDOW_RESIZABLE);
   744     SDL_VideoDisplay *display;
   745     SDL_Window window;
   746     int num_windows;
   747     SDL_Window *windows;
   748 
   749     if (!_this) {
   750         SDL_UninitializedVideo();
   751         return 0;
   752     }
   753 
   754     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   755         SDL_SetError("No OpenGL support in video driver");
   756         return 0;
   757     }
   758 
   759     /* Fullscreen windows don't have any window decorations */
   760     if (flags & SDL_WINDOW_FULLSCREEN) {
   761         flags |= SDL_WINDOW_BORDERLESS;
   762         flags &= ~SDL_WINDOW_RESIZABLE;
   763     }
   764 
   765     SDL_zero(window);
   766     window.id = _this->next_object_id++;
   767     window.x = x;
   768     window.y = y;
   769     window.w = w;
   770     window.h = h;
   771     window.flags = (flags & allowed_flags);
   772     window.display = _this->current_display;
   773 
   774     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   775         return 0;
   776     }
   777 
   778     display = &SDL_CurrentDisplay;
   779     num_windows = display->num_windows;
   780     windows =
   781         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   782     if (!windows) {
   783         if (_this->DestroyWindow) {
   784             _this->DestroyWindow(_this, &window);
   785         }
   786         return 0;
   787     }
   788     windows[num_windows] = window;
   789     display->windows = windows;
   790     display->num_windows++;
   791 
   792     if (title) {
   793         SDL_SetWindowTitle(window.id, title);
   794     }
   795     if (flags & SDL_WINDOW_MAXIMIZED) {
   796         SDL_MaximizeWindow(window.id);
   797     }
   798     if (flags & SDL_WINDOW_MINIMIZED) {
   799         SDL_MinimizeWindow(window.id);
   800     }
   801     if (flags & SDL_WINDOW_SHOWN) {
   802         SDL_ShowWindow(window.id);
   803     }
   804     if (flags & SDL_WINDOW_INPUT_GRABBED) {
   805         SDL_SetWindowGrab(window.id, 1);
   806     }
   807     return window.id;
   808 }
   809 
   810 SDL_WindowID
   811 SDL_CreateWindowFrom(const void *data)
   812 {
   813     SDL_VideoDisplay *display;
   814     SDL_Window window;
   815     int num_windows;
   816     SDL_Window *windows;
   817 
   818     if (!_this) {
   819         SDL_UninitializedVideo();
   820         return (0);
   821     }
   822 
   823     SDL_zero(window);
   824     window.id = _this->next_object_id++;
   825     window.display = _this->current_display;
   826 
   827     if (!_this->CreateWindowFrom ||
   828         _this->CreateWindowFrom(_this, &window, data) < 0) {
   829         return 0;
   830     }
   831 
   832     display = &SDL_CurrentDisplay;
   833     num_windows = display->num_windows;
   834     windows =
   835         SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
   836     if (!windows) {
   837         if (_this->DestroyWindow) {
   838             _this->DestroyWindow(_this, &window);
   839         }
   840         if (window.title) {
   841             SDL_free(window.title);
   842         }
   843         return 0;
   844     }
   845     windows[num_windows] = window;
   846     display->windows = windows;
   847     display->num_windows++;
   848 
   849     return window.id;
   850 }
   851 
   852 int
   853 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
   854 {
   855     char *title = window->title;
   856 
   857     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
   858         SDL_SetError("No OpenGL support in video driver");
   859         return -1;
   860     }
   861 
   862     if (_this->DestroyWindow) {
   863         _this->DestroyWindow(_this, window);
   864     }
   865 
   866     window->title = NULL;
   867     window->flags =
   868         (flags &
   869          ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
   870            SDL_WINDOW_INPUT_GRABBED));
   871 
   872     if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
   873         return -1;
   874     }
   875 
   876     if (title) {
   877         SDL_SetWindowTitle(window->id, title);
   878         SDL_free(title);
   879     }
   880     if (flags & SDL_WINDOW_MAXIMIZED) {
   881         SDL_MaximizeWindow(window->id);
   882     }
   883     if (flags & SDL_WINDOW_MINIMIZED) {
   884         SDL_MinimizeWindow(window->id);
   885     }
   886     if (flags & SDL_WINDOW_SHOWN) {
   887         SDL_ShowWindow(window->id);
   888     }
   889     if (flags & SDL_WINDOW_INPUT_GRABBED) {
   890         SDL_SetWindowGrab(window->id, 1);
   891     }
   892     return 0;
   893 }
   894 
   895 SDL_Window *
   896 SDL_GetWindowFromID(SDL_WindowID windowID)
   897 {
   898     int i, j;
   899 
   900     if (!_this) {
   901         SDL_UninitializedVideo();
   902         return NULL;
   903     }
   904 
   905     for (i = 0; i < _this->num_displays; ++i) {
   906         SDL_VideoDisplay *display = &_this->displays[i];
   907         for (j = 0; j < display->num_windows; ++j) {
   908             SDL_Window *window = &display->windows[j];
   909             if (window->id == windowID) {
   910                 return window;
   911             }
   912         }
   913     }
   914     return NULL;
   915 }
   916 
   917 SDL_VideoDisplay *
   918 SDL_GetDisplayFromWindow(SDL_Window * window)
   919 {
   920     if (!_this) {
   921         SDL_UninitializedVideo();
   922         return NULL;
   923     }
   924     if (!window) {
   925         return NULL;
   926     }
   927     return &_this->displays[window->display];
   928 }
   929 
   930 Uint32
   931 SDL_GetWindowFlags(SDL_WindowID windowID)
   932 {
   933     SDL_Window *window = SDL_GetWindowFromID(windowID);
   934 
   935     if (!window) {
   936         return 0;
   937     }
   938     return window->flags;
   939 }
   940 
   941 void
   942 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   943 {
   944     SDL_Window *window = SDL_GetWindowFromID(windowID);
   945     const char *last_title;
   946 
   947     if (!window || title == window->title) {
   948         return;
   949     }
   950 
   951     if (window->title) {
   952         SDL_free(window->title);
   953     }
   954     if (title) {
   955         window->title = SDL_strdup(title);
   956     } else {
   957         window->title = NULL;
   958     }
   959 
   960     if (_this->SetWindowTitle) {
   961         _this->SetWindowTitle(_this, window);
   962     }
   963 }
   964 
   965 const char *
   966 SDL_GetWindowTitle(SDL_WindowID windowID)
   967 {
   968     SDL_Window *window = SDL_GetWindowFromID(windowID);
   969 
   970     if (!window) {
   971         return NULL;
   972     }
   973     return window->title;
   974 }
   975 
   976 void
   977 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   978 {
   979     SDL_Window *window = SDL_GetWindowFromID(windowID);
   980 
   981     if (!window) {
   982         return;
   983     }
   984     window->userdata = userdata;
   985 }
   986 
   987 void *
   988 SDL_GetWindowData(SDL_WindowID windowID)
   989 {
   990     SDL_Window *window = SDL_GetWindowFromID(windowID);
   991 
   992     if (!window) {
   993         return NULL;
   994     }
   995     return window->userdata;
   996 }
   997 
   998 void
   999 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
  1000 {
  1001     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1002     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1003 
  1004     if (!window) {
  1005         return;
  1006     }
  1007 
  1008     if (x == SDL_WINDOWPOS_CENTERED) {
  1009         window->x = (display->current_mode.w - window->w) / 2;
  1010     } else if (x != SDL_WINDOWPOS_UNDEFINED) {
  1011         window->x = x;
  1012     }
  1013     if (y == SDL_WINDOWPOS_CENTERED) {
  1014         window->y = (display->current_mode.h - window->h) / 2;
  1015     } else if (y != SDL_WINDOWPOS_UNDEFINED) {
  1016         window->y = y;
  1017     }
  1018 
  1019     if (_this->SetWindowPosition) {
  1020         _this->SetWindowPosition(_this, window);
  1021     }
  1022 }
  1023 
  1024 void
  1025 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
  1026 {
  1027     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1028 
  1029     if (!window) {
  1030         return;
  1031     }
  1032     if (x) {
  1033         *x = window->x;
  1034     }
  1035     if (y) {
  1036         *y = window->y;
  1037     }
  1038 }
  1039 
  1040 void
  1041 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
  1042 {
  1043     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1044 
  1045     if (!window) {
  1046         return;
  1047     }
  1048 
  1049     window->w = w;
  1050     window->h = h;
  1051 
  1052     if (_this->SetWindowSize) {
  1053         _this->SetWindowSize(_this, window);
  1054     }
  1055 }
  1056 
  1057 void
  1058 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
  1059 {
  1060     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1061 
  1062     if (!window) {
  1063         return;
  1064     }
  1065     if (w) {
  1066         *w = window->w;
  1067     }
  1068     if (h) {
  1069         *h = window->h;
  1070     }
  1071 }
  1072 
  1073 void
  1074 SDL_ShowWindow(SDL_WindowID windowID)
  1075 {
  1076     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1077 
  1078     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1079         return;
  1080     }
  1081 
  1082     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1083 
  1084     if (_this->ShowWindow) {
  1085         _this->ShowWindow(_this, window);
  1086     }
  1087 }
  1088 
  1089 void
  1090 SDL_HideWindow(SDL_WindowID windowID)
  1091 {
  1092     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1093 
  1094     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1095         return;
  1096     }
  1097 
  1098     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1099 
  1100     if (_this->HideWindow) {
  1101         _this->HideWindow(_this, window);
  1102     }
  1103 }
  1104 
  1105 void
  1106 SDL_RaiseWindow(SDL_WindowID windowID)
  1107 {
  1108     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1109 
  1110     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1111         return;
  1112     }
  1113 
  1114     if (_this->RaiseWindow) {
  1115         _this->RaiseWindow(_this, window);
  1116     }
  1117 }
  1118 
  1119 void
  1120 SDL_MaximizeWindow(SDL_WindowID windowID)
  1121 {
  1122     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1123 
  1124     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1125         return;
  1126     }
  1127 
  1128     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1129 
  1130     if (_this->MaximizeWindow) {
  1131         _this->MaximizeWindow(_this, window);
  1132     }
  1133 }
  1134 
  1135 void
  1136 SDL_MinimizeWindow(SDL_WindowID windowID)
  1137 {
  1138     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1139 
  1140     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1141         return;
  1142     }
  1143 
  1144     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1145 
  1146     if (_this->MinimizeWindow) {
  1147         _this->MinimizeWindow(_this, window);
  1148     }
  1149 }
  1150 
  1151 void
  1152 SDL_RestoreWindow(SDL_WindowID windowID)
  1153 {
  1154     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1155 
  1156     if (!window
  1157         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1158         return;
  1159     }
  1160 
  1161     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1162 
  1163     if (_this->RestoreWindow) {
  1164         _this->RestoreWindow(_this, window);
  1165     }
  1166 }
  1167 
  1168 int
  1169 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1170 {
  1171     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1172 
  1173     if (!window) {
  1174         return -1;
  1175     }
  1176 
  1177     if (fullscreen) {
  1178         fullscreen = SDL_WINDOW_FULLSCREEN;
  1179     }
  1180     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1181         return 0;
  1182     }
  1183 
  1184     if (fullscreen) {
  1185         window->flags |= SDL_WINDOW_FULLSCREEN;
  1186 
  1187         if (FULLSCREEN_VISIBLE(window)) {
  1188             SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1189 
  1190             /* Hide any other fullscreen windows */
  1191             int i;
  1192             for (i = 0; i < display->num_windows; ++i) {
  1193                 SDL_Window *other = &display->windows[i];
  1194                 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
  1195                     SDL_MinimizeWindow(other->id);
  1196                 }
  1197             }
  1198 
  1199             SDL_SetDisplayMode(&display->fullscreen_mode);
  1200         }
  1201     } else {
  1202         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1203 
  1204         if (FULLSCREEN_VISIBLE(window)) {
  1205             SDL_SetDisplayMode(NULL);
  1206         }
  1207     }
  1208     return 0;
  1209 }
  1210 
  1211 void
  1212 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1213 {
  1214     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1215 
  1216     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1217         return;
  1218     }
  1219 
  1220     if (mode) {
  1221         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1222     } else {
  1223         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1224     }
  1225 
  1226     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1227         _this->SetWindowGrab(_this, window);
  1228     }
  1229 }
  1230 
  1231 int
  1232 SDL_GetWindowGrab(SDL_WindowID windowID)
  1233 {
  1234     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1235 
  1236     if (!window) {
  1237         return 0;
  1238     }
  1239 
  1240     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1241 }
  1242 
  1243 void
  1244 SDL_OnWindowShown(SDL_Window * window)
  1245 {
  1246 }
  1247 
  1248 void
  1249 SDL_OnWindowHidden(SDL_Window * window)
  1250 {
  1251 }
  1252 
  1253 void
  1254 SDL_OnWindowResized(SDL_Window * window)
  1255 {
  1256     SDL_Renderer *renderer = window->renderer;
  1257 
  1258     if (renderer && renderer->DisplayModeChanged) {
  1259         renderer->DisplayModeChanged(renderer);
  1260     }
  1261 }
  1262 
  1263 void
  1264 SDL_OnWindowFocusGained(SDL_Window * window)
  1265 {
  1266     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1267 
  1268     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1269         SDL_SetDisplayMode(&display->fullscreen_mode);
  1270     }
  1271     if (display->gamma && _this->SetDisplayGammaRamp) {
  1272         _this->SetDisplayGammaRamp(_this, display->gamma);
  1273     }
  1274     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1275         _this->SetWindowGrab(_this, window);
  1276     }
  1277 }
  1278 
  1279 void
  1280 SDL_OnWindowFocusLost(SDL_Window * window)
  1281 {
  1282     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1283 
  1284     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1285         SDL_MinimizeWindow(window->id);
  1286         SDL_SetDisplayMode(NULL);
  1287     }
  1288     if (display->gamma && _this->SetDisplayGammaRamp) {
  1289         _this->SetDisplayGammaRamp(_this, display->saved_gamma);
  1290     }
  1291     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1292         _this->SetWindowGrab(_this, window);
  1293     }
  1294 }
  1295 
  1296 SDL_WindowID
  1297 SDL_GetFocusWindow(void)
  1298 {
  1299     SDL_VideoDisplay *display;
  1300     int i;
  1301 
  1302     if (!_this) {
  1303         return 0;
  1304     }
  1305 
  1306     display = &SDL_CurrentDisplay;
  1307     for (i = 0; i < display->num_windows; ++i) {
  1308         SDL_Window *window = &display->windows[i];
  1309 
  1310         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1311             return window->id;
  1312         }
  1313     }
  1314     return 0;
  1315 }
  1316 
  1317 void
  1318 SDL_DestroyWindow(SDL_WindowID windowID)
  1319 {
  1320     int i, j;
  1321 
  1322     if (!_this) {
  1323         return;
  1324     }
  1325 
  1326     /* Restore video mode, etc. */
  1327     SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  1328 
  1329     for (i = 0; i < _this->num_displays; ++i) {
  1330         SDL_VideoDisplay *display = &_this->displays[i];
  1331         for (j = 0; j < display->num_windows; ++j) {
  1332             SDL_Window *window = &display->windows[j];
  1333             if (window->id != windowID) {
  1334                 continue;
  1335             }
  1336             if (window->renderer) {
  1337                 SDL_DestroyRenderer(window->id);
  1338             }
  1339             if (_this->DestroyWindow) {
  1340                 _this->DestroyWindow(_this, window);
  1341             }
  1342             if (window->title) {
  1343                 SDL_free(window->title);
  1344             }
  1345             if (j != display->num_windows - 1) {
  1346                 SDL_memcpy(&display->windows[i],
  1347                            &display->windows[i + 1],
  1348                            (display->num_windows - i - 1) * sizeof(*window));
  1349             }
  1350             --display->num_windows;
  1351             return;
  1352         }
  1353     }
  1354 }
  1355 
  1356 void
  1357 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1358 {
  1359     SDL_VideoDisplay *display = &_this->displays[displayIndex];
  1360     SDL_RenderDriver *render_drivers;
  1361 
  1362     render_drivers =
  1363         SDL_realloc(display->render_drivers,
  1364                     (display->num_render_drivers +
  1365                      1) * sizeof(*render_drivers));
  1366     if (render_drivers) {
  1367         render_drivers[display->num_render_drivers] = *driver;
  1368         display->render_drivers = render_drivers;
  1369         display->num_render_drivers++;
  1370     }
  1371 }
  1372 
  1373 int
  1374 SDL_GetNumRenderDrivers(void)
  1375 {
  1376     if (_this) {
  1377         return SDL_CurrentDisplay.num_render_drivers;
  1378     }
  1379     return 0;
  1380 }
  1381 
  1382 int
  1383 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1384 {
  1385     if (!_this) {
  1386         SDL_UninitializedVideo();
  1387         return -1;
  1388     }
  1389 
  1390     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1391         SDL_SetError("index must be in the range of 0 - %d",
  1392                      SDL_GetNumRenderDrivers() - 1);
  1393         return -1;
  1394     }
  1395     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1396     return 0;
  1397 }
  1398 
  1399 int
  1400 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1401 {
  1402     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1403 
  1404     if (!window) {
  1405         return 0;
  1406     }
  1407 
  1408     if (index < 0) {
  1409         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1410         int n = SDL_GetNumRenderDrivers();
  1411         for (index = 0; index < n; ++index) {
  1412             SDL_RenderDriver *driver =
  1413                 &SDL_CurrentDisplay.render_drivers[index];
  1414 
  1415             if (override) {
  1416                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1417                     break;
  1418                 }
  1419             } else {
  1420                 if ((driver->info.flags & flags) == flags) {
  1421                     break;
  1422                 }
  1423             }
  1424         }
  1425         if (index == n) {
  1426             SDL_SetError("Couldn't find matching render driver");
  1427             return -1;
  1428         }
  1429     }
  1430 
  1431     if (index >= SDL_GetNumRenderDrivers()) {
  1432         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1433                      SDL_GetNumRenderDrivers() - 1);
  1434         return -1;
  1435     }
  1436 
  1437     /* Free any existing renderer */
  1438     SDL_DestroyRenderer(windowID);
  1439 
  1440     /* Create a new renderer instance */
  1441     window->renderer =
  1442         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1443                                                                 flags);
  1444     SDL_SelectRenderer(window->id);
  1445 
  1446     return 0;
  1447 }
  1448 
  1449 int
  1450 SDL_SelectRenderer(SDL_WindowID windowID)
  1451 {
  1452     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1453     SDL_Renderer *renderer;
  1454 
  1455     if (!window || !window->renderer) {
  1456         return -1;
  1457     }
  1458     renderer = window->renderer;
  1459     if (renderer && renderer->ActivateRenderer) {
  1460         if (renderer->ActivateRenderer(renderer) < 0) {
  1461             return -1;
  1462         }
  1463     }
  1464     SDL_CurrentDisplay.current_renderer = renderer;
  1465     return 0;
  1466 }
  1467 
  1468 int
  1469 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1470 {
  1471     if (!_this) {
  1472         SDL_UninitializedVideo();
  1473         return -1;
  1474     }
  1475 
  1476     if (!SDL_CurrentDisplay.current_renderer) {
  1477         SDL_SetError("There is no current renderer");
  1478         return -1;
  1479     }
  1480     *info = SDL_CurrentDisplay.current_renderer->info;
  1481     return 0;
  1482 }
  1483 
  1484 SDL_TextureID
  1485 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1486 {
  1487     int hash;
  1488     SDL_Renderer *renderer;
  1489     SDL_Texture *texture;
  1490 
  1491     if (!_this) {
  1492         SDL_UninitializedVideo();
  1493         return 0;
  1494     }
  1495 
  1496     renderer = SDL_CurrentDisplay.current_renderer;
  1497     if (!renderer || !renderer->CreateTexture) {
  1498         return 0;
  1499     }
  1500 
  1501     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1502     if (!texture) {
  1503         SDL_OutOfMemory();
  1504         return 0;
  1505     }
  1506 
  1507     texture->id = _this->next_object_id++;
  1508     texture->format = format;
  1509     texture->access = access;
  1510     texture->w = w;
  1511     texture->h = h;
  1512     texture->r = 255;
  1513     texture->g = 255;
  1514     texture->b = 255;
  1515     texture->a = 255;
  1516     texture->renderer = renderer;
  1517 
  1518     if (renderer->CreateTexture(renderer, texture) < 0) {
  1519         if (renderer->DestroyTexture) {
  1520             renderer->DestroyTexture(renderer, texture);
  1521         }
  1522         SDL_free(texture);
  1523         return 0;
  1524     }
  1525 
  1526     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1527     texture->next = SDL_CurrentDisplay.textures[hash];
  1528     SDL_CurrentDisplay.textures[hash] = texture;
  1529 
  1530     return texture->id;
  1531 }
  1532 
  1533 SDL_TextureID
  1534 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1535 {
  1536     SDL_TextureID textureID;
  1537     Uint32 surface_flags = surface->flags;
  1538     SDL_PixelFormat *fmt = surface->format;
  1539     Uint8 alpha;
  1540     SDL_Rect bounds;
  1541     SDL_Surface dst;
  1542     int bpp;
  1543     Uint32 Rmask, Gmask, Bmask, Amask;
  1544 
  1545     if (!surface) {
  1546         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1547         return 0;
  1548     }
  1549 
  1550     if (format) {
  1551         if (!SDL_PixelFormatEnumToMasks
  1552             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1553             SDL_SetError("Unknown pixel format");
  1554             return 0;
  1555         }
  1556     } else {
  1557         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1558             bpp = fmt->BitsPerPixel;
  1559             Rmask = fmt->Rmask;
  1560             Gmask = fmt->Gmask;
  1561             Bmask = fmt->Bmask;
  1562             Amask = fmt->Amask;
  1563         } else {
  1564             /* Need a format with alpha */
  1565             bpp = 32;
  1566             Rmask = 0x00FF0000;
  1567             Gmask = 0x0000FF00;
  1568             Bmask = 0x000000FF;
  1569             Amask = 0xFF000000;
  1570         }
  1571         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1572         if (!format) {
  1573             SDL_SetError("Unknown pixel format");
  1574             return 0;
  1575         }
  1576     }
  1577 
  1578     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1579     if (!textureID) {
  1580         return 0;
  1581     }
  1582 
  1583     /* Set up a destination surface for the texture update */
  1584     SDL_zero(dst);
  1585     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1586     if (!dst.format) {
  1587         SDL_DestroyTexture(textureID);
  1588         return 0;
  1589     }
  1590     dst.w = surface->w;
  1591     dst.h = surface->h;
  1592     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1593         dst.flags |= SDL_PREALLOC;
  1594     } else {
  1595         dst.pitch = SDL_CalculatePitch(&dst);
  1596         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1597         if (!dst.pixels) {
  1598             SDL_DestroyTexture(textureID);
  1599             SDL_FreeFormat(dst.format);
  1600             SDL_OutOfMemory();
  1601             return 0;
  1602         }
  1603     }
  1604 
  1605     /* Copy the palette if any */
  1606     if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1607         if (fmt->palette) {
  1608             SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1609                                   fmt->palette->ncolors);
  1610             SDL_SetSurfacePalette(&dst, fmt->palette);
  1611         } else {
  1612             dst.format->palette =
  1613                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1614             if (!dst.format->palette) {
  1615                 SDL_DestroyTexture(textureID);
  1616                 SDL_FreeFormat(dst.format);
  1617                 return 0;
  1618             }
  1619             SDL_DitherColors(dst.format->palette->colors,
  1620                              SDL_BITSPERPIXEL(format));
  1621         }
  1622     }
  1623 
  1624     /* Make the texture transparent if the surface has colorkey */
  1625     if (surface_flags & SDL_SRCCOLORKEY) {
  1626         int row;
  1627         int length = dst.w * dst.format->BytesPerPixel;
  1628         Uint8 *p = (Uint8 *) dst.pixels;
  1629         for (row = 0; row < dst.h; ++row) {
  1630             SDL_memset(p, 0, length);
  1631             p += dst.pitch;
  1632         }
  1633     }
  1634 
  1635     /* Copy over the alpha channel */
  1636     if (surface_flags & SDL_SRCALPHA) {
  1637         if (fmt->Amask) {
  1638             surface->flags &= ~SDL_SRCALPHA;
  1639         } else {
  1640             /* FIXME: Need to make sure the texture has an alpha channel
  1641              *        and copy 'alpha' into the texture alpha channel.
  1642              */
  1643             alpha = surface->format->alpha;
  1644             SDL_SetAlpha(surface, 0, 0);
  1645         }
  1646     }
  1647 
  1648     /* Copy over the image data */
  1649     bounds.x = 0;
  1650     bounds.y = 0;
  1651     bounds.w = surface->w;
  1652     bounds.h = surface->h;
  1653     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1654 
  1655     /* Clean up the original surface */
  1656     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1657         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1658         if (fmt->Amask) {
  1659             surface->flags |= SDL_SRCALPHA;
  1660         } else {
  1661             SDL_SetAlpha(surface, aflags, alpha);
  1662         }
  1663     }
  1664 
  1665     /* Update the texture */
  1666     if (dst.flags & SDL_PREALLOC) {
  1667         SDL_UnlockTexture(textureID);
  1668     } else {
  1669         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1670         SDL_free(dst.pixels);
  1671     }
  1672     SDL_FreeFormat(dst.format);
  1673 
  1674     return textureID;
  1675 }
  1676 
  1677 static __inline__ SDL_Texture *
  1678 SDL_GetTextureFromID(SDL_TextureID textureID)
  1679 {
  1680     int hash;
  1681     SDL_Texture *texture;
  1682 
  1683     if (!_this) {
  1684         return NULL;
  1685     }
  1686 
  1687     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1688     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1689          texture = texture->next) {
  1690         if (texture->id == textureID) {
  1691             return texture;
  1692         }
  1693     }
  1694     return NULL;
  1695 }
  1696 
  1697 int
  1698 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1699                  int *w, int *h)
  1700 {
  1701     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1702 
  1703     if (!texture) {
  1704         return -1;
  1705     }
  1706 
  1707     if (format) {
  1708         *format = texture->format;
  1709     }
  1710     if (access) {
  1711         *access = texture->access;
  1712     }
  1713     if (w) {
  1714         *w = texture->w;
  1715     }
  1716     if (h) {
  1717         *h = texture->h;
  1718     }
  1719     return 0;
  1720 }
  1721 
  1722 int
  1723 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1724 {
  1725     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1726     SDL_Renderer *renderer;
  1727 
  1728     if (!texture) {
  1729         return -1;
  1730     }
  1731 
  1732     renderer = texture->renderer;
  1733     if (!renderer->QueryTexturePixels) {
  1734         return -1;
  1735     }
  1736     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1737 }
  1738 
  1739 int
  1740 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1741                       int firstcolor, int ncolors)
  1742 {
  1743     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1744     SDL_Renderer *renderer;
  1745 
  1746     if (!texture) {
  1747         return -1;
  1748     }
  1749 
  1750     renderer = texture->renderer;
  1751     if (!renderer->SetTexturePalette) {
  1752         return -1;
  1753     }
  1754     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1755                                        ncolors);
  1756 }
  1757 
  1758 int
  1759 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1760                       int firstcolor, int ncolors)
  1761 {
  1762     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1763     SDL_Renderer *renderer;
  1764 
  1765     if (!texture) {
  1766         return -1;
  1767     }
  1768 
  1769     renderer = texture->renderer;
  1770     if (!renderer->GetTexturePalette) {
  1771         return -1;
  1772     }
  1773     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1774                                        ncolors);
  1775 }
  1776 
  1777 int
  1778 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  1779 {
  1780     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1781     SDL_Renderer *renderer;
  1782 
  1783     if (!texture) {
  1784         return -1;
  1785     }
  1786 
  1787     renderer = texture->renderer;
  1788     if (!renderer->SetTextureColorMod) {
  1789         return -1;
  1790     }
  1791     if (r < 255 | g < 255 | b < 255) {
  1792         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1793     } else {
  1794         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1795     }
  1796     texture->r = r;
  1797     texture->g = g;
  1798     texture->b = b;
  1799     return renderer->SetTextureColorMod(renderer, texture);
  1800 }
  1801 
  1802 int
  1803 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  1804                        Uint8 * b)
  1805 {
  1806     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1807     SDL_Renderer *renderer;
  1808 
  1809     if (!texture) {
  1810         return -1;
  1811     }
  1812 
  1813     renderer = texture->renderer;
  1814     if (r) {
  1815         *r = texture->r;
  1816     }
  1817     if (g) {
  1818         *g = texture->g;
  1819     }
  1820     if (b) {
  1821         *b = texture->b;
  1822     }
  1823     return 0;
  1824 }
  1825 
  1826 int
  1827 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  1828 {
  1829     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1830     SDL_Renderer *renderer;
  1831 
  1832     if (!texture) {
  1833         return -1;
  1834     }
  1835 
  1836     renderer = texture->renderer;
  1837     if (!renderer->SetTextureAlphaMod) {
  1838         return -1;
  1839     }
  1840     if (alpha < 255) {
  1841         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1842     } else {
  1843         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1844     }
  1845     texture->a = alpha;
  1846     return renderer->SetTextureAlphaMod(renderer, texture);
  1847 }
  1848 
  1849 int
  1850 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  1851 {
  1852     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1853     SDL_Renderer *renderer;
  1854 
  1855     if (!texture) {
  1856         return -1;
  1857     }
  1858 
  1859     if (alpha) {
  1860         *alpha = texture->a;
  1861     }
  1862     return 0;
  1863 }
  1864 
  1865 int
  1866 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  1867 {
  1868     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1869     SDL_Renderer *renderer;
  1870 
  1871     if (!texture) {
  1872         return -1;
  1873     }
  1874 
  1875     renderer = texture->renderer;
  1876     if (!renderer->SetTextureBlendMode) {
  1877         return -1;
  1878     }
  1879     texture->blendMode = blendMode;
  1880     return renderer->SetTextureBlendMode(renderer, texture);
  1881 }
  1882 
  1883 int
  1884 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  1885 {
  1886     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1887     SDL_Renderer *renderer;
  1888 
  1889     if (!texture) {
  1890         return -1;
  1891     }
  1892 
  1893     if (blendMode) {
  1894         *blendMode = texture->blendMode;
  1895     }
  1896     return 0;
  1897 }
  1898 
  1899 int
  1900 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  1901 {
  1902     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1903     SDL_Renderer *renderer;
  1904 
  1905     if (!texture) {
  1906         return -1;
  1907     }
  1908 
  1909     renderer = texture->renderer;
  1910     if (!renderer->SetTextureScaleMode) {
  1911         return -1;
  1912     }
  1913     texture->scaleMode = scaleMode;
  1914     return renderer->SetTextureScaleMode(renderer, texture);
  1915 }
  1916 
  1917 int
  1918 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  1919 {
  1920     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1921     SDL_Renderer *renderer;
  1922 
  1923     if (!texture) {
  1924         return -1;
  1925     }
  1926 
  1927     if (scaleMode) {
  1928         *scaleMode = texture->scaleMode;
  1929     }
  1930     return 0;
  1931 }
  1932 
  1933 int
  1934 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1935                   const void *pixels, int pitch)
  1936 {
  1937     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1938     SDL_Renderer *renderer;
  1939     SDL_Rect full_rect;
  1940 
  1941     if (!texture) {
  1942         return -1;
  1943     }
  1944 
  1945     renderer = texture->renderer;
  1946     if (!renderer->UpdateTexture) {
  1947         return -1;
  1948     }
  1949 
  1950     if (!rect) {
  1951         full_rect.x = 0;
  1952         full_rect.y = 0;
  1953         full_rect.w = texture->w;
  1954         full_rect.h = texture->h;
  1955         rect = &full_rect;
  1956     }
  1957 
  1958     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1959 }
  1960 
  1961 int
  1962 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1963                 void **pixels, int *pitch)
  1964 {
  1965     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1966     SDL_Renderer *renderer;
  1967     SDL_Rect full_rect;
  1968 
  1969     if (!texture) {
  1970         return -1;
  1971     }
  1972 
  1973     renderer = texture->renderer;
  1974     if (!renderer->LockTexture) {
  1975         return -1;
  1976     }
  1977 
  1978     if (!rect) {
  1979         full_rect.x = 0;
  1980         full_rect.y = 0;
  1981         full_rect.w = texture->w;
  1982         full_rect.h = texture->h;
  1983         rect = &full_rect;
  1984     }
  1985 
  1986     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1987                                  pitch);
  1988 }
  1989 
  1990 void
  1991 SDL_UnlockTexture(SDL_TextureID textureID)
  1992 {
  1993     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1994     SDL_Renderer *renderer;
  1995 
  1996     if (!texture) {
  1997         return;
  1998     }
  1999 
  2000     renderer = texture->renderer;
  2001     if (!renderer->UnlockTexture) {
  2002         return;
  2003     }
  2004     renderer->UnlockTexture(renderer, texture);
  2005 }
  2006 
  2007 void
  2008 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  2009                  const SDL_Rect * rects)
  2010 {
  2011     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2012     SDL_Renderer *renderer;
  2013 
  2014     if (!texture) {
  2015         return;
  2016     }
  2017 
  2018     renderer = texture->renderer;
  2019     if (!renderer->DirtyTexture) {
  2020         return;
  2021     }
  2022     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2023 }
  2024 
  2025 int
  2026 SDL_RenderFill(Uint8 r, Uint8 g, Uint8 b, Uint8 a, const SDL_Rect * rect)
  2027 {
  2028     SDL_Renderer *renderer;
  2029     SDL_Window *window;
  2030     SDL_Rect real_rect;
  2031 
  2032     if (!_this) {
  2033         SDL_UninitializedVideo();
  2034         return -1;
  2035     }
  2036 
  2037     renderer = SDL_CurrentDisplay.current_renderer;
  2038     if (!renderer || !renderer->RenderFill) {
  2039         return -1;
  2040     }
  2041 
  2042     window = SDL_GetWindowFromID(renderer->window);
  2043     real_rect.x = 0;
  2044     real_rect.y = 0;
  2045     real_rect.w = window->w;
  2046     real_rect.h = window->h;
  2047     if (rect) {
  2048         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2049             return 0;
  2050         }
  2051     }
  2052 
  2053     return renderer->RenderFill(renderer, r, g, b, a, &real_rect);
  2054 }
  2055 
  2056 int
  2057 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2058                const SDL_Rect * dstrect)
  2059 {
  2060     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2061     SDL_Renderer *renderer;
  2062     SDL_Window *window;
  2063     SDL_Rect real_srcrect;
  2064     SDL_Rect real_dstrect;
  2065 
  2066     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  2067         return -1;
  2068     }
  2069 
  2070     renderer = SDL_CurrentDisplay.current_renderer;
  2071     if (!renderer || !renderer->RenderCopy) {
  2072         return -1;
  2073     }
  2074 
  2075     window = SDL_GetWindowFromID(renderer->window);
  2076     if (srcrect) {
  2077         real_srcrect = *srcrect;
  2078     } else {
  2079         real_srcrect.x = 0;
  2080         real_srcrect.y = 0;
  2081         real_srcrect.w = texture->w;
  2082         real_srcrect.h = texture->h;
  2083     }
  2084     if (dstrect) {
  2085         real_dstrect = *dstrect;
  2086     } else {
  2087         real_dstrect.x = 0;
  2088         real_dstrect.y = 0;
  2089         real_dstrect.w = window->w;
  2090         real_dstrect.h = window->h;
  2091     }
  2092 
  2093     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2094                                 &real_dstrect);
  2095 }
  2096 
  2097 void
  2098 SDL_RenderPresent(void)
  2099 {
  2100     SDL_Renderer *renderer;
  2101 
  2102     if (!_this) {
  2103         SDL_UninitializedVideo();
  2104         return;
  2105     }
  2106 
  2107     renderer = SDL_CurrentDisplay.current_renderer;
  2108     if (!renderer || !renderer->RenderPresent) {
  2109         return;
  2110     }
  2111     renderer->RenderPresent(renderer);
  2112 }
  2113 
  2114 void
  2115 SDL_DestroyTexture(SDL_TextureID textureID)
  2116 {
  2117     int hash;
  2118     SDL_Texture *prev, *texture;
  2119     SDL_Renderer *renderer;
  2120 
  2121     if (!_this) {
  2122         SDL_UninitializedVideo();
  2123         return;
  2124     }
  2125 
  2126     /* Look up the texture in the hash table */
  2127     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2128     prev = NULL;
  2129     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2130          prev = texture, texture = texture->next) {
  2131         if (texture->id == textureID) {
  2132             break;
  2133         }
  2134     }
  2135     if (!texture) {
  2136         return;
  2137     }
  2138 
  2139     /* Unlink the texture from the list */
  2140     if (prev) {
  2141         prev->next = texture->next;
  2142     } else {
  2143         SDL_CurrentDisplay.textures[hash] = texture->next;
  2144     }
  2145 
  2146     /* Free the texture */
  2147     renderer = texture->renderer;
  2148     renderer->DestroyTexture(renderer, texture);
  2149     SDL_free(texture);
  2150 }
  2151 
  2152 void
  2153 SDL_DestroyRenderer(SDL_WindowID windowID)
  2154 {
  2155     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2156     SDL_Renderer *renderer;
  2157     int i;
  2158 
  2159     if (!window) {
  2160         return;
  2161     }
  2162 
  2163     renderer = window->renderer;
  2164     if (!renderer) {
  2165         return;
  2166     }
  2167 
  2168     /* Free existing textures for this renderer */
  2169     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2170         SDL_Texture *texture;
  2171         SDL_Texture *prev = NULL;
  2172         SDL_Texture *next;
  2173         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2174              texture = next) {
  2175             next = texture->next;
  2176             if (texture->renderer == renderer) {
  2177                 if (prev) {
  2178                     prev->next = next;
  2179                 } else {
  2180                     SDL_CurrentDisplay.textures[i] = next;
  2181                 }
  2182                 renderer->DestroyTexture(renderer, texture);
  2183                 SDL_free(texture);
  2184             } else {
  2185                 prev = texture;
  2186             }
  2187         }
  2188     }
  2189 
  2190     /* Free the renderer instance */
  2191     renderer->DestroyRenderer(renderer);
  2192 
  2193     /* Clear references */
  2194     window->renderer = NULL;
  2195     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2196         SDL_CurrentDisplay.current_renderer = NULL;
  2197     }
  2198 }
  2199 
  2200 void
  2201 SDL_VideoQuit(void)
  2202 {
  2203     int i, j;
  2204 
  2205     if (!_this) {
  2206         return;
  2207     }
  2208 
  2209     /* Halt event processing before doing anything else */
  2210     SDL_StopEventLoop();
  2211 
  2212     /* Clean up the system video */
  2213     for (i = _this->num_displays; i--;) {
  2214         SDL_VideoDisplay *display = &_this->displays[i];
  2215         for (j = display->num_windows; j--;) {
  2216             SDL_DestroyWindow(display->windows[i].id);
  2217         }
  2218         if (display->windows) {
  2219             SDL_free(display->windows);
  2220             display->windows = NULL;
  2221         }
  2222         display->num_windows = 0;
  2223     }
  2224     _this->VideoQuit(_this);
  2225 
  2226     for (i = _this->num_displays; i--;) {
  2227         SDL_VideoDisplay *display = &_this->displays[i];
  2228         for (j = display->num_display_modes; j--;) {
  2229             if (display->display_modes[j].driverdata) {
  2230                 SDL_free(display->display_modes[j].driverdata);
  2231                 display->display_modes[j].driverdata = NULL;
  2232             }
  2233         }
  2234         if (display->display_modes) {
  2235             SDL_free(display->display_modes);
  2236             display->display_modes = NULL;
  2237         }
  2238         if (display->desktop_mode.driverdata) {
  2239             SDL_free(display->desktop_mode.driverdata);
  2240             display->desktop_mode.driverdata = NULL;
  2241         }
  2242         if (display->palette) {
  2243             SDL_FreePalette(display->palette);
  2244             display->palette = NULL;
  2245         }
  2246         if (display->gamma) {
  2247             SDL_free(display->gamma);
  2248         }
  2249         if (display->driverdata) {
  2250             SDL_free(display->driverdata);
  2251         }
  2252     }
  2253     if (_this->displays) {
  2254         SDL_free(_this->displays);
  2255         _this->displays = NULL;
  2256     }
  2257     _this->free(_this);
  2258     _this = NULL;
  2259 }
  2260 
  2261 int
  2262 SDL_GL_LoadLibrary(const char *path)
  2263 {
  2264     int retval;
  2265 
  2266     if (!_this) {
  2267         SDL_UninitializedVideo();
  2268         return -1;
  2269     }
  2270 
  2271     if (_this->GL_LoadLibrary) {
  2272         retval = _this->GL_LoadLibrary(_this, path);
  2273     } else {
  2274         SDL_SetError("No dynamic GL support in video driver");
  2275         retval = -1;
  2276     }
  2277     return (retval);
  2278 }
  2279 
  2280 void *
  2281 SDL_GL_GetProcAddress(const char *proc)
  2282 {
  2283     void *func;
  2284 
  2285     if (!_this) {
  2286         SDL_UninitializedVideo();
  2287         return NULL;
  2288     }
  2289 
  2290     func = NULL;
  2291     if (_this->GL_GetProcAddress) {
  2292         if (_this->gl_config.driver_loaded) {
  2293             func = _this->GL_GetProcAddress(_this, proc);
  2294         } else {
  2295             SDL_SetError("No GL driver has been loaded");
  2296         }
  2297     } else {
  2298         SDL_SetError("No dynamic GL support in video driver");
  2299     }
  2300     return func;
  2301 }
  2302 
  2303 SDL_bool
  2304 SDL_GL_ExtensionSupported(const char *extension)
  2305 {
  2306 #if SDL_VIDEO_OPENGL
  2307     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2308     const char *extensions;
  2309     const char *start;
  2310     const char *where, *terminator;
  2311 
  2312     /* Extension names should not have spaces. */
  2313     where = SDL_strchr(extension, ' ');
  2314     if (where || *extension == '\0') {
  2315         return SDL_FALSE;
  2316     }
  2317 
  2318     /* See if there's an environment variable override */
  2319     start = SDL_getenv(extension);
  2320     if (start && *start == '0') {
  2321         return SDL_FALSE;
  2322     }
  2323 
  2324     /* Lookup the available extensions */
  2325     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2326     if (glGetStringFunc) {
  2327         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2328     } else {
  2329         extensions = NULL;
  2330     }
  2331     if (!extensions) {
  2332         return SDL_FALSE;
  2333     }
  2334 
  2335     /* It takes a bit of care to be fool-proof about parsing the
  2336      * OpenGL extensions string. Don't be fooled by sub-strings,
  2337      * etc. */
  2338 
  2339     start = extensions;
  2340 
  2341     for (;;) {
  2342         where = SDL_strstr(start, extension);
  2343         if (!where)
  2344             break;
  2345 
  2346         terminator = where + SDL_strlen(extension);
  2347         if (where == start || *(where - 1) == ' ')
  2348             if (*terminator == ' ' || *terminator == '\0')
  2349                 return SDL_TRUE;
  2350 
  2351         start = terminator;
  2352     }
  2353     return SDL_FALSE;
  2354 #else
  2355     return SDL_FALSE;
  2356 #endif
  2357 }
  2358 
  2359 int
  2360 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2361 {
  2362 #if SDL_VIDEO_OPENGL
  2363     int retval;
  2364 
  2365     if (!_this) {
  2366         SDL_UninitializedVideo();
  2367         return -1;
  2368     }
  2369 
  2370     retval = 0;
  2371     switch (attr) {
  2372     case SDL_GL_RED_SIZE:
  2373         _this->gl_config.red_size = value;
  2374         break;
  2375     case SDL_GL_GREEN_SIZE:
  2376         _this->gl_config.green_size = value;
  2377         break;
  2378     case SDL_GL_BLUE_SIZE:
  2379         _this->gl_config.blue_size = value;
  2380         break;
  2381     case SDL_GL_ALPHA_SIZE:
  2382         _this->gl_config.alpha_size = value;
  2383         break;
  2384     case SDL_GL_DOUBLEBUFFER:
  2385         _this->gl_config.double_buffer = value;
  2386         break;
  2387     case SDL_GL_BUFFER_SIZE:
  2388         _this->gl_config.buffer_size = value;
  2389         break;
  2390     case SDL_GL_DEPTH_SIZE:
  2391         _this->gl_config.depth_size = value;
  2392         break;
  2393     case SDL_GL_STENCIL_SIZE:
  2394         _this->gl_config.stencil_size = value;
  2395         break;
  2396     case SDL_GL_ACCUM_RED_SIZE:
  2397         _this->gl_config.accum_red_size = value;
  2398         break;
  2399     case SDL_GL_ACCUM_GREEN_SIZE:
  2400         _this->gl_config.accum_green_size = value;
  2401         break;
  2402     case SDL_GL_ACCUM_BLUE_SIZE:
  2403         _this->gl_config.accum_blue_size = value;
  2404         break;
  2405     case SDL_GL_ACCUM_ALPHA_SIZE:
  2406         _this->gl_config.accum_alpha_size = value;
  2407         break;
  2408     case SDL_GL_STEREO:
  2409         _this->gl_config.stereo = value;
  2410         break;
  2411     case SDL_GL_MULTISAMPLEBUFFERS:
  2412         _this->gl_config.multisamplebuffers = value;
  2413         break;
  2414     case SDL_GL_MULTISAMPLESAMPLES:
  2415         _this->gl_config.multisamplesamples = value;
  2416         break;
  2417     case SDL_GL_ACCELERATED_VISUAL:
  2418         _this->gl_config.accelerated = value;
  2419         break;
  2420     default:
  2421         SDL_SetError("Unknown OpenGL attribute");
  2422         retval = -1;
  2423         break;
  2424     }
  2425     return retval;
  2426 #else
  2427     SDL_Unsupported();
  2428     return -1;
  2429 #endif /* SDL_VIDEO_OPENGL */
  2430 }
  2431 
  2432 int
  2433 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2434 {
  2435 #if SDL_VIDEO_OPENGL
  2436     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2437     GLenum attrib = 0;
  2438 
  2439     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2440     if (!glGetIntegervFunc) {
  2441         return -1;
  2442     }
  2443     switch (attr) {
  2444     case SDL_GL_RED_SIZE:
  2445         attrib = GL_RED_BITS;
  2446         break;
  2447     case SDL_GL_BLUE_SIZE:
  2448         attrib = GL_BLUE_BITS;
  2449         break;
  2450     case SDL_GL_GREEN_SIZE:
  2451         attrib = GL_GREEN_BITS;
  2452         break;
  2453     case SDL_GL_ALPHA_SIZE:
  2454         attrib = GL_ALPHA_BITS;
  2455         break;
  2456     case SDL_GL_DOUBLEBUFFER:
  2457         attrib = GL_DOUBLEBUFFER;
  2458         break;
  2459     case SDL_GL_DEPTH_SIZE:
  2460         attrib = GL_DEPTH_BITS;
  2461         break;
  2462     case SDL_GL_STENCIL_SIZE:
  2463         attrib = GL_STENCIL_BITS;
  2464         break;
  2465     case SDL_GL_ACCUM_RED_SIZE:
  2466         attrib = GL_ACCUM_RED_BITS;
  2467         break;
  2468     case SDL_GL_ACCUM_GREEN_SIZE:
  2469         attrib = GL_ACCUM_GREEN_BITS;
  2470         break;
  2471     case SDL_GL_ACCUM_BLUE_SIZE:
  2472         attrib = GL_ACCUM_BLUE_BITS;
  2473         break;
  2474     case SDL_GL_ACCUM_ALPHA_SIZE:
  2475         attrib = GL_ACCUM_ALPHA_BITS;
  2476         break;
  2477     case SDL_GL_STEREO:
  2478         attrib = GL_STEREO;
  2479         break;
  2480     case SDL_GL_MULTISAMPLEBUFFERS:
  2481         attrib = GL_SAMPLE_BUFFERS_ARB;
  2482         break;
  2483     case SDL_GL_MULTISAMPLESAMPLES:
  2484         attrib = GL_SAMPLES_ARB;
  2485         break;
  2486     case SDL_GL_BUFFER_SIZE:
  2487         {
  2488             GLint bits = 0;
  2489             GLint component;
  2490 
  2491             /* there doesn't seem to be a single flag in OpenGL for this! */
  2492             glGetIntegervFunc(GL_RED_BITS, &component);
  2493             bits += component;
  2494             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2495             bits += component;
  2496             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2497             bits += component;
  2498             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2499             bits += component;
  2500 
  2501             *value = bits;
  2502             return 0;
  2503         }
  2504     case SDL_GL_ACCELERATED_VISUAL:
  2505         {
  2506             /* FIXME: How do we get this information? */
  2507             *value = (_this->gl_config.accelerated != 0);
  2508             return 0;
  2509         }
  2510     default:
  2511         SDL_SetError("Unknown OpenGL attribute");
  2512         return -1;
  2513     }
  2514 
  2515     glGetIntegervFunc(attrib, (GLint *) value);
  2516     return 0;
  2517 #else
  2518     SDL_Unsupported();
  2519     return -1;
  2520 #endif /* SDL_VIDEO_OPENGL */
  2521 }
  2522 
  2523 SDL_GLContext
  2524 SDL_GL_CreateContext(SDL_WindowID windowID)
  2525 {
  2526     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2527 
  2528     if (!window) {
  2529         return NULL;
  2530     }
  2531     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2532         SDL_SetError("The specified window isn't an OpenGL window");
  2533         return NULL;
  2534     }
  2535     return _this->GL_CreateContext(_this, window);
  2536 }
  2537 
  2538 int
  2539 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2540 {
  2541     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2542 
  2543     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2544         SDL_SetError("The specified window isn't an OpenGL window");
  2545         return -1;
  2546     }
  2547     if (!context) {
  2548         window = NULL;
  2549     }
  2550     return _this->GL_MakeCurrent(_this, window, context);
  2551 }
  2552 
  2553 int
  2554 SDL_GL_SetSwapInterval(int interval)
  2555 {
  2556     if (!_this) {
  2557         SDL_UninitializedVideo();
  2558         return -1;
  2559     }
  2560 
  2561     if (_this->GL_SetSwapInterval) {
  2562         return _this->GL_SetSwapInterval(_this, interval);
  2563     } else {
  2564         SDL_SetError("Setting the swap interval is not supported");
  2565         return -1;
  2566     }
  2567 }
  2568 
  2569 int
  2570 SDL_GL_GetSwapInterval(void)
  2571 {
  2572     if (!_this) {
  2573         SDL_UninitializedVideo();
  2574         return -1;
  2575     }
  2576 
  2577     if (_this->GL_GetSwapInterval) {
  2578         return _this->GL_GetSwapInterval(_this);
  2579     } else {
  2580         SDL_SetError("Getting the swap interval is not supported");
  2581         return -1;
  2582     }
  2583 }
  2584 
  2585 void
  2586 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2587 {
  2588     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2589 
  2590     if (!window) {
  2591         return;
  2592     }
  2593     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2594         SDL_SetError("The specified window isn't an OpenGL window");
  2595         return;
  2596     }
  2597     _this->GL_SwapWindow(_this, window);
  2598 }
  2599 
  2600 void
  2601 SDL_GL_DeleteContext(SDL_GLContext context)
  2602 {
  2603     if (!_this || !context) {
  2604         return;
  2605     }
  2606     _this->GL_MakeCurrent(_this, NULL, NULL);
  2607     _this->GL_DeleteContext(_this, context);
  2608 }
  2609 
  2610 #if 0                           // FIXME
  2611 /* Utility function used by SDL_WM_SetIcon();
  2612  * flags & 1 for color key, flags & 2 for alpha channel. */
  2613 static void
  2614 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2615 {
  2616     int x, y;
  2617     Uint32 colorkey;
  2618 #define SET_MASKBIT(icon, x, y, mask) \
  2619 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2620 
  2621     colorkey = icon->format->colorkey;
  2622     switch (icon->format->BytesPerPixel) {
  2623     case 1:
  2624         {
  2625             Uint8 *pixels;
  2626             for (y = 0; y < icon->h; ++y) {
  2627                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2628                 for (x = 0; x < icon->w; ++x) {
  2629                     if (*pixels++ == colorkey) {
  2630                         SET_MASKBIT(icon, x, y, mask);
  2631                     }
  2632                 }
  2633             }
  2634         }
  2635         break;
  2636 
  2637     case 2:
  2638         {
  2639             Uint16 *pixels;
  2640             for (y = 0; y < icon->h; ++y) {
  2641                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2642                 for (x = 0; x < icon->w; ++x) {
  2643                     if ((flags & 1) && *pixels == colorkey) {
  2644                         SET_MASKBIT(icon, x, y, mask);
  2645                     } else if ((flags & 2)
  2646                                && (*pixels & icon->format->Amask) == 0) {
  2647                         SET_MASKBIT(icon, x, y, mask);
  2648                     }
  2649                     pixels++;
  2650                 }
  2651             }
  2652         }
  2653         break;
  2654 
  2655     case 4:
  2656         {
  2657             Uint32 *pixels;
  2658             for (y = 0; y < icon->h; ++y) {
  2659                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2660                 for (x = 0; x < icon->w; ++x) {
  2661                     if ((flags & 1) && *pixels == colorkey) {
  2662                         SET_MASKBIT(icon, x, y, mask);
  2663                     } else if ((flags & 2)
  2664                                && (*pixels & icon->format->Amask) == 0) {
  2665                         SET_MASKBIT(icon, x, y, mask);
  2666                     }
  2667                     pixels++;
  2668                 }
  2669             }
  2670         }
  2671         break;
  2672     }
  2673 }
  2674 
  2675 /*
  2676  * Sets the window manager icon for the display window.
  2677  */
  2678 void
  2679 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2680 {
  2681     if (icon && _this->SetIcon) {
  2682         /* Generate a mask if necessary, and create the icon! */
  2683         if (mask == NULL) {
  2684             int mask_len = icon->h * (icon->w + 7) / 8;
  2685             int flags = 0;
  2686             mask = (Uint8 *) SDL_malloc(mask_len);
  2687             if (mask == NULL) {
  2688                 return;
  2689             }
  2690             SDL_memset(mask, ~0, mask_len);
  2691             if (icon->flags & SDL_SRCCOLORKEY)
  2692                 flags |= 1;
  2693             if (icon->flags & SDL_SRCALPHA)
  2694                 flags |= 2;
  2695             if (flags) {
  2696                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2697             }
  2698             _this->SetIcon(_this, icon, mask);
  2699             SDL_free(mask);
  2700         } else {
  2701             _this->SetIcon(_this, icon, mask);
  2702         }
  2703     }
  2704 }
  2705 #endif
  2706 
  2707 SDL_bool
  2708 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2709 {
  2710     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2711 
  2712     if (!window || !_this->GetWindowWMInfo) {
  2713         return SDL_FALSE;
  2714     }
  2715     return (_this->GetWindowWMInfo(_this, window, info));
  2716 }
  2717 
  2718 /* vi: set ts=4 sw=4 expandtab: */