src/video/SDL_video.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 18:46:11 +0000
branchgsoc2008_iphone
changeset 2372 06d986e73eb0
parent 2357 ad4a291c85ab
permissions -rw-r--r--
Fixed typo
     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 
    31 
    32 #include "glrenderer/SDL_renderer_gl.h"
    33 #include "glesrenderer/SDL_renderer_gles.h"
    34 
    35 #include "SDL_renderer_sw.h"
    36 #include "../events/SDL_sysevents.h"
    37 #include "../events/SDL_events_c.h"
    38 
    39 #if SDL_VIDEO_OPENGL_ES
    40 #include "SDL_opengles.h"
    41 #endif /* SDL_VIDEO_OPENGL */
    42 
    43 #if SDL_VIDEO_OPENGL
    44 #include "SDL_opengl.h"
    45 
    46 /* On Windows, windows.h defines CreateWindow */
    47 #ifdef CreateWindow
    48 #undef CreateWindow
    49 #endif
    50 #endif /* SDL_VIDEO_OPENGL */
    51 
    52 /* Available video drivers */
    53 static VideoBootStrap *bootstrap[] = {
    54 #if SDL_VIDEO_DRIVER_COCOA
    55     &COCOA_bootstrap,
    56 #endif
    57 #if SDL_VIDEO_DRIVER_X11
    58     &X11_bootstrap,
    59 #endif
    60 #if SDL_VIDEO_DRIVER_NANOX
    61     &NX_bootstrap,
    62 #endif
    63 #if SDL_VIDEO_DRIVER_IPOD
    64     &iPod_bootstrap,
    65 #endif
    66 #if SDL_VIDEO_DRIVER_WSCONS
    67     &WSCONS_bootstrap,
    68 #endif
    69 #if SDL_VIDEO_DRIVER_FBCON
    70     &FBCON_bootstrap,
    71 #endif
    72 #if SDL_VIDEO_DRIVER_DIRECTFB
    73     &DirectFB_bootstrap,
    74 #endif
    75 #if SDL_VIDEO_DRIVER_PS2GS
    76     &PS2GS_bootstrap,
    77 #endif
    78 #if SDL_VIDEO_DRIVER_VGL
    79     &VGL_bootstrap,
    80 #endif
    81 #if SDL_VIDEO_DRIVER_SVGALIB
    82     &SVGALIB_bootstrap,
    83 #endif
    84 #if SDL_VIDEO_DRIVER_GAPI
    85     &GAPI_bootstrap,
    86 #endif
    87 #if SDL_VIDEO_DRIVER_WIN32
    88     &WIN32_bootstrap,
    89 #endif
    90 #if SDL_VIDEO_DRIVER_BWINDOW
    91     &BWINDOW_bootstrap,
    92 #endif
    93 #if SDL_VIDEO_DRIVER_PHOTON
    94     &ph_bootstrap,
    95 #endif
    96 #if SDL_VIDEO_DRIVER_EPOC
    97     &EPOC_bootstrap,
    98 #endif
    99 #if SDL_VIDEO_DRIVER_XBIOS
   100     &XBIOS_bootstrap,
   101 #endif
   102 #if SDL_VIDEO_DRIVER_GEM
   103     &GEM_bootstrap,
   104 #endif
   105 #if SDL_VIDEO_DRIVER_DC
   106     &DC_bootstrap,
   107 #endif
   108 #if SDL_VIDEO_DRIVER_RISCOS
   109     &RISCOS_bootstrap,
   110 #endif
   111 #if SDL_VIDEO_DRIVER_OS2FS
   112     &OS2FSLib_bootstrap,
   113 #endif
   114 #if SDL_VIDEO_DRIVER_UIKIT
   115 	&UIKIT_bootstrap,
   116 #endif
   117 #if SDL_VIDEO_DRIVER_DUMMY
   118     &DUMMY_bootstrap,
   119 #endif
   120     NULL
   121 };
   122 
   123 static SDL_VideoDevice *_this = NULL;
   124 
   125 /* Various local functions */
   126 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   127 void SDL_VideoQuit(void);
   128 
   129 static int
   130 cmpmodes(const void *A, const void *B)
   131 {
   132     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   133     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   134 
   135     if (a.w != b.w) {
   136         return b.w - a.w;
   137     }
   138     if (a.h != b.h) {
   139         return b.h - a.h;
   140     }
   141     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   142         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   143     }
   144     if (a.refresh_rate != b.refresh_rate) {
   145         return b.refresh_rate - a.refresh_rate;
   146     }
   147     return 0;
   148 }
   149 
   150 static void
   151 SDL_UninitializedVideo()
   152 {
   153     SDL_SetError("Video subsystem has not been initialized");
   154 }
   155 
   156 int
   157 SDL_GetNumVideoDrivers(void)
   158 {
   159     return SDL_arraysize(bootstrap) - 1;
   160 }
   161 
   162 const char *
   163 SDL_GetVideoDriver(int index)
   164 {
   165     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   166         return bootstrap[index]->name;
   167     }
   168     return NULL;
   169 }
   170 
   171 /*
   172  * Initialize the video and event subsystems -- determine native pixel format
   173  */
   174 int
   175 SDL_VideoInit(const char *driver_name, Uint32 flags)
   176 {
   177     SDL_VideoDevice *video;
   178     int index;
   179     int i;
   180 
   181     /* Toggle the event thread flags, based on OS requirements */
   182 #if defined(MUST_THREAD_EVENTS)
   183     flags |= SDL_INIT_EVENTTHREAD;
   184 #elif defined(CANT_THREAD_EVENTS)
   185     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   186         SDL_SetError("OS doesn't support threaded events");
   187         return -1;
   188     }
   189 #endif
   190 
   191     /* Start the event loop */
   192     if (SDL_StartEventLoop(flags) < 0) {
   193         return -1;
   194     }
   195 
   196     /* Check to make sure we don't overwrite '_this' */
   197     if (_this != NULL) {
   198         SDL_VideoQuit();
   199     }
   200 
   201     /* Select the proper video driver */
   202     index = 0;
   203     video = NULL;
   204     if (driver_name == NULL) {
   205         driver_name = SDL_getenv("SDL_VIDEODRIVER");
   206     }
   207     if (driver_name != NULL) {
   208         for (i = 0; bootstrap[i]; ++i) {
   209             if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
   210                 if (bootstrap[i]->available()) {
   211                     video = bootstrap[i]->create(index);
   212                 }
   213                 break;
   214             }
   215         }
   216     } else {
   217         for (i = 0; bootstrap[i]; ++i) {
   218             if (bootstrap[i]->available()) {
   219                 video = bootstrap[i]->create(index);
   220                 if (video != NULL) {
   221                     break;
   222                 }
   223             }
   224         }
   225     }
   226     if (video == NULL) {
   227         if (driver_name) {
   228             SDL_SetError("%s not available", driver_name);
   229         } else {
   230             SDL_SetError("No available video device");
   231         }
   232         return -1;
   233     }
   234     _this = video;
   235     _this->name = bootstrap[i]->name;
   236     _this->next_object_id = 1;
   237 
   238 
   239     /* Set some very sane GL defaults */
   240     _this->gl_config.driver_loaded = 0;
   241     _this->gl_config.dll_handle = NULL;
   242     _this->gl_config.red_size = 3;
   243     _this->gl_config.green_size = 3;
   244     _this->gl_config.blue_size = 2;
   245     _this->gl_config.alpha_size = 0;
   246     _this->gl_config.buffer_size = 0;
   247     _this->gl_config.depth_size = 16;
   248     _this->gl_config.stencil_size = 0;
   249     _this->gl_config.double_buffer = 1;
   250     _this->gl_config.accum_red_size = 0;
   251     _this->gl_config.accum_green_size = 0;
   252     _this->gl_config.accum_blue_size = 0;
   253     _this->gl_config.accum_alpha_size = 0;
   254     _this->gl_config.stereo = 0;
   255     _this->gl_config.multisamplebuffers = 0;
   256     _this->gl_config.multisamplesamples = 0;
   257 	_this->gl_config.retained_backing = 1; /* most systems have retained backing on the color buffers */
   258     _this->gl_config.accelerated = -1;  /* not known, don't set */
   259 
   260     /* Initialize the video subsystem */
   261     if (_this->VideoInit(_this) < 0) {
   262         SDL_VideoQuit();
   263         return -1;
   264     }
   265 
   266     /* Make sure some displays were added */
   267     if (_this->num_displays == 0) {
   268         SDL_SetError("The video driver did not add any displays");
   269         SDL_VideoQuit();
   270         return (-1);
   271     }
   272 
   273     /* The software renderer is always available */
   274     for (i = 0; i < _this->num_displays; ++i) {
   275 #if SDL_VIDEO_RENDER_OGL
   276         SDL_AddRenderDriver(i, &GL_RenderDriver);
   277 #endif
   278 		
   279 #if SDL_VIDEO_RENDER_OGL_ES
   280 		SDL_AddRenderDriver(i, &GL_ES_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 
   959     if (!window || title == window->title) {
   960         return;
   961     }
   962 
   963     if (window->title) {
   964         SDL_free(window->title);
   965     }
   966     if (title) {
   967         window->title = SDL_strdup(title);
   968     } else {
   969         window->title = NULL;
   970     }
   971 
   972     if (_this->SetWindowTitle) {
   973         _this->SetWindowTitle(_this, window);
   974     }
   975 }
   976 
   977 const char *
   978 SDL_GetWindowTitle(SDL_WindowID windowID)
   979 {
   980     SDL_Window *window = SDL_GetWindowFromID(windowID);
   981 
   982     if (!window) {
   983         return NULL;
   984     }
   985     return window->title;
   986 }
   987 
   988 void
   989 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   990 {
   991     SDL_Window *window = SDL_GetWindowFromID(windowID);
   992 
   993     if (!window) {
   994         return;
   995     }
   996     window->userdata = userdata;
   997 }
   998 
   999 void *
  1000 SDL_GetWindowData(SDL_WindowID windowID)
  1001 {
  1002     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1003 
  1004     if (!window) {
  1005         return NULL;
  1006     }
  1007     return window->userdata;
  1008 }
  1009 
  1010 void
  1011 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
  1012 {
  1013     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1014     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1015 
  1016     if (!window) {
  1017         return;
  1018     }
  1019 
  1020     if (x == SDL_WINDOWPOS_CENTERED) {
  1021         window->x = (display->current_mode.w - window->w) / 2;
  1022     } else if (x != SDL_WINDOWPOS_UNDEFINED) {
  1023         window->x = x;
  1024     }
  1025     if (y == SDL_WINDOWPOS_CENTERED) {
  1026         window->y = (display->current_mode.h - window->h) / 2;
  1027     } else if (y != SDL_WINDOWPOS_UNDEFINED) {
  1028         window->y = y;
  1029     }
  1030 
  1031     if (_this->SetWindowPosition) {
  1032         _this->SetWindowPosition(_this, window);
  1033     }
  1034 }
  1035 
  1036 void
  1037 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
  1038 {
  1039     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1040 
  1041     if (!window) {
  1042         return;
  1043     }
  1044     if (x) {
  1045         *x = window->x;
  1046     }
  1047     if (y) {
  1048         *y = window->y;
  1049     }
  1050 }
  1051 
  1052 void
  1053 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
  1054 {
  1055     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1056 
  1057     if (!window) {
  1058         return;
  1059     }
  1060 
  1061     window->w = w;
  1062     window->h = h;
  1063 
  1064     if (_this->SetWindowSize) {
  1065         _this->SetWindowSize(_this, window);
  1066     }
  1067 }
  1068 
  1069 void
  1070 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
  1071 {
  1072     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1073 
  1074     if (!window) {
  1075         return;
  1076     }
  1077     if (w) {
  1078         *w = window->w;
  1079     }
  1080     if (h) {
  1081         *h = window->h;
  1082     }
  1083 }
  1084 
  1085 void
  1086 SDL_ShowWindow(SDL_WindowID windowID)
  1087 {
  1088     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1089 
  1090     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
  1091         return;
  1092     }
  1093 
  1094     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
  1095 
  1096     if (_this->ShowWindow) {
  1097         _this->ShowWindow(_this, window);
  1098     }
  1099 }
  1100 
  1101 void
  1102 SDL_HideWindow(SDL_WindowID windowID)
  1103 {
  1104     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1105 
  1106     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1107         return;
  1108     }
  1109 
  1110     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  1111 
  1112     if (_this->HideWindow) {
  1113         _this->HideWindow(_this, window);
  1114     }
  1115 }
  1116 
  1117 void
  1118 SDL_RaiseWindow(SDL_WindowID windowID)
  1119 {
  1120     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1121 
  1122     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
  1123         return;
  1124     }
  1125 
  1126     if (_this->RaiseWindow) {
  1127         _this->RaiseWindow(_this, window);
  1128     }
  1129 }
  1130 
  1131 void
  1132 SDL_MaximizeWindow(SDL_WindowID windowID)
  1133 {
  1134     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1135 
  1136     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
  1137         return;
  1138     }
  1139 
  1140     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1141 
  1142     if (_this->MaximizeWindow) {
  1143         _this->MaximizeWindow(_this, window);
  1144     }
  1145 }
  1146 
  1147 void
  1148 SDL_MinimizeWindow(SDL_WindowID windowID)
  1149 {
  1150     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1151 
  1152     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
  1153         return;
  1154     }
  1155 
  1156     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  1157 
  1158     if (_this->MinimizeWindow) {
  1159         _this->MinimizeWindow(_this, window);
  1160     }
  1161 }
  1162 
  1163 void
  1164 SDL_RestoreWindow(SDL_WindowID windowID)
  1165 {
  1166     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1167 
  1168     if (!window
  1169         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
  1170         return;
  1171     }
  1172 
  1173     SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1174 
  1175     if (_this->RestoreWindow) {
  1176         _this->RestoreWindow(_this, window);
  1177     }
  1178 }
  1179 
  1180 int
  1181 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
  1182 {
  1183     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1184 
  1185     if (!window) {
  1186         return -1;
  1187     }
  1188 
  1189     if (fullscreen) {
  1190         fullscreen = SDL_WINDOW_FULLSCREEN;
  1191     }
  1192     if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
  1193         return 0;
  1194     }
  1195 
  1196     if (fullscreen) {
  1197         window->flags |= SDL_WINDOW_FULLSCREEN;
  1198 
  1199         if (FULLSCREEN_VISIBLE(window)) {
  1200             SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1201 
  1202             /* Hide any other fullscreen windows */
  1203             int i;
  1204             for (i = 0; i < display->num_windows; ++i) {
  1205                 SDL_Window *other = &display->windows[i];
  1206                 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
  1207                     SDL_MinimizeWindow(other->id);
  1208                 }
  1209             }
  1210 
  1211             SDL_SetDisplayMode(&display->fullscreen_mode);
  1212         }
  1213     } else {
  1214         window->flags &= ~SDL_WINDOW_FULLSCREEN;
  1215 
  1216         if (FULLSCREEN_VISIBLE(window)) {
  1217             SDL_SetDisplayMode(NULL);
  1218         }
  1219     }
  1220     return 0;
  1221 }
  1222 
  1223 void
  1224 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1225 {
  1226     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1227 
  1228     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1229         return;
  1230     }
  1231 
  1232     if (mode) {
  1233         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1234     } else {
  1235         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1236     }
  1237 
  1238     if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
  1239         _this->SetWindowGrab(_this, window);
  1240     }
  1241 }
  1242 
  1243 int
  1244 SDL_GetWindowGrab(SDL_WindowID windowID)
  1245 {
  1246     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1247 
  1248     if (!window) {
  1249         return 0;
  1250     }
  1251 
  1252     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1253 }
  1254 
  1255 void
  1256 SDL_OnWindowShown(SDL_Window * window)
  1257 {
  1258 }
  1259 
  1260 void
  1261 SDL_OnWindowHidden(SDL_Window * window)
  1262 {
  1263 }
  1264 
  1265 void
  1266 SDL_OnWindowResized(SDL_Window * window)
  1267 {
  1268     SDL_Renderer *renderer = window->renderer;
  1269 
  1270     if (renderer && renderer->DisplayModeChanged) {
  1271         renderer->DisplayModeChanged(renderer);
  1272     }
  1273 }
  1274 
  1275 void
  1276 SDL_OnWindowFocusGained(SDL_Window * window)
  1277 {
  1278     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1279 
  1280     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1281         SDL_SetDisplayMode(&display->fullscreen_mode);
  1282     }
  1283     if (display->gamma && _this->SetDisplayGammaRamp) {
  1284         _this->SetDisplayGammaRamp(_this, display->gamma);
  1285     }
  1286     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1287         _this->SetWindowGrab(_this, window);
  1288     }
  1289 }
  1290 
  1291 void
  1292 SDL_OnWindowFocusLost(SDL_Window * window)
  1293 {
  1294     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
  1295 
  1296     if (window->flags & SDL_WINDOW_FULLSCREEN) {
  1297         SDL_MinimizeWindow(window->id);
  1298         SDL_SetDisplayMode(NULL);
  1299     }
  1300     if (display->gamma && _this->SetDisplayGammaRamp) {
  1301         _this->SetDisplayGammaRamp(_this, display->saved_gamma);
  1302     }
  1303     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
  1304         _this->SetWindowGrab(_this, window);
  1305     }
  1306 }
  1307 
  1308 SDL_WindowID
  1309 SDL_GetFocusWindow(void)
  1310 {
  1311     SDL_VideoDisplay *display;
  1312     int i;
  1313 
  1314     if (!_this) {
  1315         return 0;
  1316     }
  1317 
  1318     display = &SDL_CurrentDisplay;
  1319     for (i = 0; i < display->num_windows; ++i) {
  1320         SDL_Window *window = &display->windows[i];
  1321 
  1322         if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
  1323             return window->id;
  1324         }
  1325     }
  1326     return 0;
  1327 }
  1328 
  1329 void
  1330 SDL_DestroyWindow(SDL_WindowID windowID)
  1331 {
  1332     int i, j;
  1333 
  1334     if (!_this) {
  1335         return;
  1336     }
  1337 
  1338     /* Restore video mode, etc. */
  1339     SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  1340 
  1341     for (i = 0; i < _this->num_displays; ++i) {
  1342         SDL_VideoDisplay *display = &_this->displays[i];
  1343         for (j = 0; j < display->num_windows; ++j) {
  1344             SDL_Window *window = &display->windows[j];
  1345             if (window->id != windowID) {
  1346                 continue;
  1347             }
  1348             if (window->title) {
  1349                 SDL_free(window->title);
  1350                 window->title = NULL;
  1351             }
  1352             if (window->renderer) {
  1353                 SDL_DestroyRenderer(window->id);
  1354                 window->renderer = NULL;
  1355             }
  1356             if (_this->DestroyWindow) {
  1357                 _this->DestroyWindow(_this, window);
  1358             }
  1359             if (j != display->num_windows - 1) {
  1360                 SDL_memcpy(&display->windows[i],
  1361                            &display->windows[i + 1],
  1362                            (display->num_windows - i - 1) * sizeof(*window));
  1363             }
  1364             --display->num_windows;
  1365             return;
  1366         }
  1367     }
  1368 }
  1369 
  1370 void
  1371 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1372 {
  1373     SDL_VideoDisplay *display;
  1374     SDL_RenderDriver *render_drivers;
  1375 
  1376     if (displayIndex >= _this->num_displays) {
  1377         return;
  1378     }
  1379     display = &_this->displays[displayIndex];
  1380 
  1381     render_drivers =
  1382         SDL_realloc(display->render_drivers,
  1383                     (display->num_render_drivers +
  1384                      1) * sizeof(*render_drivers));
  1385     if (render_drivers) {
  1386         render_drivers[display->num_render_drivers] = *driver;
  1387         display->render_drivers = render_drivers;
  1388         display->num_render_drivers++;
  1389     }
  1390 }
  1391 
  1392 int
  1393 SDL_GetNumRenderDrivers(void)
  1394 {
  1395     if (_this) {
  1396         return SDL_CurrentDisplay.num_render_drivers;
  1397     }
  1398     return 0;
  1399 }
  1400 
  1401 int
  1402 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  1403 {
  1404     if (!_this) {
  1405         SDL_UninitializedVideo();
  1406         return -1;
  1407     }
  1408 
  1409     if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  1410         SDL_SetError("index must be in the range of 0 - %d",
  1411                      SDL_GetNumRenderDrivers() - 1);
  1412         return -1;
  1413     }
  1414     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1415     return 0;
  1416 }
  1417 
  1418 int
  1419 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1420 {
  1421     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1422 
  1423     if (!window) {
  1424         return 0;
  1425     }
  1426 
  1427     if (index < 0) {
  1428         const char *override = SDL_getenv("SDL_VIDEO_RENDERER");
  1429         		
  1430 		int n = SDL_GetNumRenderDrivers();
  1431 				
  1432         for (index = 0; index < n; ++index) {
  1433             SDL_RenderDriver *driver =
  1434                 &SDL_CurrentDisplay.render_drivers[index];
  1435 
  1436             if (override) {
  1437                 if (SDL_strcasecmp(override, driver->info.name) == 0) {
  1438                     break;
  1439                 }
  1440             } else {
  1441                 if ((driver->info.flags & flags) == flags) {
  1442                     break;
  1443                 }
  1444             }
  1445         }
  1446         if (index == n) {
  1447             SDL_SetError("Couldn't find matching render driver");
  1448             return -1;
  1449         }
  1450     }
  1451 
  1452     if (index >= SDL_GetNumRenderDrivers()) {
  1453         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1454                      SDL_GetNumRenderDrivers() - 1);
  1455         return -1;
  1456     }
  1457 
  1458     /* Free any existing renderer */
  1459     SDL_DestroyRenderer(windowID);
  1460 
  1461     /* Create a new renderer instance */
  1462     window->renderer = SDL_CurrentDisplay.render_drivers[index]
  1463         .CreateRenderer(window, flags);
  1464 	
  1465     SDL_SelectRenderer(window->id);
  1466 
  1467     return 0;
  1468 }
  1469 
  1470 int
  1471 SDL_SelectRenderer(SDL_WindowID windowID)
  1472 {
  1473     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1474     SDL_Renderer *renderer;
  1475 
  1476     if (!window || !window->renderer) {
  1477         return -1;
  1478     }
  1479     renderer = window->renderer;
  1480     if (renderer && renderer->ActivateRenderer) {
  1481         if (renderer->ActivateRenderer(renderer) < 0) {
  1482             return -1;
  1483         }
  1484     }
  1485     SDL_CurrentDisplay.current_renderer = renderer;
  1486     return 0;
  1487 }
  1488 
  1489 int
  1490 SDL_GetRendererInfo(SDL_RendererInfo * info)
  1491 {
  1492     if (!_this) {
  1493         SDL_UninitializedVideo();
  1494         return -1;
  1495     }
  1496 
  1497     if (!SDL_CurrentDisplay.current_renderer) {
  1498         SDL_SetError("There is no current renderer");
  1499         return -1;
  1500     }
  1501     *info = SDL_CurrentDisplay.current_renderer->info;
  1502     return 0;
  1503 }
  1504 
  1505 SDL_TextureID
  1506 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1507 {
  1508     int hash;
  1509     SDL_Renderer *renderer;
  1510     SDL_Texture *texture;
  1511 
  1512     if (!_this) {
  1513         SDL_UninitializedVideo();
  1514         return 0;
  1515     }
  1516 
  1517     renderer = SDL_CurrentDisplay.current_renderer;
  1518     if (!renderer || !renderer->CreateTexture) {
  1519         return 0;
  1520     }
  1521 
  1522     texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  1523     if (!texture) {
  1524         SDL_OutOfMemory();
  1525         return 0;
  1526     }
  1527 
  1528     texture->id = _this->next_object_id++;
  1529     texture->format = format;
  1530     texture->access = access;
  1531     texture->w = w;
  1532     texture->h = h;
  1533     texture->r = 255;
  1534     texture->g = 255;
  1535     texture->b = 255;
  1536     texture->a = 255;
  1537     texture->renderer = renderer;
  1538 
  1539     if (renderer->CreateTexture(renderer, texture) < 0) {
  1540         if (renderer->DestroyTexture) {
  1541             renderer->DestroyTexture(renderer, texture);
  1542         }
  1543         SDL_free(texture);
  1544         return 0;
  1545     }
  1546 
  1547     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1548     texture->next = SDL_CurrentDisplay.textures[hash];
  1549     SDL_CurrentDisplay.textures[hash] = texture;
  1550 
  1551     return texture->id;
  1552 }
  1553 
  1554 SDL_TextureID
  1555 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
  1556 {
  1557     SDL_TextureID textureID;
  1558     SDL_PixelFormat *fmt;
  1559     int bpp;
  1560     Uint32 Rmask, Gmask, Bmask, Amask;
  1561 
  1562     if (!surface) {
  1563         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1564         return 0;
  1565     }
  1566     fmt = surface->format;
  1567 
  1568     if (format) { /* if we were passed in a format */
  1569         if (!SDL_PixelFormatEnumToMasks
  1570             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1571             SDL_SetError("Unknown pixel format");
  1572             return 0;
  1573         }
  1574     } else {
  1575         if (surface->format->Amask
  1576             || !(surface->map->info.flags &
  1577                  (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
  1578 			bpp = fmt->BitsPerPixel;
  1579             Rmask = fmt->Rmask;
  1580             Gmask = fmt->Gmask;
  1581             Bmask = fmt->Bmask;
  1582             Amask = fmt->Amask;
  1583         } else {
  1584             /* Need a format with alpha */
  1585             bpp = 32;
  1586             Rmask = 0x00FF0000;
  1587             Gmask = 0x0000FF00;
  1588             Bmask = 0x000000FF;
  1589             Amask = 0xFF000000;
  1590         }
  1591         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1592         if (!format) {
  1593             SDL_SetError("Unknown pixel format");
  1594             return 0;
  1595         }
  1596     }
  1597 
  1598     textureID = SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
  1599                           surface->h);
  1600     if (!textureID) {
  1601         return 0;
  1602     }
  1603 
  1604     if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
  1605         && Bmask == fmt->Bmask && Amask == fmt->Amask) {
  1606         if (SDL_MUSTLOCK(surface)) {
  1607             if (SDL_LockSurface(surface) < 0) {
  1608                 SDL_DestroyTexture(textureID);
  1609                 return 0;
  1610             }
  1611             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1612                               surface->pitch);
  1613             SDL_UnlockSurface(surface);
  1614         } else {
  1615             SDL_UpdateTexture(textureID, NULL, surface->pixels,
  1616                               surface->pitch);
  1617         }
  1618     } else {
  1619         SDL_PixelFormat *dst_fmt;
  1620         SDL_Surface *dst = NULL;
  1621 
  1622         /* Set up a destination surface for the texture update */
  1623         dst_fmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1624         if (dst_fmt) {
  1625             if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1626                 dst_fmt->palette =
  1627                     SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
  1628                 if (dst_fmt->palette) {
  1629                     /* FIXME: Should we try to copy fmt->palette? */
  1630                     SDL_DitherColors(dst_fmt->palette->colors,
  1631                                      SDL_BITSPERPIXEL(format));
  1632                 }
  1633             }			
  1634 			dst = SDL_ConvertSurface(surface, dst_fmt, 0);
  1635             if (dst) {
  1636                 SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
  1637                 SDL_FreeSurface(dst);
  1638             }
  1639             if (dst_fmt->palette) {
  1640                 SDL_FreePalette(dst_fmt->palette);
  1641             }
  1642             SDL_FreeFormat(dst_fmt);
  1643         }
  1644         if (!dst) {
  1645             SDL_DestroyTexture(textureID);
  1646             return 0;
  1647         }
  1648 	}
  1649 
  1650     if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
  1651         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1652                               fmt->palette->ncolors);
  1653     }
  1654 
  1655     return textureID;
  1656 }
  1657 
  1658 static __inline__ SDL_Texture *
  1659 SDL_GetTextureFromID(SDL_TextureID textureID)
  1660 {
  1661     int hash;
  1662     SDL_Texture *texture;
  1663 
  1664     if (!_this) {
  1665         return NULL;
  1666     }
  1667 
  1668     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1669     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1670          texture = texture->next) {
  1671         if (texture->id == textureID) {
  1672             return texture;
  1673         }
  1674     }
  1675     return NULL;
  1676 }
  1677 
  1678 int
  1679 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1680                  int *w, int *h)
  1681 {
  1682     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1683 
  1684     if (!texture) {
  1685         return -1;
  1686     }
  1687 
  1688     if (format) {
  1689         *format = texture->format;
  1690     }
  1691     if (access) {
  1692         *access = texture->access;
  1693     }
  1694     if (w) {
  1695         *w = texture->w;
  1696     }
  1697     if (h) {
  1698         *h = texture->h;
  1699     }
  1700     return 0;
  1701 }
  1702 
  1703 int
  1704 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1705 {
  1706     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1707     SDL_Renderer *renderer;
  1708 
  1709     if (!texture) {
  1710         return -1;
  1711     }
  1712 
  1713     renderer = texture->renderer;
  1714     if (!renderer->QueryTexturePixels) {
  1715         return -1;
  1716     }
  1717     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1718 }
  1719 
  1720 int
  1721 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1722                       int firstcolor, int ncolors)
  1723 {
  1724     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1725     SDL_Renderer *renderer;
  1726 
  1727     if (!texture) {
  1728         return -1;
  1729     }
  1730 
  1731     renderer = texture->renderer;
  1732     if (!renderer->SetTexturePalette) {
  1733         return -1;
  1734     }
  1735     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1736                                        ncolors);
  1737 }
  1738 
  1739 int
  1740 SDL_GetTexturePalette(SDL_TextureID textureID, 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->GetTexturePalette) {
  1752         return -1;
  1753     }
  1754     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1755                                        ncolors);
  1756 }
  1757 
  1758 int
  1759 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
  1760 {
  1761     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1762     SDL_Renderer *renderer;
  1763 
  1764     if (!texture) {
  1765         return -1;
  1766     }
  1767 
  1768     renderer = texture->renderer;
  1769     if (!renderer->SetTextureColorMod) {
  1770         return -1;
  1771     }
  1772     if (r < 255 || g < 255 || b < 255) {
  1773         texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1774     } else {
  1775         texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1776     }
  1777     texture->r = r;
  1778     texture->g = g;
  1779     texture->b = b;
  1780     return renderer->SetTextureColorMod(renderer, texture);
  1781 }
  1782 
  1783 int
  1784 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
  1785                        Uint8 * b)
  1786 {
  1787     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1788     SDL_Renderer *renderer;
  1789 
  1790     if (!texture) {
  1791         return -1;
  1792     }
  1793 
  1794     renderer = texture->renderer;
  1795     if (r) {
  1796         *r = texture->r;
  1797     }
  1798     if (g) {
  1799         *g = texture->g;
  1800     }
  1801     if (b) {
  1802         *b = texture->b;
  1803     }
  1804     return 0;
  1805 }
  1806 
  1807 int
  1808 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
  1809 {
  1810     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1811     SDL_Renderer *renderer;
  1812 
  1813     if (!texture) {
  1814         return -1;
  1815     }
  1816 
  1817     renderer = texture->renderer;
  1818     if (!renderer->SetTextureAlphaMod) {
  1819         return -1;
  1820     }
  1821     if (alpha < 255) {
  1822         texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1823     } else {
  1824         texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1825     }
  1826     texture->a = alpha;
  1827     return renderer->SetTextureAlphaMod(renderer, texture);
  1828 }
  1829 
  1830 int
  1831 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
  1832 {
  1833     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1834 
  1835     if (!texture) {
  1836         return -1;
  1837     }
  1838 
  1839     if (alpha) {
  1840         *alpha = texture->a;
  1841     }
  1842     return 0;
  1843 }
  1844 
  1845 int
  1846 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
  1847 {
  1848     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1849     SDL_Renderer *renderer;
  1850 
  1851     if (!texture) {
  1852         return -1;
  1853     }
  1854 
  1855     renderer = texture->renderer;
  1856     if (!renderer->SetTextureBlendMode) {
  1857         return -1;
  1858     }
  1859     texture->blendMode = blendMode;
  1860     return renderer->SetTextureBlendMode(renderer, texture);
  1861 }
  1862 
  1863 int
  1864 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
  1865 {
  1866     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1867 
  1868     if (!texture) {
  1869         return -1;
  1870     }
  1871 
  1872     if (blendMode) {
  1873         *blendMode = texture->blendMode;
  1874     }
  1875     return 0;
  1876 }
  1877 
  1878 int
  1879 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
  1880 {
  1881     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1882     SDL_Renderer *renderer;
  1883 
  1884     if (!texture) {
  1885         return -1;
  1886     }
  1887 
  1888     renderer = texture->renderer;
  1889     if (!renderer->SetTextureScaleMode) {
  1890         return -1;
  1891     }
  1892     texture->scaleMode = scaleMode;
  1893     return renderer->SetTextureScaleMode(renderer, texture);
  1894 }
  1895 
  1896 int
  1897 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
  1898 {
  1899     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1900 
  1901     if (!texture) {
  1902         return -1;
  1903     }
  1904 
  1905     if (scaleMode) {
  1906         *scaleMode = texture->scaleMode;
  1907     }
  1908     return 0;
  1909 }
  1910 
  1911 int
  1912 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1913                   const void *pixels, int pitch)
  1914 {
  1915     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1916     SDL_Renderer *renderer;
  1917     SDL_Rect full_rect;
  1918 
  1919     if (!texture) {
  1920         return -1;
  1921     }
  1922 
  1923     renderer = texture->renderer;
  1924     if (!renderer->UpdateTexture) {
  1925         return -1;
  1926     }
  1927 
  1928     if (!rect) {
  1929         full_rect.x = 0;
  1930         full_rect.y = 0;
  1931         full_rect.w = texture->w;
  1932         full_rect.h = texture->h;
  1933         rect = &full_rect;
  1934     }
  1935 
  1936     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1937 }
  1938 
  1939 int
  1940 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1941                 void **pixels, int *pitch)
  1942 {
  1943     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1944     SDL_Renderer *renderer;
  1945     SDL_Rect full_rect;
  1946 
  1947     if (!texture) {
  1948         return -1;
  1949     }
  1950     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1951         SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1952         return -1;
  1953     }
  1954 
  1955     renderer = texture->renderer;
  1956     if (!renderer->LockTexture) {
  1957         return -1;
  1958     }
  1959 
  1960     if (!rect) {
  1961         full_rect.x = 0;
  1962         full_rect.y = 0;
  1963         full_rect.w = texture->w;
  1964         full_rect.h = texture->h;
  1965         rect = &full_rect;
  1966     }
  1967 
  1968     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1969                                  pitch);
  1970 }
  1971 
  1972 void
  1973 SDL_UnlockTexture(SDL_TextureID textureID)
  1974 {
  1975     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1976     SDL_Renderer *renderer;
  1977 
  1978     if (!texture) {
  1979         return;
  1980     }
  1981     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1982         return;
  1983     }
  1984 
  1985     renderer = texture->renderer;
  1986     if (!renderer->UnlockTexture) {
  1987         return;
  1988     }
  1989     renderer->UnlockTexture(renderer, texture);
  1990 }
  1991 
  1992 void
  1993 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1994                  const SDL_Rect * rects)
  1995 {
  1996     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1997     SDL_Renderer *renderer;
  1998 
  1999     if (!texture) {
  2000         return;
  2001     }
  2002     if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  2003         return;
  2004     }
  2005 
  2006     renderer = texture->renderer;
  2007     if (!renderer->DirtyTexture) {
  2008         return;
  2009     }
  2010     renderer->DirtyTexture(renderer, texture, numrects, rects);
  2011 }
  2012 
  2013 int
  2014 SDL_RenderFill(Uint8 r, Uint8 g, Uint8 b, Uint8 a, const SDL_Rect * rect)
  2015 {
  2016 		
  2017     SDL_Renderer *renderer;
  2018     SDL_Window *window;
  2019     SDL_Rect real_rect;
  2020 
  2021     if (!_this) {
  2022         SDL_UninitializedVideo();
  2023         return -1;
  2024     }
  2025 
  2026     renderer = SDL_CurrentDisplay.current_renderer;
  2027     if (!renderer || !renderer->RenderFill) {
  2028         return -1;
  2029     }
  2030 
  2031     window = SDL_GetWindowFromID(renderer->window);
  2032     real_rect.x = 0;
  2033     real_rect.y = 0;
  2034     real_rect.w = window->w;
  2035     real_rect.h = window->h;
  2036     if (rect) {
  2037         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2038             return 0;
  2039         }
  2040     }
  2041 
  2042     return renderer->RenderFill(renderer, r, g, b, a, &real_rect);
  2043 }
  2044 
  2045 int
  2046 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  2047                const SDL_Rect * dstrect)
  2048 {
  2049     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  2050     SDL_Renderer *renderer;
  2051     SDL_Window *window;
  2052     SDL_Rect real_srcrect;
  2053     SDL_Rect real_dstrect;
  2054 
  2055     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  2056         return -1;
  2057     }
  2058 
  2059     renderer = SDL_CurrentDisplay.current_renderer;
  2060     if (!renderer || !renderer->RenderCopy) {
  2061         return -1;
  2062     }
  2063 
  2064     window = SDL_GetWindowFromID(renderer->window);
  2065     if (srcrect) {
  2066         real_srcrect = *srcrect;
  2067     } else {
  2068         real_srcrect.x = 0;
  2069         real_srcrect.y = 0;
  2070         real_srcrect.w = texture->w;
  2071         real_srcrect.h = texture->h;
  2072     }
  2073     if (dstrect) {
  2074         real_dstrect = *dstrect;
  2075     } else {
  2076         real_dstrect.x = 0;
  2077         real_dstrect.y = 0;
  2078         real_dstrect.w = window->w;
  2079         real_dstrect.h = window->h;
  2080     }
  2081 
  2082     return renderer->RenderCopy(renderer, texture, &real_srcrect,
  2083                                 &real_dstrect);
  2084 }
  2085 
  2086 void
  2087 SDL_RenderPresent(void)
  2088 {
  2089     SDL_Renderer *renderer;
  2090 
  2091     if (!_this) {
  2092         SDL_UninitializedVideo();
  2093         return;
  2094     }
  2095 
  2096     renderer = SDL_CurrentDisplay.current_renderer;
  2097     if (!renderer || !renderer->RenderPresent) {
  2098         return;
  2099     }
  2100     renderer->RenderPresent(renderer);
  2101 }
  2102 
  2103 void
  2104 SDL_DestroyTexture(SDL_TextureID textureID)
  2105 {
  2106     int hash;
  2107     SDL_Texture *prev, *texture;
  2108     SDL_Renderer *renderer;
  2109 
  2110     if (!_this) {
  2111         SDL_UninitializedVideo();
  2112         return;
  2113     }
  2114 
  2115     /* Look up the texture in the hash table */
  2116     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  2117     prev = NULL;
  2118     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  2119          prev = texture, texture = texture->next) {
  2120         if (texture->id == textureID) {
  2121             break;
  2122         }
  2123     }
  2124     if (!texture) {
  2125         return;
  2126     }
  2127 
  2128     /* Unlink the texture from the list */
  2129     if (prev) {
  2130         prev->next = texture->next;
  2131     } else {
  2132         SDL_CurrentDisplay.textures[hash] = texture->next;
  2133     }
  2134 
  2135     /* Free the texture */
  2136     renderer = texture->renderer;
  2137     renderer->DestroyTexture(renderer, texture);
  2138     SDL_free(texture);
  2139 }
  2140 
  2141 void
  2142 SDL_DestroyRenderer(SDL_WindowID windowID)
  2143 {
  2144     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2145     SDL_Renderer *renderer;
  2146     int i;
  2147 
  2148     if (!window) {
  2149         return;
  2150     }
  2151 
  2152     renderer = window->renderer;
  2153     if (!renderer) {
  2154         return;
  2155     }
  2156 
  2157     /* Free existing textures for this renderer */
  2158     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  2159         SDL_Texture *texture;
  2160         SDL_Texture *prev = NULL;
  2161         SDL_Texture *next;
  2162         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  2163              texture = next) {
  2164             next = texture->next;
  2165             if (texture->renderer == renderer) {
  2166                 if (prev) {
  2167                     prev->next = next;
  2168                 } else {
  2169                     SDL_CurrentDisplay.textures[i] = next;
  2170                 }
  2171                 renderer->DestroyTexture(renderer, texture);
  2172                 SDL_free(texture);
  2173             } else {
  2174                 prev = texture;
  2175             }
  2176         }
  2177     }
  2178 
  2179     /* Free the renderer instance */
  2180     renderer->DestroyRenderer(renderer);
  2181 
  2182     /* Clear references */
  2183     window->renderer = NULL;
  2184     if (SDL_CurrentDisplay.current_renderer == renderer) {
  2185         SDL_CurrentDisplay.current_renderer = NULL;
  2186     }
  2187 }
  2188 
  2189 void
  2190 SDL_VideoQuit(void)
  2191 {
  2192     int i, j;
  2193 
  2194     if (!_this) {
  2195         return;
  2196     }
  2197 
  2198     /* Halt event processing before doing anything else */
  2199     SDL_StopEventLoop();
  2200 
  2201     /* Clean up the system video */
  2202     for (i = _this->num_displays; i--;) {
  2203         SDL_VideoDisplay *display = &_this->displays[i];
  2204         for (j = display->num_windows; j--;) {
  2205             SDL_DestroyWindow(display->windows[i].id);
  2206         }
  2207         if (display->windows) {
  2208             SDL_free(display->windows);
  2209             display->windows = NULL;
  2210         }
  2211         display->num_windows = 0;
  2212         if (display->render_drivers) {
  2213             SDL_free(display->render_drivers);
  2214             display->render_drivers = NULL;
  2215         }
  2216         display->num_render_drivers = 0;
  2217     }
  2218     _this->VideoQuit(_this);
  2219 
  2220     for (i = _this->num_displays; i--;) {
  2221         SDL_VideoDisplay *display = &_this->displays[i];
  2222         for (j = display->num_display_modes; j--;) {
  2223             if (display->display_modes[j].driverdata) {
  2224                 SDL_free(display->display_modes[j].driverdata);
  2225                 display->display_modes[j].driverdata = NULL;
  2226             }
  2227         }
  2228         if (display->display_modes) {
  2229             SDL_free(display->display_modes);
  2230             display->display_modes = NULL;
  2231         }
  2232         if (display->desktop_mode.driverdata) {
  2233             SDL_free(display->desktop_mode.driverdata);
  2234             display->desktop_mode.driverdata = NULL;
  2235         }
  2236         if (display->palette) {
  2237             SDL_FreePalette(display->palette);
  2238             display->palette = NULL;
  2239         }
  2240         if (display->gamma) {
  2241             SDL_free(display->gamma);
  2242             display->gamma = NULL;
  2243         }
  2244         if (display->driverdata) {
  2245             SDL_free(display->driverdata);
  2246             display->driverdata = NULL;
  2247         }
  2248     }
  2249     if (_this->displays) {
  2250         SDL_free(_this->displays);
  2251         _this->displays = NULL;
  2252     }
  2253     _this->free(_this);
  2254     _this = NULL;
  2255 }
  2256 
  2257 int
  2258 SDL_GL_LoadLibrary(const char *path)
  2259 {
  2260     int retval;
  2261 
  2262     if (!_this) {
  2263         SDL_UninitializedVideo();
  2264         return -1;
  2265     }
  2266 
  2267     if (_this->GL_LoadLibrary) {
  2268         retval = _this->GL_LoadLibrary(_this, path);
  2269     } else {
  2270         SDL_SetError("No dynamic GL support in video driver");
  2271         retval = -1;
  2272     }
  2273     return (retval);
  2274 }
  2275 
  2276 void *
  2277 SDL_GL_GetProcAddress(const char *proc)
  2278 {
  2279     void *func;
  2280 
  2281     if (!_this) {
  2282         SDL_UninitializedVideo();
  2283         return NULL;
  2284     }
  2285 
  2286     func = NULL;
  2287     if (_this->GL_GetProcAddress) {
  2288         if (_this->gl_config.driver_loaded) {
  2289             func = _this->GL_GetProcAddress(_this, proc);
  2290         } else {
  2291             SDL_SetError("No GL driver has been loaded");
  2292         }
  2293     } else {
  2294         SDL_SetError("No dynamic GL support in video driver");
  2295     }
  2296     return func;
  2297 }
  2298 
  2299 SDL_bool
  2300 SDL_GL_ExtensionSupported(const char *extension)
  2301 {
  2302 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2303 	
  2304 	const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
  2305     const char *extensions;
  2306     const char *start;
  2307     const char *where, *terminator;
  2308 
  2309     /* Extension names should not have spaces. */
  2310     where = SDL_strchr(extension, ' ');
  2311     if (where || *extension == '\0') {
  2312         return SDL_FALSE;
  2313     }
  2314 
  2315     /* See if there's an environment variable override */
  2316     start = SDL_getenv(extension);
  2317     if (start && *start == '0') {
  2318         return SDL_FALSE;
  2319     }
  2320 
  2321     /* Lookup the available extensions */
  2322     glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
  2323     if (glGetStringFunc) {
  2324         extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
  2325     } else {
  2326         extensions = NULL;
  2327     }
  2328     if (!extensions) {
  2329         return SDL_FALSE;
  2330     }
  2331 
  2332     /* It takes a bit of care to be fool-proof about parsing the
  2333      * OpenGL extensions string. Don't be fooled by sub-strings,
  2334      * etc. */
  2335 
  2336     start = extensions;
  2337 
  2338     for (;;) {
  2339         where = SDL_strstr(start, extension);
  2340         if (!where)
  2341             break;
  2342 
  2343         terminator = where + SDL_strlen(extension);
  2344         if (where == start || *(where - 1) == ' ')
  2345             if (*terminator == ' ' || *terminator == '\0')
  2346                 return SDL_TRUE;
  2347 
  2348         start = terminator;
  2349     }
  2350     return SDL_FALSE;
  2351 #else
  2352     return SDL_FALSE;
  2353 #endif
  2354 }
  2355 
  2356 int
  2357 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  2358 {
  2359 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2360     int retval;
  2361 
  2362     if (!_this) {
  2363         SDL_UninitializedVideo();
  2364         return -1;
  2365     }
  2366 
  2367     retval = 0;
  2368     switch (attr) {
  2369     case SDL_GL_RED_SIZE:
  2370         _this->gl_config.red_size = value;
  2371         break;
  2372     case SDL_GL_GREEN_SIZE:
  2373         _this->gl_config.green_size = value;
  2374         break;
  2375     case SDL_GL_BLUE_SIZE:
  2376         _this->gl_config.blue_size = value;
  2377         break;
  2378     case SDL_GL_ALPHA_SIZE:
  2379         _this->gl_config.alpha_size = value;
  2380         break;
  2381     case SDL_GL_DOUBLEBUFFER:
  2382         _this->gl_config.double_buffer = value;
  2383         break;
  2384     case SDL_GL_BUFFER_SIZE:
  2385         _this->gl_config.buffer_size = value;
  2386         break;
  2387     case SDL_GL_DEPTH_SIZE:
  2388         _this->gl_config.depth_size = value;
  2389         break;
  2390     case SDL_GL_STENCIL_SIZE:
  2391         _this->gl_config.stencil_size = value;
  2392         break;
  2393     case SDL_GL_ACCUM_RED_SIZE:
  2394         _this->gl_config.accum_red_size = value;
  2395         break;
  2396     case SDL_GL_ACCUM_GREEN_SIZE:
  2397         _this->gl_config.accum_green_size = value;
  2398         break;
  2399     case SDL_GL_ACCUM_BLUE_SIZE:
  2400         _this->gl_config.accum_blue_size = value;
  2401         break;
  2402     case SDL_GL_ACCUM_ALPHA_SIZE:
  2403         _this->gl_config.accum_alpha_size = value;
  2404         break;
  2405     case SDL_GL_STEREO:
  2406         _this->gl_config.stereo = value;
  2407         break;
  2408     case SDL_GL_MULTISAMPLEBUFFERS:
  2409         _this->gl_config.multisamplebuffers = value;
  2410         break;
  2411     case SDL_GL_MULTISAMPLESAMPLES:
  2412         _this->gl_config.multisamplesamples = value;
  2413         break;
  2414     case SDL_GL_ACCELERATED_VISUAL:
  2415         _this->gl_config.accelerated = value;
  2416         break;
  2417 	case SDL_GL_RETAINED_BACKING:
  2418 		_this->gl_config.retained_backing = value;
  2419 		break;
  2420     default:
  2421         SDL_SetError("Unknown OpenGL attribute");
  2422         retval = -1;
  2423         break;
  2424     }
  2425     return retval;
  2426 #else
  2427     SDL_Unsupported();
  2428     return -1;
  2429 #endif /* SDL_VIDEO_OPENGL */
  2430 }
  2431 
  2432 int
  2433 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  2434 {
  2435 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
  2436     void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
  2437     GLenum attrib = 0;
  2438 
  2439     glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
  2440     if (!glGetIntegervFunc) {
  2441         return -1;
  2442     }
  2443     switch (attr) {
  2444 	case SDL_GL_RETAINED_BACKING:
  2445 		*value = _this->gl_config.retained_backing;
  2446 		return 0;
  2447     case SDL_GL_RED_SIZE:
  2448         attrib = GL_RED_BITS;
  2449         break;
  2450     case SDL_GL_BLUE_SIZE:
  2451         attrib = GL_BLUE_BITS;
  2452         break;
  2453     case SDL_GL_GREEN_SIZE:
  2454         attrib = GL_GREEN_BITS;
  2455         break;
  2456     case SDL_GL_ALPHA_SIZE:
  2457         attrib = GL_ALPHA_BITS;
  2458         break;
  2459     case SDL_GL_DOUBLEBUFFER:
  2460 	#ifndef SDL_VIDEO_OPENGL_ES
  2461         attrib = GL_DOUBLEBUFFER;
  2462         break;
  2463 	#else
  2464 		/* I believe double buffering is the only option in OpenGL ES
  2465 		   -- in any case, GL_DOUBLEBUFFER doesn't exist */
  2466 		*value = 1;
  2467 		return 0;
  2468 	#endif
  2469     case SDL_GL_DEPTH_SIZE:
  2470         attrib = GL_DEPTH_BITS;
  2471         break;
  2472     case SDL_GL_STENCIL_SIZE:
  2473         attrib = GL_STENCIL_BITS;
  2474         break;
  2475 #ifndef SDL_VIDEO_OPENGL_ES
  2476     case SDL_GL_ACCUM_RED_SIZE:
  2477         attrib = GL_ACCUM_RED_BITS;
  2478         break;
  2479     case SDL_GL_ACCUM_GREEN_SIZE:
  2480         attrib = GL_ACCUM_GREEN_BITS;
  2481         break;
  2482     case SDL_GL_ACCUM_BLUE_SIZE:
  2483         attrib = GL_ACCUM_BLUE_BITS;
  2484         break;
  2485     case SDL_GL_ACCUM_ALPHA_SIZE:
  2486         attrib = GL_ACCUM_ALPHA_BITS;
  2487         break;
  2488 	case SDL_GL_STEREO:
  2489 		attrib = GL_STEREO;
  2490 		break;
  2491 #else
  2492 	    case SDL_GL_ACCUM_RED_SIZE:
  2493 		case SDL_GL_ACCUM_GREEN_SIZE:
  2494 		case SDL_GL_ACCUM_BLUE_SIZE:
  2495 		case SDL_GL_ACCUM_ALPHA_SIZE:
  2496 		case SDL_GL_STEREO:
  2497 			/* none of these are supported in OpenGL ES */
  2498 			*value = 0;
  2499 			return 0;
  2500 #endif
  2501 		case SDL_GL_MULTISAMPLEBUFFERS:
  2502 		#ifndef SDL_VIDEO_OPENGL_ES
  2503 			attrib = GL_SAMPLE_BUFFERS_ARB;
  2504 		#else
  2505 			attrib = GL_SAMPLE_BUFFERS;
  2506 		#endif
  2507 			break;
  2508 		case SDL_GL_MULTISAMPLESAMPLES:
  2509 		#ifndef SDL_VIDEO_OPENGL_ES
  2510 			attrib = GL_SAMPLES_ARB;
  2511 		#else
  2512 			attrib = GL_SAMPLES;
  2513 		#endif
  2514 			break;
  2515     case SDL_GL_BUFFER_SIZE:
  2516         {
  2517             GLint bits = 0;
  2518             GLint component;
  2519 
  2520             /* there doesn't seem to be a single flag in OpenGL for this! */
  2521             glGetIntegervFunc(GL_RED_BITS, &component);
  2522             bits += component;
  2523             glGetIntegervFunc(GL_GREEN_BITS, &component);
  2524             bits += component;
  2525             glGetIntegervFunc(GL_BLUE_BITS, &component);
  2526             bits += component;
  2527             glGetIntegervFunc(GL_ALPHA_BITS, &component);
  2528             bits += component;
  2529 
  2530             *value = bits;
  2531             return 0;
  2532         }
  2533     case SDL_GL_ACCELERATED_VISUAL:
  2534         {
  2535             /* FIXME: How do we get this information? */
  2536             *value = (_this->gl_config.accelerated != 0);
  2537             return 0;
  2538         }
  2539     default:
  2540         SDL_SetError("Unknown OpenGL attribute");
  2541         return -1;
  2542     }
  2543 
  2544     glGetIntegervFunc(attrib, (GLint *) value);
  2545     return 0;
  2546 #else
  2547     SDL_Unsupported();
  2548     return -1;
  2549 #endif /* SDL_VIDEO_OPENGL */
  2550 }
  2551 
  2552 SDL_GLContext
  2553 SDL_GL_CreateContext(SDL_WindowID windowID)
  2554 {
  2555     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2556 
  2557     if (!window) {
  2558         return NULL;
  2559     }
  2560     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2561         return NULL;
  2562     }
  2563     return _this->GL_CreateContext(_this, window);
  2564 }
  2565 
  2566 int
  2567 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
  2568 {
  2569     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2570 
  2571     if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
  2572         SDL_SetError("The specified window isn't an OpenGL window");
  2573         return -1;
  2574     }
  2575     if (!context) {
  2576         window = NULL;
  2577     }
  2578     return _this->GL_MakeCurrent(_this, window, context);
  2579 }
  2580 
  2581 int
  2582 SDL_GL_SetSwapInterval(int interval)
  2583 {
  2584     if (!_this) {
  2585         SDL_UninitializedVideo();
  2586         return -1;
  2587     }
  2588 
  2589     if (_this->GL_SetSwapInterval) {
  2590         return _this->GL_SetSwapInterval(_this, interval);
  2591     } else {
  2592         SDL_SetError("Setting the swap interval is not supported");
  2593         return -1;
  2594     }
  2595 }
  2596 
  2597 int
  2598 SDL_GL_GetSwapInterval(void)
  2599 {
  2600     if (!_this) {
  2601         SDL_UninitializedVideo();
  2602         return -1;
  2603     }
  2604 
  2605     if (_this->GL_GetSwapInterval) {
  2606         return _this->GL_GetSwapInterval(_this);
  2607     } else {
  2608         SDL_SetError("Getting the swap interval is not supported");
  2609         return -1;
  2610     }
  2611 }
  2612 
  2613 void
  2614 SDL_GL_SwapWindow(SDL_WindowID windowID)
  2615 {
  2616     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2617 
  2618     if (!window) {
  2619 		SDL_SetError("The specified window doesn't exist");
  2620         return;
  2621     }
  2622     if (!(window->flags & SDL_WINDOW_OPENGL)) {
  2623         SDL_SetError("The specified window isn't an OpenGL window");
  2624         return;
  2625     }
  2626     _this->GL_SwapWindow(_this, window);
  2627 }
  2628 
  2629 void
  2630 SDL_GL_DeleteContext(SDL_GLContext context)
  2631 {
  2632     if (!_this || !context) {
  2633         return;
  2634     }
  2635     _this->GL_MakeCurrent(_this, NULL, NULL);
  2636     _this->GL_DeleteContext(_this, context);
  2637 }
  2638 
  2639 #if 0                           // FIXME
  2640 /* Utility function used by SDL_WM_SetIcon();
  2641  * flags & 1 for color key, flags & 2 for alpha channel. */
  2642 static void
  2643 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  2644 {
  2645     int x, y;
  2646     Uint32 colorkey;
  2647 #define SET_MASKBIT(icon, x, y, mask) \
  2648 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  2649 
  2650     colorkey = icon->format->colorkey;
  2651     switch (icon->format->BytesPerPixel) {
  2652     case 1:
  2653         {
  2654             Uint8 *pixels;
  2655             for (y = 0; y < icon->h; ++y) {
  2656                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  2657                 for (x = 0; x < icon->w; ++x) {
  2658                     if (*pixels++ == colorkey) {
  2659                         SET_MASKBIT(icon, x, y, mask);
  2660                     }
  2661                 }
  2662             }
  2663         }
  2664         break;
  2665 
  2666     case 2:
  2667         {
  2668             Uint16 *pixels;
  2669             for (y = 0; y < icon->h; ++y) {
  2670                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  2671                 for (x = 0; x < icon->w; ++x) {
  2672                     if ((flags & 1) && *pixels == colorkey) {
  2673                         SET_MASKBIT(icon, x, y, mask);
  2674                     } else if ((flags & 2)
  2675                                && (*pixels & icon->format->Amask) == 0) {
  2676                         SET_MASKBIT(icon, x, y, mask);
  2677                     }
  2678                     pixels++;
  2679                 }
  2680             }
  2681         }
  2682         break;
  2683 
  2684     case 4:
  2685         {
  2686             Uint32 *pixels;
  2687             for (y = 0; y < icon->h; ++y) {
  2688                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  2689                 for (x = 0; x < icon->w; ++x) {
  2690                     if ((flags & 1) && *pixels == colorkey) {
  2691                         SET_MASKBIT(icon, x, y, mask);
  2692                     } else if ((flags & 2)
  2693                                && (*pixels & icon->format->Amask) == 0) {
  2694                         SET_MASKBIT(icon, x, y, mask);
  2695                     }
  2696                     pixels++;
  2697                 }
  2698             }
  2699         }
  2700         break;
  2701     }
  2702 }
  2703 
  2704 /*
  2705  * Sets the window manager icon for the display window.
  2706  */
  2707 void
  2708 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2709 {
  2710     if (icon && _this->SetIcon) {
  2711         /* Generate a mask if necessary, and create the icon! */
  2712         if (mask == NULL) {
  2713             int mask_len = icon->h * (icon->w + 7) / 8;
  2714             int flags = 0;
  2715             mask = (Uint8 *) SDL_malloc(mask_len);
  2716             if (mask == NULL) {
  2717                 return;
  2718             }
  2719             SDL_memset(mask, ~0, mask_len);
  2720             if (icon->flags & SDL_SRCCOLORKEY)
  2721                 flags |= 1;
  2722             if (icon->flags & SDL_SRCALPHA)
  2723                 flags |= 2;
  2724             if (flags) {
  2725                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2726             }
  2727             _this->SetIcon(_this, icon, mask);
  2728             SDL_free(mask);
  2729         } else {
  2730             _this->SetIcon(_this, icon, mask);
  2731         }
  2732     }
  2733 }
  2734 #endif
  2735 
  2736 SDL_bool
  2737 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
  2738 {
  2739     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2740 
  2741     if (!window || !_this->GetWindowWMInfo) {
  2742         return SDL_FALSE;
  2743     }
  2744     return (_this->GetWindowWMInfo(_this, window, info));
  2745 }
  2746 
  2747 /* vi: set ts=4 sw=4 expandtab: */