2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 /* The high-level video driver subsystem */
27 #include "SDL_video.h"
28 #include "SDL_sysvideo.h"
30 #include "SDL_pixels_c.h"
31 #include "SDL_renderer_gl.h"
32 #include "SDL_renderer_gles.h"
33 #include "SDL_renderer_sw.h"
34 #include "../events/SDL_sysevents.h"
35 #include "../events/SDL_events_c.h"
37 #if SDL_VIDEO_OPENGL_ES
38 #include "SDL_opengles.h"
39 #endif /* SDL_VIDEO_OPENGL_ES */
42 #include "SDL_opengl.h"
44 /* On Windows, windows.h defines CreateWindow */
48 #endif /* SDL_VIDEO_OPENGL */
50 /* Available video drivers */
51 static VideoBootStrap *bootstrap[] = {
52 #if SDL_VIDEO_DRIVER_COCOA
55 #if SDL_VIDEO_DRIVER_X11
58 #if SDL_VIDEO_DRIVER_FBCON
61 #if SDL_VIDEO_DRIVER_DIRECTFB
64 #if SDL_VIDEO_DRIVER_PS2GS
67 #if SDL_VIDEO_DRIVER_PS3
70 #if SDL_VIDEO_DRIVER_SVGALIB
73 #if SDL_VIDEO_DRIVER_GAPI
76 #if SDL_VIDEO_DRIVER_WIN32
79 #if SDL_VIDEO_DRIVER_BWINDOW
82 #if SDL_VIDEO_DRIVER_PHOTON
85 #if SDL_VIDEO_DRIVER_QNXGF
88 #if SDL_VIDEO_DRIVER_EPOC
91 #if SDL_VIDEO_DRIVER_RISCOS
94 #if SDL_VIDEO_DRIVER_NDS
97 #if SDL_VIDEO_DRIVER_UIKIT
100 #if SDL_VIDEO_DRIVER_DUMMY
103 #if SDL_VIDEO_DRIVER_PANDORA
109 static SDL_VideoDevice *_this = NULL;
111 /* Various local functions */
112 static void SDL_UpdateWindowGrab(SDL_Window * window);
115 cmpmodes(const void *A, const void *B)
117 SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
118 SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
126 if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
127 return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
129 if (SDL_PIXELLAYOUT(a.format) != SDL_PIXELLAYOUT(b.format)) {
130 return SDL_PIXELLAYOUT(b.format) - SDL_PIXELLAYOUT(a.format);
132 if (a.refresh_rate != b.refresh_rate) {
133 return b.refresh_rate - a.refresh_rate;
139 SDL_UninitializedVideo()
141 SDL_SetError("Video subsystem has not been initialized");
145 SDL_GetNumVideoDrivers(void)
147 return SDL_arraysize(bootstrap) - 1;
151 SDL_GetVideoDriver(int index)
153 if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
154 return bootstrap[index]->name;
160 * Initialize the video and event subsystems -- determine native pixel format
163 SDL_VideoInit(const char *driver_name, Uint32 flags)
165 SDL_VideoDevice *video;
169 /* Toggle the event thread flags, based on OS requirements */
170 #if defined(MUST_THREAD_EVENTS)
171 flags |= SDL_INIT_EVENTTHREAD;
172 #elif defined(CANT_THREAD_EVENTS)
173 if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
174 SDL_SetError("OS doesn't support threaded events");
179 /* Start the event loop */
180 if (SDL_StartEventLoop(flags) < 0) {
183 /* Check to make sure we don't overwrite '_this' */
187 /* Select the proper video driver */
190 if (driver_name == NULL) {
191 driver_name = SDL_getenv("SDL_VIDEODRIVER");
193 if (driver_name != NULL) {
194 for (i = 0; bootstrap[i]; ++i) {
195 if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
196 video = bootstrap[i]->create(index);
201 for (i = 0; bootstrap[i]; ++i) {
202 if (bootstrap[i]->available()) {
203 video = bootstrap[i]->create(index);
212 SDL_SetError("%s not available", driver_name);
214 SDL_SetError("No available video device");
219 _this->name = bootstrap[i]->name;
220 _this->next_object_id = 1;
223 /* Set some very sane GL defaults */
224 _this->gl_config.driver_loaded = 0;
225 _this->gl_config.dll_handle = NULL;
226 _this->gl_config.red_size = 3;
227 _this->gl_config.green_size = 3;
228 _this->gl_config.blue_size = 2;
229 _this->gl_config.alpha_size = 0;
230 _this->gl_config.buffer_size = 0;
231 _this->gl_config.depth_size = 16;
232 _this->gl_config.stencil_size = 0;
233 _this->gl_config.double_buffer = 1;
234 _this->gl_config.accum_red_size = 0;
235 _this->gl_config.accum_green_size = 0;
236 _this->gl_config.accum_blue_size = 0;
237 _this->gl_config.accum_alpha_size = 0;
238 _this->gl_config.stereo = 0;
239 _this->gl_config.multisamplebuffers = 0;
240 _this->gl_config.multisamplesamples = 0;
241 _this->gl_config.retained_backing = 1;
242 _this->gl_config.accelerated = -1; /* not known, don't set */
243 _this->gl_config.major_version = 2;
244 _this->gl_config.minor_version = 1;
246 /* Initialize the video subsystem */
247 if (_this->VideoInit(_this) < 0) {
251 /* Make sure some displays were added */
252 if (_this->num_displays == 0) {
253 SDL_SetError("The video driver did not add any displays");
257 /* The software renderer is always available */
258 for (i = 0; i < _this->num_displays; ++i) {
259 SDL_VideoDisplay *display = &_this->displays[i];
260 if (_this->GL_CreateContext) {
261 #if SDL_VIDEO_RENDER_OGL
262 SDL_AddRenderDriver(display, &GL_RenderDriver);
264 #if SDL_VIDEO_RENDER_OGL_ES
265 SDL_AddRenderDriver(display, &GL_ES_RenderDriver);
268 if (display->num_render_drivers > 0) {
269 SDL_AddRenderDriver(display, &SW_RenderDriver);
273 /* We're ready to go! */
278 SDL_GetCurrentVideoDriver()
281 SDL_UninitializedVideo();
294 SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
296 SDL_VideoDisplay display;
300 display.desktop_mode = *desktop_mode;
302 display.current_mode = display.desktop_mode;
304 return SDL_AddVideoDisplay(&display);
308 SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
310 SDL_VideoDisplay *displays;
314 SDL_realloc(_this->displays,
315 (_this->num_displays + 1) * sizeof(*displays));
317 index = _this->num_displays++;
318 displays[index] = *display;
319 displays[index].device = _this;
320 _this->displays = displays;
328 SDL_GetNumVideoDisplays(void)
331 SDL_UninitializedVideo();
334 return _this->num_displays;
338 SDL_GetDisplayBounds(int index, SDL_Rect * rect)
341 SDL_UninitializedVideo();
344 if (index < 0 || index >= _this->num_displays) {
345 SDL_SetError("index must be in the range 0 - %d",
346 _this->num_displays - 1);
350 SDL_VideoDisplay *display = &_this->displays[index];
352 if (_this->GetDisplayBounds) {
353 if (_this->GetDisplayBounds(_this, display, rect) < 0) {
357 /* Assume that the displays are left to right */
362 SDL_GetDisplayBounds(index-1, rect);
365 rect->w = display->desktop_mode.w;
366 rect->h = display->desktop_mode.h;
373 SDL_SelectVideoDisplay(int index)
376 SDL_UninitializedVideo();
379 if (index < 0 || index >= _this->num_displays) {
380 SDL_SetError("index must be in the range 0 - %d",
381 _this->num_displays - 1);
384 _this->current_display = index;
389 SDL_GetCurrentVideoDisplay(void)
392 SDL_UninitializedVideo();
395 return _this->current_display;
399 SDL_AddDisplayMode(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
401 SDL_DisplayMode *modes;
404 /* Make sure we don't already have the mode in the list */
405 modes = display->display_modes;
406 nmodes = display->num_display_modes;
407 for (i = nmodes; i--;) {
408 if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
413 /* Go ahead and add the new mode */
414 if (nmodes == display->max_display_modes) {
417 (display->max_display_modes + 32) * sizeof(*modes));
421 display->display_modes = modes;
422 display->max_display_modes += 32;
424 modes[nmodes] = *mode;
425 display->num_display_modes++;
427 /* Re-sort video modes */
428 SDL_qsort(display->display_modes, display->num_display_modes,
429 sizeof(SDL_DisplayMode), cmpmodes);
435 SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
437 if (!display->num_display_modes && _this->GetDisplayModes) {
438 _this->GetDisplayModes(_this, display);
439 SDL_qsort(display->display_modes, display->num_display_modes,
440 sizeof(SDL_DisplayMode), cmpmodes);
442 return display->num_display_modes;
446 SDL_GetNumDisplayModes()
449 return SDL_GetNumDisplayModesForDisplay(&SDL_CurrentDisplay);
455 SDL_GetDisplayModeForDisplay(SDL_VideoDisplay * display, int index, SDL_DisplayMode * mode)
457 if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
458 SDL_SetError("index must be in the range of 0 - %d",
459 SDL_GetNumDisplayModesForDisplay(display) - 1);
463 *mode = display->display_modes[index];
469 SDL_GetDisplayMode(int index, SDL_DisplayMode * mode)
471 return SDL_GetDisplayModeForDisplay(&SDL_CurrentDisplay, index, mode);
475 SDL_GetDesktopDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
478 *mode = display->desktop_mode;
484 SDL_GetDesktopDisplayMode(SDL_DisplayMode * mode)
487 SDL_UninitializedVideo();
490 return SDL_GetDesktopDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
494 SDL_GetCurrentDisplayModeForDisplay(SDL_VideoDisplay * display, SDL_DisplayMode * mode)
497 *mode = display->current_mode;
503 SDL_GetCurrentDisplayMode(SDL_DisplayMode * mode)
506 SDL_UninitializedVideo();
509 return SDL_GetCurrentDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
513 SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
514 const SDL_DisplayMode * mode,
515 SDL_DisplayMode * closest)
517 Uint32 target_format;
518 int target_refresh_rate;
520 SDL_DisplayMode *current, *match;
522 if (!mode || !closest) {
523 SDL_SetError("Missing desired mode or closest mode parameter");
527 /* Default to the desktop format */
529 target_format = mode->format;
531 target_format = display->desktop_mode.format;
534 /* Default to the desktop refresh rate */
535 if (mode->refresh_rate) {
536 target_refresh_rate = mode->refresh_rate;
538 target_refresh_rate = display->desktop_mode.refresh_rate;
542 for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
543 current = &display->display_modes[i];
545 if (current->w && (current->w < mode->w)) {
546 /* Out of sorted modes large enough here */
549 if (current->h && (current->h < mode->h)) {
550 if (current->w && (current->w == mode->w)) {
551 /* Out of sorted modes large enough here */
554 /* Wider, but not tall enough, due to a different
555 aspect ratio. This mode must be skipped, but closer
556 modes may still follow. */
559 if (!match || current->w < match->w || current->h < match->h) {
563 if (current->format != match->format) {
564 /* Sorted highest depth to lowest */
565 if (current->format == target_format ||
566 (SDL_BITSPERPIXEL(current->format) >=
567 SDL_BITSPERPIXEL(target_format)
568 && SDL_PIXELTYPE(current->format) ==
569 SDL_PIXELTYPE(target_format))) {
574 if (current->refresh_rate != match->refresh_rate) {
575 /* Sorted highest refresh to lowest */
576 if (current->refresh_rate >= target_refresh_rate) {
583 closest->format = match->format;
585 closest->format = mode->format;
587 if (match->w && match->h) {
588 closest->w = match->w;
589 closest->h = match->h;
591 closest->w = mode->w;
592 closest->h = mode->h;
594 if (match->refresh_rate) {
595 closest->refresh_rate = match->refresh_rate;
597 closest->refresh_rate = mode->refresh_rate;
599 closest->driverdata = match->driverdata;
602 * Pick some reasonable defaults if the app and driver don't
605 if (!closest->format) {
606 closest->format = SDL_PIXELFORMAT_RGB888;
620 SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
621 SDL_DisplayMode * closest)
624 SDL_UninitializedVideo();
627 return SDL_GetClosestDisplayModeForDisplay(&SDL_CurrentDisplay, mode, closest);
631 SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
633 SDL_DisplayMode display_mode;
634 SDL_DisplayMode current_mode;
638 display_mode = *mode;
640 /* Default to the current mode */
641 if (!display_mode.format) {
642 display_mode.format = display->current_mode.format;
644 if (!display_mode.w) {
645 display_mode.w = display->current_mode.w;
647 if (!display_mode.h) {
648 display_mode.h = display->current_mode.h;
650 if (!display_mode.refresh_rate) {
651 display_mode.refresh_rate = display->current_mode.refresh_rate;
654 /* Get a good video mode, the closest one possible */
655 if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
656 SDL_SetError("No video mode large enough for %dx%d",
657 display_mode.w, display_mode.h);
661 display_mode = display->desktop_mode;
664 /* See if there's anything left to do */
665 SDL_GetCurrentDisplayModeForDisplay(display, ¤t_mode);
666 if (SDL_memcmp(&display_mode, ¤t_mode, sizeof(display_mode)) == 0) {
670 /* Actually change the display mode */
671 if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
674 display->current_mode = display_mode;
676 /* Set up a palette, if necessary */
677 if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
678 ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
682 if ((!ncolors && display->palette) || (ncolors && !display->palette)
683 || (ncolors && ncolors != display->palette->ncolors)) {
684 if (display->palette) {
685 SDL_FreePalette(display->palette);
686 display->palette = NULL;
689 display->palette = SDL_AllocPalette(ncolors);
690 if (!display->palette) {
693 SDL_DitherColors(display->palette->colors,
694 SDL_BITSPERPIXEL(display_mode.format));
702 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
705 SDL_UninitializedVideo();
708 return SDL_SetDisplayModeForDisplay(&SDL_CurrentDisplay, mode);
712 SDL_SetWindowDisplayMode(SDL_WindowID windowID, const SDL_DisplayMode * mode)
714 SDL_Window *window = SDL_GetWindowFromID(windowID);
721 window->fullscreen_mode = *mode;
723 SDL_zero(window->fullscreen_mode);
729 SDL_GetWindowDisplayMode(SDL_WindowID windowID, SDL_DisplayMode * mode)
731 SDL_Window *window = SDL_GetWindowFromID(windowID);
732 SDL_DisplayMode fullscreen_mode;
738 fullscreen_mode = window->fullscreen_mode;
739 if (!fullscreen_mode.w) {
740 fullscreen_mode.w = window->w;
742 if (!fullscreen_mode.h) {
743 fullscreen_mode.h = window->h;
746 if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayFromWindow(window),
749 SDL_SetError("Couldn't find display mode match");
754 *mode = fullscreen_mode;
760 SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool attempt)
762 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
765 /* See if we're already processing a window */
766 if (display->updating_fullscreen) {
770 display->updating_fullscreen = SDL_TRUE;
772 /* See if we even want to do anything here */
773 if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
774 (window->flags & SDL_WINDOW_SHOWN)) {
776 /* We just gained some state, try to gain all states */
777 if (window->flags & SDL_WINDOW_MINIMIZED) {
778 SDL_RestoreWindow(window->id);
780 SDL_RaiseWindow(window->id);
783 /* We just lost some state, try to release all states */
784 SDL_MinimizeWindow(window->id);
788 if (FULLSCREEN_VISIBLE(window)) {
789 /* Hide any other fullscreen windows */
790 for (i = 0; i < display->num_windows; ++i) {
791 SDL_Window *other = &display->windows[i];
792 if (other != window && FULLSCREEN_VISIBLE(other)) {
793 SDL_MinimizeWindow(other->id);
798 display->updating_fullscreen = SDL_FALSE;
800 /* See if there are any fullscreen windows */
801 for (i = 0; i < display->num_windows; ++i) {
802 window = &display->windows[i];
803 if (FULLSCREEN_VISIBLE(window)) {
804 SDL_DisplayMode fullscreen_mode;
805 if (SDL_GetWindowDisplayMode(window->id, &fullscreen_mode) == 0) {
806 SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
807 display->fullscreen_window = window;
813 /* Nope, restore the desktop mode */
814 SDL_SetDisplayModeForDisplay(display, NULL);
815 display->fullscreen_window = NULL;
819 SDL_SetPaletteForDisplay(SDL_VideoDisplay * display, const SDL_Color * colors, int firstcolor, int ncolors)
821 SDL_Palette *palette;
824 palette = display->palette;
826 SDL_SetError("Display mode does not have a palette");
829 status = SDL_SetPaletteColors(palette, colors, firstcolor, ncolors);
831 if (_this->SetDisplayPalette) {
832 if (_this->SetDisplayPalette(_this, display, palette) < 0) {
840 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
843 SDL_UninitializedVideo();
846 return SDL_SetPaletteForDisplay(&SDL_CurrentDisplay, colors, firstcolor, ncolors);
850 SDL_GetPaletteForDisplay(SDL_VideoDisplay * display, SDL_Color * colors, int firstcolor, int ncolors)
852 SDL_Palette *palette;
854 palette = display->palette;
855 if (!palette || !palette->ncolors) {
856 SDL_SetError("Display mode does not have a palette");
859 if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) {
860 SDL_SetError("Palette indices are out of range");
863 SDL_memcpy(colors, &palette->colors[firstcolor],
864 ncolors * sizeof(*colors));
869 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
872 SDL_UninitializedVideo();
875 return SDL_GetPaletteForDisplay(&SDL_CurrentDisplay, colors, firstcolor, ncolors);
879 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
881 const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
883 SDL_WINDOW_BORDERLESS |
884 SDL_WINDOW_RESIZABLE |
885 SDL_WINDOW_INPUT_GRABBED);
886 SDL_VideoDisplay *display;
892 /* Initialize the video system if needed */
893 if (SDL_VideoInit(NULL, 0) < 0) {
897 if (flags & SDL_WINDOW_OPENGL) {
898 if (!_this->GL_CreateContext) {
899 SDL_SetError("No OpenGL support in video driver");
902 SDL_GL_LoadLibrary(NULL);
905 window.id = _this->next_object_id++;
910 window.flags = (flags & allowed_flags);
911 window.display = _this->current_display;
913 if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
914 if (flags & SDL_WINDOW_OPENGL) {
915 SDL_GL_UnloadLibrary();
919 display = &SDL_CurrentDisplay;
920 num_windows = display->num_windows;
922 SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
924 if (_this->DestroyWindow) {
925 _this->DestroyWindow(_this, &window);
927 if (flags & SDL_WINDOW_OPENGL) {
928 SDL_GL_UnloadLibrary();
932 windows[num_windows] = window;
933 display->windows = windows;
934 display->num_windows++;
937 SDL_SetWindowTitle(window.id, title);
939 if (flags & SDL_WINDOW_MAXIMIZED) {
940 SDL_MaximizeWindow(window.id);
942 if (flags & SDL_WINDOW_MINIMIZED) {
943 SDL_MinimizeWindow(window.id);
945 if (flags & SDL_WINDOW_SHOWN) {
946 SDL_ShowWindow(window.id);
948 SDL_UpdateWindowGrab(&window);
954 SDL_CreateWindowFrom(const void *data)
956 SDL_VideoDisplay *display;
962 SDL_UninitializedVideo();
966 window.id = _this->next_object_id++;
967 window.display = _this->current_display;
968 window.flags = SDL_WINDOW_FOREIGN;
970 if (!_this->CreateWindowFrom ||
971 _this->CreateWindowFrom(_this, &window, data) < 0) {
974 /* FIXME: Find out what display this window is actually on... */
975 display = &SDL_CurrentDisplay;
976 num_windows = display->num_windows;
978 SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
980 if (_this->DestroyWindow) {
981 _this->DestroyWindow(_this, &window);
984 SDL_free(window.title);
988 windows[num_windows] = window;
989 display->windows = windows;
990 display->num_windows++;
996 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
998 const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
1000 SDL_WINDOW_BORDERLESS |
1001 SDL_WINDOW_RESIZABLE |
1002 SDL_WINDOW_INPUT_GRABBED |
1003 SDL_WINDOW_FOREIGN);
1004 char *title = window->title;
1006 if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
1007 SDL_SetError("No OpenGL support in video driver");
1010 if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
1011 if (flags & SDL_WINDOW_OPENGL) {
1012 SDL_GL_LoadLibrary(NULL);
1014 SDL_GL_UnloadLibrary();
1018 if (window->flags & SDL_WINDOW_FOREIGN) {
1019 /* Can't destroy and re-create foreign windows, hrm */
1020 flags |= SDL_WINDOW_FOREIGN;
1022 flags &= ~SDL_WINDOW_FOREIGN;
1025 if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
1026 _this->DestroyWindow(_this, window);
1029 window->title = NULL;
1030 window->flags = (flags & allowed_flags);
1032 if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
1033 if (_this->CreateWindow(_this, window) < 0) {
1034 if (flags & SDL_WINDOW_OPENGL) {
1035 SDL_GL_UnloadLibrary();
1042 SDL_SetWindowTitle(window->id, title);
1045 if (flags & SDL_WINDOW_MAXIMIZED) {
1046 SDL_MaximizeWindow(window->id);
1048 if (flags & SDL_WINDOW_MINIMIZED) {
1049 SDL_MinimizeWindow(window->id);
1051 if (flags & SDL_WINDOW_SHOWN) {
1052 SDL_ShowWindow(window->id);
1054 SDL_UpdateWindowGrab(window);
1060 SDL_GetWindowFromID(SDL_WindowID windowID)
1065 SDL_UninitializedVideo();
1069 for (i = 0; i < _this->num_displays; ++i) {
1070 SDL_VideoDisplay *display = &_this->displays[i];
1071 for (j = 0; j < display->num_windows; ++j) {
1072 SDL_Window *window = &display->windows[j];
1073 if (window->id == windowID) {
1079 /* Just return the first active window */
1080 for (i = 0; i < _this->num_displays; ++i) {
1081 SDL_VideoDisplay *display = &_this->displays[i];
1082 for (j = 0; j < display->num_windows; ++j) {
1083 SDL_Window *window = &display->windows[j];
1088 /* Couldn't find the window with the requested ID */
1089 SDL_SetError("Invalid window ID");
1094 SDL_GetDisplayFromWindow(SDL_Window * window)
1097 SDL_UninitializedVideo();
1103 return &_this->displays[window->display];
1106 static __inline__ SDL_Renderer *
1107 SDL_GetCurrentRenderer(SDL_bool create)
1110 SDL_UninitializedVideo();
1113 if (!SDL_CurrentRenderer) {
1115 SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
1118 if (SDL_CreateRenderer(0, -1, 0) < 0) {
1122 return SDL_CurrentRenderer;
1126 SDL_GetWindowFlags(SDL_WindowID windowID)
1128 SDL_Window *window = SDL_GetWindowFromID(windowID);
1133 return window->flags;
1137 SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
1139 SDL_Window *window = SDL_GetWindowFromID(windowID);
1141 if (!window || title == window->title) {
1144 if (window->title) {
1145 SDL_free(window->title);
1148 window->title = SDL_strdup(title);
1150 window->title = NULL;
1153 if (_this->SetWindowTitle) {
1154 _this->SetWindowTitle(_this, window);
1159 SDL_GetWindowTitle(SDL_WindowID windowID)
1161 SDL_Window *window = SDL_GetWindowFromID(windowID);
1166 return window->title;
1170 SDL_SetWindowIcon(SDL_WindowID windowID, SDL_Surface * icon)
1172 SDL_Window *window = SDL_GetWindowFromID(windowID);
1177 if (_this->SetWindowIcon) {
1178 _this->SetWindowIcon(_this, window, icon);
1183 SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
1185 SDL_Window *window = SDL_GetWindowFromID(windowID);
1190 window->userdata = userdata;
1194 SDL_GetWindowData(SDL_WindowID windowID)
1196 SDL_Window *window = SDL_GetWindowFromID(windowID);
1201 return window->userdata;
1205 SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
1207 SDL_Window *window = SDL_GetWindowFromID(windowID);
1208 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1213 if (x != SDL_WINDOWPOS_UNDEFINED) {
1216 if (y != SDL_WINDOWPOS_UNDEFINED) {
1219 if (_this->SetWindowPosition) {
1220 _this->SetWindowPosition(_this, window);
1222 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MOVED, x, y);
1226 SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
1228 SDL_Window *window = SDL_GetWindowFromID(windowID);
1242 SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
1244 SDL_Window *window = SDL_GetWindowFromID(windowID);
1252 if (_this->SetWindowSize) {
1253 _this->SetWindowSize(_this, window);
1255 SDL_OnWindowResized(window);
1259 SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
1261 SDL_Window *window = SDL_GetWindowFromID(windowID);
1281 SDL_ShowWindow(SDL_WindowID windowID)
1283 SDL_Window *window = SDL_GetWindowFromID(windowID);
1285 if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
1289 if (_this->ShowWindow) {
1290 _this->ShowWindow(_this, window);
1292 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_SHOWN, 0, 0);
1296 SDL_HideWindow(SDL_WindowID windowID)
1298 SDL_Window *window = SDL_GetWindowFromID(windowID);
1300 if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
1304 if (_this->HideWindow) {
1305 _this->HideWindow(_this, window);
1307 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_HIDDEN, 0, 0);
1311 SDL_RaiseWindow(SDL_WindowID windowID)
1313 SDL_Window *window = SDL_GetWindowFromID(windowID);
1315 if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
1318 if (_this->RaiseWindow) {
1319 _this->RaiseWindow(_this, window);
1321 /* FIXME: What we really want is a way to request focus */
1322 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
1327 SDL_MaximizeWindow(SDL_WindowID windowID)
1329 SDL_Window *window = SDL_GetWindowFromID(windowID);
1331 if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
1335 if (_this->MaximizeWindow) {
1336 _this->MaximizeWindow(_this, window);
1338 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
1342 SDL_MinimizeWindow(SDL_WindowID windowID)
1344 SDL_Window *window = SDL_GetWindowFromID(windowID);
1346 if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
1350 if (_this->MinimizeWindow) {
1351 _this->MinimizeWindow(_this, window);
1353 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
1357 SDL_RestoreWindow(SDL_WindowID windowID)
1359 SDL_Window *window = SDL_GetWindowFromID(windowID);
1362 || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
1366 if (_this->RestoreWindow) {
1367 _this->RestoreWindow(_this, window);
1369 SDL_SendWindowEvent(window->id, SDL_WINDOWEVENT_RESTORED, 0, 0);
1373 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
1375 SDL_Window *window = SDL_GetWindowFromID(windowID);
1381 fullscreen = SDL_WINDOW_FULLSCREEN;
1383 if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
1387 window->flags |= SDL_WINDOW_FULLSCREEN;
1389 SDL_UpdateFullscreenMode(window, SDL_TRUE);
1391 window->flags &= ~SDL_WINDOW_FULLSCREEN;
1393 SDL_UpdateFullscreenMode(window, SDL_FALSE);
1399 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
1401 SDL_Window *window = SDL_GetWindowFromID(windowID);
1403 if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
1407 window->flags |= SDL_WINDOW_INPUT_GRABBED;
1409 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
1411 SDL_UpdateWindowGrab(window);
1415 SDL_UpdateWindowGrab(SDL_Window * window)
1417 if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
1418 _this->SetWindowGrab(_this, window);
1423 SDL_GetWindowGrab(SDL_WindowID windowID)
1425 SDL_Window *window = SDL_GetWindowFromID(windowID);
1430 return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
1434 SDL_OnWindowShown(SDL_Window * window)
1436 SDL_RaiseWindow(window->id);
1437 SDL_UpdateFullscreenMode(window, SDL_TRUE);
1441 SDL_OnWindowHidden(SDL_Window * window)
1443 SDL_UpdateFullscreenMode(window, SDL_FALSE);
1447 SDL_OnWindowResized(SDL_Window * window)
1449 SDL_Renderer *renderer = window->renderer;
1451 if (renderer && renderer->DisplayModeChanged) {
1452 renderer->DisplayModeChanged(renderer);
1457 SDL_OnWindowMinimized(SDL_Window * window)
1459 SDL_UpdateFullscreenMode(window, SDL_FALSE);
1463 SDL_OnWindowRestored(SDL_Window * window)
1465 SDL_RaiseWindow(window->id);
1466 SDL_UpdateFullscreenMode(window, SDL_TRUE);
1470 SDL_OnWindowFocusGained(SDL_Window * window)
1472 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1474 if (display->gamma && _this->SetDisplayGammaRamp) {
1475 _this->SetDisplayGammaRamp(_this, display, display->gamma);
1477 if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
1478 && _this->SetWindowGrab) {
1479 _this->SetWindowGrab(_this, window);
1484 SDL_OnWindowFocusLost(SDL_Window * window)
1486 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1488 /* If we're fullscreen on a single-head system and lose focus, minimize */
1489 if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
1490 _this->num_displays == 1) {
1491 SDL_MinimizeWindow(window->id);
1494 if (display->gamma && _this->SetDisplayGammaRamp) {
1495 _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
1497 if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
1498 && _this->SetWindowGrab) {
1499 _this->SetWindowGrab(_this, window);
1504 SDL_GetFocusWindow(void)
1506 SDL_VideoDisplay *display;
1512 display = &SDL_CurrentDisplay;
1513 for (i = 0; i < display->num_windows; ++i) {
1514 SDL_Window *window = &display->windows[i];
1516 if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
1524 SDL_DestroyWindow(SDL_WindowID windowID)
1532 for (i = 0; i < _this->num_displays; ++i) {
1533 SDL_VideoDisplay *display = &_this->displays[i];
1534 for (j = 0; j < display->num_windows; ++j) {
1535 SDL_Window *window = &display->windows[j];
1536 if (window->id != windowID) {
1539 if (window->title) {
1540 SDL_free(window->title);
1541 window->title = NULL;
1543 if (window->renderer) {
1544 SDL_DestroyRenderer(window->id);
1545 window->renderer = NULL;
1548 /* Restore video mode, etc. */
1549 SDL_UpdateFullscreenMode(window, SDL_FALSE);
1551 if (_this->DestroyWindow) {
1552 _this->DestroyWindow(_this, window);
1554 if (window->flags & SDL_WINDOW_OPENGL) {
1555 SDL_GL_UnloadLibrary();
1557 if (j != display->num_windows - 1) {
1558 SDL_memcpy(&display->windows[i],
1559 &display->windows[i + 1],
1560 (display->num_windows - i - 1) * sizeof(*window));
1562 --display->num_windows;
1569 SDL_AddRenderDriver(SDL_VideoDisplay * display, const SDL_RenderDriver * driver)
1571 SDL_RenderDriver *render_drivers;
1574 SDL_realloc(display->render_drivers,
1575 (display->num_render_drivers +
1576 1) * sizeof(*render_drivers));
1577 if (render_drivers) {
1578 render_drivers[display->num_render_drivers] = *driver;
1579 display->render_drivers = render_drivers;
1580 display->num_render_drivers++;
1585 SDL_GetNumRenderDrivers(void)
1588 return SDL_CurrentDisplay.num_render_drivers;
1594 SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
1597 SDL_UninitializedVideo();
1600 if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
1601 SDL_SetError("index must be in the range of 0 - %d",
1602 SDL_GetNumRenderDrivers() - 1);
1605 *info = SDL_CurrentDisplay.render_drivers[index].info;
1610 SDL_CreateRenderer(SDL_WindowID windowID, int index, Uint32 flags)
1612 SDL_Window *window = SDL_GetWindowFromID(windowID);
1615 SDL_SetError("Invalid window ID");
1619 /* Free any existing renderer */
1620 SDL_DestroyRenderer(windowID);
1623 char *override = SDL_getenv("SDL_VIDEO_RENDERER");
1624 int n = SDL_GetNumRenderDrivers();
1626 #if SDL_VIDEO_RENDER_OGL
1627 if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
1628 override = "opengl";
1630 #endif /* SDL_VIDEO_RENDER_OGL */
1631 #if SDL_VIDEO_RENDER_OGL_ES
1632 if (!override && (window->flags & SDL_WINDOW_OPENGL)) {
1633 override = "opengl_es";
1635 #endif /* SDL_VIDEO_RENDER_OGL_ES */
1637 for (index = 0; index < n; ++index) {
1638 SDL_RenderDriver *driver =
1639 &SDL_CurrentDisplay.render_drivers[index];
1641 if (SDL_strcasecmp(override, driver->info.name) == 0) {
1642 /* Create a new renderer instance */
1643 window->renderer = driver->CreateRenderer(window, flags);
1648 for (index = 0; index < n; ++index) {
1649 SDL_RenderDriver *driver =
1650 &SDL_CurrentDisplay.render_drivers[index];
1652 if ((driver->info.flags & flags) == flags) {
1653 /* Create a new renderer instance */
1654 window->renderer = driver->CreateRenderer(window, flags);
1655 if (window->renderer) {
1656 /* Yay, we got one! */
1663 SDL_SetError("Couldn't find matching render driver");
1667 if (index >= SDL_GetNumRenderDrivers()) {
1668 SDL_SetError("index must be -1 or in the range of 0 - %d",
1669 SDL_GetNumRenderDrivers() - 1);
1672 /* Create a new renderer instance */
1673 window->renderer = SDL_CurrentDisplay.render_drivers[index].CreateRenderer(window, flags);
1676 if (window->renderer == NULL) {
1677 /* Assuming renderer set its error */
1681 SDL_SelectRenderer(window->id);
1687 SDL_SelectRenderer(SDL_WindowID windowID)
1689 SDL_Window *window = SDL_GetWindowFromID(windowID);
1690 SDL_Renderer *renderer;
1693 SDL_SetError("Invalid window ID");
1696 renderer = window->renderer;
1698 SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
1701 if (renderer->ActivateRenderer) {
1702 if (renderer->ActivateRenderer(renderer) < 0) {
1706 SDL_CurrentDisplay.current_renderer = renderer;
1711 SDL_GetRendererInfo(SDL_RendererInfo * info)
1713 SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
1717 *info = renderer->info;
1722 SDL_CreateTexture(Uint32 format, int access, int w, int h)
1725 SDL_Renderer *renderer;
1726 SDL_Texture *texture;
1728 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
1732 if (!renderer->CreateTexture) {
1736 texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
1741 texture->id = _this->next_object_id++;
1742 texture->format = format;
1743 texture->access = access;
1750 texture->renderer = renderer;
1752 if (renderer->CreateTexture(renderer, texture) < 0) {
1753 if (renderer->DestroyTexture) {
1754 renderer->DestroyTexture(renderer, texture);
1759 hash = (texture->id % SDL_arraysize(SDL_CurrentDisplay.textures));
1760 texture->next = SDL_CurrentDisplay.textures[hash];
1761 SDL_CurrentDisplay.textures[hash] = texture;
1767 SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
1769 SDL_TextureID textureID;
1770 Uint32 requested_format = format;
1771 SDL_PixelFormat *fmt;
1772 SDL_Renderer *renderer;
1774 Uint32 Rmask, Gmask, Bmask, Amask;
1777 SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
1780 fmt = surface->format;
1782 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
1788 if (!SDL_PixelFormatEnumToMasks
1789 (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
1790 SDL_SetError("Unknown pixel format");
1794 if (surface->format->Amask
1795 || !(surface->map->info.flags &
1796 (SDL_COPY_COLORKEY | SDL_COPY_MASK | SDL_COPY_BLEND))) {
1800 /* Pixel formats, sorted by best first */
1801 static const Uint32 sdl_pformats[] = {
1802 SDL_PIXELFORMAT_ARGB8888,
1803 SDL_PIXELFORMAT_RGBA8888,
1804 SDL_PIXELFORMAT_ABGR8888,
1805 SDL_PIXELFORMAT_BGRA8888,
1806 SDL_PIXELFORMAT_RGB888,
1807 SDL_PIXELFORMAT_BGR888,
1808 SDL_PIXELFORMAT_RGB24,
1809 SDL_PIXELFORMAT_BGR24,
1810 SDL_PIXELFORMAT_RGB565,
1811 SDL_PIXELFORMAT_BGR565,
1812 SDL_PIXELFORMAT_ARGB1555,
1813 SDL_PIXELFORMAT_ABGR1555,
1814 SDL_PIXELFORMAT_RGB555,
1815 SDL_PIXELFORMAT_BGR555,
1816 SDL_PIXELFORMAT_ARGB4444,
1817 SDL_PIXELFORMAT_ABGR4444,
1818 SDL_PIXELFORMAT_RGB444,
1819 SDL_PIXELFORMAT_ARGB2101010,
1820 SDL_PIXELFORMAT_INDEX8,
1821 SDL_PIXELFORMAT_INDEX4LSB,
1822 SDL_PIXELFORMAT_INDEX4MSB,
1823 SDL_PIXELFORMAT_RGB332,
1824 SDL_PIXELFORMAT_INDEX1LSB,
1825 SDL_PIXELFORMAT_INDEX1MSB,
1826 SDL_PIXELFORMAT_UNKNOWN
1829 bpp = fmt->BitsPerPixel;
1836 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
1838 SDL_SetError("Unknown pixel format");
1842 /* Search requested format in the supported texture */
1843 /* formats by current renderer */
1844 for (it = 0; it < renderer->info.num_texture_formats; it++) {
1845 if (renderer->info.texture_formats[it] == format) {
1850 /* If requested format can't be found, search any best */
1851 /* format which renderer provides */
1852 if (it == renderer->info.num_texture_formats) {
1855 if (sdl_pformats[pfmt] == SDL_PIXELFORMAT_UNKNOWN) {
1859 for (it = 0; it < renderer->info.num_texture_formats;
1861 if (renderer->info.texture_formats[it] ==
1862 sdl_pformats[pfmt]) {
1867 if (it != renderer->info.num_texture_formats) {
1868 /* The best format has been found */
1874 /* If any format can't be found, then return an error */
1875 if (it == renderer->info.num_texture_formats) {
1877 ("Any of the supported pixel formats can't be found");
1881 /* Convert found pixel format back to color masks */
1882 if (SDL_PixelFormatEnumToMasks
1883 (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
1884 &Bmask, &Amask) != SDL_TRUE) {
1885 SDL_SetError("Unknown pixel format");
1890 /* Need a format with alpha */
1894 /* Pixel formats with alpha, sorted by best first */
1895 static const Uint32 sdl_alpha_pformats[] = {
1896 SDL_PIXELFORMAT_ARGB8888,
1897 SDL_PIXELFORMAT_RGBA8888,
1898 SDL_PIXELFORMAT_ABGR8888,
1899 SDL_PIXELFORMAT_BGRA8888,
1900 SDL_PIXELFORMAT_ARGB1555,
1901 SDL_PIXELFORMAT_ABGR1555,
1902 SDL_PIXELFORMAT_ARGB4444,
1903 SDL_PIXELFORMAT_ABGR4444,
1904 SDL_PIXELFORMAT_ARGB2101010,
1905 SDL_PIXELFORMAT_UNKNOWN
1908 if (surface->format->Amask) {
1909 /* If surface already has alpha, then try an original */
1910 /* surface format first */
1911 bpp = fmt->BitsPerPixel;
1925 SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
1927 SDL_SetError("Unknown pixel format");
1931 /* Search this format in the supported texture formats */
1932 /* by current renderer */
1933 for (it = 0; it < renderer->info.num_texture_formats; it++) {
1934 if (renderer->info.texture_formats[it] == format) {
1939 /* If this format can't be found, search any best */
1940 /* compatible format with alpha which renderer provides */
1941 if (it == renderer->info.num_texture_formats) {
1944 if (sdl_alpha_pformats[apfmt] == SDL_PIXELFORMAT_UNKNOWN) {
1948 for (it = 0; it < renderer->info.num_texture_formats;
1950 if (renderer->info.texture_formats[it] ==
1951 sdl_alpha_pformats[apfmt]) {
1956 if (it != renderer->info.num_texture_formats) {
1957 /* Compatible format has been found */
1963 /* If compatible format can't be found, then return an error */
1964 if (it == renderer->info.num_texture_formats) {
1965 SDL_SetError("Compatible pixel format can't be found");
1969 /* Convert found pixel format back to color masks */
1970 if (SDL_PixelFormatEnumToMasks
1971 (renderer->info.texture_formats[it], &bpp, &Rmask, &Gmask,
1972 &Bmask, &Amask) != SDL_TRUE) {
1973 SDL_SetError("Unknown pixel format");
1979 format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
1981 SDL_SetError("Unknown pixel format");
1987 SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
1989 if (!textureID && !requested_format) {
1990 SDL_DisplayMode desktop_mode;
1991 SDL_GetDesktopDisplayMode(&desktop_mode);
1992 format = desktop_mode.format;
1994 SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
2000 if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
2001 && Bmask == fmt->Bmask && Amask == fmt->Amask) {
2002 if (SDL_MUSTLOCK(surface)) {
2003 SDL_LockSurface(surface);
2004 SDL_UpdateTexture(textureID, NULL, surface->pixels,
2006 SDL_UnlockSurface(surface);
2008 SDL_UpdateTexture(textureID, NULL, surface->pixels,
2012 SDL_PixelFormat dst_fmt;
2013 SDL_Surface *dst = NULL;
2015 /* Set up a destination surface for the texture update */
2016 SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
2017 if (SDL_ISPIXELFORMAT_INDEXED(format)) {
2019 SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
2020 if (dst_fmt.palette) {
2022 * FIXME: Should we try to copy
2025 SDL_DitherColors(dst_fmt.palette->colors,
2026 SDL_BITSPERPIXEL(format));
2029 dst = SDL_ConvertSurface(surface, &dst_fmt, 0);
2031 SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
2032 SDL_FreeSurface(dst);
2034 if (dst_fmt.palette) {
2035 SDL_FreePalette(dst_fmt.palette);
2038 SDL_DestroyTexture(textureID);
2048 SDL_GetSurfaceColorMod(surface, &r, &g, &b);
2049 SDL_SetTextureColorMod(textureID, r, g, b);
2051 SDL_GetSurfaceAlphaMod(surface, &a);
2052 SDL_SetTextureAlphaMod(textureID, a);
2054 SDL_GetSurfaceBlendMode(surface, &blendMode);
2055 SDL_SetTextureBlendMode(textureID, blendMode);
2057 SDL_GetSurfaceScaleMode(surface, &scaleMode);
2058 SDL_SetTextureScaleMode(textureID, scaleMode);
2061 if (SDL_ISPIXELFORMAT_INDEXED(format) && fmt->palette) {
2062 SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
2063 fmt->palette->ncolors);
2068 static __inline__ SDL_Texture *
2069 SDL_GetTextureFromID(SDL_TextureID textureID)
2072 SDL_Texture *texture;
2077 hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
2078 for (texture = SDL_CurrentDisplay.textures[hash]; texture;
2079 texture = texture->next) {
2080 if (texture->id == textureID) {
2088 SDL_QueryTexture(SDL_TextureID textureID, Uint32 * format, int *access,
2091 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2097 *format = texture->format;
2100 *access = texture->access;
2112 SDL_QueryTexturePixels(SDL_TextureID textureID, void **pixels, int *pitch)
2114 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2115 SDL_Renderer *renderer;
2120 renderer = texture->renderer;
2121 if (!renderer->QueryTexturePixels) {
2125 return renderer->QueryTexturePixels(renderer, texture, pixels, pitch);
2129 SDL_SetTexturePalette(SDL_TextureID textureID, const SDL_Color * colors,
2130 int firstcolor, int ncolors)
2132 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2133 SDL_Renderer *renderer;
2138 renderer = texture->renderer;
2139 if (!renderer->SetTexturePalette) {
2143 return renderer->SetTexturePalette(renderer, texture, colors, firstcolor,
2148 SDL_GetTexturePalette(SDL_TextureID textureID, SDL_Color * colors,
2149 int firstcolor, int ncolors)
2151 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2152 SDL_Renderer *renderer;
2157 renderer = texture->renderer;
2158 if (!renderer->GetTexturePalette) {
2162 return renderer->GetTexturePalette(renderer, texture, colors, firstcolor,
2167 SDL_SetTextureColorMod(SDL_TextureID textureID, Uint8 r, Uint8 g, Uint8 b)
2169 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2170 SDL_Renderer *renderer;
2175 renderer = texture->renderer;
2176 if (!renderer->SetTextureColorMod) {
2180 if (r < 255 || g < 255 || b < 255) {
2181 texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
2183 texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
2188 return renderer->SetTextureColorMod(renderer, texture);
2192 SDL_GetTextureColorMod(SDL_TextureID textureID, Uint8 * r, Uint8 * g,
2195 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2196 SDL_Renderer *renderer;
2201 renderer = texture->renderer;
2215 SDL_SetTextureAlphaMod(SDL_TextureID textureID, Uint8 alpha)
2217 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2218 SDL_Renderer *renderer;
2223 renderer = texture->renderer;
2224 if (!renderer->SetTextureAlphaMod) {
2229 texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
2231 texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
2234 return renderer->SetTextureAlphaMod(renderer, texture);
2238 SDL_GetTextureAlphaMod(SDL_TextureID textureID, Uint8 * alpha)
2240 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2246 *alpha = texture->a;
2252 SDL_SetTextureBlendMode(SDL_TextureID textureID, int blendMode)
2254 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2255 SDL_Renderer *renderer;
2260 renderer = texture->renderer;
2261 if (!renderer->SetTextureBlendMode) {
2265 texture->blendMode = blendMode;
2266 return renderer->SetTextureBlendMode(renderer, texture);
2270 SDL_GetTextureBlendMode(SDL_TextureID textureID, int *blendMode)
2272 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2278 *blendMode = texture->blendMode;
2284 SDL_SetTextureScaleMode(SDL_TextureID textureID, int scaleMode)
2286 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2287 SDL_Renderer *renderer;
2292 renderer = texture->renderer;
2293 if (!renderer->SetTextureScaleMode) {
2297 texture->scaleMode = scaleMode;
2298 return renderer->SetTextureScaleMode(renderer, texture);
2302 SDL_GetTextureScaleMode(SDL_TextureID textureID, int *scaleMode)
2304 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2310 *scaleMode = texture->scaleMode;
2316 SDL_UpdateTexture(SDL_TextureID textureID, const SDL_Rect * rect,
2317 const void *pixels, int pitch)
2319 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2320 SDL_Renderer *renderer;
2326 renderer = texture->renderer;
2327 if (!renderer->UpdateTexture) {
2334 full_rect.w = texture->w;
2335 full_rect.h = texture->h;
2338 return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
2342 SDL_LockTexture(SDL_TextureID textureID, const SDL_Rect * rect, int markDirty,
2343 void **pixels, int *pitch)
2345 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2346 SDL_Renderer *renderer;
2352 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
2353 SDL_SetError("SDL_LockTexture(): texture must be streaming");
2356 renderer = texture->renderer;
2357 if (!renderer->LockTexture) {
2364 full_rect.w = texture->w;
2365 full_rect.h = texture->h;
2368 return renderer->LockTexture(renderer, texture, rect, markDirty, pixels,
2373 SDL_UnlockTexture(SDL_TextureID textureID)
2375 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2376 SDL_Renderer *renderer;
2381 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
2384 renderer = texture->renderer;
2385 if (!renderer->UnlockTexture) {
2388 renderer->UnlockTexture(renderer, texture);
2392 SDL_DirtyTexture(SDL_TextureID textureID, int numrects,
2393 const SDL_Rect * rects)
2395 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2396 SDL_Renderer *renderer;
2401 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
2404 renderer = texture->renderer;
2405 if (!renderer->DirtyTexture) {
2408 renderer->DirtyTexture(renderer, texture, numrects, rects);
2412 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
2414 SDL_Renderer *renderer;
2416 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2424 if (renderer->SetDrawColor) {
2425 return renderer->SetDrawColor(renderer);
2432 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
2434 SDL_Renderer *renderer;
2436 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2456 SDL_SetRenderDrawBlendMode(int blendMode)
2458 SDL_Renderer *renderer;
2460 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2464 renderer->blendMode = blendMode;
2465 if (renderer->SetDrawBlendMode) {
2466 return renderer->SetDrawBlendMode(renderer);
2473 SDL_GetRenderDrawBlendMode(int *blendMode)
2475 SDL_Renderer *renderer;
2477 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2481 *blendMode = renderer->blendMode;
2486 SDL_RenderPoint(int x, int y)
2488 SDL_Renderer *renderer;
2491 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2495 if (!renderer->RenderPoint) {
2499 window = SDL_GetWindowFromID(renderer->window);
2500 if (x < 0 || y < 0 || x >= window->w || y >= window->h) {
2503 return renderer->RenderPoint(renderer, x, y);
2507 SDL_RenderLine(int x1, int y1, int x2, int y2)
2509 SDL_Renderer *renderer;
2513 if (x1 == x2 && y1 == y2) {
2514 return SDL_RenderPoint(x1, y1);
2517 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2521 if (!renderer->RenderLine) {
2525 window = SDL_GetWindowFromID(renderer->window);
2529 real_rect.w = window->w;
2530 real_rect.h = window->h;
2531 if (!SDL_IntersectRectAndLine(&real_rect, &x1, &y1, &x2, &y2)) {
2534 return renderer->RenderLine(renderer, x1, y1, x2, y2);
2538 SDL_RenderFill(const SDL_Rect * rect)
2540 SDL_Renderer *renderer;
2544 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2548 if (!renderer->RenderFill) {
2552 window = SDL_GetWindowFromID(renderer->window);
2556 real_rect.w = window->w;
2557 real_rect.h = window->h;
2559 if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
2563 return renderer->RenderFill(renderer, &real_rect);
2567 SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
2568 const SDL_Rect * dstrect)
2570 SDL_Texture *texture = SDL_GetTextureFromID(textureID);
2571 SDL_Renderer *renderer;
2573 SDL_Rect real_srcrect;
2574 SDL_Rect real_dstrect;
2576 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2581 SDL_SetError("Texture not found");
2584 if (texture->renderer != renderer) {
2585 SDL_SetError("Texture was not created with this renderer");
2588 if (!renderer->RenderCopy) {
2592 window = SDL_GetWindowFromID(renderer->window);
2596 real_srcrect.w = texture->w;
2597 real_srcrect.h = texture->h;
2599 if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
2606 real_dstrect.w = window->w;
2607 real_dstrect.h = window->h;
2609 if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
2612 /* Clip srcrect by the same amount as dstrect was clipped */
2613 if (dstrect->w != real_dstrect.w) {
2614 int deltax = (real_dstrect.x - dstrect->x);
2615 int deltaw = (real_dstrect.w - dstrect->w);
2616 real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
2617 real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
2619 if (dstrect->h != real_dstrect.h) {
2620 int deltay = (real_dstrect.y - dstrect->y);
2621 int deltah = (real_dstrect.h - dstrect->h);
2622 real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
2623 real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
2627 return renderer->RenderCopy(renderer, texture, &real_srcrect,
2632 SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
2633 void * pixels, int pitch)
2635 SDL_Renderer *renderer;
2639 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2643 if (!renderer->RenderReadPixels) {
2647 window = SDL_GetWindowFromID(renderer->window);
2650 format = SDL_GetDisplayFromWindow(window)->current_mode.format;
2655 real_rect.w = window->w;
2656 real_rect.h = window->h;
2658 if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
2661 if (real_rect.y > rect->y) {
2662 pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
2664 if (real_rect.x > rect->x) {
2665 Uint32 format = SDL_CurrentDisplay.current_mode.format;
2666 int bpp = SDL_BYTESPERPIXEL(format);
2667 pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
2671 return renderer->RenderReadPixels(renderer, &real_rect,
2672 format, pixels, pitch);
2676 SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
2677 const void * pixels, int pitch)
2679 SDL_Renderer *renderer;
2683 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2687 if (!renderer->RenderWritePixels) {
2691 window = SDL_GetWindowFromID(renderer->window);
2694 format = SDL_GetDisplayFromWindow(window)->current_mode.format;
2699 real_rect.w = window->w;
2700 real_rect.h = window->h;
2702 if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
2705 if (real_rect.y > rect->y) {
2706 pixels = (const Uint8 *)pixels + pitch * (real_rect.y - rect->y);
2708 if (real_rect.x > rect->x) {
2709 Uint32 format = SDL_CurrentDisplay.current_mode.format;
2710 int bpp = SDL_BYTESPERPIXEL(format);
2711 pixels = (const Uint8 *)pixels + bpp * (real_rect.x - rect->x);
2715 return renderer->RenderWritePixels(renderer, &real_rect,
2716 format, pixels, pitch);
2720 SDL_RenderPresent(void)
2722 SDL_Renderer *renderer;
2724 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2725 if (!renderer || !renderer->RenderPresent) {
2728 renderer->RenderPresent(renderer);
2732 SDL_DestroyTexture(SDL_TextureID textureID)
2735 SDL_Texture *prev, *texture;
2736 SDL_Renderer *renderer;
2739 SDL_UninitializedVideo();
2742 /* Look up the texture in the hash table */
2743 hash = (textureID % SDL_arraysize(SDL_CurrentDisplay.textures));
2745 for (texture = SDL_CurrentDisplay.textures[hash]; texture;
2746 prev = texture, texture = texture->next) {
2747 if (texture->id == textureID) {
2754 /* Unlink the texture from the list */
2756 prev->next = texture->next;
2758 SDL_CurrentDisplay.textures[hash] = texture->next;
2761 /* Free the texture */
2762 renderer = texture->renderer;
2763 renderer->DestroyTexture(renderer, texture);
2768 SDL_DestroyRenderer(SDL_WindowID windowID)
2770 SDL_Window *window = SDL_GetWindowFromID(windowID);
2771 SDL_Renderer *renderer;
2777 renderer = window->renderer;
2781 /* Free existing textures for this renderer */
2782 for (i = 0; i < SDL_arraysize(SDL_CurrentDisplay.textures); ++i) {
2783 SDL_Texture *texture;
2784 SDL_Texture *prev = NULL;
2786 for (texture = SDL_CurrentDisplay.textures[i]; texture;
2788 next = texture->next;
2789 if (texture->renderer == renderer) {
2793 SDL_CurrentDisplay.textures[i] = next;
2795 renderer->DestroyTexture(renderer, texture);
2803 /* Free the renderer instance */
2804 renderer->DestroyRenderer(renderer);
2806 /* Clear references */
2807 window->renderer = NULL;
2808 if (SDL_CurrentDisplay.current_renderer == renderer) {
2809 SDL_CurrentDisplay.current_renderer = NULL;
2814 SDL_IsScreenSaverEnabled()
2819 return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
2823 SDL_EnableScreenSaver()
2828 if (!_this->suspend_screensaver) {
2831 _this->suspend_screensaver = SDL_FALSE;
2832 if (_this->SuspendScreenSaver) {
2833 _this->SuspendScreenSaver(_this);
2838 SDL_DisableScreenSaver()
2843 if (_this->suspend_screensaver) {
2846 _this->suspend_screensaver = SDL_TRUE;
2847 if (_this->SuspendScreenSaver) {
2848 _this->SuspendScreenSaver(_this);
2860 /* Halt event processing before doing anything else */
2861 SDL_StopEventLoop();
2862 SDL_EnableScreenSaver();
2864 /* Clean up the system video */
2865 for (i = _this->num_displays; i--;) {
2866 SDL_VideoDisplay *display = &_this->displays[i];
2867 for (j = display->num_windows; j--;) {
2868 SDL_DestroyWindow(display->windows[i].id);
2870 if (display->windows) {
2871 SDL_free(display->windows);
2872 display->windows = NULL;
2874 display->num_windows = 0;
2875 if (display->render_drivers) {
2876 SDL_free(display->render_drivers);
2877 display->render_drivers = NULL;
2879 display->num_render_drivers = 0;
2881 _this->VideoQuit(_this);
2883 for (i = _this->num_displays; i--;) {
2884 SDL_VideoDisplay *display = &_this->displays[i];
2885 for (j = display->num_display_modes; j--;) {
2886 if (display->display_modes[j].driverdata) {
2887 SDL_free(display->display_modes[j].driverdata);
2888 display->display_modes[j].driverdata = NULL;
2891 if (display->display_modes) {
2892 SDL_free(display->display_modes);
2893 display->display_modes = NULL;
2895 if (display->desktop_mode.driverdata) {
2896 SDL_free(display->desktop_mode.driverdata);
2897 display->desktop_mode.driverdata = NULL;
2899 if (display->palette) {
2900 SDL_FreePalette(display->palette);
2901 display->palette = NULL;
2903 if (display->gamma) {
2904 SDL_free(display->gamma);
2905 display->gamma = NULL;
2907 if (display->driverdata) {
2908 SDL_free(display->driverdata);
2909 display->driverdata = NULL;
2912 if (_this->displays) {
2913 SDL_free(_this->displays);
2914 _this->displays = NULL;
2921 SDL_GL_LoadLibrary(const char *path)
2926 SDL_UninitializedVideo();
2929 if (_this->gl_config.driver_loaded) {
2930 if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
2931 SDL_SetError("OpenGL library already loaded");
2936 if (!_this->GL_LoadLibrary) {
2937 SDL_SetError("No dynamic GL support in video driver");
2940 retval = _this->GL_LoadLibrary(_this, path);
2943 ++_this->gl_config.driver_loaded;
2949 SDL_GL_GetProcAddress(const char *proc)
2954 SDL_UninitializedVideo();
2958 if (_this->GL_GetProcAddress) {
2959 if (_this->gl_config.driver_loaded) {
2960 func = _this->GL_GetProcAddress(_this, proc);
2962 SDL_SetError("No GL driver has been loaded");
2965 SDL_SetError("No dynamic GL support in video driver");
2971 SDL_GL_UnloadLibrary(void)
2974 SDL_UninitializedVideo();
2977 if (_this->gl_config.driver_loaded > 0) {
2978 if (--_this->gl_config.driver_loaded > 0) {
2981 if (_this->GL_UnloadLibrary) {
2982 _this->GL_UnloadLibrary(_this);
2988 SDL_GL_ExtensionSupported(const char *extension)
2990 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
2991 const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
2992 const char *extensions;
2994 const char *where, *terminator;
2996 /* Extension names should not have spaces. */
2997 where = SDL_strchr(extension, ' ');
2998 if (where || *extension == '\0') {
3001 /* See if there's an environment variable override */
3002 start = SDL_getenv(extension);
3003 if (start && *start == '0') {
3006 /* Lookup the available extensions */
3007 glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
3008 if (glGetStringFunc) {
3009 extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
3017 * It takes a bit of care to be fool-proof about parsing the OpenGL
3018 * extensions string. Don't be fooled by sub-strings, etc.
3024 where = SDL_strstr(start, extension);
3028 terminator = where + SDL_strlen(extension);
3029 if (where == start || *(where - 1) == ' ')
3030 if (*terminator == ' ' || *terminator == '\0')
3042 SDL_GL_SetAttribute(SDL_GLattr attr, int value)
3044 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
3048 SDL_UninitializedVideo();
3053 case SDL_GL_RED_SIZE:
3054 _this->gl_config.red_size = value;
3056 case SDL_GL_GREEN_SIZE:
3057 _this->gl_config.green_size = value;
3059 case SDL_GL_BLUE_SIZE:
3060 _this->gl_config.blue_size = value;
3062 case SDL_GL_ALPHA_SIZE:
3063 _this->gl_config.alpha_size = value;
3065 case SDL_GL_DOUBLEBUFFER:
3066 _this->gl_config.double_buffer = value;
3068 case SDL_GL_BUFFER_SIZE:
3069 _this->gl_config.buffer_size = value;
3071 case SDL_GL_DEPTH_SIZE:
3072 _this->gl_config.depth_size = value;
3074 case SDL_GL_STENCIL_SIZE:
3075 _this->gl_config.stencil_size = value;
3077 case SDL_GL_ACCUM_RED_SIZE:
3078 _this->gl_config.accum_red_size = value;
3080 case SDL_GL_ACCUM_GREEN_SIZE:
3081 _this->gl_config.accum_green_size = value;
3083 case SDL_GL_ACCUM_BLUE_SIZE:
3084 _this->gl_config.accum_blue_size = value;
3086 case SDL_GL_ACCUM_ALPHA_SIZE:
3087 _this->gl_config.accum_alpha_size = value;
3090 _this->gl_config.stereo = value;
3092 case SDL_GL_MULTISAMPLEBUFFERS:
3093 _this->gl_config.multisamplebuffers = value;
3095 case SDL_GL_MULTISAMPLESAMPLES:
3096 _this->gl_config.multisamplesamples = value;
3098 case SDL_GL_ACCELERATED_VISUAL:
3099 _this->gl_config.accelerated = value;
3101 case SDL_GL_RETAINED_BACKING:
3102 _this->gl_config.retained_backing = value;
3104 case SDL_GL_CONTEXT_MAJOR_VERSION:
3105 _this->gl_config.major_version = value;
3107 case SDL_GL_CONTEXT_MINOR_VERSION:
3108 _this->gl_config.minor_version = value;
3111 SDL_SetError("Unknown OpenGL attribute");
3119 #endif /* SDL_VIDEO_OPENGL */
3123 SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
3125 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
3126 void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
3127 GLenum(APIENTRY * glGetErrorFunc) (void);
3131 glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
3132 if (!glGetIntegervFunc) {
3136 glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
3137 if (!glGetErrorFunc) {
3141 /* Clear value in any case */
3145 case SDL_GL_RETAINED_BACKING:
3146 *value = _this->gl_config.retained_backing;
3148 case SDL_GL_RED_SIZE:
3149 attrib = GL_RED_BITS;
3151 case SDL_GL_BLUE_SIZE:
3152 attrib = GL_BLUE_BITS;
3154 case SDL_GL_GREEN_SIZE:
3155 attrib = GL_GREEN_BITS;
3157 case SDL_GL_ALPHA_SIZE:
3158 attrib = GL_ALPHA_BITS;
3160 case SDL_GL_DOUBLEBUFFER:
3161 #ifndef SDL_VIDEO_OPENGL_ES
3162 attrib = GL_DOUBLEBUFFER;
3165 /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER */
3166 /* parameter which switches double buffer to single buffer. OpenGL ES */
3167 /* SDL driver must set proper value after initialization */
3168 *value = _this->gl_config.double_buffer;
3171 case SDL_GL_DEPTH_SIZE:
3172 attrib = GL_DEPTH_BITS;
3174 case SDL_GL_STENCIL_SIZE:
3175 attrib = GL_STENCIL_BITS;
3177 #ifndef SDL_VIDEO_OPENGL_ES
3178 case SDL_GL_ACCUM_RED_SIZE:
3179 attrib = GL_ACCUM_RED_BITS;
3181 case SDL_GL_ACCUM_GREEN_SIZE:
3182 attrib = GL_ACCUM_GREEN_BITS;
3184 case SDL_GL_ACCUM_BLUE_SIZE:
3185 attrib = GL_ACCUM_BLUE_BITS;
3187 case SDL_GL_ACCUM_ALPHA_SIZE:
3188 attrib = GL_ACCUM_ALPHA_BITS;
3194 case SDL_GL_ACCUM_RED_SIZE:
3195 case SDL_GL_ACCUM_GREEN_SIZE:
3196 case SDL_GL_ACCUM_BLUE_SIZE:
3197 case SDL_GL_ACCUM_ALPHA_SIZE:
3199 /* none of these are supported in OpenGL ES */
3203 case SDL_GL_MULTISAMPLEBUFFERS:
3204 #ifndef SDL_VIDEO_OPENGL_ES
3205 attrib = GL_SAMPLE_BUFFERS_ARB;
3207 attrib = GL_SAMPLE_BUFFERS;
3210 case SDL_GL_MULTISAMPLESAMPLES:
3211 #ifndef SDL_VIDEO_OPENGL_ES
3212 attrib = GL_SAMPLES_ARB;
3214 attrib = GL_SAMPLES;
3217 case SDL_GL_BUFFER_SIZE:
3223 * there doesn't seem to be a single flag in OpenGL
3226 glGetIntegervFunc(GL_RED_BITS, &component);
3228 glGetIntegervFunc(GL_GREEN_BITS, &component);
3230 glGetIntegervFunc(GL_BLUE_BITS, &component);
3232 glGetIntegervFunc(GL_ALPHA_BITS, &component);
3238 case SDL_GL_ACCELERATED_VISUAL:
3240 /* FIXME: How do we get this information? */
3241 *value = (_this->gl_config.accelerated != 0);
3245 SDL_SetError("Unknown OpenGL attribute");
3249 glGetIntegervFunc(attrib, (GLint *) value);
3250 error = glGetErrorFunc();
3251 if (error != GL_NO_ERROR) {
3253 case GL_INVALID_ENUM:
3255 SDL_SetError("OpenGL error: GL_INVALID_ENUM");
3258 case GL_INVALID_VALUE:
3260 SDL_SetError("OpenGL error: GL_INVALID_VALUE");
3265 SDL_SetError("OpenGL error: %08X", error);
3275 #endif /* SDL_VIDEO_OPENGL */
3279 SDL_GL_CreateContext(SDL_WindowID windowID)
3281 SDL_Window *window = SDL_GetWindowFromID(windowID);
3286 if (!(window->flags & SDL_WINDOW_OPENGL)) {
3287 SDL_SetError("The specified window isn't an OpenGL window");
3290 return _this->GL_CreateContext(_this, window);
3294 SDL_GL_MakeCurrent(SDL_WindowID windowID, SDL_GLContext context)
3296 SDL_Window *window = SDL_GetWindowFromID(windowID);
3298 if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
3299 SDL_SetError("The specified window isn't an OpenGL window");
3305 return _this->GL_MakeCurrent(_this, window, context);
3309 SDL_GL_SetSwapInterval(int interval)
3312 SDL_UninitializedVideo();
3315 if (_this->GL_SetSwapInterval) {
3316 return _this->GL_SetSwapInterval(_this, interval);
3318 SDL_SetError("Setting the swap interval is not supported");
3324 SDL_GL_GetSwapInterval(void)
3327 SDL_UninitializedVideo();
3330 if (_this->GL_GetSwapInterval) {
3331 return _this->GL_GetSwapInterval(_this);
3333 SDL_SetError("Getting the swap interval is not supported");
3339 SDL_GL_SwapWindow(SDL_WindowID windowID)
3341 SDL_Window *window = SDL_GetWindowFromID(windowID);
3346 if (!(window->flags & SDL_WINDOW_OPENGL)) {
3347 SDL_SetError("The specified window isn't an OpenGL window");
3350 _this->GL_SwapWindow(_this, window);
3354 SDL_GL_DeleteContext(SDL_GLContext context)
3356 if (!_this || !context) {
3359 _this->GL_MakeCurrent(_this, NULL, NULL);
3360 _this->GL_DeleteContext(_this, context);
3365 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
3366 * & 2 for alpha channel.
3369 CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
3373 #define SET_MASKBIT(icon, x, y, mask) \
3374 mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
3376 colorkey = icon->format->colorkey;
3377 switch (icon->format->BytesPerPixel) {
3381 for (y = 0; y < icon->h; ++y) {
3382 pixels = (Uint8 *) icon->pixels + y * icon->pitch;
3383 for (x = 0; x < icon->w; ++x) {
3384 if (*pixels++ == colorkey) {
3385 SET_MASKBIT(icon, x, y, mask);
3395 for (y = 0; y < icon->h; ++y) {
3396 pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
3397 for (x = 0; x < icon->w; ++x) {
3398 if ((flags & 1) && *pixels == colorkey) {
3399 SET_MASKBIT(icon, x, y, mask);
3400 } else if ((flags & 2)
3401 && (*pixels & icon->format->Amask) == 0) {
3402 SET_MASKBIT(icon, x, y, mask);
3413 for (y = 0; y < icon->h; ++y) {
3414 pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
3415 for (x = 0; x < icon->w; ++x) {
3416 if ((flags & 1) && *pixels == colorkey) {
3417 SET_MASKBIT(icon, x, y, mask);
3418 } else if ((flags & 2)
3419 && (*pixels & icon->format->Amask) == 0) {
3420 SET_MASKBIT(icon, x, y, mask);
3431 * Sets the window manager icon for the display window.
3434 SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
3436 if (icon && _this->SetIcon) {
3437 /* Generate a mask if necessary, and create the icon! */
3439 int mask_len = icon->h * (icon->w + 7) / 8;
3441 mask = (Uint8 *) SDL_malloc(mask_len);
3445 SDL_memset(mask, ~0, mask_len);
3446 if (icon->flags & SDL_SRCCOLORKEY)
3448 if (icon->flags & SDL_SRCALPHA)
3451 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
3453 _this->SetIcon(_this, icon, mask);
3456 _this->SetIcon(_this, icon, mask);
3463 SDL_GetWindowWMInfo(SDL_WindowID windowID, struct SDL_SysWMinfo *info)
3465 SDL_Window *window = SDL_GetWindowFromID(windowID);
3467 if (!window || !_this->GetWindowWMInfo) {
3470 return (_this->GetWindowWMInfo(_this, window, info));
3474 SDL_StartTextInput(void)
3476 if (_this->StartTextInput) {
3477 _this->StartTextInput(_this);
3479 SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
3480 SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
3484 SDL_StopTextInput(void)
3486 if (_this->StopTextInput) {
3487 _this->StopTextInput(_this);
3489 SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
3490 SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
3494 SDL_SetTextInputRect(SDL_Rect *rect)
3496 if (_this->SetTextInputRect) {
3497 _this->SetTextInputRect(_this, rect);
3501 /* vi: set ts=4 sw=4 expandtab: */