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