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