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