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