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