2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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_sysvideo.h"
29 #include "SDL_pixels_c.h"
30 #include "SDL_cursor_c.h"
31 #include "../events/SDL_sysevents.h"
32 #include "../events/SDL_events_c.h"
34 /* Available video drivers */
35 static VideoBootStrap *bootstrap[] = {
36 #if SDL_VIDEO_DRIVER_QUARTZ
39 #if SDL_VIDEO_DRIVER_X11
42 #if SDL_VIDEO_DRIVER_DGA
45 #if SDL_VIDEO_DRIVER_NANOX
48 #if SDL_VIDEO_DRIVER_IPOD
51 #if SDL_VIDEO_DRIVER_QTOPIA
54 #if SDL_VIDEO_DRIVER_WSCONS
57 #if SDL_VIDEO_DRIVER_FBCON
60 #if SDL_VIDEO_DRIVER_DIRECTFB
63 #if SDL_VIDEO_DRIVER_PS2GS
66 #if SDL_VIDEO_DRIVER_PS3
69 #if SDL_VIDEO_DRIVER_GGI
72 #if SDL_VIDEO_DRIVER_VGL
75 #if SDL_VIDEO_DRIVER_SVGALIB
78 #if SDL_VIDEO_DRIVER_GAPI
81 #if SDL_VIDEO_DRIVER_WINDIB
84 #if SDL_VIDEO_DRIVER_DDRAW
87 #if SDL_VIDEO_DRIVER_BWINDOW
90 #if SDL_VIDEO_DRIVER_TOOLBOX
93 #if SDL_VIDEO_DRIVER_DRAWSPROCKET
96 #if SDL_VIDEO_DRIVER_PHOTON
99 #if SDL_VIDEO_DRIVER_EPOC
102 #if SDL_VIDEO_DRIVER_XBIOS
105 #if SDL_VIDEO_DRIVER_GEM
108 #if SDL_VIDEO_DRIVER_PICOGUI
111 #if SDL_VIDEO_DRIVER_DC
114 #if SDL_VIDEO_DRIVER_NDS
117 #if SDL_VIDEO_DRIVER_RISCOS
120 #if SDL_VIDEO_DRIVER_OS2FS
123 #if SDL_VIDEO_DRIVER_AALIB
126 #if SDL_VIDEO_DRIVER_CACA
129 #if SDL_VIDEO_DRIVER_DUMMY
132 #ifdef ENABLE_OVLWMVIDEO
135 #ifdef ENABLE_DOPEVIDEO
138 #ifdef ENABLE_L4FBVIDEO
145 SDL_VideoDevice *current_video = NULL;
147 /* Various local functions */
148 int SDL_VideoInit(const char *driver_name, Uint32 flags);
149 void SDL_VideoQuit(void);
150 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect* rects);
152 static SDL_GrabMode SDL_WM_GrabInputOff(void);
154 static int lock_count = 0;
159 * Initialize the video and event subsystems -- determine native pixel format
161 int SDL_VideoInit (const char *driver_name, Uint32 flags)
163 SDL_VideoDevice *video;
166 SDL_PixelFormat vformat;
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 /* Check to make sure we don't overwrite 'current_video' */
180 if ( current_video != NULL ) {
184 /* Select the proper video driver */
187 if ( driver_name != NULL ) {
188 #if 0 /* This will be replaced with a better driver selection API */
189 if ( SDL_strrchr(driver_name, ':') != NULL ) {
190 index = atoi(SDL_strrchr(driver_name, ':')+1);
193 for ( i=0; bootstrap[i]; ++i ) {
194 if ( SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
195 if ( bootstrap[i]->available() ) {
196 video = bootstrap[i]->create(index);
202 for ( i=0; bootstrap[i]; ++i ) {
203 if ( bootstrap[i]->available() ) {
204 video = bootstrap[i]->create(index);
205 if ( video != NULL ) {
211 if ( video == NULL ) {
212 SDL_SetError("No available video device");
215 current_video = video;
216 current_video->name = bootstrap[i]->name;
218 /* Do some basic variable initialization */
219 video->screen = NULL;
220 video->shadow = NULL;
221 video->visible = NULL;
222 video->physpal = NULL;
223 video->gammacols = NULL;
225 video->wm_title = NULL;
226 video->wm_icon = NULL;
229 SDL_memset(&video->info, 0, (sizeof video->info));
231 video->displayformatalphapixel = NULL;
233 /* Set some very sane GL defaults */
234 video->gl_config.driver_loaded = 0;
235 video->gl_config.dll_handle = NULL;
236 video->gl_config.red_size = 3;
237 video->gl_config.green_size = 3;
238 video->gl_config.blue_size = 2;
239 video->gl_config.alpha_size = 0;
240 video->gl_config.buffer_size = 0;
241 video->gl_config.depth_size = 16;
242 video->gl_config.stencil_size = 0;
243 video->gl_config.double_buffer = 1;
244 video->gl_config.accum_red_size = 0;
245 video->gl_config.accum_green_size = 0;
246 video->gl_config.accum_blue_size = 0;
247 video->gl_config.accum_alpha_size = 0;
248 video->gl_config.stereo = 0;
249 video->gl_config.multisamplebuffers = 0;
250 video->gl_config.multisamplesamples = 0;
251 video->gl_config.accelerated = -1; /* not known, don't set */
252 video->gl_config.swap_control = -1; /* not known, don't set */
254 /* Initialize the video subsystem */
255 SDL_memset(&vformat, 0, sizeof(vformat));
256 if ( video->VideoInit(video, &vformat) < 0 ) {
261 /* Create a zero sized video surface of the appropriate format */
262 video_flags = SDL_SWSURFACE;
263 SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0,
264 vformat.BitsPerPixel,
265 vformat.Rmask, vformat.Gmask, vformat.Bmask, 0);
266 if ( SDL_VideoSurface == NULL ) {
270 SDL_PublicSurface = NULL; /* Until SDL_SetVideoMode() */
272 #if 0 /* Don't change the current palette - may be used by other programs.
273 * The application can't do anything with the display surface until
274 * a video mode has been set anyway. :)
276 /* If we have a palettized surface, create a default palette */
277 if ( SDL_VideoSurface->format->palette ) {
278 SDL_PixelFormat *vf = SDL_VideoSurface->format;
279 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
280 video->SetColors(video,
281 0, vf->palette->ncolors, vf->palette->colors);
284 video->info.vfmt = SDL_VideoSurface->format;
286 /* Start the event loop */
287 if ( SDL_StartEventLoop(flags) < 0 ) {
291 SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
293 /* We're ready to go! */
297 char *SDL_VideoDriverName(char *namebuf, int maxlen)
299 if ( current_video != NULL ) {
300 SDL_strlcpy(namebuf, current_video->name, maxlen);
307 * Get the current display surface
309 SDL_Surface *SDL_GetVideoSurface(void)
311 SDL_Surface *visible;
314 if ( current_video ) {
315 visible = current_video->visible;
321 * Get the current information about the video hardware
323 const SDL_VideoInfo *SDL_GetVideoInfo(void)
325 const SDL_VideoInfo *info;
328 if ( current_video ) {
329 info = ¤t_video->info;
335 * Return a pointer to an array of available screen dimensions for the
336 * given format, sorted largest to smallest. Returns NULL if there are
337 * no dimensions available for a particular format, or (SDL_Rect **)-1
338 * if any dimension is okay for the given format. If 'format' is NULL,
339 * the mode list will be for the format given by SDL_GetVideoInfo()->vfmt
341 SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags)
343 SDL_VideoDevice *video = current_video;
344 SDL_VideoDevice *this = current_video;
348 if ( SDL_VideoSurface ) {
349 if ( format == NULL ) {
350 format = SDL_VideoSurface->format;
352 modes = video->ListModes(this, format, flags);
358 * Check to see if a particular video mode is supported.
359 * It returns 0 if the requested mode is not supported under any bit depth,
360 * or returns the bits-per-pixel of the closest available mode with the
361 * given width and height. If this bits-per-pixel is different from the
362 * one used when setting the video mode, SDL_SetVideoMode() will succeed,
363 * but will emulate the requested bits-per-pixel with a shadow surface.
365 static Uint8 SDL_closest_depths[4][8] = {
366 /* 8 bit closest depth ordering */
367 { 0, 8, 16, 15, 32, 24, 0, 0 },
368 /* 15,16 bit closest depth ordering */
369 { 0, 16, 15, 32, 24, 8, 0, 0 },
370 /* 24 bit closest depth ordering */
371 { 0, 24, 32, 16, 15, 8, 0, 0 },
372 /* 32 bit closest depth ordering */
373 { 0, 32, 16, 15, 24, 8, 0, 0 }
377 #ifdef __MACOS__ /* MPW optimization bug? */
378 #define NEGATIVE_ONE 0xFFFFFFFF
380 #define NEGATIVE_ONE -1
383 int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
387 SDL_PixelFormat format;
390 /* Currently 1 and 4 bpp are not supported */
391 if ( bpp < 8 || bpp > 32 ) {
394 if ( (width <= 0) || (height <= 0) ) {
398 /* Search through the list valid of modes */
399 SDL_memset(&format, 0, sizeof(format));
401 table = ((bpp+7)/8)-1;
402 SDL_closest_depths[table][0] = bpp;
403 SDL_closest_depths[table][7] = 0;
404 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
405 format.BitsPerPixel = SDL_closest_depths[table][b];
406 sizes = SDL_ListModes(&format, flags);
407 if ( sizes == (SDL_Rect **)0 ) {
408 /* No sizes supported at this bit-depth */
411 if (sizes == (SDL_Rect **)NEGATIVE_ONE) {
412 /* Any size supported at this bit-depth */
415 } else if (current_video->handles_any_size) {
416 /* Driver can center a smaller surface to simulate fullscreen */
417 for ( i=0; sizes[i]; ++i ) {
418 if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) {
419 supported = 1; /* this mode can fit the centered window. */
424 for ( i=0; sizes[i]; ++i ) {
425 if ((sizes[i]->w == width) && (sizes[i]->h == height)) {
433 return(SDL_closest_depths[table][b]);
440 * Get the closest non-emulated video mode to the one requested
442 static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags)
447 SDL_PixelFormat format;
450 /* Check parameters */
451 if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) {
452 SDL_SetError("Invalid bits per pixel (range is {8...32})");
455 if ((*w <= 0) || (*h <= 0)) {
456 SDL_SetError("Invalid width or height");
460 /* Try the original video mode, get the closest depth */
461 native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags);
462 if ( native_bpp == *BitsPerPixel ) {
465 if ( native_bpp > 0 ) {
466 *BitsPerPixel = native_bpp;
470 /* No exact size match at any depth, look for closest match */
471 SDL_memset(&format, 0, sizeof(format));
473 table = ((*BitsPerPixel+7)/8)-1;
474 SDL_closest_depths[table][0] = *BitsPerPixel;
475 SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
476 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
479 format.BitsPerPixel = SDL_closest_depths[table][b];
480 sizes = SDL_ListModes(&format, flags);
481 if ( sizes == (SDL_Rect **)0 ) {
482 /* No sizes supported at this bit-depth */
486 for ( i=0; sizes[i]; ++i ) {
487 /* Mode with both dimensions bigger or equal than asked ? */
488 if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
489 /* Mode with any dimension smaller or equal than current best ? */
490 if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
491 /* Now choose the mode that has less pixels */
492 if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) {
502 *BitsPerPixel = SDL_closest_depths[table][b];
506 SDL_SetError("No video mode large enough for %dx%d", *w, *h);
511 /* This should probably go somewhere else -- like SDL_surface.c */
512 static void SDL_ClearSurface(SDL_Surface *surface)
516 black = SDL_MapRGB(surface->format, 0, 0, 0);
517 SDL_FillRect(surface, NULL, black);
518 if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) {
520 SDL_FillRect(surface, NULL, black);
522 if (surface->flags&SDL_FULLSCREEN) {
528 * Create a shadow surface suitable for fooling the app. :-)
530 static void SDL_CreateShadowSurface(int depth)
532 Uint32 Rmask, Gmask, Bmask;
534 /* Allocate the shadow surface */
535 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
536 Rmask = (SDL_VideoSurface->format)->Rmask;
537 Gmask = (SDL_VideoSurface->format)->Gmask;
538 Bmask = (SDL_VideoSurface->format)->Bmask;
540 Rmask = Gmask = Bmask = 0;
542 SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
543 SDL_VideoSurface->w, SDL_VideoSurface->h,
544 depth, Rmask, Gmask, Bmask, 0);
545 if ( SDL_ShadowSurface == NULL ) {
549 /* 8-bit shadow surfaces report that they have exclusive palette */
550 if ( SDL_ShadowSurface->format->palette ) {
551 SDL_ShadowSurface->flags |= SDL_HWPALETTE;
552 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
553 SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
554 SDL_VideoSurface->format->palette->colors,
555 SDL_VideoSurface->format->palette->ncolors*
559 SDL_ShadowSurface->format->palette->colors, depth);
563 /* If the video surface is resizable, the shadow should say so */
564 if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) {
565 SDL_ShadowSurface->flags |= SDL_RESIZABLE;
567 /* If the video surface has no frame, the shadow should say so */
568 if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) {
569 SDL_ShadowSurface->flags |= SDL_NOFRAME;
571 /* If the video surface is fullscreen, the shadow should say so */
572 if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
573 SDL_ShadowSurface->flags |= SDL_FULLSCREEN;
575 /* If the video surface is flippable, the shadow should say so */
576 if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
577 SDL_ShadowSurface->flags |= SDL_DOUBLEBUF;
583 #include <sys/neutrino.h>
584 #endif /* __QNXNTO__ */
587 extern int sysevents_mouse_pressed;
591 * Set the requested video mode, allocating a shadow buffer if necessary.
593 SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
595 SDL_VideoDevice *video, *this;
596 SDL_Surface *prev_mode, *mode;
601 SDL_GrabMode saved_grab;
604 sysevents_mouse_pressed = 0;
607 /* Start up the video driver, if necessary..
608 WARNING: This is the only function protected this way!
610 if ( ! current_video ) {
611 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) {
615 this = video = current_video;
617 /* Default to the current width and height */
619 width = video->info.current_w;
622 height = video->info.current_h;
624 /* Default to the current video bpp */
626 flags |= SDL_ANYFORMAT;
627 bpp = SDL_VideoSurface->format->BitsPerPixel;
630 /* Get a good video mode, the closest one possible */
634 if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) {
638 /* Check the requested flags */
639 /* There's no palette in > 8 bits-per-pixel mode */
640 if ( video_bpp > 8 ) {
641 flags &= ~SDL_HWPALETTE;
644 if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) {
645 /* There's no windowed double-buffering */
646 flags &= ~SDL_DOUBLEBUF;
649 if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
650 /* Use hardware surfaces when double-buffering */
651 flags |= SDL_HWSURFACE;
654 is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL );
656 /* These flags are for 2D video modes only */
657 flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF);
660 /* Reset the keyboard here so event callbacks can run */
663 SDL_SetMouseRange(width, height);
664 SDL_cursorstate &= ~CURSOR_USINGSW;
666 /* Clean up any previous video mode */
667 if ( SDL_PublicSurface != NULL ) {
668 SDL_PublicSurface = NULL;
670 if ( SDL_ShadowSurface != NULL ) {
671 SDL_Surface *ready_to_go;
672 ready_to_go = SDL_ShadowSurface;
673 SDL_ShadowSurface = NULL;
674 SDL_FreeSurface(ready_to_go);
676 if ( video->physpal ) {
677 SDL_free(video->physpal->colors);
678 SDL_free(video->physpal);
679 video->physpal = NULL;
681 if( video->gammacols) {
682 SDL_free(video->gammacols);
683 video->gammacols = NULL;
686 /* Save the previous grab state and turn off grab for mode switch */
687 saved_grab = SDL_WM_GrabInputOff();
689 /* Try to set the video mode, along with offset and clipping */
690 prev_mode = SDL_VideoSurface;
692 SDL_VideoSurface = NULL; /* In case it's freed by driver */
693 mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags);
694 if ( mode ) { /* Prevent resize events from mode change */
695 /* But not on OS/2 */
697 SDL_PrivateResize(mode->w, mode->h);
700 /* Sam - If we asked for OpenGL mode, and didn't get it, fail */
701 if ( is_opengl && !(mode->flags & SDL_OPENGL) ) {
703 SDL_SetError("OpenGL not available");
708 * If you try to set an SDL_OPENGL surface, and fail to find a
709 * matching visual, then the next call to SDL_SetVideoMode()
710 * will segfault, since we no longer point to a dummy surface,
713 * WARNING, we need to make sure that the previous mode hasn't
714 * already been freed by the video driver. What do we do in
715 * that case? Should we call SDL_VideoInit() again?
717 SDL_VideoSurface = (mode != NULL) ? mode : prev_mode;
719 if ( (mode != NULL) && (!is_opengl) ) {
721 if ( (mode->w < width) || (mode->h < height) ) {
722 SDL_SetError("Video mode smaller than requested");
726 /* If we have a palettized surface, create a default palette */
727 if ( mode->format->palette ) {
728 SDL_PixelFormat *vf = mode->format;
729 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
730 video->SetColors(this, 0, vf->palette->ncolors,
731 vf->palette->colors);
734 /* Clear the surface to black */
738 SDL_SetClipRect(mode, NULL);
739 SDL_ClearSurface(mode);
741 /* Now adjust the offsets to match the desired mode */
742 video->offset_x = (mode->w-width)/2;
743 video->offset_y = (mode->h-height)/2;
744 mode->offset = video->offset_y*mode->pitch +
745 video->offset_x*mode->format->BytesPerPixel;
748 "Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n",
750 mode->w, mode->h, mode->format->BitsPerPixel, mode->offset);
754 SDL_SetClipRect(mode, NULL);
759 /* If we failed setting a video mode, return NULL... (Uh Oh!) */
760 if ( mode == NULL ) {
764 /* If there is no window manager, set the SDL_NOFRAME flag */
765 if ( ! video->info.wm_available ) {
766 mode->flags |= SDL_NOFRAME;
769 /* Reset the mouse cursor and grab for new video mode */
771 if ( video->UpdateMouse ) {
772 video->UpdateMouse(this);
774 SDL_WM_GrabInput(saved_grab);
775 SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */
778 /* Load GL symbols (before MakeCurrent, where we need glGetString). */
779 if ( flags & (SDL_OPENGL | SDL_OPENGLBLIT) ) {
781 #if defined(__QNXNTO__) && (_NTO_VERSION < 630)
782 #define __SDL_NOGETPROCADDR__
783 #elif defined(__MINT__)
784 #define __SDL_NOGETPROCADDR__
786 #ifdef __SDL_NOGETPROCADDR__
787 #define SDL_PROC(ret,func,params) video->func=func;
789 #define SDL_PROC(ret,func,params) \
791 video->func = SDL_GL_GetProcAddress(#func); \
792 if ( ! video->func ) { \
793 SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
798 #endif /* __SDL_NOGETPROCADDR__ */
800 #include "SDL_glfuncs.h"
803 #endif /* SDL_VIDEO_OPENGL */
805 /* If we're running OpenGL, make the context current */
806 if ( (video->screen->flags & SDL_OPENGL) &&
807 video->GL_MakeCurrent ) {
808 if ( video->GL_MakeCurrent(this) < 0 ) {
813 /* Set up a fake SDL surface for OpenGL "blitting" */
814 if ( (flags & SDL_OPENGLBLIT) == SDL_OPENGLBLIT ) {
815 /* Load GL functions for performing the texture updates */
818 /* Create a software surface for blitting */
819 #ifdef GL_VERSION_1_2
820 /* If the implementation either supports the packed pixels
821 extension, or implements the core OpenGL 1.2 API, it will
822 support the GL_UNSIGNED_SHORT_5_6_5 texture format.
825 (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
826 (SDL_atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
829 SDL_VideoSurface = SDL_CreateRGBSurface(
841 #endif /* OpenGL 1.2 */
844 SDL_VideoSurface = SDL_CreateRGBSurface(
849 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
862 if ( ! SDL_VideoSurface ) {
865 SDL_VideoSurface->flags = mode->flags | SDL_OPENGLBLIT;
867 /* Free the original video mode surface (is this safe?) */
868 SDL_FreeSurface(mode);
870 /* Set the surface completely opaque & white by default */
871 SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
872 video->glGenTextures( 1, &video->texture );
873 video->glBindTexture( GL_TEXTURE_2D, video->texture );
877 video->is_32bit ? GL_RGBA : GL_RGB,
881 video->is_32bit ? GL_RGBA : GL_RGB,
882 #ifdef GL_VERSION_1_2
883 video->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
889 video->UpdateRects = SDL_GL_UpdateRectsLock;
891 SDL_SetError("Somebody forgot to #define SDL_VIDEO_OPENGL");
896 /* Create a shadow surface if necessary */
897 /* There are three conditions under which we create a shadow surface:
898 1. We need a particular bits-per-pixel that we didn't get.
899 2. We need a hardware palette and didn't get one.
900 3. We need a software surface and got a hardware surface.
902 if ( !(SDL_VideoSurface->flags & SDL_OPENGL) &&
904 ( !(flags&SDL_ANYFORMAT) &&
905 (SDL_VideoSurface->format->BitsPerPixel != bpp)) ||
906 ( (flags&SDL_HWPALETTE) &&
907 !(SDL_VideoSurface->flags&SDL_HWPALETTE)) ||
908 /* If the surface is in hardware, video writes are visible
909 as soon as they are performed, so we need to buffer them
911 ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) &&
912 (SDL_VideoSurface->flags&SDL_HWSURFACE)) ||
913 ( (flags&SDL_DOUBLEBUF) &&
914 (SDL_VideoSurface->flags&SDL_HWSURFACE) &&
915 !(SDL_VideoSurface->flags&SDL_DOUBLEBUF))
917 SDL_CreateShadowSurface(bpp);
918 if ( SDL_ShadowSurface == NULL ) {
919 SDL_SetError("Couldn't create shadow surface");
922 SDL_PublicSurface = SDL_ShadowSurface;
924 SDL_PublicSurface = SDL_VideoSurface;
926 video->info.vfmt = SDL_VideoSurface->format;
927 video->info.current_w = SDL_VideoSurface->w;
928 video->info.current_h = SDL_VideoSurface->h;
931 return(SDL_PublicSurface);
935 * Convert a surface into the video pixel format.
937 SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface)
941 if ( ! SDL_PublicSurface ) {
942 SDL_SetError("No video mode has been set");
945 /* Set the flags appropriate for copying to display surface */
946 if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw)
947 flags = SDL_HWSURFACE;
949 flags = SDL_SWSURFACE;
950 #ifdef AUTORLE_DISPLAYFORMAT
951 flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA));
952 flags |= SDL_RLEACCELOK;
954 flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK);
956 return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
960 * Convert a surface into a format that's suitable for blitting to
961 * the screen, but including an alpha channel.
963 SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface)
966 SDL_PixelFormat *format;
967 SDL_Surface *converted;
969 /* default to ARGB8888 */
970 Uint32 amask = 0xff000000;
971 Uint32 rmask = 0x00ff0000;
972 Uint32 gmask = 0x0000ff00;
973 Uint32 bmask = 0x000000ff;
975 if ( ! SDL_PublicSurface ) {
976 SDL_SetError("No video mode has been set");
979 vf = SDL_PublicSurface->format;
981 switch(vf->BytesPerPixel) {
983 /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
984 For anything else (like ARGB4444) it doesn't matter
985 since we have no special code for it anyway */
986 if ( (vf->Rmask == 0x1f) &&
987 (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
995 /* Keep the video format, as long as the high 8 bits are
997 if ( (vf->Rmask == 0xff) && (vf->Bmask == 0xff0000) ) {
1000 } else if ( vf->Rmask == 0xFF00 && (vf->Bmask == 0xFF000000) ) {
1009 /* We have no other optimised formats right now. When/if a new
1010 optimised alpha format is written, add the converter here */
1013 format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
1014 flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
1015 flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
1016 converted = SDL_ConvertSurface(surface, format, flags);
1017 SDL_FreeFormat(format);
1022 * Update a specific portion of the physical screen
1024 void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
1029 /* Perform some checking */
1034 if ( (int)(x+w) > screen->w )
1036 if ( (int)(y+h) > screen->h )
1039 /* Fill the rectangle */
1044 SDL_UpdateRects(screen, 1, &rect);
1047 void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects)
1050 SDL_VideoDevice *video = current_video;
1051 SDL_VideoDevice *this = current_video;
1053 if ( (screen->flags & (SDL_OPENGL | SDL_OPENGLBLIT)) == SDL_OPENGL ) {
1054 SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()");
1057 if ( screen == SDL_ShadowSurface ) {
1058 /* Blit the shadow surface using saved mapping */
1059 SDL_Palette *pal = screen->format->palette;
1060 SDL_Color *saved_colors = NULL;
1061 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1062 /* simulated 8bpp, use correct physical palette */
1063 saved_colors = pal->colors;
1064 if ( video->gammacols ) {
1065 /* gamma-corrected palette */
1066 pal->colors = video->gammacols;
1067 } else if ( video->physpal ) {
1068 /* physical palette different from logical */
1069 pal->colors = video->physpal->colors;
1072 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1074 SDL_DrawCursor(SDL_ShadowSurface);
1075 for ( i=0; i<numrects; ++i ) {
1076 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1077 SDL_VideoSurface, &rects[i]);
1079 SDL_EraseCursor(SDL_ShadowSurface);
1082 for ( i=0; i<numrects; ++i ) {
1083 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1084 SDL_VideoSurface, &rects[i]);
1087 if ( saved_colors ) {
1088 pal->colors = saved_colors;
1091 /* Fall through to video surface update */
1092 screen = SDL_VideoSurface;
1094 if ( screen == SDL_VideoSurface ) {
1095 /* Update the video surface */
1096 if ( screen->offset ) {
1097 for ( i=0; i<numrects; ++i ) {
1098 rects[i].x += video->offset_x;
1099 rects[i].y += video->offset_y;
1101 video->UpdateRects(this, numrects, rects);
1102 for ( i=0; i<numrects; ++i ) {
1103 rects[i].x -= video->offset_x;
1104 rects[i].y -= video->offset_y;
1107 video->UpdateRects(this, numrects, rects);
1113 * Performs hardware double buffering, if possible, or a full update if not.
1115 int SDL_Flip(SDL_Surface *screen)
1117 SDL_VideoDevice *video = current_video;
1118 /* Copy the shadow surface to the video surface */
1119 if ( screen == SDL_ShadowSurface ) {
1121 SDL_Palette *pal = screen->format->palette;
1122 SDL_Color *saved_colors = NULL;
1123 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1124 /* simulated 8bpp, use correct physical palette */
1125 saved_colors = pal->colors;
1126 if ( video->gammacols ) {
1127 /* gamma-corrected palette */
1128 pal->colors = video->gammacols;
1129 } else if ( video->physpal ) {
1130 /* physical palette different from logical */
1131 pal->colors = video->physpal->colors;
1139 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1141 SDL_DrawCursor(SDL_ShadowSurface);
1142 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1143 SDL_VideoSurface, &rect);
1144 SDL_EraseCursor(SDL_ShadowSurface);
1147 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1148 SDL_VideoSurface, &rect);
1150 if ( saved_colors ) {
1151 pal->colors = saved_colors;
1154 /* Fall through to video surface update */
1155 screen = SDL_VideoSurface;
1157 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
1158 SDL_VideoDevice *this = current_video;
1159 return(video->FlipHWSurface(this, SDL_VideoSurface));
1161 SDL_UpdateRect(screen, 0, 0, 0, 0);
1166 static void SetPalette_logical(SDL_Surface *screen, SDL_Color *colors,
1167 int firstcolor, int ncolors)
1169 SDL_Palette *pal = screen->format->palette;
1170 SDL_Palette *vidpal;
1172 if ( colors != (pal->colors + firstcolor) ) {
1173 SDL_memcpy(pal->colors + firstcolor, colors,
1174 ncolors * sizeof(*colors));
1177 if ( current_video && SDL_VideoSurface ) {
1178 vidpal = SDL_VideoSurface->format->palette;
1179 if ( (screen == SDL_ShadowSurface) && vidpal ) {
1181 * This is a shadow surface, and the physical
1182 * framebuffer is also indexed. Propagate the
1183 * changes to its logical palette so that
1184 * updates are always identity blits
1186 SDL_memcpy(vidpal->colors + firstcolor, colors,
1187 ncolors * sizeof(*colors));
1190 SDL_FormatChanged(screen);
1193 static int SetPalette_physical(SDL_Surface *screen,
1194 SDL_Color *colors, int firstcolor, int ncolors)
1196 SDL_VideoDevice *video = current_video;
1199 if ( video->physpal ) {
1200 /* We need to copy the new colors, since we haven't
1201 * already done the copy in the logical set above.
1203 SDL_memcpy(video->physpal->colors + firstcolor,
1204 colors, ncolors * sizeof(*colors));
1206 if ( screen == SDL_ShadowSurface ) {
1207 if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
1209 * The real screen is also indexed - set its physical
1210 * palette. The physical palette does not include the
1211 * gamma modification, we apply it directly instead,
1212 * but this only happens if we have hardware palette.
1214 screen = SDL_VideoSurface;
1217 * The video surface is not indexed - invalidate any
1218 * active shadow-to-video blit mappings.
1220 if ( screen->map->dst == SDL_VideoSurface ) {
1221 SDL_InvalidateMap(screen->map);
1223 if ( video->gamma ) {
1224 if( ! video->gammacols ) {
1225 SDL_Palette *pp = video->physpal;
1227 pp = screen->format->palette;
1228 video->gammacols = SDL_malloc(pp->ncolors
1229 * sizeof(SDL_Color));
1230 SDL_ApplyGamma(video->gamma,
1235 SDL_ApplyGamma(video->gamma, colors,
1241 SDL_UpdateRect(screen, 0, 0, 0, 0);
1245 if ( screen == SDL_VideoSurface ) {
1246 SDL_Color gcolors[256];
1248 if ( video->gamma ) {
1249 SDL_ApplyGamma(video->gamma, colors, gcolors, ncolors);
1252 gotall = video->SetColors(video, firstcolor, ncolors, colors);
1254 /* The video flags shouldn't have SDL_HWPALETTE, and
1255 the video driver is responsible for copying back the
1256 correct colors into the video surface palette.
1260 SDL_CursorPaletteChanged();
1266 * Set the physical and/or logical colormap of a surface:
1267 * Only the screen has a physical colormap. It determines what is actually
1268 * sent to the display.
1269 * The logical colormap is used to map blits to/from the surface.
1270 * 'which' is one or both of SDL_LOGPAL, SDL_PHYSPAL
1272 * Return nonzero if all colours were set as requested, or 0 otherwise.
1274 int SDL_SetPalette(SDL_Surface *screen, int which,
1275 SDL_Color *colors, int firstcolor, int ncolors)
1284 if ( !current_video || screen != SDL_PublicSurface ) {
1285 /* only screens have physical palettes */
1286 which &= ~SDL_PHYSPAL;
1287 } else if ( (screen->flags & SDL_HWPALETTE) != SDL_HWPALETTE ) {
1288 /* hardware palettes required for split colormaps */
1289 which |= SDL_PHYSPAL | SDL_LOGPAL;
1292 /* Verify the parameters */
1293 pal = screen->format->palette;
1295 return 0; /* not a palettized surface */
1298 palsize = 1 << screen->format->BitsPerPixel;
1299 if ( ncolors > (palsize - firstcolor) ) {
1300 ncolors = (palsize - firstcolor);
1304 if ( which & SDL_LOGPAL ) {
1306 * Logical palette change: The actual screen isn't affected,
1307 * but the internal colormap is altered so that the
1308 * interpretation of the pixel values (for blits etc) is
1311 SetPalette_logical(screen, colors, firstcolor, ncolors);
1313 if ( which & SDL_PHYSPAL ) {
1314 SDL_VideoDevice *video = current_video;
1316 * Physical palette change: This doesn't affect the
1317 * program's idea of what the screen looks like, but changes
1318 * its actual appearance.
1320 if ( !video->physpal && !(which & SDL_LOGPAL) ) {
1321 /* Lazy physical palette allocation */
1323 SDL_Palette *pp = SDL_malloc(sizeof(*pp));
1327 video->physpal = pp;
1328 pp->ncolors = pal->ncolors;
1329 size = pp->ncolors * sizeof(SDL_Color);
1330 pp->colors = SDL_malloc(size);
1331 if ( !pp->colors ) {
1334 SDL_memcpy(pp->colors, pal->colors, size);
1336 if ( ! SetPalette_physical(screen,
1337 colors, firstcolor, ncolors) ) {
1344 int SDL_SetColors(SDL_Surface *screen, SDL_Color *colors, int firstcolor,
1347 return SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL,
1348 colors, firstcolor, ncolors);
1352 * Clean up the video subsystem
1354 void SDL_VideoQuit (void)
1356 SDL_Surface *ready_to_go;
1358 if ( current_video ) {
1359 SDL_VideoDevice *video = current_video;
1360 SDL_VideoDevice *this = current_video;
1362 /* Halt event processing before doing anything else */
1363 SDL_StopEventLoop();
1365 /* Clean up allocated window manager items */
1366 if ( SDL_PublicSurface ) {
1367 SDL_PublicSurface = NULL;
1371 /* Just in case... */
1372 SDL_WM_GrabInputOff();
1374 /* Clean up the system video */
1375 video->VideoQuit(this);
1377 /* Free any lingering surfaces */
1378 ready_to_go = SDL_ShadowSurface;
1379 SDL_ShadowSurface = NULL;
1380 SDL_FreeSurface(ready_to_go);
1381 if ( SDL_VideoSurface != NULL ) {
1382 ready_to_go = SDL_VideoSurface;
1383 SDL_VideoSurface = NULL;
1384 SDL_FreeSurface(ready_to_go);
1386 SDL_PublicSurface = NULL;
1388 /* Clean up miscellaneous memory */
1389 if ( video->physpal ) {
1390 SDL_free(video->physpal->colors);
1391 SDL_free(video->physpal);
1392 video->physpal = NULL;
1394 if ( video->gammacols ) {
1395 SDL_free(video->gammacols);
1396 video->gammacols = NULL;
1398 if ( video->gamma ) {
1399 SDL_free(video->gamma);
1400 video->gamma = NULL;
1402 if ( video->wm_title != NULL ) {
1403 SDL_free(video->wm_title);
1404 video->wm_title = NULL;
1406 if ( video->wm_icon != NULL ) {
1407 SDL_free(video->wm_icon);
1408 video->wm_icon = NULL;
1411 /* Finish cleaning up video subsystem */
1413 current_video = NULL;
1418 /* Load the GL driver library */
1419 int SDL_GL_LoadLibrary(const char *path)
1421 SDL_VideoDevice *video = current_video;
1422 SDL_VideoDevice *this = current_video;
1426 if ( video == NULL ) {
1427 SDL_SetError("Video subsystem has not been initialized");
1429 if ( video->GL_LoadLibrary ) {
1430 retval = video->GL_LoadLibrary(this, path);
1432 SDL_SetError("No dynamic GL support in video driver");
1438 void *SDL_GL_GetProcAddress(const char* proc)
1440 SDL_VideoDevice *video = current_video;
1441 SDL_VideoDevice *this = current_video;
1445 if ( video->GL_GetProcAddress ) {
1446 if ( video->gl_config.driver_loaded ) {
1447 func = video->GL_GetProcAddress(this, proc);
1449 SDL_SetError("No GL driver has been loaded");
1452 SDL_SetError("No dynamic GL support in video driver");
1457 /* Set the specified GL attribute for setting up a GL video mode */
1458 int SDL_GL_SetAttribute( SDL_GLattr attr, int value )
1461 SDL_VideoDevice *video = current_video;
1465 case SDL_GL_RED_SIZE:
1466 video->gl_config.red_size = value;
1468 case SDL_GL_GREEN_SIZE:
1469 video->gl_config.green_size = value;
1471 case SDL_GL_BLUE_SIZE:
1472 video->gl_config.blue_size = value;
1474 case SDL_GL_ALPHA_SIZE:
1475 video->gl_config.alpha_size = value;
1477 case SDL_GL_DOUBLEBUFFER:
1478 video->gl_config.double_buffer = value;
1480 case SDL_GL_BUFFER_SIZE:
1481 video->gl_config.buffer_size = value;
1483 case SDL_GL_DEPTH_SIZE:
1484 video->gl_config.depth_size = value;
1486 case SDL_GL_STENCIL_SIZE:
1487 video->gl_config.stencil_size = value;
1489 case SDL_GL_ACCUM_RED_SIZE:
1490 video->gl_config.accum_red_size = value;
1492 case SDL_GL_ACCUM_GREEN_SIZE:
1493 video->gl_config.accum_green_size = value;
1495 case SDL_GL_ACCUM_BLUE_SIZE:
1496 video->gl_config.accum_blue_size = value;
1498 case SDL_GL_ACCUM_ALPHA_SIZE:
1499 video->gl_config.accum_alpha_size = value;
1502 video->gl_config.stereo = value;
1504 case SDL_GL_MULTISAMPLEBUFFERS:
1505 video->gl_config.multisamplebuffers = value;
1507 case SDL_GL_MULTISAMPLESAMPLES:
1508 video->gl_config.multisamplesamples = value;
1510 case SDL_GL_ACCELERATED_VISUAL:
1511 video->gl_config.accelerated = value;
1513 case SDL_GL_SWAP_CONTROL:
1514 video->gl_config.swap_control = value;
1517 SDL_SetError("Unknown OpenGL attribute");
1524 /* Retrieve an attribute value from the windowing system. */
1525 int SDL_GL_GetAttribute(SDL_GLattr attr, int* value)
1528 SDL_VideoDevice* video = current_video;
1529 SDL_VideoDevice* this = current_video;
1531 if ( video->GL_GetAttribute ) {
1532 retval = this->GL_GetAttribute(this, attr, value);
1535 SDL_SetError("GL_GetAttribute not supported");
1540 /* Perform a GL buffer swap on the current GL context */
1541 void SDL_GL_SwapBuffers(void)
1543 SDL_VideoDevice *video = current_video;
1544 SDL_VideoDevice *this = current_video;
1546 if ( video->screen->flags & SDL_OPENGL ) {
1547 video->GL_SwapBuffers(this);
1549 SDL_SetError("OpenGL video mode has not been set");
1553 /* Update rects with locking */
1554 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect *rects)
1557 SDL_GL_UpdateRects(numrects, rects);
1561 /* Update rects without state setting and changing (the caller is responsible for it) */
1562 void SDL_GL_UpdateRects(int numrects, SDL_Rect *rects)
1564 #if SDL_VIDEO_OPENGL
1565 SDL_VideoDevice *this = current_video;
1566 SDL_Rect update, tmp;
1569 for ( i = 0; i < numrects; i++ )
1573 for ( y = 0; y <= rects[i].h / 256; y++ )
1577 for ( x = 0; x <= rects[i].w / 256; x++ )
1584 if ( update.w > 256 )
1587 if ( update.h > 256 )
1591 this->glTexSubImage2D(
1598 this->is_32bit? GL_RGBA : GL_RGB,
1599 #ifdef GL_VERSION_1_2
1600 this->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
1604 (Uint8 *)this->screen->pixels +
1605 this->screen->format->BytesPerPixel * update.x +
1606 update.y * this->screen->pitch );
1610 * Note the parens around the function name:
1611 * This is because some OpenGL implementations define glTexCoord etc
1612 * as macros, and we don't want them expanded here.
1614 this->glBegin(GL_TRIANGLE_STRIP);
1615 (this->glTexCoord2f)( 0.0, 0.0 );
1616 (this->glVertex2i)( update.x, update.y );
1617 (this->glTexCoord2f)( (float)(update.w / 256.0), 0.0 );
1618 (this->glVertex2i)( update.x + update.w, update.y );
1619 (this->glTexCoord2f)( 0.0, (float)(update.h / 256.0) );
1620 (this->glVertex2i)( update.x, update.y + update.h );
1621 (this->glTexCoord2f)( (float)(update.w / 256.0), (float)(update.h / 256.0) );
1622 (this->glVertex2i)( update.x + update.w , update.y + update.h );
1635 /* Lock == save current state */
1638 #if SDL_VIDEO_OPENGL
1642 SDL_VideoDevice *this = current_video;
1644 this->glPushAttrib( GL_ALL_ATTRIB_BITS ); /* TODO: narrow range of what is saved */
1645 #ifdef GL_CLIENT_PIXEL_STORE_BIT
1646 this->glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
1649 this->glEnable(GL_TEXTURE_2D);
1650 this->glEnable(GL_BLEND);
1651 this->glDisable(GL_FOG);
1652 this->glDisable(GL_ALPHA_TEST);
1653 this->glDisable(GL_DEPTH_TEST);
1654 this->glDisable(GL_SCISSOR_TEST);
1655 this->glDisable(GL_STENCIL_TEST);
1656 this->glDisable(GL_CULL_FACE);
1658 this->glBindTexture( GL_TEXTURE_2D, this->texture );
1659 this->glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1660 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
1661 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
1662 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
1663 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
1665 this->glPixelStorei( GL_UNPACK_ROW_LENGTH, this->screen->pitch / this->screen->format->BytesPerPixel );
1666 this->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1667 (this->glColor4f)(1.0, 1.0, 1.0, 1.0); /* Solaris workaround */
1669 this->glViewport(0, 0, this->screen->w, this->screen->h);
1670 this->glMatrixMode(GL_PROJECTION);
1671 this->glPushMatrix();
1672 this->glLoadIdentity();
1674 this->glOrtho(0.0, (GLdouble) this->screen->w, (GLdouble) this->screen->h, 0.0, 0.0, 1.0);
1676 this->glMatrixMode(GL_MODELVIEW);
1677 this->glPushMatrix();
1678 this->glLoadIdentity();
1683 /* Unlock == restore saved state */
1684 void SDL_GL_Unlock()
1686 #if SDL_VIDEO_OPENGL
1690 SDL_VideoDevice *this = current_video;
1692 this->glPopMatrix();
1693 this->glMatrixMode(GL_PROJECTION);
1694 this->glPopMatrix();
1696 this->glPopClientAttrib();
1697 this->glPopAttrib();
1703 void SDL_Audio_SetCaption(const char *caption);
1706 * Sets/Gets the title and icon text of the display window, if any.
1708 void SDL_WM_SetCaption (const char *title, const char *icon)
1710 SDL_VideoDevice *video = current_video;
1711 SDL_VideoDevice *this = current_video;
1715 if ( video->wm_title ) {
1716 SDL_free(video->wm_title);
1718 video->wm_title = SDL_strdup(title);
1721 if ( video->wm_icon ) {
1722 SDL_free(video->wm_icon);
1724 video->wm_icon = SDL_strdup(icon);
1726 if ( (title || icon) && (video->SetCaption != NULL) ) {
1727 video->SetCaption(this, video->wm_title,video->wm_icon);
1731 /* PulseAudio can make use of this information. */
1732 SDL_Audio_SetCaption(title);
1735 void SDL_WM_GetCaption (char **title, char **icon)
1737 SDL_VideoDevice *video = current_video;
1741 *title = video->wm_title;
1744 *icon = video->wm_icon;
1749 /* Utility function used by SDL_WM_SetIcon();
1750 * flags & 1 for color key, flags & 2 for alpha channel. */
1751 static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
1755 #define SET_MASKBIT(icon, x, y, mask) \
1756 mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
1758 colorkey = icon->format->colorkey;
1759 switch (icon->format->BytesPerPixel) {
1760 case 1: { Uint8 *pixels;
1761 for ( y=0; y<icon->h; ++y ) {
1762 pixels = (Uint8 *)icon->pixels + y*icon->pitch;
1763 for ( x=0; x<icon->w; ++x ) {
1764 if ( *pixels++ == colorkey ) {
1765 SET_MASKBIT(icon, x, y, mask);
1772 case 2: { Uint16 *pixels;
1773 for ( y=0; y<icon->h; ++y ) {
1774 pixels = (Uint16 *)icon->pixels +
1776 for ( x=0; x<icon->w; ++x ) {
1777 if ( (flags & 1) && *pixels == colorkey ) {
1778 SET_MASKBIT(icon, x, y, mask);
1779 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1780 SET_MASKBIT(icon, x, y, mask);
1788 case 4: { Uint32 *pixels;
1789 for ( y=0; y<icon->h; ++y ) {
1790 pixels = (Uint32 *)icon->pixels +
1792 for ( x=0; x<icon->w; ++x ) {
1793 if ( (flags & 1) && *pixels == colorkey ) {
1794 SET_MASKBIT(icon, x, y, mask);
1795 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1796 SET_MASKBIT(icon, x, y, mask);
1807 * Sets the window manager icon for the display window.
1809 void SDL_WM_SetIcon (SDL_Surface *icon, Uint8 *mask)
1811 SDL_VideoDevice *video = current_video;
1812 SDL_VideoDevice *this = current_video;
1814 if ( icon && video->SetIcon ) {
1815 /* Generate a mask if necessary, and create the icon! */
1816 if ( mask == NULL ) {
1817 int mask_len = icon->h*(icon->w+7)/8;
1819 mask = (Uint8 *)SDL_malloc(mask_len);
1820 if ( mask == NULL ) {
1823 SDL_memset(mask, ~0, mask_len);
1824 if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
1825 if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
1827 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
1829 video->SetIcon(video, icon, mask);
1832 video->SetIcon(this, icon, mask);
1838 * Grab or ungrab the keyboard and mouse input.
1839 * This function returns the final grab mode after calling the
1840 * driver dependent function.
1842 static SDL_GrabMode SDL_WM_GrabInputRaw(SDL_GrabMode mode)
1844 SDL_VideoDevice *video = current_video;
1845 SDL_VideoDevice *this = current_video;
1847 /* Only do something if we have support for grabs */
1848 if ( video->GrabInput == NULL ) {
1849 return(video->input_grab);
1852 /* If the final grab mode if off, only then do we actually grab */
1854 printf("SDL_WM_GrabInputRaw(%d) ... ", mode);
1856 if ( mode == SDL_GRAB_OFF ) {
1857 if ( video->input_grab != SDL_GRAB_OFF ) {
1858 mode = video->GrabInput(this, mode);
1861 if ( video->input_grab == SDL_GRAB_OFF ) {
1862 mode = video->GrabInput(this, mode);
1865 if ( mode != video->input_grab ) {
1866 video->input_grab = mode;
1867 if ( video->CheckMouseMode ) {
1868 video->CheckMouseMode(this);
1872 printf("Final mode %d\n", video->input_grab);
1875 /* Return the final grab state */
1876 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1877 mode -= SDL_GRAB_FULLSCREEN;
1881 SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode)
1883 SDL_VideoDevice *video = current_video;
1885 /* If the video isn't initialized yet, we can't do anything */
1887 return SDL_GRAB_OFF;
1890 /* Return the current mode on query */
1891 if ( mode == SDL_GRAB_QUERY ) {
1892 mode = video->input_grab;
1893 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1894 mode -= SDL_GRAB_FULLSCREEN;
1900 printf("SDL_WM_GrabInput(%d) ... ", mode);
1902 /* If the video surface is fullscreen, we always grab */
1903 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1904 mode -= SDL_GRAB_FULLSCREEN;
1906 if ( SDL_VideoSurface && (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
1907 mode += SDL_GRAB_FULLSCREEN;
1909 return(SDL_WM_GrabInputRaw(mode));
1911 static SDL_GrabMode SDL_WM_GrabInputOff(void)
1915 /* First query the current grab state */
1916 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
1918 /* Now explicitly turn off input grab */
1919 SDL_WM_GrabInputRaw(SDL_GRAB_OFF);
1921 /* Return the old state */
1926 * Iconify the window in window managed environments.
1927 * A successful iconification will result in an SDL_APPACTIVE loss event.
1929 int SDL_WM_IconifyWindow(void)
1931 SDL_VideoDevice *video = current_video;
1932 SDL_VideoDevice *this = current_video;
1936 if ( video->IconifyWindow ) {
1937 retval = video->IconifyWindow(this);
1943 * Toggle fullscreen mode
1945 int SDL_WM_ToggleFullScreen(SDL_Surface *surface)
1947 SDL_VideoDevice *video = current_video;
1948 SDL_VideoDevice *this = current_video;
1952 if ( SDL_PublicSurface && (surface == SDL_PublicSurface) &&
1953 video->ToggleFullScreen ) {
1954 if ( surface->flags & SDL_FULLSCREEN ) {
1955 toggled = video->ToggleFullScreen(this, 0);
1957 SDL_VideoSurface->flags &= ~SDL_FULLSCREEN;
1958 SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
1961 toggled = video->ToggleFullScreen(this, 1);
1963 SDL_VideoSurface->flags |= SDL_FULLSCREEN;
1964 SDL_PublicSurface->flags |= SDL_FULLSCREEN;
1967 /* Double-check the grab state inside SDL_WM_GrabInput() */
1969 SDL_WM_GrabInput(video->input_grab);
1976 * Get some platform dependent window manager information
1978 int SDL_GetWMInfo (SDL_SysWMinfo *info)
1980 SDL_VideoDevice *video = current_video;
1981 SDL_VideoDevice *this = current_video;
1983 if ( video && video->GetWMInfo ) {
1984 return(video->GetWMInfo(this, info));