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