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