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