src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Aug 2006 00:09:04 +0000
changeset 1969 5d3724f64f2b
parent 1967 01e29c3e9a29
child 1970 db3ba6c0d0df
permissions -rw-r--r--
Clarified the difference between render drivers and render contexts
     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_GetNumRenderDrivers(void)
  1366 {
  1367     if (_this) {
  1368         return SDL_CurrentDisplay.num_render_drivers;
  1369     }
  1370     return 0;
  1371 }
  1372 
  1373 int
  1374 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1375 {
  1376     if (!_this) {
  1377         SDL_UninitializedVideo();
  1378         return -1;
  1379     }
  1380 
  1381     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1382         SDL_SetError("index must be in the range of 0 - %d",
  1383                      SDL_GetNumRenderDrivers() - 1);
  1384         return -1;
  1385     }
  1386     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1387     return 0;
  1388 }
  1389 
  1390 int
  1391 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1392 {
  1393     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1394 
  1395     if (!window) {
  1396         return 0;
  1397     }
  1398 
  1399     if (index < 0) {
  1400         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1401         int n = SDL_GetNumRenderDrivers();
  1402         for (index = 0; index < n; ++index) {
  1403             SDL_RenderDriver *driver =
  1404                 &SDL_CurrentDisplay.render_drivers[index];
  1405 
  1406             if (override) {
  1407                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1408                     break;
  1409                 }
  1410             } else {
  1411                 if ((driver->info.flags & flags) == flags) {
  1412                     break;
  1413                 }
  1414             }
  1415         }
  1416         if (index == n) {
  1417             SDL_SetError("Couldn't find matching render driver");
  1418             return -1;
  1419         }
  1420     }
  1421 
  1422     if (index >= SDL_GetNumRenderDrivers()) {
  1423         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1424                      SDL_GetNumRenderDrivers() - 1);
  1425         return -1;
  1426     }
  1427 
  1428     /* Free any existing renderer */
  1429     SDL_DestroyRenderer(windowID);
  1430 
  1431     /* Create a new renderer instance */
  1432     window->renderer =
  1433         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1434                                                                 flags);
  1435     SDL_SelectRenderer(window->id);
  1436 
  1437     return 0;
  1438 }
  1439 
  1440 int
  1441 SDL_SelectRenderer(SDL_WindowID windowID)
  1442 {
  1443     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1444     SDL_Renderer *renderer;
  1445 
  1446     if (!window || !window->renderer) {
  1447         return -1;
  1448     }
  1449     renderer = window->renderer;
  1450     if (renderer && renderer->ActivateRenderer) {
  1451         if (renderer->ActivateRenderer(renderer) < 0) {
  1452             return -1;
  1453         }
  1454     }
  1455     SDL_CurrentDisplay.current_renderer = renderer;
  1456     return 0;
  1457 }
  1458 
  1459 int
  1460 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1461 {
  1462     if (!_this) {
  1463         SDL_UninitializedVideo();
  1464         return -1;
  1465     }
  1466 
  1467     if (!SDL_CurrentDisplay.current_renderer) {
  1468         SDL_SetError("There is no current renderer");
  1469         return -1;
  1470     }
  1471     *info = SDL_CurrentDisplay.current_renderer->info;
  1472     return 0;
  1473 }
  1474 
  1475 SDL_TextureID
  1476 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1477 {
  1478     int hash;
  1479     SDL_Renderer *renderer;
  1480     SDL_Texture *texture;
  1481 
  1482     if (!_this) {
  1483         SDL_UninitializedVideo();
  1484         return 0;
  1485     }
  1486 
  1487     renderer = SDL_CurrentDisplay.current_renderer;
  1488     if (!renderer || !renderer->CreateTexture) {
  1489         return 0;
  1490     }
  1491 
  1492     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1493     if (!texture) {
  1494         SDL_OutOfMemory();
  1495         return 0;
  1496     }
  1497 
  1498     texture->id = _this->next_object_id++;
  1499     texture->format = format;
  1500     texture->access = access;
  1501     texture->w = w;
  1502     texture->h = h;
  1503     texture->renderer = renderer;
  1504 
  1505     if (renderer->CreateTexture(renderer, texture) < 0) {
  1506         if (renderer->DestroyTexture) {
  1507             renderer->DestroyTexture(renderer, texture);
  1508         }
  1509         SDL_free(texture);
  1510         return 0;
  1511     }
  1512 
  1513     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1514     texture->next = SDL_CurrentDisplay.textures[hash];
  1515     SDL_CurrentDisplay.textures[hash] = texture;
  1516 
  1517     return texture->id;
  1518 }
  1519 
  1520 SDL_TextureID
  1521 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1522 {
  1523     SDL_TextureID textureID;
  1524     Uint32 surface_flags = surface->flags;
  1525     SDL_PixelFormat *fmt = surface->format;
  1526     Uint8 alpha;
  1527     SDL_Rect bounds;
  1528     SDL_Surface dst;
  1529     int bpp;
  1530     Uint32 Rmask, Gmask, Bmask, Amask;
  1531 
  1532     if (!surface) {
  1533         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1534         return 0;
  1535     }
  1536 
  1537     if (format) {
  1538         if (!SDL_PixelFormatEnumToMasks
  1539             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1540             SDL_SetError("Unknown pixel format");
  1541             return 0;
  1542         }
  1543     } else {
  1544         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1545             bpp = fmt->BitsPerPixel;
  1546             Rmask = fmt->Rmask;
  1547             Gmask = fmt->Gmask;
  1548             Bmask = fmt->Bmask;
  1549             Amask = fmt->Amask;
  1550         } else {
  1551             /* Need a format with alpha */
  1552             bpp = 32;
  1553             Rmask = 0x00FF0000;
  1554             Gmask = 0x0000FF00;
  1555             Bmask = 0x000000FF;
  1556             Amask = 0xFF000000;
  1557         }
  1558         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1559         if (!format) {
  1560             SDL_SetError("Unknown pixel format");
  1561             return 0;
  1562         }
  1563     }
  1564 
  1565     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1566     if (!textureID) {
  1567         return 0;
  1568     }
  1569 
  1570     /* Set up a destination surface for the texture update */
  1571     SDL_zero(dst);
  1572     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1573     if (!dst.format) {
  1574         SDL_DestroyTexture(textureID);
  1575         return 0;
  1576     }
  1577     dst.w = surface->w;
  1578     dst.h = surface->h;
  1579     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1580         dst.flags |= SDL_PREALLOC;
  1581     } else {
  1582         dst.pitch = SDL_CalculatePitch(&dst);
  1583         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1584         if (!dst.pixels) {
  1585             SDL_DestroyTexture(textureID);
  1586             SDL_FreeFormat(dst.format);
  1587             SDL_OutOfMemory();
  1588             return 0;
  1589         }
  1590     }
  1591 
  1592     /* Copy the palette if any */
  1593     if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1594         if (fmt->palette) {
  1595             SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1596                                   fmt->palette->ncolors);
  1597             SDL_SetSurfacePalette(&dst, fmt->palette);
  1598         } else {
  1599             dst.format->palette =
  1600                 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1601             if (!dst.format->palette) {
  1602                 SDL_DestroyTexture(textureID);
  1603                 SDL_FreeFormat(dst.format);
  1604                 return 0;
  1605             }
  1606             SDL_DitherColors(dst.format->palette->colors,
  1607                              SDL_BITSPERPIXEL(format));
  1608         }
  1609     }
  1610 
  1611     /* Make the texture transparent if the surface has colorkey */
  1612     if (surface_flags & SDL_SRCCOLORKEY) {
  1613         int row;
  1614         int length = dst.w * dst.format->BytesPerPixel;
  1615         Uint8 *p = (Uint8 *) dst.pixels;
  1616         for (row = 0; row < dst.h; ++row) {
  1617             SDL_memset(p, 0, length);
  1618             p += dst.pitch;
  1619         }
  1620     }
  1621 
  1622     /* Copy over the alpha channel */
  1623     if (surface_flags & SDL_SRCALPHA) {
  1624         if (fmt->Amask) {
  1625             surface->flags &= ~SDL_SRCALPHA;
  1626         } else {
  1627             /* FIXME: Need to make sure the texture has an alpha channel
  1628              *        and copy 'alpha' into the texture alpha channel.
  1629              */
  1630             alpha = surface->format->alpha;
  1631             SDL_SetAlpha(surface, 0, 0);
  1632         }
  1633     }
  1634 
  1635     /* Copy over the image data */
  1636     bounds.x = 0;
  1637     bounds.y = 0;
  1638     bounds.w = surface->w;
  1639     bounds.h = surface->h;
  1640     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1641 
  1642     /* Clean up the original surface */
  1643     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1644         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1645         if (fmt->Amask) {
  1646             surface->flags |= SDL_SRCALPHA;
  1647         } else {
  1648             SDL_SetAlpha(surface, aflags, alpha);
  1649         }
  1650     }
  1651 
  1652     /* Update the texture */
  1653     if (dst.flags & SDL_PREALLOC) {
  1654         SDL_UnlockTexture(textureID);
  1655     } else {
  1656         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1657         SDL_free(dst.pixels);
  1658     }
  1659     SDL_FreeFormat(dst.format);
  1660 
  1661     return textureID;
  1662 }
  1663 
  1664 static __inline__ SDL_Texture *
  1665 SDL_GetTextureFromID(SDL_TextureID textureID)
  1666 {
  1667     int hash;
  1668     SDL_Texture *texture;
  1669 
  1670     if (!_this) {
  1671         return NULL;
  1672     }
  1673 
  1674     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1675     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1676          texture = texture->next) {
  1677         if (texture->id == textureID) {
  1678             return texture;
  1679         }
  1680     }
  1681     return NULL;
  1682 }
  1683 
  1684 int
  1685 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1686                  int *w, int *h)
  1687 {
  1688     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1689 
  1690     if (!texture) {
  1691         return -1;
  1692     }
  1693 
  1694     if (format) {
  1695         *format = texture->format;
  1696     }
  1697     if (access) {
  1698         *access = texture->access;
  1699     }
  1700     if (w) {
  1701         *w = texture->w;
  1702     }
  1703     if (h) {
  1704         *h = texture->h;
  1705     }
  1706     return 0;
  1707 }
  1708 
  1709 int
  1710 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1711 {
  1712     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1713     SDL_Renderer *renderer;
  1714 
  1715     if (!texture) {
  1716         return -1;
  1717     }
  1718 
  1719     renderer = texture->renderer;
  1720     if (!renderer->QueryTexturePixels) {
  1721         return -1;
  1722     }
  1723     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1724 }
  1725 
  1726 int
  1727 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1728                       int firstcolor, int ncolors)
  1729 {
  1730     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1731     SDL_Renderer *renderer;
  1732 
  1733     if (!texture) {
  1734         return -1;
  1735     }
  1736 
  1737     renderer = texture->renderer;
  1738     if (!renderer->SetTexturePalette) {
  1739         return -1;
  1740     }
  1741     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1742                                        ncolors);
  1743 }
  1744 
  1745 int
  1746 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1747                       int firstcolor, int ncolors)
  1748 {
  1749     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1750     SDL_Renderer *renderer;
  1751 
  1752     if (!texture) {
  1753         return -1;
  1754     }
  1755 
  1756     renderer = texture->renderer;
  1757     if (!renderer->GetTexturePalette) {
  1758         return -1;
  1759     }
  1760     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1761                                        ncolors);
  1762 }
  1763 
  1764 int
  1765 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1766                   const void *pixels, int pitch)
  1767 {
  1768     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1769     SDL_Renderer *renderer;
  1770     SDL_Rect full_rect;
  1771 
  1772     if (!texture) {
  1773         return -1;
  1774     }
  1775 
  1776     renderer = texture->renderer;
  1777     if (!renderer->UpdateTexture) {
  1778         return -1;
  1779     }
  1780 
  1781     if (!rect) {
  1782         full_rect.x = 0;
  1783         full_rect.y = 0;
  1784         full_rect.w = texture->w;
  1785         full_rect.h = texture->h;
  1786         rect = &full_rect;
  1787     }
  1788 
  1789     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1790 }
  1791 
  1792 int
  1793 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1794                 void **pixels, int *pitch)
  1795 {
  1796     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1797     SDL_Renderer *renderer;
  1798     SDL_Rect full_rect;
  1799 
  1800     if (!texture) {
  1801         return -1;
  1802     }
  1803 
  1804     renderer = texture->renderer;
  1805     if (!renderer->LockTexture) {
  1806         return -1;
  1807     }
  1808 
  1809     if (!rect) {
  1810         full_rect.x = 0;
  1811         full_rect.y = 0;
  1812         full_rect.w = texture->w;
  1813         full_rect.h = texture->h;
  1814         rect = &full_rect;
  1815     }
  1816 
  1817     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1818                                  pitch);
  1819 }
  1820 
  1821 void
  1822 SDL_UnlockTexture(SDL_TextureID textureID)
  1823 {
  1824     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1825     SDL_Renderer *renderer;
  1826 
  1827     if (!texture) {
  1828         return;
  1829     }
  1830 
  1831     renderer = texture->renderer;
  1832     if (!renderer->UnlockTexture) {
  1833         return;
  1834     }
  1835     renderer->UnlockTexture(renderer, texture);
  1836 }
  1837 
  1838 void
  1839 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1840                  const SDL_Rect * rects)
  1841 {
  1842     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1843     SDL_Renderer *renderer;
  1844 
  1845     if (!texture) {
  1846         return;
  1847     }
  1848 
  1849     renderer = texture->renderer;
  1850     if (!renderer->DirtyTexture) {
  1851         return;
  1852     }
  1853     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1854 }
  1855 
  1856 int
  1857 SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
  1858 {
  1859     SDL_Renderer *renderer;
  1860     SDL_Window *window;
  1861     SDL_Rect real_rect;
  1862 
  1863     if (!_this) {
  1864         SDL_UninitializedVideo();
  1865         return -1;
  1866     }
  1867 
  1868     renderer = SDL_CurrentDisplay.current_renderer;
  1869     if (!renderer || !renderer->RenderFill) {
  1870         return -1;
  1871     }
  1872 
  1873     window = SDL_GetWindowFromID(renderer->window);
  1874     real_rect.x = 0;
  1875     real_rect.y = 0;
  1876     real_rect.w = window->w;
  1877     real_rect.h = window->h;
  1878     if (rect) {
  1879         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1880             return 0;
  1881         }
  1882     }
  1883 
  1884     return renderer->RenderFill(renderer, &real_rect, color);
  1885 }
  1886 
  1887 int
  1888 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1889                const SDL_Rect * dstrect, int blendMode, int scaleMode)
  1890 {
  1891     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1892     SDL_Renderer *renderer;
  1893     SDL_Window *window;
  1894     SDL_Rect real_srcrect;
  1895     SDL_Rect real_dstrect;
  1896 
  1897     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1898         return -1;
  1899     }
  1900 
  1901     renderer = SDL_CurrentDisplay.current_renderer;
  1902     if (!renderer || !renderer->RenderCopy) {
  1903         return -1;
  1904     }
  1905 
  1906     window = SDL_GetWindowFromID(renderer->window);
  1907     if (srcrect) {
  1908         real_srcrect = *srcrect;
  1909     } else {
  1910         real_srcrect.x = 0;
  1911         real_srcrect.y = 0;
  1912         real_srcrect.w = texture->w;
  1913         real_srcrect.h = texture->h;
  1914     }
  1915     if (dstrect) {
  1916         real_dstrect = *dstrect;
  1917     } else {
  1918         real_dstrect.x = 0;
  1919         real_dstrect.y = 0;
  1920         real_dstrect.w = window->w;
  1921         real_dstrect.h = window->h;
  1922     }
  1923 
  1924     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  1925                                 &real_dstrect, blendMode, scaleMode);
  1926 }
  1927 
  1928 void
  1929 SDL_RenderPresent(void)
  1930 {
  1931     SDL_Renderer *renderer;
  1932 
  1933     if (!_this) {
  1934         SDL_UninitializedVideo();
  1935         return;
  1936     }
  1937 
  1938     renderer = SDL_CurrentDisplay.current_renderer;
  1939     if (!renderer || !renderer->RenderPresent) {
  1940         return;
  1941     }
  1942     renderer->RenderPresent(renderer);
  1943 }
  1944 
  1945 void
  1946 SDL_DestroyTexture(SDL_TextureID textureID)
  1947 {
  1948     int hash;
  1949     SDL_Texture *prev, *texture;
  1950     SDL_Renderer *renderer;
  1951 
  1952     if (!_this) {
  1953         SDL_UninitializedVideo();
  1954         return;
  1955     }
  1956 
  1957     /* Look up the texture in the hash table */
  1958     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1959     prev = NULL;
  1960     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1961          prev = texture, texture = texture->next) {
  1962         if (texture->id == textureID) {
  1963             break;
  1964         }
  1965     }
  1966     if (!texture) {
  1967         return;
  1968     }
  1969 
  1970     /* Unlink the texture from the list */
  1971     if (prev) {
  1972         prev->next = texture->next;
  1973     } else {
  1974         SDL_CurrentDisplay.textures[hash] = texture->next;
  1975     }
  1976 
  1977     /* Free the texture */
  1978     renderer = texture->renderer;
  1979     renderer->DestroyTexture(renderer, texture);
  1980     SDL_free(texture);
  1981 }
  1982 
  1983 void
  1984 SDL_DestroyRenderer(SDL_WindowID windowID)
  1985 {
  1986     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1987     SDL_Renderer *renderer;
  1988     int i;
  1989 
  1990     if (!window) {
  1991         return;
  1992     }
  1993 
  1994     renderer = window->renderer;
  1995     if (!renderer) {
  1996         return;
  1997     }
  1998 
  1999     /* Free existing textures for this renderer */
  2000     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2001         SDL_Texture *texture;
  2002         SDL_Texture *prev = NULL;
  2003         SDL_Texture *next;
  2004         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2005              texture = next) {
  2006             next = texture->next;
  2007             if (texture->renderer == renderer) {
  2008                 if (prev) {
  2009                     prev->next = next;
  2010                 } else {
  2011                     SDL_CurrentDisplay.textures[i] = next;
  2012                 }
  2013                 renderer->DestroyTexture(renderer, texture);
  2014                 SDL_free(texture);
  2015             } else {
  2016                 prev = texture;
  2017             }
  2018         }
  2019     }
  2020 
  2021     /* Free the renderer instance */
  2022     renderer->DestroyRenderer(renderer);
  2023 
  2024     /* Clear references */
  2025     window->renderer = NULL;
  2026     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2027         SDL_CurrentDisplay.current_renderer = NULL;
  2028     }
  2029 }
  2030 
  2031 void
  2032 SDL_VideoQuit(void)
  2033 {
  2034     int i, j;
  2035 
  2036     if (!_this) {
  2037         return;
  2038     }
  2039 
  2040     /* Halt event processing before doing anything else */
  2041     SDL_StopEventLoop();
  2042 
  2043     /* Clean up the system video */
  2044     for (i = _this->num_displays; i--;) {
  2045         SDL_VideoDisplay *display = &_this->displays[i];
  2046         for (j = display->num_windows; j--;) {
  2047             SDL_DestroyWindow(display->windows[i].id);
  2048         }
  2049         if (display->windows) {
  2050             SDL_free(display->windows);
  2051             display->windows = NULL;
  2052         }
  2053         display->num_windows = 0;
  2054     }
  2055     _this->VideoQuit(_this);
  2056 
  2057     for (i = _this->num_displays; i--;) {
  2058         SDL_VideoDisplay *display = &_this->displays[i];
  2059         for (j = display->num_display_modes; j--;) {
  2060             if (display->display_modes[j].driverdata) {
  2061                 SDL_free(display->display_modes[j].driverdata);
  2062                 display->display_modes[j].driverdata = NULL;
  2063             }
  2064         }
  2065         if (display->display_modes) {
  2066             SDL_free(display->display_modes);
  2067             display->display_modes = NULL;
  2068         }
  2069         if (display->desktop_mode.driverdata) {
  2070             SDL_free(display->desktop_mode.driverdata);
  2071             display->desktop_mode.driverdata = NULL;
  2072         }
  2073         if (display->palette) {
  2074             SDL_FreePalette(display->palette);
  2075             display->palette = NULL;
  2076         }
  2077         if (display->gamma) {
  2078             SDL_free(display->gamma);
  2079         }
  2080         if (display->driverdata) {
  2081             SDL_free(display->driverdata);
  2082         }
  2083     }
  2084     if (_this->displays) {
  2085         SDL_free(_this->displays);
  2086         _this->displays = NULL;
  2087     }
  2088     _this->free(_this);
  2089     _this = NULL;
  2090 }
  2091 
  2092 int
  2093 SDL_GL_LoadLibrary(const char *path)
  2094 {
  2095     int retval;
  2096 
  2097     if (!_this) {
  2098         SDL_UninitializedVideo();
  2099         return -1;
  2100     }
  2101 
  2102     if (_this->GL_LoadLibrary) {
  2103         retval = _this->GL_LoadLibrary(_this, path);
  2104     } else {
  2105         SDL_SetError("No dynamic GL support in video driver");
  2106         retval = -1;
  2107     }
  2108     return (retval);
  2109 }
  2110 
  2111 void *
  2112 SDL_GL_GetProcAddress(const char *proc)
  2113 {
  2114     void *func;
  2115 
  2116     if (!_this) {
  2117         SDL_UninitializedVideo();
  2118         return NULL;
  2119     }
  2120 
  2121     func = NULL;
  2122     if (_this->GL_GetProcAddress) {
  2123         if (_this->gl_config.driver_loaded) {
  2124             func = _this->GL_GetProcAddress(_this, proc);
  2125         } else {
  2126             SDL_SetError("No GL driver has been loaded");
  2127         }
  2128     } else {
  2129         SDL_SetError("No dynamic GL support in video driver");
  2130     }
  2131     return func;
  2132 }
  2133 
  2134 SDL_bool
  2135 SDL_GL_ExtensionSupported(const char *extension)
  2136 {
  2137 #if SDL_VIDEO_OPENGL
  2138     const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2139     const char *extensions;
  2140     const char *start;
  2141     const char *where, *terminator;
  2142 
  2143     /* Extension names should not have spaces. */
  2144     where = SDL_strchr(extension, ' ');
  2145     if (where || *extension == '\0') {
  2146         return SDL_FALSE;
  2147     }
  2148 
  2149     /* See if there's an environment variable override */
  2150     start = SDL_getenv(extension);
  2151     if (start && *start == '0') {
  2152         return SDL_FALSE;
  2153     }
  2154 
  2155     /* Lookup the available extensions */
  2156     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2157     if (glGetStringFunc) {
  2158         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2159     } else {
  2160         extensions = NULL;
  2161     }
  2162     if (!extensions) {
  2163         return SDL_FALSE;
  2164     }
  2165 
  2166     /* It takes a bit of care to be fool-proof about parsing the
  2167      * OpenGL extensions string. Don't be fooled by sub-strings,
  2168      * etc. */
  2169 
  2170     start = extensions;
  2171 
  2172     for (;;) {
  2173         where = SDL_strstr(start, extension);
  2174         if (!where)
  2175             break;
  2176 
  2177         terminator = where + SDL_strlen(extension);
  2178         if (where == start || *(where - 1) == ' ')
  2179             if (*terminator == ' ' || *terminator == '\0')
  2180                 return SDL_TRUE;
  2181 
  2182         start = terminator;
  2183     }
  2184     return SDL_FALSE;
  2185 #else
  2186     return SDL_FALSE;
  2187 #endif
  2188 }
  2189 
  2190 int
  2191 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2192 {
  2193 #if SDL_VIDEO_OPENGL
  2194     int retval;
  2195 
  2196     if (!_this) {
  2197         SDL_UninitializedVideo();
  2198         return -1;
  2199     }
  2200 
  2201     retval = 0;
  2202     switch (attr) {
  2203     case SDL_GL_RED_SIZE:
  2204         _this->gl_config.red_size = value;
  2205         break;
  2206     case SDL_GL_GREEN_SIZE:
  2207         _this->gl_config.green_size = value;
  2208         break;
  2209     case SDL_GL_BLUE_SIZE:
  2210         _this->gl_config.blue_size = value;
  2211         break;
  2212     case SDL_GL_ALPHA_SIZE:
  2213         _this->gl_config.alpha_size = value;
  2214         break;
  2215     case SDL_GL_DOUBLEBUFFER:
  2216         _this->gl_config.double_buffer = value;
  2217         break;
  2218     case SDL_GL_BUFFER_SIZE:
  2219         _this->gl_config.buffer_size = value;
  2220         break;
  2221     case SDL_GL_DEPTH_SIZE:
  2222         _this->gl_config.depth_size = value;
  2223         break;
  2224     case SDL_GL_STENCIL_SIZE:
  2225         _this->gl_config.stencil_size = value;
  2226         break;
  2227     case SDL_GL_ACCUM_RED_SIZE:
  2228         _this->gl_config.accum_red_size = value;
  2229         break;
  2230     case SDL_GL_ACCUM_GREEN_SIZE:
  2231         _this->gl_config.accum_green_size = value;
  2232         break;
  2233     case SDL_GL_ACCUM_BLUE_SIZE:
  2234         _this->gl_config.accum_blue_size = value;
  2235         break;
  2236     case SDL_GL_ACCUM_ALPHA_SIZE:
  2237         _this->gl_config.accum_alpha_size = value;
  2238         break;
  2239     case SDL_GL_STEREO:
  2240         _this->gl_config.stereo = value;
  2241         break;
  2242     case SDL_GL_MULTISAMPLEBUFFERS:
  2243         _this->gl_config.multisamplebuffers = value;
  2244         break;
  2245     case SDL_GL_MULTISAMPLESAMPLES:
  2246         _this->gl_config.multisamplesamples = value;
  2247         break;
  2248     case SDL_GL_ACCELERATED_VISUAL:
  2249         _this->gl_config.accelerated = value;
  2250         break;
  2251     default:
  2252         SDL_SetError("Unknown OpenGL attribute");
  2253         retval = -1;
  2254         break;
  2255     }
  2256     return retval;
  2257 #else
  2258     SDL_Unsupported();
  2259     return -1;
  2260 #endif /* SDL_VIDEO_OPENGL */
  2261 }
  2262 
  2263 int
  2264 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2265 {
  2266 #if SDL_VIDEO_OPENGL
  2267     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2268     GLenum attrib = 0;
  2269 
  2270     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2271     if (!glGetIntegervFunc) {
  2272         return -1;
  2273     }
  2274     switch (attr) {
  2275     case SDL_GL_RED_SIZE:
  2276         attrib = GL_RED_BITS;
  2277         break;
  2278     case SDL_GL_BLUE_SIZE:
  2279         attrib = GL_BLUE_BITS;
  2280         break;
  2281     case SDL_GL_GREEN_SIZE:
  2282         attrib = GL_GREEN_BITS;
  2283         break;
  2284     case SDL_GL_ALPHA_SIZE:
  2285         attrib = GL_ALPHA_BITS;
  2286         break;
  2287     case SDL_GL_DOUBLEBUFFER:
  2288         attrib = GL_DOUBLEBUFFER;
  2289         break;
  2290     case SDL_GL_DEPTH_SIZE:
  2291         attrib = GL_DEPTH_BITS;
  2292         break;
  2293     case SDL_GL_STENCIL_SIZE:
  2294         attrib = GL_STENCIL_BITS;
  2295         break;
  2296     case SDL_GL_ACCUM_RED_SIZE:
  2297         attrib = GL_ACCUM_RED_BITS;
  2298         break;
  2299     case SDL_GL_ACCUM_GREEN_SIZE:
  2300         attrib = GL_ACCUM_GREEN_BITS;
  2301         break;
  2302     case SDL_GL_ACCUM_BLUE_SIZE:
  2303         attrib = GL_ACCUM_BLUE_BITS;
  2304         break;
  2305     case SDL_GL_ACCUM_ALPHA_SIZE:
  2306         attrib = GL_ACCUM_ALPHA_BITS;
  2307         break;
  2308     case SDL_GL_STEREO:
  2309         attrib = GL_STEREO;
  2310         break;
  2311     case SDL_GL_MULTISAMPLEBUFFERS:
  2312         attrib = GL_SAMPLE_BUFFERS_ARB;
  2313         break;
  2314     case SDL_GL_MULTISAMPLESAMPLES:
  2315         attrib = GL_SAMPLES_ARB;
  2316         break;
  2317     case SDL_GL_BUFFER_SIZE:
  2318         {
  2319             GLint bits = 0;
  2320             GLint component;
  2321 
  2322             /* there doesn't seem to be a single flag in OpenGL for this! */
  2323             glGetIntegervFunc(GL_RED_BITS, &component);
  2324             bits += component;
  2325             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2326             bits += component;
  2327             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2328             bits += component;
  2329             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2330             bits += component;
  2331 
  2332             *value = bits;
  2333             return 0;
  2334         }
  2335     case SDL_GL_ACCELERATED_VISUAL:
  2336         {
  2337             /* FIXME: How do we get this information? */
  2338             *value = (_this->gl_config.accelerated != 0);
  2339             return 0;
  2340         }
  2341     default:
  2342         SDL_SetError("Unknown OpenGL attribute");
  2343         return -1;
  2344     }
  2345 
  2346     glGetIntegervFunc(attrib, (GLint *) value);
  2347     return 0;
  2348 #else
  2349     SDL_Unsupported();
  2350     return -1;
  2351 #endif /* SDL_VIDEO_OPENGL */
  2352 }
  2353 
  2354 SDL_GLContext
  2355 SDL_GL_CreateContext(SDL_WindowID windowID)
  2356 {
  2357     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2358 
  2359     if (!window) {
  2360         return NULL;
  2361     }
  2362     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2363         SDL_SetError("The specified window isn't an OpenGL window");
  2364         return NULL;
  2365     }
  2366     return _this->GL_CreateContext(_this, window);
  2367 }
  2368 
  2369 int
  2370 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2371 {
  2372     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2373 
  2374     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2375         SDL_SetError("The specified window isn't an OpenGL window");
  2376         return -1;
  2377     }
  2378     if (!context) {
  2379         window = NULL;
  2380     }
  2381     return _this->GL_MakeCurrent(_this, window, context);
  2382 }
  2383 
  2384 int
  2385 SDL_GL_SetSwapInterval(int interval)
  2386 {
  2387     if (!_this) {
  2388         SDL_UninitializedVideo();
  2389         return -1;
  2390     }
  2391 
  2392     if (_this->GL_SetSwapInterval) {
  2393         return _this->GL_SetSwapInterval(_this, interval);
  2394     } else {
  2395         SDL_SetError("Setting the swap interval is not supported");
  2396         return -1;
  2397     }
  2398 }
  2399 
  2400 int
  2401 SDL_GL_GetSwapInterval(void)
  2402 {
  2403     if (!_this) {
  2404         SDL_UninitializedVideo();
  2405         return -1;
  2406     }
  2407 
  2408     if (_this->GL_GetSwapInterval) {
  2409         return _this->GL_GetSwapInterval(_this);
  2410     } else {
  2411         SDL_SetError("Getting the swap interval is not supported");
  2412         return -1;
  2413     }
  2414 }
  2415 
  2416 void
  2417 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2418 {
  2419     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2420 
  2421     if (!window) {
  2422         return;
  2423     }
  2424     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2425         SDL_SetError("The specified window isn't an OpenGL window");
  2426         return;
  2427     }
  2428     _this->GL_SwapWindow(_this, window);
  2429 }
  2430 
  2431 void
  2432 SDL_GL_DeleteContext(SDL_GLContext context)
  2433 {
  2434     if (!_this || !context) {
  2435         return;
  2436     }
  2437     _this->GL_MakeCurrent(_this, NULL, NULL);
  2438     _this->GL_DeleteContext(_this, context);
  2439 }
  2440 
  2441 #if 0                           // FIXME
  2442 /* Utility function used by SDL_WM_SetIcon();
  2443  * flags & 1 for color key, flags & 2 for alpha channel. */
  2444 static void
  2445 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2446 {
  2447     int x, y;
  2448     Uint32 colorkey;
  2449 #define SET_MASKBIT(icon, x, y, mask) \
  2450 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2451 
  2452     colorkey = icon->format->colorkey;
  2453     switch (icon->format->BytesPerPixel) {
  2454     case 1:
  2455         {
  2456             Uint8 *pixels;
  2457             for (y = 0; y < icon->h; ++y) {
  2458                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2459                 for (x = 0; x < icon->w; ++x) {
  2460                     if (*pixels++ == colorkey) {
  2461                         SET_MASKBIT(icon, x, y, mask);
  2462                     }
  2463                 }
  2464             }
  2465         }
  2466         break;
  2467 
  2468     case 2:
  2469         {
  2470             Uint16 *pixels;
  2471             for (y = 0; y < icon->h; ++y) {
  2472                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2473                 for (x = 0; x < icon->w; ++x) {
  2474                     if ((flags & 1) && *pixels == colorkey) {
  2475                         SET_MASKBIT(icon, x, y, mask);
  2476                     } else if ((flags & 2)
  2477                                && (*pixels & icon->format->Amask) == 0) {
  2478                         SET_MASKBIT(icon, x, y, mask);
  2479                     }
  2480                     pixels++;
  2481                 }
  2482             }
  2483         }
  2484         break;
  2485 
  2486     case 4:
  2487         {
  2488             Uint32 *pixels;
  2489             for (y = 0; y < icon->h; ++y) {
  2490                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2491                 for (x = 0; x < icon->w; ++x) {
  2492                     if ((flags & 1) && *pixels == colorkey) {
  2493                         SET_MASKBIT(icon, x, y, mask);
  2494                     } else if ((flags & 2)
  2495                                && (*pixels & icon->format->Amask) == 0) {
  2496                         SET_MASKBIT(icon, x, y, mask);
  2497                     }
  2498                     pixels++;
  2499                 }
  2500             }
  2501         }
  2502         break;
  2503     }
  2504 }
  2505 
  2506 /*
  2507  * Sets the window manager icon for the display window.
  2508  */
  2509 void
  2510 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2511 {
  2512     if (icon && _this->SetIcon) {
  2513         /* Generate a mask if necessary, and create the icon! */
  2514         if (mask == NULL) {
  2515             int mask_len = icon->h * (icon->w + 7) / 8;
  2516             int flags = 0;
  2517             mask = (Uint8 *) SDL_malloc(mask_len);
  2518             if (mask == NULL) {
  2519                 return;
  2520             }
  2521             SDL_memset(mask, ~0, mask_len);
  2522             if (icon->flags & SDL_SRCCOLORKEY)
  2523                 flags |= 1;
  2524             if (icon->flags & SDL_SRCALPHA)
  2525                 flags |= 2;
  2526             if (flags) {
  2527                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2528             }
  2529             _this->SetIcon(_this, icon, mask);
  2530             SDL_free(mask);
  2531         } else {
  2532             _this->SetIcon(_this, icon, mask);
  2533         }
  2534     }
  2535 }
  2536 #endif
  2537 
  2538 SDL_bool
  2539 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2540 {
  2541     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2542 
  2543     if (!window || !_this->GetWindowWMInfo) {
  2544         return SDL_FALSE;
  2545     }
  2546     return (_this->GetWindowWMInfo(_this, window, info));
  2547 }
  2548 
  2549 /* vi: set ts=4 sw=4 expandtab: */