src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 24 Jun 2006 16:51:01 +0000
branchSDL-1.3
changeset 1706 1577404809f0
parent 1684 c4aa1a2f48f1
child 1707 57ce47f033a5
permissions -rw-r--r--
Added a multi-window version of testsprite using the new API.
     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_sw.h"
    31 #include "../events/SDL_sysevents.h"
    32 #include "../events/SDL_events_c.h"
    33 
    34 /* Available video drivers */
    35 static VideoBootStrap *bootstrap[] = {
    36 #if SDL_VIDEO_DRIVER_QUARTZ
    37     &QZ_bootstrap,
    38 #endif
    39 #if SDL_VIDEO_DRIVER_X11
    40     &X11_bootstrap,
    41 #endif
    42 #if SDL_VIDEO_DRIVER_DGA
    43     &DGA_bootstrap,
    44 #endif
    45 #if SDL_VIDEO_DRIVER_NANOX
    46     &NX_bootstrap,
    47 #endif
    48 #if SDL_VIDEO_DRIVER_IPOD
    49     &iPod_bootstrap,
    50 #endif
    51 #if SDL_VIDEO_DRIVER_QTOPIA
    52     &Qtopia_bootstrap,
    53 #endif
    54 #if SDL_VIDEO_DRIVER_WSCONS
    55     &WSCONS_bootstrap,
    56 #endif
    57 #if SDL_VIDEO_DRIVER_FBCON
    58     &FBCON_bootstrap,
    59 #endif
    60 #if SDL_VIDEO_DRIVER_DIRECTFB
    61     &DirectFB_bootstrap,
    62 #endif
    63 #if SDL_VIDEO_DRIVER_PS2GS
    64     &PS2GS_bootstrap,
    65 #endif
    66 #if SDL_VIDEO_DRIVER_GGI
    67     &GGI_bootstrap,
    68 #endif
    69 #if SDL_VIDEO_DRIVER_VGL
    70     &VGL_bootstrap,
    71 #endif
    72 #if SDL_VIDEO_DRIVER_SVGALIB
    73     &SVGALIB_bootstrap,
    74 #endif
    75 #if SDL_VIDEO_DRIVER_GAPI
    76     &GAPI_bootstrap,
    77 #endif
    78 #if SDL_VIDEO_DRIVER_WINDIB
    79     &WINDIB_bootstrap,
    80 #endif
    81 #if SDL_VIDEO_DRIVER_DDRAW
    82     &DIRECTX_bootstrap,
    83 #endif
    84 #if SDL_VIDEO_DRIVER_BWINDOW
    85     &BWINDOW_bootstrap,
    86 #endif
    87 #if SDL_VIDEO_DRIVER_TOOLBOX
    88     &TOOLBOX_bootstrap,
    89 #endif
    90 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
    91     &DSp_bootstrap,
    92 #endif
    93 #if SDL_VIDEO_DRIVER_CYBERGRAPHICS
    94     &CGX_bootstrap,
    95 #endif
    96 #if SDL_VIDEO_DRIVER_PHOTON
    97     &ph_bootstrap,
    98 #endif
    99 #if SDL_VIDEO_DRIVER_EPOC
   100     &EPOC_bootstrap,
   101 #endif
   102 #if SDL_VIDEO_DRIVER_XBIOS
   103     &XBIOS_bootstrap,
   104 #endif
   105 #if SDL_VIDEO_DRIVER_GEM
   106     &GEM_bootstrap,
   107 #endif
   108 #if SDL_VIDEO_DRIVER_PICOGUI
   109     &PG_bootstrap,
   110 #endif
   111 #if SDL_VIDEO_DRIVER_DC
   112     &DC_bootstrap,
   113 #endif
   114 #if SDL_VIDEO_DRIVER_RISCOS
   115     &RISCOS_bootstrap,
   116 #endif
   117 #if SDL_VIDEO_DRIVER_OS2FS
   118     &OS2FSLib_bootstrap,
   119 #endif
   120 #if SDL_VIDEO_DRIVER_AALIB
   121     &AALIB_bootstrap,
   122 #endif
   123 #if SDL_VIDEO_DRIVER_DUMMY
   124     &DUMMY_bootstrap,
   125 #endif
   126 #if SDL_VIDEO_DRIVER_GLSDL
   127     &glSDL_bootstrap,
   128 #endif
   129     NULL
   130 };
   131 
   132 static SDL_VideoDevice *_this = NULL;
   133 
   134 /* Various local functions */
   135 int SDL_VideoInit(const char *driver_name, Uint32 flags);
   136 void SDL_VideoQuit(void);
   137 
   138 static int
   139 cmpmodes(const void *A, const void *B)
   140 {
   141     SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
   142     SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
   143 
   144     if (a.w != b.w) {
   145         return b.w - a.w;
   146     }
   147     if (a.h != b.h) {
   148         return b.h - a.h;
   149     }
   150     if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
   151         return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
   152     }
   153     if (a.refresh_rate != b.refresh_rate) {
   154         return b.refresh_rate - a.refresh_rate;
   155     }
   156     return 0;
   157 }
   158 
   159 int
   160 SDL_GetNumVideoDrivers(void)
   161 {
   162     return SDL_arraysize(bootstrap) - 1;
   163 }
   164 
   165 const char *
   166 SDL_GetVideoDriver(int index)
   167 {
   168     if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
   169         return bootstrap[index]->name;
   170     }
   171     return NULL;
   172 }
   173 
   174 /*
   175  * Initialize the video and event subsystems -- determine native pixel format
   176  */
   177 int
   178 SDL_VideoInit(const char *driver_name, Uint32 flags)
   179 {
   180     SDL_VideoDevice *video;
   181     int index;
   182     int i;
   183     int bpp;
   184     Uint32 Rmask, Gmask, Bmask, Amask;
   185 
   186     /* Toggle the event thread flags, based on OS requirements */
   187 #if defined(MUST_THREAD_EVENTS)
   188     flags |= SDL_INIT_EVENTTHREAD;
   189 #elif defined(CANT_THREAD_EVENTS)
   190     if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
   191         SDL_SetError("OS doesn't support threaded events");
   192         return -1;
   193     }
   194 #endif
   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         for (i = 0; bootstrap[i]; ++i) {
   206             if (SDL_strncmp(bootstrap[i]->name, driver_name,
   207                             SDL_strlen(bootstrap[i]->name)) == 0) {
   208                 if (bootstrap[i]->available()) {
   209                     video = bootstrap[i]->create(index);
   210                 }
   211                 break;
   212             }
   213         }
   214     } else {
   215         for (i = 0; bootstrap[i]; ++i) {
   216             if (bootstrap[i]->available()) {
   217                 video = bootstrap[i]->create(index);
   218                 if (video != NULL) {
   219                     break;
   220                 }
   221             }
   222         }
   223     }
   224     if (video == NULL) {
   225         if (driver_name) {
   226             SDL_SetError("%s not available", driver_name);
   227         } else {
   228             SDL_SetError("No available video device");
   229         }
   230         return -1;
   231     }
   232     _this = video;
   233     _this->name = bootstrap[i]->name;
   234     _this->next_object_id = 1;
   235 
   236 
   237     /* Set some very sane GL defaults */
   238     _this->gl_config.driver_loaded = 0;
   239     _this->gl_config.dll_handle = NULL;
   240     _this->gl_config.red_size = 3;
   241     _this->gl_config.green_size = 3;
   242     _this->gl_config.blue_size = 2;
   243     _this->gl_config.alpha_size = 0;
   244     _this->gl_config.buffer_size = 0;
   245     _this->gl_config.depth_size = 16;
   246     _this->gl_config.stencil_size = 0;
   247     _this->gl_config.double_buffer = 1;
   248     _this->gl_config.accum_red_size = 0;
   249     _this->gl_config.accum_green_size = 0;
   250     _this->gl_config.accum_blue_size = 0;
   251     _this->gl_config.accum_alpha_size = 0;
   252     _this->gl_config.stereo = 0;
   253     _this->gl_config.multisamplebuffers = 0;
   254     _this->gl_config.multisamplesamples = 0;
   255     _this->gl_config.accelerated = -1;  /* not known, don't set */
   256     _this->gl_config.swap_control = -1; /* not known, don't set */
   257 
   258     /* Initialize the video subsystem */
   259     if (_this->VideoInit(_this) < 0) {
   260         SDL_VideoQuit();
   261         return -1;
   262     }
   263 
   264     /* Make sure some displays were added */
   265     if (_this->num_displays == 0) {
   266         SDL_SetError("The video driver did not add any displays");
   267         SDL_VideoQuit();
   268         return (-1);
   269     }
   270 
   271     /* Sort the video modes */
   272     for (i = 0; i < _this->num_displays; ++i) {
   273         SDL_qsort(_this->displays[i].display_modes,
   274                   _this->displays[i].num_display_modes,
   275                   sizeof(SDL_DisplayMode), cmpmodes);
   276     }
   277 
   278     /* The software renderer is always available */
   279     for (i = 0; i < _this->num_displays; ++i) {
   280         if (_this->displays[i].num_render_drivers > 0) {
   281             SDL_AddRenderDriver(i, &SDL_SW_RenderDriver);
   282         }
   283     }
   284 
   285     /* Start the event loop */
   286     if (SDL_StartEventLoop(flags) < 0) {
   287         SDL_VideoQuit();
   288         return -1;
   289     }
   290 
   291     /* We're ready to go! */
   292     return 0;
   293 }
   294 
   295 const char *
   296 SDL_GetCurrentVideoDriver()
   297 {
   298     if (!_this) {
   299         return NULL;
   300     }
   301     return _this->name;
   302 }
   303 
   304 SDL_VideoDevice *
   305 SDL_GetVideoDevice()
   306 {
   307     return _this;
   308 }
   309 
   310 int
   311 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
   312 {
   313     SDL_VideoDisplay display;
   314 
   315     SDL_zero(display);
   316     if (desktop_mode) {
   317         display.desktop_mode = *desktop_mode;
   318     }
   319     display.current_mode = display.desktop_mode;
   320 
   321     return SDL_AddVideoDisplay(&display);
   322 }
   323 
   324 int
   325 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
   326 {
   327     SDL_VideoDisplay *displays;
   328     int index = -1;
   329 
   330     displays =
   331         SDL_realloc(_this->displays,
   332                     (_this->num_displays + 1) * sizeof(*displays));
   333     if (displays) {
   334         index = _this->num_displays++;
   335         displays[index] = *display;
   336         displays[index].device = _this;
   337         _this->displays = displays;
   338     } else {
   339         SDL_OutOfMemory();
   340     }
   341     return index;
   342 }
   343 
   344 int
   345 SDL_GetNumVideoDisplays(void)
   346 {
   347     if (!_this) {
   348         return 0;
   349     }
   350     return _this->num_displays;
   351 }
   352 
   353 int
   354 SDL_SelectVideoDisplay(int index)
   355 {
   356     if (!_this) {
   357         SDL_SetError("Video subsystem has not been initialized");
   358         return (-1);
   359     }
   360     if (index >= 0) {
   361         if (index >= _this->num_displays) {
   362             SDL_SetError("index must be in the range 0 - %d",
   363                          _this->num_displays - 1);
   364             return -1;
   365         }
   366         _this->current_display = index;
   367     }
   368     return _this->current_display;
   369 }
   370 
   371 void
   372 SDL_AddDisplayMode(int displayIndex, const SDL_DisplayMode * mode)
   373 {
   374     SDL_VideoDisplay *display = &_this->displays[displayIndex];
   375     SDL_DisplayMode *modes;
   376     int i, nmodes;
   377 
   378     /* Make sure we don't already have the mode in the list */
   379     modes = display->display_modes;
   380     nmodes = display->num_display_modes;
   381     for (i = 0; i < nmodes; ++i) {
   382         if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
   383             return;
   384         }
   385     }
   386 
   387     /* Go ahead and add the new mode */
   388     modes = SDL_realloc(modes, (nmodes + 1) * sizeof(*mode));
   389     if (modes) {
   390         display->display_modes = modes;
   391         modes[nmodes] = *mode;
   392         display->num_display_modes++;
   393     }
   394 }
   395 
   396 int
   397 SDL_GetNumDisplayModes()
   398 {
   399     if (_this) {
   400         return SDL_CurrentDisplay.num_display_modes;
   401     }
   402     return 0;
   403 }
   404 
   405 const SDL_DisplayMode *
   406 SDL_GetDisplayMode(int index)
   407 {
   408     if (index < 0 || index >= SDL_GetNumDisplayModes()) {
   409         SDL_SetError("index must be in the range of 0 - %d",
   410                      SDL_GetNumDisplayModes() - 1);
   411         return NULL;
   412     }
   413     return &SDL_CurrentDisplay.display_modes[index];
   414 }
   415 
   416 const SDL_DisplayMode *
   417 SDL_GetDesktopDisplayMode(void)
   418 {
   419     if (_this) {
   420         return &SDL_CurrentDisplay.desktop_mode;
   421     }
   422     return NULL;
   423 }
   424 
   425 const SDL_DisplayMode *
   426 SDL_GetCurrentDisplayMode(void)
   427 {
   428     if (_this) {
   429         return &SDL_CurrentDisplay.current_mode;
   430     }
   431     return NULL;
   432 }
   433 
   434 SDL_DisplayMode *
   435 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
   436                           SDL_DisplayMode * closest)
   437 {
   438     Uint32 target_format;
   439     int target_refresh_rate;
   440     int i;
   441     SDL_DisplayMode *current, *match;
   442 
   443     if (!_this || !mode || !closest) {
   444         return NULL;
   445     }
   446 
   447     /* Default to the desktop format */
   448     if (mode->format) {
   449         target_format = mode->format;
   450     } else {
   451         target_format = SDL_CurrentDisplay.desktop_mode.format;
   452     }
   453 
   454     /* Default to the desktop refresh rate */
   455     if (mode->refresh_rate) {
   456         target_refresh_rate = mode->refresh_rate;
   457     } else {
   458         target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
   459     }
   460 
   461     match = NULL;
   462     for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
   463         current = &SDL_CurrentDisplay.display_modes[i];
   464 
   465         if ((current->w && current->h) &&
   466             (current->w < mode->w || current->h < mode->h)) {
   467             /* Out of sorted modes large enough here */
   468             break;
   469         }
   470         if (!match || current->w < match->w || current->h < match->h) {
   471             match = current;
   472             continue;
   473         }
   474         if (current->format != match->format) {
   475             /* Sorted highest depth to lowest */
   476             if (current->format == target_format ||
   477                 (SDL_BITSPERPIXEL(current->format) >=
   478                  SDL_BITSPERPIXEL(target_format)
   479                  && SDL_PIXELTYPE(current->format) ==
   480                  SDL_PIXELTYPE(target_format))) {
   481                 match = current;
   482             }
   483             continue;
   484         }
   485         if (current->refresh_rate != match->refresh_rate) {
   486             /* Sorted highest refresh to lowest */
   487             if (current->refresh_rate >= target_refresh_rate) {
   488                 match = current;
   489             }
   490         }
   491     }
   492     if (match) {
   493         if (match->format) {
   494             closest->format = match->format;
   495         } else {
   496             closest->format = mode->format;
   497         }
   498         if (match->w && match->h) {
   499             closest->w = match->w;
   500             closest->h = match->h;
   501         } else {
   502             closest->w = mode->w;
   503             closest->h = mode->h;
   504         }
   505         if (match->refresh_rate) {
   506             closest->refresh_rate = match->refresh_rate;
   507         } else {
   508             closest->refresh_rate = mode->refresh_rate;
   509         }
   510         /* Pick some reasonable defaults if the app and driver don't care */
   511         if (!closest->format) {
   512             closest->format = SDL_PixelFormat_RGB888;
   513         }
   514         if (!closest->w) {
   515             closest->w = 640;
   516         }
   517         if (!closest->h) {
   518             closest->h = 480;
   519         }
   520         return closest;
   521     }
   522     return NULL;
   523 }
   524 
   525 int
   526 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
   527 {
   528     SDL_VideoDisplay *display;
   529     SDL_DisplayMode display_mode;
   530     int i, ncolors;
   531 
   532     if (!_this) {
   533         SDL_SetError("Video subsystem has not been initialized");
   534         return -1;
   535     }
   536 
   537     /* Make sure there's an actual display mode to set */
   538     if (!mode) {
   539         SDL_SetError("No mode passed to SDL_SetDisplayMode");
   540         return -1;
   541     }
   542     display = &SDL_CurrentDisplay;
   543     display_mode = *mode;
   544 
   545     /* Default to the current mode */
   546     if (!display_mode.format) {
   547         display_mode.format = display->current_mode.format;
   548     }
   549     if (!display_mode.w) {
   550         display_mode.w = display->current_mode.w;
   551     }
   552     if (!display_mode.h) {
   553         display_mode.h = display->current_mode.h;
   554     }
   555     if (!display_mode.refresh_rate) {
   556         display_mode.refresh_rate = display->current_mode.refresh_rate;
   557     }
   558 
   559     /* Get a good video mode, the closest one possible */
   560     if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
   561         SDL_SetError("No video mode large enough for %dx%d",
   562                      display_mode.w, display_mode.h);
   563         return -1;
   564     }
   565 
   566     /* See if there's anything left to do */
   567     if (SDL_memcmp
   568         (&display_mode, SDL_GetCurrentDisplayMode(),
   569          sizeof(display_mode)) == 0) {
   570         return 0;
   571     }
   572 
   573     /* Set up a palette, if necessary */
   574     if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
   575         ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
   576     } else {
   577         ncolors = 0;
   578     }
   579     if ((!ncolors && display->palette) || (ncolors && !display->palette)
   580         || (ncolors && ncolors != display->palette->ncolors)) {
   581         if (display->palette) {
   582             SDL_FreePalette(display->palette);
   583             display->palette = NULL;
   584         }
   585         if (ncolors) {
   586             display->palette = SDL_AllocPalette(ncolors);
   587             if (!display->palette) {
   588                 return -1;
   589             }
   590             SDL_DitherColors(display->palette->colors,
   591                              SDL_BITSPERPIXEL(display_mode.format));
   592         }
   593     }
   594 
   595     return _this->SetDisplayMode(_this, &display_mode);
   596 }
   597 
   598 int
   599 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
   600 {
   601     SDL_Palette *palette;
   602     int status = 0;
   603 
   604     if (!_this) {
   605         SDL_SetError("Video subsystem has not been initialized");
   606         return -1;
   607     }
   608     if (!SDL_CurrentDisplay.palette) {
   609         SDL_SetError("Display mode does not have a palette");
   610         return -1;
   611     }
   612 
   613     status =
   614         SDL_SetPaletteColors(SDL_CurrentDisplay.palette, colors, firstcolor,
   615                              ncolors);
   616 
   617     if (_this->SetDisplayPalette) {
   618         if (_this->SetDisplayPalette(_this, palette) < 0) {
   619             status = -1;
   620         }
   621     }
   622     return status;
   623 }
   624 
   625 int
   626 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
   627 {
   628     SDL_Palette *palette;
   629 
   630     if (!_this) {
   631         SDL_SetError("Video subsystem has not been initialized");
   632         return -1;
   633     }
   634 
   635     palette = SDL_CurrentDisplay.palette;
   636     if (!palette->ncolors) {
   637         SDL_SetError("Display mode does not have a palette");
   638         return -1;
   639     }
   640 
   641     if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
   642         SDL_SetError("Palette indices are out of range");
   643         return -1;
   644     }
   645 
   646     SDL_memcpy(colors, &palette->colors[firstcolor],
   647                ncolors * sizeof(*colors));
   648     return 0;
   649 }
   650 
   651 SDL_WindowID
   652 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
   653 {
   654     const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
   655                                   SDL_WINDOW_BORDERLESS |
   656                                   SDL_WINDOW_SHOWN |
   657                                   SDL_WINDOW_OPENGL |
   658                                   SDL_WINDOW_RESIZABLE |
   659                                   SDL_WINDOW_MAXIMIZED |
   660                                   SDL_WINDOW_MINIMIZED |
   661                                   SDL_WINDOW_INPUT_GRABBED);
   662     SDL_Window window;
   663     int num_windows;
   664     SDL_Window *windows;
   665 
   666     if (!_this) {
   667         SDL_SetError("Video subsystem has not been initialized");
   668         return 0;
   669     }
   670 
   671     SDL_zero(window);
   672     window.id = _this->next_object_id++;
   673     window.title = title ? SDL_strdup(title) : NULL;
   674     window.x = x;
   675     window.y = y;
   676     window.w = w;
   677     window.h = h;
   678     window.flags = (flags & allowed_flags);
   679     window.display = _this->current_display;
   680 
   681     if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
   682         if (window.title) {
   683             SDL_free(window.title);
   684         }
   685         return 0;
   686     }
   687 
   688     num_windows = SDL_CurrentDisplay.num_windows;
   689     windows =
   690         SDL_realloc(SDL_CurrentDisplay.windows,
   691                     (num_windows + 1) * sizeof(*windows));
   692     if (!windows) {
   693         if (_this->DestroyWindow) {
   694             _this->DestroyWindow(_this, &window);
   695         }
   696         if (window.title) {
   697             SDL_free(window.title);
   698         }
   699         return 0;
   700     }
   701     windows[num_windows] = window;
   702     SDL_CurrentDisplay.windows = windows;
   703     SDL_CurrentDisplay.num_windows++;
   704 
   705     return window.id;
   706 }
   707 
   708 SDL_WindowID
   709 SDL_CreateWindowFrom(const void *data)
   710 {
   711     SDL_Window window;
   712     int num_windows;
   713     SDL_Window *windows;
   714 
   715     if (!_this) {
   716         SDL_SetError("Video subsystem has not been initialized");
   717         return (0);
   718     }
   719 
   720     SDL_zero(window);
   721     window.id = _this->next_object_id++;
   722     window.display = _this->current_display;
   723 
   724     if (!_this->CreateWindowFrom ||
   725         _this->CreateWindowFrom(_this, &window, data) < 0) {
   726         return 0;
   727     }
   728 
   729     num_windows = SDL_CurrentDisplay.num_windows;
   730     windows =
   731         SDL_realloc(SDL_CurrentDisplay.windows,
   732                     (num_windows + 1) * sizeof(*windows));
   733     if (!windows) {
   734         if (_this->DestroyWindow) {
   735             _this->DestroyWindow(_this, &window);
   736         }
   737         if (window.title) {
   738             SDL_free(window.title);
   739         }
   740         return 0;
   741     }
   742     windows[num_windows] = window;
   743     SDL_CurrentDisplay.windows = windows;
   744     SDL_CurrentDisplay.num_windows++;
   745 
   746     return window.id;
   747 }
   748 
   749 SDL_Window *
   750 SDL_GetWindowFromID(SDL_WindowID windowID)
   751 {
   752     int i, j;
   753 
   754     if (!_this) {
   755         return NULL;
   756     }
   757 
   758     for (i = 0; i < _this->num_displays; ++i) {
   759         SDL_VideoDisplay *display = &_this->displays[i];
   760         for (j = 0; j < display->num_windows; ++j) {
   761             SDL_Window *window = &display->windows[j];
   762             if (window->id == windowID) {
   763                 return window;
   764             }
   765         }
   766     }
   767     return NULL;
   768 }
   769 
   770 SDL_VideoDisplay *
   771 SDL_GetDisplayFromWindow(SDL_Window * window)
   772 {
   773     if (!_this) {
   774         return NULL;
   775     }
   776     return &_this->displays[window->display];
   777 }
   778 
   779 Uint32
   780 SDL_GetWindowFlags(SDL_WindowID windowID)
   781 {
   782     SDL_Window *window = SDL_GetWindowFromID(windowID);
   783 
   784     if (!window) {
   785         return 0;
   786     }
   787     return window->flags;
   788 }
   789 
   790 void
   791 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
   792 {
   793     SDL_Window *window = SDL_GetWindowFromID(windowID);
   794 
   795     if (!window) {
   796         return;
   797     }
   798     if (window->title) {
   799         SDL_free(window->title);
   800     }
   801     window->title = SDL_strdup(title);
   802 
   803     if (_this->SetWindowTitle) {
   804         _this->SetWindowTitle(_this, window);
   805     }
   806 }
   807 
   808 const char *
   809 SDL_GetWindowTitle(SDL_WindowID windowID)
   810 {
   811     SDL_Window *window = SDL_GetWindowFromID(windowID);
   812 
   813     if (!window) {
   814         return NULL;
   815     }
   816     return window->title;
   817 }
   818 
   819 void
   820 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
   821 {
   822     SDL_Window *window = SDL_GetWindowFromID(windowID);
   823 
   824     if (!window) {
   825         return;
   826     }
   827     window->userdata = userdata;
   828 }
   829 
   830 void *
   831 SDL_GetWindowData(SDL_WindowID windowID)
   832 {
   833     SDL_Window *window = SDL_GetWindowFromID(windowID);
   834 
   835     if (!window) {
   836         return NULL;
   837     }
   838     return window->userdata;
   839 }
   840 
   841 void
   842 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
   843 {
   844     SDL_Window *window = SDL_GetWindowFromID(windowID);
   845 
   846     if (!window) {
   847         return;
   848     }
   849 
   850     window->x = x;
   851     window->y = y;
   852 
   853     if (_this->SetWindowPosition) {
   854         _this->SetWindowPosition(_this, window);
   855     }
   856 }
   857 
   858 void
   859 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
   860 {
   861     SDL_Window *window = SDL_GetWindowFromID(windowID);
   862 
   863     if (!window) {
   864         return;
   865     }
   866     if (x) {
   867         *x = window->x;
   868     }
   869     if (y) {
   870         *y = window->y;
   871     }
   872 }
   873 
   874 void
   875 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
   876 {
   877     SDL_Window *window = SDL_GetWindowFromID(windowID);
   878 
   879     if (!window) {
   880         return;
   881     }
   882 
   883     window->w = w;
   884     window->h = h;
   885 
   886     if (_this->SetWindowSize) {
   887         _this->SetWindowSize(_this, window);
   888     }
   889 }
   890 
   891 void
   892 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
   893 {
   894     SDL_Window *window = SDL_GetWindowFromID(windowID);
   895 
   896     if (!window) {
   897         return;
   898     }
   899     if (w) {
   900         *w = window->w;
   901     }
   902     if (h) {
   903         *h = window->h;
   904     }
   905 }
   906 
   907 void
   908 SDL_ShowWindow(SDL_WindowID windowID)
   909 {
   910     SDL_Window *window = SDL_GetWindowFromID(windowID);
   911 
   912     if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
   913         return;
   914     }
   915 
   916     window->flags |= SDL_WINDOW_SHOWN;
   917 
   918     if (_this->ShowWindow) {
   919         _this->ShowWindow(_this, window);
   920     }
   921 }
   922 
   923 void
   924 SDL_HideWindow(SDL_WindowID windowID)
   925 {
   926     SDL_Window *window = SDL_GetWindowFromID(windowID);
   927 
   928     if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
   929         return;
   930     }
   931 
   932     window->flags &= ~SDL_WINDOW_SHOWN;
   933 
   934     if (_this->HideWindow) {
   935         _this->HideWindow(_this, window);
   936     }
   937 }
   938 
   939 void
   940 SDL_RaiseWindow(SDL_WindowID windowID)
   941 {
   942     SDL_Window *window = SDL_GetWindowFromID(windowID);
   943 
   944     if (!window) {
   945         return;
   946     }
   947 
   948     if (_this->RaiseWindow) {
   949         _this->RaiseWindow(_this, window);
   950     }
   951 }
   952 
   953 void
   954 SDL_MaximizeWindow(SDL_WindowID windowID)
   955 {
   956     SDL_Window *window = SDL_GetWindowFromID(windowID);
   957 
   958     if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
   959         return;
   960     }
   961 
   962     window->flags |= SDL_WINDOW_MAXIMIZED;
   963 
   964     if (_this->MaximizeWindow) {
   965         _this->MaximizeWindow(_this, window);
   966     }
   967 }
   968 
   969 void
   970 SDL_MinimizeWindow(SDL_WindowID windowID)
   971 {
   972     SDL_Window *window = SDL_GetWindowFromID(windowID);
   973 
   974     if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
   975         return;
   976     }
   977 
   978     window->flags |= SDL_WINDOW_MINIMIZED;
   979 
   980     if (_this->MinimizeWindow) {
   981         _this->MinimizeWindow(_this, window);
   982     }
   983 }
   984 
   985 void
   986 SDL_RestoreWindow(SDL_WindowID windowID)
   987 {
   988     SDL_Window *window = SDL_GetWindowFromID(windowID);
   989 
   990     if (!window
   991         || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
   992         return;
   993     }
   994 
   995     window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
   996 
   997     if (_this->RestoreWindow) {
   998         _this->RestoreWindow(_this, window);
   999     }
  1000 }
  1001 
  1002 void
  1003 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
  1004 {
  1005     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1006 
  1007     if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
  1008         return;
  1009     }
  1010 
  1011     if (mode) {
  1012         window->flags |= SDL_WINDOW_INPUT_GRABBED;
  1013     } else {
  1014         window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1015     }
  1016 
  1017     if (_this->SetWindowGrab) {
  1018         _this->SetWindowGrab(_this, window);
  1019     }
  1020 }
  1021 
  1022 int
  1023 SDL_GetWindowGrab(SDL_WindowID windowID)
  1024 {
  1025     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1026 
  1027     if (!window) {
  1028         return 0;
  1029     }
  1030 
  1031     return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
  1032 }
  1033 
  1034 void
  1035 SDL_DestroyWindow(SDL_WindowID windowID)
  1036 {
  1037     int i, j;
  1038 
  1039     if (!_this) {
  1040         return;
  1041     }
  1042 
  1043     for (i = 0; i < _this->num_displays; ++i) {
  1044         SDL_VideoDisplay *display = &_this->displays[i];
  1045         for (j = 0; j < display->num_windows; ++j) {
  1046             SDL_Window *window = &display->windows[j];
  1047             if (window->id != windowID) {
  1048                 continue;
  1049             }
  1050             if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  1051                 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
  1052                 _this->SetWindowGrab(_this, window);
  1053             }
  1054             if (window->renderer) {
  1055                 SDL_DestroyRenderer(window->id);
  1056             }
  1057             if (_this->DestroyWindow) {
  1058                 _this->DestroyWindow(_this, window);
  1059             }
  1060             if (window->title) {
  1061                 SDL_free(window->title);
  1062             }
  1063             if (window->gamma) {
  1064                 SDL_free(window->gamma);
  1065             }
  1066             if (j != display->num_windows - 1) {
  1067                 SDL_memcpy(&display->windows[i],
  1068                            &display->windows[i + 1],
  1069                            (display->num_windows - i - 1) * sizeof(*window));
  1070             }
  1071             --display->num_windows;
  1072             return;
  1073         }
  1074     }
  1075 }
  1076 
  1077 void
  1078 SDL_AddRenderDriver(int displayIndex, const SDL_RenderDriver * driver)
  1079 {
  1080     SDL_VideoDisplay *display = &_this->displays[displayIndex];
  1081     SDL_RenderDriver *render_drivers;
  1082 
  1083     render_drivers =
  1084         SDL_realloc(display->render_drivers,
  1085                     (display->num_render_drivers +
  1086                      1) * sizeof(*render_drivers));
  1087     if (render_drivers) {
  1088         render_drivers[display->num_render_drivers] = *driver;
  1089         display->render_drivers = render_drivers;
  1090         display->num_render_drivers++;
  1091     }
  1092 }
  1093 
  1094 int
  1095 SDL_GetNumRenderers(void)
  1096 {
  1097     if (_this) {
  1098         return SDL_CurrentDisplay.num_render_drivers;
  1099     }
  1100     return 0;
  1101 }
  1102 
  1103 int
  1104 SDL_GetRendererInfo(int index, SDL_RendererInfo * info)
  1105 {
  1106     if (index < 0 || index >= SDL_GetNumRenderers()) {
  1107         SDL_SetError("index must be in the range of 0 - %d",
  1108                      SDL_GetNumRenderers() - 1);
  1109         return -1;
  1110     }
  1111     *info = SDL_CurrentDisplay.render_drivers[index].info;
  1112     return 0;
  1113 }
  1114 
  1115 int
  1116 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
  1117 {
  1118     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1119 
  1120     if (!window) {
  1121         return 0;
  1122     }
  1123 
  1124     if (index < 0) {
  1125         int n = SDL_GetNumRenderers();
  1126         for (index = 0; index < n; ++index) {
  1127             SDL_RenderDriver *driver =
  1128                 &SDL_CurrentDisplay.render_drivers[index];
  1129 
  1130             /* Skip minimal drivers in automatic scans */
  1131             if (!(flags & SDL_Renderer_Minimal)
  1132                 && (driver->info.flags & SDL_Renderer_Minimal)) {
  1133                 continue;
  1134             }
  1135             if ((driver->info.flags & flags) == flags) {
  1136                 break;
  1137             }
  1138         }
  1139         if (index == n) {
  1140             SDL_SetError("Couldn't find matching render driver");
  1141             return -1;
  1142         }
  1143     }
  1144 
  1145     if (index >= SDL_GetNumRenderers()) {
  1146         SDL_SetError("index must be -1 or in the range of 0 - %d",
  1147                      SDL_GetNumRenderers() - 1);
  1148         return -1;
  1149     }
  1150 
  1151     /* Free any existing renderer */
  1152     SDL_DestroyRenderer(windowID);
  1153 
  1154     /* Create a new renderer instance */
  1155     window->renderer =
  1156         SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window,
  1157                                                                 flags);
  1158     if (!window->renderer) {
  1159         return -1;
  1160     }
  1161     SDL_CurrentDisplay.current_renderer = window->renderer;
  1162 
  1163     return 0;
  1164 }
  1165 
  1166 int
  1167 SDL_SelectRenderer(SDL_WindowID windowID)
  1168 {
  1169     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1170 
  1171     if (!window || !window->renderer) {
  1172         return -1;
  1173     }
  1174     SDL_CurrentDisplay.current_renderer = window->renderer;
  1175     return 0;
  1176 }
  1177 
  1178 SDL_TextureID
  1179 SDL_CreateTexture(Uint32 format, int access, int w, int h)
  1180 {
  1181     int hash;
  1182     SDL_Renderer *renderer;
  1183     SDL_Texture *texture;
  1184 
  1185     if (!_this) {
  1186         return 0;
  1187     }
  1188 
  1189     renderer = SDL_CurrentDisplay.current_renderer;
  1190     if (!renderer || !renderer->CreateTexture) {
  1191         return 0;
  1192     }
  1193 
  1194     texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
  1195     if (!texture) {
  1196         SDL_OutOfMemory();
  1197         return 0;
  1198     }
  1199 
  1200     SDL_zerop(texture);
  1201     texture->id = _this->next_object_id++;
  1202     texture->format = format;
  1203     texture->access = access;
  1204     texture->w = w;
  1205     texture->h = h;
  1206     texture->renderer = renderer;
  1207 
  1208     if (renderer->CreateTexture(renderer, texture) < 0) {
  1209         SDL_free(texture);
  1210         return 0;
  1211     }
  1212 
  1213     hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
  1214     texture->next = SDL_CurrentDisplay.textures[hash];
  1215     SDL_CurrentDisplay.textures[hash] = texture;
  1216 
  1217     return texture->id;
  1218 }
  1219 
  1220 SDL_TextureID
  1221 SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
  1222 {
  1223     SDL_TextureID textureID;
  1224     Uint32 surface_flags = surface->flags;
  1225     SDL_PixelFormat *fmt = surface->format;
  1226     Uint32 colorkey;
  1227     Uint8 alpha;
  1228     SDL_Rect bounds;
  1229     SDL_Surface dst;
  1230     int bpp;
  1231     Uint32 Rmask, Gmask, Bmask, Amask;
  1232 
  1233     if (!surface) {
  1234         SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1235         return 0;
  1236     }
  1237 
  1238     if (format) {
  1239         if (!SDL_PixelFormatEnumToMasks
  1240             (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
  1241             SDL_SetError("Unknown pixel format");
  1242             return 0;
  1243         }
  1244     } else {
  1245         if (fmt->Amask || !(surface_flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA))) {
  1246             bpp = fmt->BitsPerPixel;
  1247             Rmask = fmt->Rmask;
  1248             Gmask = fmt->Gmask;
  1249             Bmask = fmt->Bmask;
  1250             Amask = fmt->Amask;
  1251         } else {
  1252             /* Need a format with alpha */
  1253             bpp = 32;
  1254             Rmask = 0x00FF0000;
  1255             Gmask = 0x0000FF00;
  1256             Bmask = 0x000000FF;
  1257             Amask = 0xFF000000;
  1258         }
  1259         format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
  1260         if (!format) {
  1261             SDL_SetError("Unknown pixel format");
  1262             return 0;
  1263         }
  1264     }
  1265 
  1266     textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
  1267     if (!textureID) {
  1268         return 0;
  1269     }
  1270 
  1271     /* Set up a destination surface for the texture update */
  1272     SDL_zero(dst);
  1273     dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
  1274     if (!dst.format) {
  1275         SDL_DestroyTexture(textureID);
  1276         return 0;
  1277     }
  1278     dst.w = surface->w;
  1279     dst.h = surface->h;
  1280     if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
  1281         dst.flags |= SDL_PREALLOC;
  1282     } else {
  1283         dst.pitch = SDL_CalculatePitch(&dst);
  1284         dst.pixels = SDL_malloc(dst.h * dst.pitch);
  1285         if (!dst.pixels) {
  1286             SDL_DestroyTexture(textureID);
  1287             SDL_FreeFormat(dst.format);
  1288             SDL_OutOfMemory();
  1289             return 0;
  1290         }
  1291     }
  1292 
  1293     /* Copy the palette if any */
  1294     if (fmt->palette) {
  1295         SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
  1296                               fmt->palette->ncolors);
  1297         SDL_SetSurfacePalette(&dst, fmt->palette);
  1298     }
  1299 
  1300     /* Make the texture transparent if the surface has colorkey */
  1301     if (surface_flags & SDL_SRCCOLORKEY) {
  1302         int row;
  1303         int length = dst.w * dst.format->BytesPerPixel;
  1304         Uint8 *p = (Uint8 *) dst.pixels;
  1305         for (row = 0; row < dst.h; ++row) {
  1306             SDL_memset(p, 0, length);
  1307             p += dst.pitch;
  1308         }
  1309     }
  1310 
  1311     /* Copy over the alpha channel */
  1312     if (surface_flags & SDL_SRCALPHA) {
  1313         if (fmt->Amask) {
  1314             surface->flags &= ~SDL_SRCALPHA;
  1315         } else {
  1316             /* FIXME: Need to make sure the texture has an alpha channel
  1317              *        and copy 'alpha' into the texture alpha channel.
  1318              */
  1319             alpha = surface->format->alpha;
  1320             SDL_SetAlpha(surface, 0, 0);
  1321         }
  1322     }
  1323 
  1324     /* Copy over the image data */
  1325     bounds.x = 0;
  1326     bounds.y = 0;
  1327     bounds.w = surface->w;
  1328     bounds.h = surface->h;
  1329     SDL_LowerBlit(surface, &bounds, &dst, &bounds);
  1330 
  1331     /* Clean up the original surface */
  1332     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1333         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
  1334         SDL_SetColorKey(surface, cflags, colorkey);
  1335     }
  1336     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1337         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1338         if (fmt->Amask) {
  1339             surface->flags |= SDL_SRCALPHA;
  1340         } else {
  1341             SDL_SetAlpha(surface, aflags, alpha);
  1342         }
  1343     }
  1344 
  1345     /* Update the texture */
  1346     if (dst.flags & SDL_PREALLOC) {
  1347         SDL_UnlockTexture(textureID);
  1348     } else {
  1349         SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
  1350         SDL_free(dst.pixels);
  1351     }
  1352     SDL_FreeFormat(dst.format);
  1353 
  1354     return textureID;
  1355 }
  1356 
  1357 static __inline__ SDL_Texture *
  1358 SDL_GetTextureFromID(SDL_TextureID textureID)
  1359 {
  1360     int hash;
  1361     SDL_Texture *texture;
  1362 
  1363     if (!_this) {
  1364         return NULL;
  1365     }
  1366 
  1367     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1368     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1369          texture = texture->next) {
  1370         if (texture->id == textureID) {
  1371             return texture;
  1372         }
  1373     }
  1374     return NULL;
  1375 }
  1376 
  1377 int
  1378 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
  1379                  int *w, int *h)
  1380 {
  1381     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1382 
  1383     if (!texture) {
  1384         return -1;
  1385     }
  1386 
  1387     if (format) {
  1388         *format = texture->format;
  1389     }
  1390     if (access) {
  1391         *access = texture->access;
  1392     }
  1393     if (w) {
  1394         *w = texture->w;
  1395     }
  1396     if (h) {
  1397         *h = texture->h;
  1398     }
  1399     return 0;
  1400 }
  1401 
  1402 int
  1403 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
  1404 {
  1405     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1406     SDL_Renderer *renderer;
  1407 
  1408     if (!texture) {
  1409         return -1;
  1410     }
  1411 
  1412     renderer = texture->renderer;
  1413     if (!renderer->QueryTexturePixels) {
  1414         return -1;
  1415     }
  1416     return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
  1417 }
  1418 
  1419 int
  1420 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
  1421                       int firstcolor, int ncolors)
  1422 {
  1423     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1424     SDL_Renderer *renderer;
  1425 
  1426     if (!texture) {
  1427         return -1;
  1428     }
  1429 
  1430     renderer = texture->renderer;
  1431     if (!renderer->SetTexturePalette) {
  1432         return -1;
  1433     }
  1434     return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
  1435                                        ncolors);
  1436 }
  1437 
  1438 int
  1439 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
  1440                       int firstcolor, int ncolors)
  1441 {
  1442     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1443     SDL_Renderer *renderer;
  1444 
  1445     if (!texture) {
  1446         return -1;
  1447     }
  1448 
  1449     renderer = texture->renderer;
  1450     if (!renderer->GetTexturePalette) {
  1451         return -1;
  1452     }
  1453     return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
  1454                                        ncolors);
  1455 }
  1456 
  1457 int
  1458 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
  1459                   const void *pixels, int pitch)
  1460 {
  1461     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1462     SDL_Renderer *renderer;
  1463     SDL_Rect full_rect;
  1464 
  1465     if (!texture) {
  1466         return -1;
  1467     }
  1468 
  1469     renderer = texture->renderer;
  1470     if (!renderer->UpdateTexture) {
  1471         return -1;
  1472     }
  1473 
  1474     if (!rect) {
  1475         full_rect.x = 0;
  1476         full_rect.y = 0;
  1477         full_rect.w = texture->w;
  1478         full_rect.h = texture->h;
  1479         rect = &full_rect;
  1480     }
  1481 
  1482     return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1483 }
  1484 
  1485 int
  1486 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
  1487                 void **pixels, int *pitch)
  1488 {
  1489     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1490     SDL_Renderer *renderer;
  1491     SDL_Rect full_rect;
  1492 
  1493     if (!texture) {
  1494         return -1;
  1495     }
  1496 
  1497     renderer = texture->renderer;
  1498     if (!renderer->LockTexture) {
  1499         return -1;
  1500     }
  1501 
  1502     if (!rect) {
  1503         full_rect.x = 0;
  1504         full_rect.y = 0;
  1505         full_rect.w = texture->w;
  1506         full_rect.h = texture->h;
  1507         rect = &full_rect;
  1508     }
  1509 
  1510     return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
  1511                                  pitch);
  1512 }
  1513 
  1514 void
  1515 SDL_UnlockTexture(SDL_TextureID textureID)
  1516 {
  1517     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1518     SDL_Renderer *renderer;
  1519 
  1520     if (!texture) {
  1521         return;
  1522     }
  1523 
  1524     renderer = texture->renderer;
  1525     if (!renderer->UnlockTexture) {
  1526         return;
  1527     }
  1528     return renderer->UnlockTexture(renderer, texture);
  1529 }
  1530 
  1531 void
  1532 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
  1533                  const SDL_Rect * rects)
  1534 {
  1535     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1536     SDL_Renderer *renderer;
  1537 
  1538     if (!texture) {
  1539         return;
  1540     }
  1541 
  1542     renderer = texture->renderer;
  1543     if (!renderer->DirtyTexture) {
  1544         return;
  1545     }
  1546     renderer->DirtyTexture(renderer, texture, numrects, rects);
  1547 }
  1548 
  1549 void
  1550 SDL_SelectRenderTexture(SDL_TextureID textureID)
  1551 {
  1552     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1553     SDL_Renderer *renderer;
  1554 
  1555     if (!texture || texture->access != SDL_TextureAccess_Render) {
  1556         return;
  1557     }
  1558     renderer = texture->renderer;
  1559     if (!renderer->SelectRenderTexture) {
  1560         return;
  1561     }
  1562     renderer->SelectRenderTexture(renderer, texture);
  1563 }
  1564 
  1565 int
  1566 SDL_RenderFill(const SDL_Rect * rect, Uint32 color)
  1567 {
  1568     SDL_Renderer *renderer;
  1569     SDL_Rect full_rect;
  1570 
  1571     if (!_this) {
  1572         return -1;
  1573     }
  1574 
  1575     renderer = SDL_CurrentDisplay.current_renderer;
  1576     if (!renderer || !renderer->RenderFill) {
  1577         return -1;
  1578     }
  1579 
  1580     if (!rect) {
  1581         SDL_Window *window = SDL_GetWindowFromID(renderer->window);
  1582         full_rect.x = 0;
  1583         full_rect.y = 0;
  1584         full_rect.w = window->w;
  1585         full_rect.h = window->h;
  1586         rect = &full_rect;
  1587     }
  1588 
  1589     renderer->RenderFill(renderer, rect, color);
  1590 }
  1591 
  1592 int
  1593 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
  1594                const SDL_Rect * dstrect, int blendMode, int scaleMode)
  1595 {
  1596     SDL_Texture *texture = SDL_GetTextureFromID(textureID);
  1597     SDL_Renderer *renderer;
  1598     SDL_Rect full_srcrect;
  1599     SDL_Rect full_dstrect;
  1600 
  1601     if (!texture || texture->renderer != SDL_CurrentDisplay.current_renderer) {
  1602         return;
  1603     }
  1604 
  1605     renderer = SDL_CurrentDisplay.current_renderer;
  1606     if (!renderer || !renderer->RenderCopy) {
  1607         return -1;
  1608     }
  1609 
  1610     if (!srcrect) {
  1611         full_srcrect.x = 0;
  1612         full_srcrect.y = 0;
  1613         full_srcrect.w = texture->w;
  1614         full_srcrect.h = texture->h;
  1615         srcrect = &full_srcrect;
  1616     }
  1617     if (!dstrect) {
  1618         SDL_Window *window = SDL_GetWindowFromID(renderer->window);
  1619         full_dstrect.x = 0;
  1620         full_dstrect.y = 0;
  1621         full_dstrect.w = window->w;
  1622         full_dstrect.h = window->h;
  1623         dstrect = &full_dstrect;
  1624     }
  1625 
  1626     return renderer->RenderCopy(renderer, texture, srcrect, dstrect,
  1627                                 blendMode, scaleMode);
  1628 }
  1629 
  1630 int
  1631 SDL_RenderReadPixels(const SDL_Rect * rect, void *pixels, int pitch)
  1632 {
  1633     SDL_Renderer *renderer;
  1634     SDL_Rect full_rect;
  1635 
  1636     if (!_this) {
  1637         return -1;
  1638     }
  1639 
  1640     renderer = SDL_CurrentDisplay.current_renderer;
  1641     if (!renderer || !renderer->RenderReadPixels) {
  1642         return -1;
  1643     }
  1644 
  1645     if (!rect) {
  1646         SDL_Window *window = SDL_GetWindowFromID(renderer->window);
  1647         full_rect.x = 0;
  1648         full_rect.y = 0;
  1649         full_rect.w = window->w;
  1650         full_rect.h = window->h;
  1651         rect = &full_rect;
  1652     }
  1653 
  1654     return renderer->RenderReadPixels(renderer, rect, pixels, pitch);
  1655 }
  1656 
  1657 int
  1658 SDL_RenderWritePixels(const SDL_Rect * rect, const void *pixels, int pitch)
  1659 {
  1660     SDL_Renderer *renderer;
  1661     SDL_Rect full_rect;
  1662 
  1663     if (!_this) {
  1664         return -1;
  1665     }
  1666 
  1667     renderer = SDL_CurrentDisplay.current_renderer;
  1668     if (!renderer || !renderer->RenderWritePixels) {
  1669         return -1;
  1670     }
  1671 
  1672     if (!rect) {
  1673         SDL_Window *window = SDL_GetWindowFromID(renderer->window);
  1674         full_rect.x = 0;
  1675         full_rect.y = 0;
  1676         full_rect.w = window->w;
  1677         full_rect.h = window->h;
  1678         rect = &full_rect;
  1679     }
  1680 
  1681     return renderer->RenderWritePixels(renderer, rect, pixels, pitch);
  1682 }
  1683 
  1684 void
  1685 SDL_RenderPresent(void)
  1686 {
  1687     SDL_Renderer *renderer;
  1688 
  1689     if (!_this) {
  1690         return;
  1691     }
  1692 
  1693     renderer = SDL_CurrentDisplay.current_renderer;
  1694     if (!renderer || !renderer->RenderPresent) {
  1695         return;
  1696     }
  1697 
  1698     renderer->RenderPresent(renderer);
  1699 }
  1700 
  1701 void
  1702 SDL_DestroyTexture(SDL_TextureID textureID)
  1703 {
  1704     int hash;
  1705     SDL_Texture *prev, *texture;
  1706     SDL_Renderer *renderer;
  1707 
  1708     if (!_this) {
  1709         return;
  1710     }
  1711 
  1712     /* Look up the texture in the hash table */
  1713     hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
  1714     prev = NULL;
  1715     for (texture = SDL_CurrentDisplay.textures[hash]; texture;
  1716          prev = texture, texture = texture->next) {
  1717         if (texture->id == textureID) {
  1718             break;
  1719         }
  1720     }
  1721     if (!texture) {
  1722         return;
  1723     }
  1724 
  1725     /* Unlink the texture from the list */
  1726     if (prev) {
  1727         prev->next = texture->next;
  1728     } else {
  1729         SDL_CurrentDisplay.textures[hash] = texture->next;
  1730     }
  1731 
  1732     /* Free the texture */
  1733     renderer = texture->renderer;
  1734     renderer->DestroyTexture(renderer, texture);
  1735     SDL_free(texture);
  1736 }
  1737 
  1738 void
  1739 SDL_DestroyRenderer(SDL_WindowID windowID)
  1740 {
  1741     SDL_Window *window = SDL_GetWindowFromID(windowID);
  1742     SDL_Renderer *renderer;
  1743     int i;
  1744 
  1745     if (!window) {
  1746         return;
  1747     }
  1748 
  1749     renderer = window->renderer;
  1750     if (!renderer) {
  1751         return;
  1752     }
  1753 
  1754     /* Free existing textures for this renderer */
  1755     for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
  1756         SDL_Texture *texture;
  1757         SDL_Texture *prev = NULL;
  1758         SDL_Texture *next;
  1759         for (texture = SDL_CurrentDisplay.textures[i]; texture;
  1760              texture = next) {
  1761             next = texture->next;
  1762             if (texture->renderer == renderer) {
  1763                 if (prev) {
  1764                     prev->next = next;
  1765                 } else {
  1766                     SDL_CurrentDisplay.textures[i] = next;
  1767                 }
  1768                 renderer->DestroyTexture(renderer, texture);
  1769                 SDL_free(texture);
  1770             } else {
  1771                 prev = texture;
  1772             }
  1773         }
  1774     }
  1775 
  1776     /* Free the renderer instance */
  1777     renderer->DestroyRenderer(renderer);
  1778 
  1779     /* Clear references */
  1780     window->renderer = NULL;
  1781     if (SDL_CurrentDisplay.current_renderer == renderer) {
  1782         SDL_CurrentDisplay.current_renderer = NULL;
  1783     }
  1784 }
  1785 
  1786 void
  1787 SDL_VideoQuit(void)
  1788 {
  1789     int i, j;
  1790 
  1791     if (!_this) {
  1792         return;
  1793     }
  1794 
  1795     /* Halt event processing before doing anything else */
  1796     SDL_StopEventLoop();
  1797 
  1798     /* Clean up the system video */
  1799     for (i = _this->num_displays; i--;) {
  1800         SDL_VideoDisplay *display = &_this->displays[i];
  1801         for (j = display->num_windows; j--;) {
  1802             SDL_DestroyWindow(display->windows[i].id);
  1803         }
  1804         if (display->windows) {
  1805             SDL_free(display->windows);
  1806             display->windows = NULL;
  1807         }
  1808         if (display->palette) {
  1809             SDL_FreePalette(display->palette);
  1810             display->palette = NULL;
  1811         }
  1812     }
  1813     _this->VideoQuit(_this);
  1814     if (_this->displays) {
  1815         SDL_free(_this->displays);
  1816     }
  1817     _this->free(_this);
  1818     _this = NULL;
  1819 }
  1820 
  1821 /* Load the GL driver library */
  1822 int
  1823 SDL_GL_LoadLibrary(const char *path)
  1824 {
  1825     int retval;
  1826 
  1827     retval = -1;
  1828     if (_this == NULL) {
  1829         SDL_SetError("Video subsystem has not been initialized");
  1830     } else {
  1831         if (_this->GL_LoadLibrary) {
  1832             retval = _this->GL_LoadLibrary(_this, path);
  1833         } else {
  1834             SDL_SetError("No dynamic GL support in video driver");
  1835         }
  1836     }
  1837     return (retval);
  1838 }
  1839 
  1840 void *
  1841 SDL_GL_GetProcAddress(const char *proc)
  1842 {
  1843     void *func;
  1844 
  1845     func = NULL;
  1846     if (_this->GL_GetProcAddress) {
  1847         if (_this->gl_config.driver_loaded) {
  1848             func = _this->GL_GetProcAddress(_this, proc);
  1849         } else {
  1850             SDL_SetError("No GL driver has been loaded");
  1851         }
  1852     } else {
  1853         SDL_SetError("No dynamic GL support in video driver");
  1854     }
  1855     return func;
  1856 }
  1857 
  1858 /* Set the specified GL attribute for setting up a GL video mode */
  1859 int
  1860 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
  1861 {
  1862     int retval;
  1863 
  1864     retval = 0;
  1865     switch (attr) {
  1866     case SDL_GL_RED_SIZE:
  1867         _this->gl_config.red_size = value;
  1868         break;
  1869     case SDL_GL_GREEN_SIZE:
  1870         _this->gl_config.green_size = value;
  1871         break;
  1872     case SDL_GL_BLUE_SIZE:
  1873         _this->gl_config.blue_size = value;
  1874         break;
  1875     case SDL_GL_ALPHA_SIZE:
  1876         _this->gl_config.alpha_size = value;
  1877         break;
  1878     case SDL_GL_DOUBLEBUFFER:
  1879         _this->gl_config.double_buffer = value;
  1880         break;
  1881     case SDL_GL_BUFFER_SIZE:
  1882         _this->gl_config.buffer_size = value;
  1883         break;
  1884     case SDL_GL_DEPTH_SIZE:
  1885         _this->gl_config.depth_size = value;
  1886         break;
  1887     case SDL_GL_STENCIL_SIZE:
  1888         _this->gl_config.stencil_size = value;
  1889         break;
  1890     case SDL_GL_ACCUM_RED_SIZE:
  1891         _this->gl_config.accum_red_size = value;
  1892         break;
  1893     case SDL_GL_ACCUM_GREEN_SIZE:
  1894         _this->gl_config.accum_green_size = value;
  1895         break;
  1896     case SDL_GL_ACCUM_BLUE_SIZE:
  1897         _this->gl_config.accum_blue_size = value;
  1898         break;
  1899     case SDL_GL_ACCUM_ALPHA_SIZE:
  1900         _this->gl_config.accum_alpha_size = value;
  1901         break;
  1902     case SDL_GL_STEREO:
  1903         _this->gl_config.stereo = value;
  1904         break;
  1905     case SDL_GL_MULTISAMPLEBUFFERS:
  1906         _this->gl_config.multisamplebuffers = value;
  1907         break;
  1908     case SDL_GL_MULTISAMPLESAMPLES:
  1909         _this->gl_config.multisamplesamples = value;
  1910         break;
  1911     case SDL_GL_ACCELERATED_VISUAL:
  1912         _this->gl_config.accelerated = value;
  1913         break;
  1914     case SDL_GL_SWAP_CONTROL:
  1915         _this->gl_config.swap_control = value;
  1916         break;
  1917     default:
  1918         SDL_SetError("Unknown OpenGL attribute");
  1919         retval = -1;
  1920         break;
  1921     }
  1922     return (retval);
  1923 }
  1924 
  1925 /* Retrieve an attribute value from the windowing system. */
  1926 int
  1927 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
  1928 {
  1929     int retval = -1;
  1930 
  1931     if (_this->GL_GetAttribute) {
  1932         retval = _this->GL_GetAttribute(_this, attr, value);
  1933     } else {
  1934         *value = 0;
  1935         SDL_SetError("GL_GetAttribute not supported");
  1936     }
  1937     return retval;
  1938 }
  1939 
  1940 /* Perform a GL buffer swap on the current GL context */
  1941 void
  1942 SDL_GL_SwapBuffers(void)
  1943 {
  1944     // FIXME: Track the current window context - do we provide N contexts, and match them to M windows, or is there a one-to-one mapping?
  1945     _this->GL_SwapBuffers(_this);
  1946 }
  1947 
  1948 #if 0                           // FIXME
  1949 /* Utility function used by SDL_WM_SetIcon();
  1950  * flags & 1 for color key, flags & 2 for alpha channel. */
  1951 static void
  1952 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
  1953 {
  1954     int x, y;
  1955     Uint32 colorkey;
  1956 #define SET_MASKBIT(icon, x, y, mask) \
  1957 	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
  1958 
  1959     colorkey = icon->format->colorkey;
  1960     switch (icon->format->BytesPerPixel) {
  1961     case 1:
  1962         {
  1963             Uint8 *pixels;
  1964             for (y = 0; y < icon->h; ++y) {
  1965                 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
  1966                 for (x = 0; x < icon->w; ++x) {
  1967                     if (*pixels++ == colorkey) {
  1968                         SET_MASKBIT(icon, x, y, mask);
  1969                     }
  1970                 }
  1971             }
  1972         }
  1973         break;
  1974 
  1975     case 2:
  1976         {
  1977             Uint16 *pixels;
  1978             for (y = 0; y < icon->h; ++y) {
  1979                 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
  1980                 for (x = 0; x < icon->w; ++x) {
  1981                     if ((flags & 1) && *pixels == colorkey) {
  1982                         SET_MASKBIT(icon, x, y, mask);
  1983                     } else if ((flags & 2)
  1984                                && (*pixels & icon->format->Amask) == 0) {
  1985                         SET_MASKBIT(icon, x, y, mask);
  1986                     }
  1987                     pixels++;
  1988                 }
  1989             }
  1990         }
  1991         break;
  1992 
  1993     case 4:
  1994         {
  1995             Uint32 *pixels;
  1996             for (y = 0; y < icon->h; ++y) {
  1997                 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
  1998                 for (x = 0; x < icon->w; ++x) {
  1999                     if ((flags & 1) && *pixels == colorkey) {
  2000                         SET_MASKBIT(icon, x, y, mask);
  2001                     } else if ((flags & 2)
  2002                                && (*pixels & icon->format->Amask) == 0) {
  2003                         SET_MASKBIT(icon, x, y, mask);
  2004                     }
  2005                     pixels++;
  2006                 }
  2007             }
  2008         }
  2009         break;
  2010     }
  2011 }
  2012 
  2013 /*
  2014  * Sets the window manager icon for the display window.
  2015  */
  2016 void
  2017 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
  2018 {
  2019     if (icon && _this->SetIcon) {
  2020         /* Generate a mask if necessary, and create the icon! */
  2021         if (mask == NULL) {
  2022             int mask_len = icon->h * (icon->w + 7) / 8;
  2023             int flags = 0;
  2024             mask = (Uint8 *) SDL_malloc(mask_len);
  2025             if (mask == NULL) {
  2026                 return;
  2027             }
  2028             SDL_memset(mask, ~0, mask_len);
  2029             if (icon->flags & SDL_SRCCOLORKEY)
  2030                 flags |= 1;
  2031             if (icon->flags & SDL_SRCALPHA)
  2032                 flags |= 2;
  2033             if (flags) {
  2034                 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  2035             }
  2036             _this->SetIcon(_this, icon, mask);
  2037             SDL_free(mask);
  2038         } else {
  2039             _this->SetIcon(_this, icon, mask);
  2040         }
  2041     }
  2042 }
  2043 #endif
  2044 
  2045 SDL_bool
  2046 SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
  2047 {
  2048     SDL_Window *window = SDL_GetWindowFromID(windowID);
  2049 
  2050     if (!window || !_this->GetWindowWMInfo) {
  2051         return SDL_FALSE;
  2052     }
  2053     return (_this->GetWindowWMInfo(_this, window, info));
  2054 }
  2055 
  2056 /* vi: set ts=4 sw=4 expandtab: */