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