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_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_OMAPDSS
132 #if SDL_VIDEO_DRIVER_DUMMY
138 SDL_VideoDevice *current_video = NULL;
140 /* Various local functions */
141 int SDL_VideoInit(const char *driver_name, Uint32 flags);
142 void SDL_VideoQuit(void);
143 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect* rects);
145 static SDL_GrabMode SDL_WM_GrabInputOff(void);
147 static int lock_count = 0;
152 * Initialize the video and event subsystems -- determine native pixel format
154 int SDL_VideoInit (const char *driver_name, Uint32 flags)
156 SDL_VideoDevice *video;
159 SDL_PixelFormat vformat;
162 /* Toggle the event thread flags, based on OS requirements */
163 #if defined(MUST_THREAD_EVENTS)
164 flags |= SDL_INIT_EVENTTHREAD;
165 #elif defined(CANT_THREAD_EVENTS)
166 if ( (flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
167 SDL_SetError("OS doesn't support threaded events");
172 /* Check to make sure we don't overwrite 'current_video' */
173 if ( current_video != NULL ) {
177 /* Select the proper video driver */
180 if ( driver_name != NULL ) {
181 #if 0 /* This will be replaced with a better driver selection API */
182 if ( SDL_strrchr(driver_name, ':') != NULL ) {
183 index = atoi(SDL_strrchr(driver_name, ':')+1);
186 for ( i=0; bootstrap[i]; ++i ) {
187 if ( SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
188 if ( bootstrap[i]->available() ) {
189 video = bootstrap[i]->create(index);
195 for ( i=0; bootstrap[i]; ++i ) {
196 if ( bootstrap[i]->available() ) {
197 video = bootstrap[i]->create(index);
198 if ( video != NULL ) {
204 if ( video == NULL ) {
205 SDL_SetError("No available video device");
208 current_video = video;
209 current_video->name = bootstrap[i]->name;
211 /* Do some basic variable initialization */
212 video->screen = NULL;
213 video->shadow = NULL;
214 video->visible = NULL;
215 video->physpal = NULL;
216 video->gammacols = NULL;
218 video->wm_title = NULL;
219 video->wm_icon = NULL;
222 SDL_memset(&video->info, 0, (sizeof video->info));
224 video->displayformatalphapixel = NULL;
226 /* Set some very sane GL defaults */
227 video->gl_config.driver_loaded = 0;
228 video->gl_config.dll_handle = NULL;
229 video->gl_config.red_size = 3;
230 video->gl_config.green_size = 3;
231 video->gl_config.blue_size = 2;
232 video->gl_config.alpha_size = 0;
233 video->gl_config.buffer_size = 0;
234 video->gl_config.depth_size = 16;
235 video->gl_config.stencil_size = 0;
236 video->gl_config.double_buffer = 1;
237 video->gl_config.accum_red_size = 0;
238 video->gl_config.accum_green_size = 0;
239 video->gl_config.accum_blue_size = 0;
240 video->gl_config.accum_alpha_size = 0;
241 video->gl_config.stereo = 0;
242 video->gl_config.multisamplebuffers = 0;
243 video->gl_config.multisamplesamples = 0;
244 video->gl_config.accelerated = -1; /* not known, don't set */
245 video->gl_config.swap_control = -1; /* not known, don't set */
247 /* Initialize the video subsystem */
248 SDL_memset(&vformat, 0, sizeof(vformat));
249 if ( video->VideoInit(video, &vformat) < 0 ) {
254 /* Create a zero sized video surface of the appropriate format */
255 video_flags = SDL_SWSURFACE;
256 SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0,
257 vformat.BitsPerPixel,
258 vformat.Rmask, vformat.Gmask, vformat.Bmask, 0);
259 if ( SDL_VideoSurface == NULL ) {
263 SDL_PublicSurface = NULL; /* Until SDL_SetVideoMode() */
265 #if 0 /* Don't change the current palette - may be used by other programs.
266 * The application can't do anything with the display surface until
267 * a video mode has been set anyway. :)
269 /* If we have a palettized surface, create a default palette */
270 if ( SDL_VideoSurface->format->palette ) {
271 SDL_PixelFormat *vf = SDL_VideoSurface->format;
272 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
273 video->SetColors(video,
274 0, vf->palette->ncolors, vf->palette->colors);
277 video->info.vfmt = SDL_VideoSurface->format;
279 /* Start the event loop */
280 if ( SDL_StartEventLoop(flags) < 0 ) {
284 SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
286 /* We're ready to go! */
290 char *SDL_VideoDriverName(char *namebuf, int maxlen)
292 if ( current_video != NULL ) {
293 SDL_strlcpy(namebuf, current_video->name, maxlen);
300 * Get the current display surface
302 SDL_Surface *SDL_GetVideoSurface(void)
304 SDL_Surface *visible;
307 if ( current_video ) {
308 visible = current_video->visible;
314 * Get the current information about the video hardware
316 const SDL_VideoInfo *SDL_GetVideoInfo(void)
318 const SDL_VideoInfo *info;
321 if ( current_video ) {
322 info = ¤t_video->info;
328 * Return a pointer to an array of available screen dimensions for the
329 * given format, sorted largest to smallest. Returns NULL if there are
330 * no dimensions available for a particular format, or (SDL_Rect **)-1
331 * if any dimension is okay for the given format. If 'format' is NULL,
332 * the mode list will be for the format given by SDL_GetVideoInfo()->vfmt
334 SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags)
336 SDL_VideoDevice *video = current_video;
337 SDL_VideoDevice *this = current_video;
341 if ( SDL_VideoSurface ) {
342 if ( format == NULL ) {
343 format = SDL_VideoSurface->format;
345 modes = video->ListModes(this, format, flags);
351 * Check to see if a particular video mode is supported.
352 * It returns 0 if the requested mode is not supported under any bit depth,
353 * or returns the bits-per-pixel of the closest available mode with the
354 * given width and height. If this bits-per-pixel is different from the
355 * one used when setting the video mode, SDL_SetVideoMode() will succeed,
356 * but will emulate the requested bits-per-pixel with a shadow surface.
358 static Uint8 SDL_closest_depths[4][8] = {
359 /* 8 bit closest depth ordering */
360 { 0, 8, 16, 15, 32, 24, 0, 0 },
361 /* 15,16 bit closest depth ordering */
362 { 0, 16, 15, 32, 24, 8, 0, 0 },
363 /* 24 bit closest depth ordering */
364 { 0, 24, 32, 16, 15, 8, 0, 0 },
365 /* 32 bit closest depth ordering */
366 { 0, 32, 16, 15, 24, 8, 0, 0 }
370 #ifdef __MACOS__ /* MPW optimization bug? */
371 #define NEGATIVE_ONE 0xFFFFFFFF
373 #define NEGATIVE_ONE -1
376 int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
380 SDL_PixelFormat format;
383 /* Currently 1 and 4 bpp are not supported */
384 if ( bpp < 8 || bpp > 32 ) {
387 if ( (width <= 0) || (height <= 0) ) {
391 /* Search through the list valid of modes */
392 SDL_memset(&format, 0, sizeof(format));
394 table = ((bpp+7)/8)-1;
395 SDL_closest_depths[table][0] = bpp;
396 SDL_closest_depths[table][7] = 0;
397 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
398 format.BitsPerPixel = SDL_closest_depths[table][b];
399 sizes = SDL_ListModes(&format, flags);
400 if ( sizes == (SDL_Rect **)0 ) {
401 /* No sizes supported at this bit-depth */
404 if (sizes == (SDL_Rect **)NEGATIVE_ONE) {
405 /* Any size supported at this bit-depth */
408 } else if (current_video->handles_any_size) {
409 /* Driver can center a smaller surface to simulate fullscreen */
410 for ( i=0; sizes[i]; ++i ) {
411 if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) {
412 supported = 1; /* this mode can fit the centered window. */
417 for ( i=0; sizes[i]; ++i ) {
418 if ((sizes[i]->w == width) && (sizes[i]->h == height)) {
426 return(SDL_closest_depths[table][b]);
433 * Get the closest non-emulated video mode to the one requested
435 static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags)
440 SDL_PixelFormat format;
443 /* Check parameters */
444 if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) {
445 SDL_SetError("Invalid bits per pixel (range is {8...32})");
448 if ((*w <= 0) || (*h <= 0)) {
449 SDL_SetError("Invalid width or height");
453 /* Try the original video mode, get the closest depth */
454 native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags);
455 if ( native_bpp == *BitsPerPixel ) {
458 if ( native_bpp > 0 ) {
459 *BitsPerPixel = native_bpp;
463 /* No exact size match at any depth, look for closest match */
464 SDL_memset(&format, 0, sizeof(format));
466 table = ((*BitsPerPixel+7)/8)-1;
467 SDL_closest_depths[table][0] = *BitsPerPixel;
468 SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
469 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
472 format.BitsPerPixel = SDL_closest_depths[table][b];
473 sizes = SDL_ListModes(&format, flags);
474 if ( sizes == (SDL_Rect **)0 ) {
475 /* No sizes supported at this bit-depth */
479 for ( i=0; sizes[i]; ++i ) {
480 /* Mode with both dimensions bigger or equal than asked ? */
481 if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
482 /* Mode with any dimension smaller or equal than current best ? */
483 if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
484 /* Now choose the mode that has less pixels */
485 if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) {
495 *BitsPerPixel = SDL_closest_depths[table][b];
499 SDL_SetError("No video mode large enough for %dx%d", *w, *h);
504 /* This should probably go somewhere else -- like SDL_surface.c */
505 static void SDL_ClearSurface(SDL_Surface *surface)
509 black = SDL_MapRGB(surface->format, 0, 0, 0);
510 SDL_FillRect(surface, NULL, black);
511 if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) {
513 SDL_FillRect(surface, NULL, black);
515 if (surface->flags&SDL_FULLSCREEN) {
521 * Create a shadow surface suitable for fooling the app. :-)
523 static void SDL_CreateShadowSurface(int depth)
525 Uint32 Rmask, Gmask, Bmask;
527 /* Allocate the shadow surface */
528 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
529 Rmask = (SDL_VideoSurface->format)->Rmask;
530 Gmask = (SDL_VideoSurface->format)->Gmask;
531 Bmask = (SDL_VideoSurface->format)->Bmask;
533 Rmask = Gmask = Bmask = 0;
535 SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
536 SDL_VideoSurface->w, SDL_VideoSurface->h,
537 depth, Rmask, Gmask, Bmask, 0);
538 if ( SDL_ShadowSurface == NULL ) {
542 /* 8-bit shadow surfaces report that they have exclusive palette */
543 if ( SDL_ShadowSurface->format->palette ) {
544 SDL_ShadowSurface->flags |= SDL_HWPALETTE;
545 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
546 SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
547 SDL_VideoSurface->format->palette->colors,
548 SDL_VideoSurface->format->palette->ncolors*
552 SDL_ShadowSurface->format->palette->colors, depth);
556 /* If the video surface is resizable, the shadow should say so */
557 if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) {
558 SDL_ShadowSurface->flags |= SDL_RESIZABLE;
560 /* If the video surface has no frame, the shadow should say so */
561 if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) {
562 SDL_ShadowSurface->flags |= SDL_NOFRAME;
564 /* If the video surface is fullscreen, the shadow should say so */
565 if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
566 SDL_ShadowSurface->flags |= SDL_FULLSCREEN;
568 /* If the video surface is flippable, the shadow should say so */
569 if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
570 SDL_ShadowSurface->flags |= SDL_DOUBLEBUF;
576 #include <sys/neutrino.h>
577 #endif /* __QNXNTO__ */
580 * Set the requested video mode, allocating a shadow buffer if necessary.
582 SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
584 SDL_VideoDevice *video, *this;
585 SDL_Surface *prev_mode, *mode;
590 SDL_GrabMode saved_grab;
592 /* Start up the video driver, if necessary..
593 WARNING: This is the only function protected this way!
595 if ( ! current_video ) {
596 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) {
600 this = video = current_video;
602 /* Default to the current width and height */
604 width = video->info.current_w;
607 height = video->info.current_h;
609 /* Default to the current video bpp */
611 flags |= SDL_ANYFORMAT;
612 bpp = SDL_VideoSurface->format->BitsPerPixel;
615 /* Get a good video mode, the closest one possible */
619 if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) {
623 /* Check the requested flags */
624 /* There's no palette in > 8 bits-per-pixel mode */
625 if ( video_bpp > 8 ) {
626 flags &= ~SDL_HWPALETTE;
629 if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) {
630 /* There's no windowed double-buffering */
631 flags &= ~SDL_DOUBLEBUF;
634 if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
635 /* Use hardware surfaces when double-buffering */
636 flags |= SDL_HWSURFACE;
639 is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL );
641 /* These flags are for 2D video modes only */
642 flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF);
645 /* Reset the keyboard here so event callbacks can run */
648 SDL_SetMouseRange(width, height);
649 SDL_cursorstate &= ~CURSOR_USINGSW;
651 /* Clean up any previous video mode */
652 if ( SDL_PublicSurface != NULL ) {
653 SDL_PublicSurface = NULL;
655 if ( SDL_ShadowSurface != NULL ) {
656 SDL_Surface *ready_to_go;
657 ready_to_go = SDL_ShadowSurface;
658 SDL_ShadowSurface = NULL;
659 SDL_FreeSurface(ready_to_go);
661 if ( video->physpal ) {
662 SDL_free(video->physpal->colors);
663 SDL_free(video->physpal);
664 video->physpal = NULL;
666 if( video->gammacols) {
667 SDL_free(video->gammacols);
668 video->gammacols = NULL;
671 /* Save the previous grab state and turn off grab for mode switch */
672 saved_grab = SDL_WM_GrabInputOff();
674 /* Try to set the video mode, along with offset and clipping */
675 prev_mode = SDL_VideoSurface;
677 SDL_VideoSurface = NULL; /* In case it's freed by driver */
678 mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags);
679 if ( mode ) { /* Prevent resize events from mode change */
680 /* But not on OS/2 */
682 SDL_PrivateResize(mode->w, mode->h);
685 /* Sam - If we asked for OpenGL mode, and didn't get it, fail */
686 if ( is_opengl && !(mode->flags & SDL_OPENGL) ) {
688 SDL_SetError("OpenGL not available");
693 * If you try to set an SDL_OPENGL surface, and fail to find a
694 * matching visual, then the next call to SDL_SetVideoMode()
695 * will segfault, since we no longer point to a dummy surface,
698 * WARNING, we need to make sure that the previous mode hasn't
699 * already been freed by the video driver. What do we do in
700 * that case? Should we call SDL_VideoInit() again?
702 SDL_VideoSurface = (mode != NULL) ? mode : prev_mode;
704 if ( (mode != NULL) && (!is_opengl) ) {
706 if ( (mode->w < width) || (mode->h < height) ) {
707 SDL_SetError("Video mode smaller than requested");
711 /* If we have a palettized surface, create a default palette */
712 if ( mode->format->palette ) {
713 SDL_PixelFormat *vf = mode->format;
714 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
715 video->SetColors(this, 0, vf->palette->ncolors,
716 vf->palette->colors);
719 /* Clear the surface to black */
723 SDL_SetClipRect(mode, NULL);
724 SDL_ClearSurface(mode);
726 /* Now adjust the offsets to match the desired mode */
727 video->offset_x = (mode->w-width)/2;
728 video->offset_y = (mode->h-height)/2;
729 mode->offset = video->offset_y*mode->pitch +
730 video->offset_x*mode->format->BytesPerPixel;
733 "Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n",
735 mode->w, mode->h, mode->format->BitsPerPixel, mode->offset);
739 SDL_SetClipRect(mode, NULL);
744 /* If we failed setting a video mode, return NULL... (Uh Oh!) */
745 if ( mode == NULL ) {
749 /* If there is no window manager, set the SDL_NOFRAME flag */
750 if ( ! video->info.wm_available ) {
751 mode->flags |= SDL_NOFRAME;
754 /* Reset the mouse cursor and grab for new video mode */
756 if ( video->UpdateMouse ) {
757 video->UpdateMouse(this);
759 SDL_WM_GrabInput(saved_grab);
760 SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */
763 /* Load GL symbols (before MakeCurrent, where we need glGetString). */
764 if ( flags & (SDL_OPENGL | SDL_OPENGLBLIT) ) {
766 #if defined(__QNXNTO__) && (_NTO_VERSION < 630)
767 #define __SDL_NOGETPROCADDR__
768 #elif defined(__MINT__)
769 #define __SDL_NOGETPROCADDR__
771 #ifdef __SDL_NOGETPROCADDR__
772 #define SDL_PROC(ret,func,params) video->func=func;
774 #define SDL_PROC(ret,func,params) \
776 video->func = SDL_GL_GetProcAddress(#func); \
777 if ( ! video->func ) { \
778 SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
783 #endif /* __SDL_NOGETPROCADDR__ */
785 #include "SDL_glfuncs.h"
788 #endif /* SDL_VIDEO_OPENGL */
790 /* If we're running OpenGL, make the context current */
791 if ( (video->screen->flags & SDL_OPENGL) &&
792 video->GL_MakeCurrent ) {
793 if ( video->GL_MakeCurrent(this) < 0 ) {
798 /* Set up a fake SDL surface for OpenGL "blitting" */
799 if ( (flags & SDL_OPENGLBLIT) == SDL_OPENGLBLIT ) {
800 /* Load GL functions for performing the texture updates */
803 /* Create a software surface for blitting */
804 #ifdef GL_VERSION_1_2
805 /* If the implementation either supports the packed pixels
806 extension, or implements the core OpenGL 1.2 API, it will
807 support the GL_UNSIGNED_SHORT_5_6_5 texture format.
810 (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
811 (SDL_atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
814 SDL_VideoSurface = SDL_CreateRGBSurface(
826 #endif /* OpenGL 1.2 */
829 SDL_VideoSurface = SDL_CreateRGBSurface(
834 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
847 if ( ! SDL_VideoSurface ) {
850 SDL_VideoSurface->flags = mode->flags | SDL_OPENGLBLIT;
852 /* Free the original video mode surface (is this safe?) */
853 SDL_FreeSurface(mode);
855 /* Set the surface completely opaque & white by default */
856 SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
857 video->glGenTextures( 1, &video->texture );
858 video->glBindTexture( GL_TEXTURE_2D, video->texture );
862 video->is_32bit ? GL_RGBA : GL_RGB,
866 video->is_32bit ? GL_RGBA : GL_RGB,
867 #ifdef GL_VERSION_1_2
868 video->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
874 video->UpdateRects = SDL_GL_UpdateRectsLock;
876 SDL_SetError("Somebody forgot to #define SDL_VIDEO_OPENGL");
881 /* Create a shadow surface if necessary */
882 /* There are three conditions under which we create a shadow surface:
883 1. We need a particular bits-per-pixel that we didn't get.
884 2. We need a hardware palette and didn't get one.
885 3. We need a software surface and got a hardware surface.
887 if ( !(SDL_VideoSurface->flags & SDL_OPENGL) &&
889 ( !(flags&SDL_ANYFORMAT) &&
890 (SDL_VideoSurface->format->BitsPerPixel != bpp)) ||
891 ( (flags&SDL_HWPALETTE) &&
892 !(SDL_VideoSurface->flags&SDL_HWPALETTE)) ||
893 /* If the surface is in hardware, video writes are visible
894 as soon as they are performed, so we need to buffer them
896 ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) &&
897 (SDL_VideoSurface->flags&SDL_HWSURFACE)) ||
898 ( (flags&SDL_DOUBLEBUF) &&
899 (SDL_VideoSurface->flags&SDL_HWSURFACE) &&
900 !(SDL_VideoSurface->flags&SDL_DOUBLEBUF))
902 SDL_CreateShadowSurface(bpp);
903 if ( SDL_ShadowSurface == NULL ) {
904 SDL_SetError("Couldn't create shadow surface");
907 SDL_PublicSurface = SDL_ShadowSurface;
909 SDL_PublicSurface = SDL_VideoSurface;
911 video->info.vfmt = SDL_VideoSurface->format;
912 video->info.current_w = SDL_VideoSurface->w;
913 video->info.current_h = SDL_VideoSurface->h;
916 return(SDL_PublicSurface);
920 * Convert a surface into the video pixel format.
922 SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface)
926 if ( ! SDL_PublicSurface ) {
927 SDL_SetError("No video mode has been set");
930 /* Set the flags appropriate for copying to display surface */
931 if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw)
932 flags = SDL_HWSURFACE;
934 flags = SDL_SWSURFACE;
935 #ifdef AUTORLE_DISPLAYFORMAT
936 flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA));
937 flags |= SDL_RLEACCELOK;
939 flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK);
941 return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
945 * Convert a surface into a format that's suitable for blitting to
946 * the screen, but including an alpha channel.
948 SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface)
951 SDL_PixelFormat *format;
952 SDL_Surface *converted;
954 /* default to ARGB8888 */
955 Uint32 amask = 0xff000000;
956 Uint32 rmask = 0x00ff0000;
957 Uint32 gmask = 0x0000ff00;
958 Uint32 bmask = 0x000000ff;
960 if ( ! SDL_PublicSurface ) {
961 SDL_SetError("No video mode has been set");
964 vf = SDL_PublicSurface->format;
966 switch(vf->BytesPerPixel) {
968 /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
969 For anything else (like ARGB4444) it doesn't matter
970 since we have no special code for it anyway */
971 if ( (vf->Rmask == 0x1f) &&
972 (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
980 /* Keep the video format, as long as the high 8 bits are
982 if ( (vf->Rmask == 0xff) && (vf->Bmask == 0xff0000) ) {
989 /* We have no other optimised formats right now. When/if a new
990 optimised alpha format is written, add the converter here */
993 format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
994 flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
995 flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
996 converted = SDL_ConvertSurface(surface, format, flags);
997 SDL_FreeFormat(format);
1002 * Update a specific portion of the physical screen
1004 void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
1009 /* Perform some checking */
1014 if ( (int)(x+w) > screen->w )
1016 if ( (int)(y+h) > screen->h )
1019 /* Fill the rectangle */
1024 SDL_UpdateRects(screen, 1, &rect);
1027 void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects)
1030 SDL_VideoDevice *video = current_video;
1031 SDL_VideoDevice *this = current_video;
1033 if ( (screen->flags & (SDL_OPENGL | SDL_OPENGLBLIT)) == SDL_OPENGL ) {
1034 SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()");
1037 if ( screen == SDL_ShadowSurface ) {
1038 /* Blit the shadow surface using saved mapping */
1039 SDL_Palette *pal = screen->format->palette;
1040 SDL_Color *saved_colors = NULL;
1041 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1042 /* simulated 8bpp, use correct physical palette */
1043 saved_colors = pal->colors;
1044 if ( video->gammacols ) {
1045 /* gamma-corrected palette */
1046 pal->colors = video->gammacols;
1047 } else if ( video->physpal ) {
1048 /* physical palette different from logical */
1049 pal->colors = video->physpal->colors;
1052 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1054 SDL_DrawCursor(SDL_ShadowSurface);
1055 for ( i=0; i<numrects; ++i ) {
1056 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1057 SDL_VideoSurface, &rects[i]);
1059 SDL_EraseCursor(SDL_ShadowSurface);
1062 for ( i=0; i<numrects; ++i ) {
1063 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1064 SDL_VideoSurface, &rects[i]);
1067 if ( saved_colors ) {
1068 pal->colors = saved_colors;
1071 /* Fall through to video surface update */
1072 screen = SDL_VideoSurface;
1074 if ( screen == SDL_VideoSurface ) {
1075 /* Update the video surface */
1076 if ( screen->offset ) {
1077 for ( i=0; i<numrects; ++i ) {
1078 rects[i].x += video->offset_x;
1079 rects[i].y += video->offset_y;
1081 video->UpdateRects(this, numrects, rects);
1082 for ( i=0; i<numrects; ++i ) {
1083 rects[i].x -= video->offset_x;
1084 rects[i].y -= video->offset_y;
1087 video->UpdateRects(this, numrects, rects);
1093 * Performs hardware double buffering, if possible, or a full update if not.
1095 int SDL_Flip(SDL_Surface *screen)
1097 SDL_VideoDevice *video = current_video;
1098 /* Copy the shadow surface to the video surface */
1099 if ( screen == SDL_ShadowSurface ) {
1101 SDL_Palette *pal = screen->format->palette;
1102 SDL_Color *saved_colors = NULL;
1103 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1104 /* simulated 8bpp, use correct physical palette */
1105 saved_colors = pal->colors;
1106 if ( video->gammacols ) {
1107 /* gamma-corrected palette */
1108 pal->colors = video->gammacols;
1109 } else if ( video->physpal ) {
1110 /* physical palette different from logical */
1111 pal->colors = video->physpal->colors;
1119 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1121 SDL_DrawCursor(SDL_ShadowSurface);
1122 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1123 SDL_VideoSurface, &rect);
1124 SDL_EraseCursor(SDL_ShadowSurface);
1127 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1128 SDL_VideoSurface, &rect);
1130 if ( saved_colors ) {
1131 pal->colors = saved_colors;
1134 /* Fall through to video surface update */
1135 screen = SDL_VideoSurface;
1137 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
1138 SDL_VideoDevice *this = current_video;
1139 return(video->FlipHWSurface(this, SDL_VideoSurface));
1141 SDL_UpdateRect(screen, 0, 0, 0, 0);
1146 static void SetPalette_logical(SDL_Surface *screen, SDL_Color *colors,
1147 int firstcolor, int ncolors)
1149 SDL_Palette *pal = screen->format->palette;
1150 SDL_Palette *vidpal;
1152 if ( colors != (pal->colors + firstcolor) ) {
1153 SDL_memcpy(pal->colors + firstcolor, colors,
1154 ncolors * sizeof(*colors));
1157 if ( current_video && SDL_VideoSurface ) {
1158 vidpal = SDL_VideoSurface->format->palette;
1159 if ( (screen == SDL_ShadowSurface) && vidpal ) {
1161 * This is a shadow surface, and the physical
1162 * framebuffer is also indexed. Propagate the
1163 * changes to its logical palette so that
1164 * updates are always identity blits
1166 SDL_memcpy(vidpal->colors + firstcolor, colors,
1167 ncolors * sizeof(*colors));
1170 SDL_FormatChanged(screen);
1173 static int SetPalette_physical(SDL_Surface *screen,
1174 SDL_Color *colors, int firstcolor, int ncolors)
1176 SDL_VideoDevice *video = current_video;
1179 if ( video->physpal ) {
1180 /* We need to copy the new colors, since we haven't
1181 * already done the copy in the logical set above.
1183 SDL_memcpy(video->physpal->colors + firstcolor,
1184 colors, ncolors * sizeof(*colors));
1186 if ( screen == SDL_ShadowSurface ) {
1187 if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
1189 * The real screen is also indexed - set its physical
1190 * palette. The physical palette does not include the
1191 * gamma modification, we apply it directly instead,
1192 * but this only happens if we have hardware palette.
1194 screen = SDL_VideoSurface;
1197 * The video surface is not indexed - invalidate any
1198 * active shadow-to-video blit mappings.
1200 if ( screen->map->dst == SDL_VideoSurface ) {
1201 SDL_InvalidateMap(screen->map);
1203 if ( video->gamma ) {
1204 if( ! video->gammacols ) {
1205 SDL_Palette *pp = video->physpal;
1207 pp = screen->format->palette;
1208 video->gammacols = SDL_malloc(pp->ncolors
1209 * sizeof(SDL_Color));
1210 SDL_ApplyGamma(video->gamma,
1215 SDL_ApplyGamma(video->gamma, colors,
1221 SDL_UpdateRect(screen, 0, 0, 0, 0);
1225 if ( screen == SDL_VideoSurface ) {
1226 SDL_Color gcolors[256];
1228 if ( video->gamma ) {
1229 SDL_ApplyGamma(video->gamma, colors, gcolors, ncolors);
1232 gotall = video->SetColors(video, firstcolor, ncolors, colors);
1234 /* The video flags shouldn't have SDL_HWPALETTE, and
1235 the video driver is responsible for copying back the
1236 correct colors into the video surface palette.
1240 SDL_CursorPaletteChanged();
1246 * Set the physical and/or logical colormap of a surface:
1247 * Only the screen has a physical colormap. It determines what is actually
1248 * sent to the display.
1249 * The logical colormap is used to map blits to/from the surface.
1250 * 'which' is one or both of SDL_LOGPAL, SDL_PHYSPAL
1252 * Return nonzero if all colours were set as requested, or 0 otherwise.
1254 int SDL_SetPalette(SDL_Surface *screen, int which,
1255 SDL_Color *colors, int firstcolor, int ncolors)
1264 if ( !current_video || screen != SDL_PublicSurface ) {
1265 /* only screens have physical palettes */
1266 which &= ~SDL_PHYSPAL;
1267 } else if ( (screen->flags & SDL_HWPALETTE) != SDL_HWPALETTE ) {
1268 /* hardware palettes required for split colormaps */
1269 which |= SDL_PHYSPAL | SDL_LOGPAL;
1272 /* Verify the parameters */
1273 pal = screen->format->palette;
1275 return 0; /* not a palettized surface */
1278 palsize = 1 << screen->format->BitsPerPixel;
1279 if ( ncolors > (palsize - firstcolor) ) {
1280 ncolors = (palsize - firstcolor);
1284 if ( which & SDL_LOGPAL ) {
1286 * Logical palette change: The actual screen isn't affected,
1287 * but the internal colormap is altered so that the
1288 * interpretation of the pixel values (for blits etc) is
1291 SetPalette_logical(screen, colors, firstcolor, ncolors);
1293 if ( which & SDL_PHYSPAL ) {
1294 SDL_VideoDevice *video = current_video;
1296 * Physical palette change: This doesn't affect the
1297 * program's idea of what the screen looks like, but changes
1298 * its actual appearance.
1300 if ( !video->physpal && !(which & SDL_LOGPAL) ) {
1301 /* Lazy physical palette allocation */
1303 SDL_Palette *pp = SDL_malloc(sizeof(*pp));
1307 video->physpal = pp;
1308 pp->ncolors = pal->ncolors;
1309 size = pp->ncolors * sizeof(SDL_Color);
1310 pp->colors = SDL_malloc(size);
1311 if ( !pp->colors ) {
1314 SDL_memcpy(pp->colors, pal->colors, size);
1316 if ( ! SetPalette_physical(screen,
1317 colors, firstcolor, ncolors) ) {
1324 int SDL_SetColors(SDL_Surface *screen, SDL_Color *colors, int firstcolor,
1327 return SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL,
1328 colors, firstcolor, ncolors);
1332 * Clean up the video subsystem
1334 void SDL_VideoQuit (void)
1336 SDL_Surface *ready_to_go;
1338 if ( current_video ) {
1339 SDL_VideoDevice *video = current_video;
1340 SDL_VideoDevice *this = current_video;
1342 /* Halt event processing before doing anything else */
1343 SDL_StopEventLoop();
1345 /* Clean up allocated window manager items */
1346 if ( SDL_PublicSurface ) {
1347 SDL_PublicSurface = NULL;
1351 /* Just in case... */
1352 SDL_WM_GrabInputOff();
1354 /* Clean up the system video */
1355 video->VideoQuit(this);
1357 /* Free any lingering surfaces */
1358 ready_to_go = SDL_ShadowSurface;
1359 SDL_ShadowSurface = NULL;
1360 SDL_FreeSurface(ready_to_go);
1361 if ( SDL_VideoSurface != NULL ) {
1362 ready_to_go = SDL_VideoSurface;
1363 SDL_VideoSurface = NULL;
1364 SDL_FreeSurface(ready_to_go);
1366 SDL_PublicSurface = NULL;
1368 /* Clean up miscellaneous memory */
1369 if ( video->physpal ) {
1370 SDL_free(video->physpal->colors);
1371 SDL_free(video->physpal);
1372 video->physpal = NULL;
1374 if ( video->gammacols ) {
1375 SDL_free(video->gammacols);
1376 video->gammacols = NULL;
1378 if ( video->gamma ) {
1379 SDL_free(video->gamma);
1380 video->gamma = NULL;
1382 if ( video->wm_title != NULL ) {
1383 SDL_free(video->wm_title);
1384 video->wm_title = NULL;
1386 if ( video->wm_icon != NULL ) {
1387 SDL_free(video->wm_icon);
1388 video->wm_icon = NULL;
1391 /* Finish cleaning up video subsystem */
1393 current_video = NULL;
1398 /* Load the GL driver library */
1399 int SDL_GL_LoadLibrary(const char *path)
1401 SDL_VideoDevice *video = current_video;
1402 SDL_VideoDevice *this = current_video;
1406 if ( video == NULL ) {
1407 SDL_SetError("Video subsystem has not been initialized");
1409 if ( video->GL_LoadLibrary ) {
1410 retval = video->GL_LoadLibrary(this, path);
1412 SDL_SetError("No dynamic GL support in video driver");
1418 void *SDL_GL_GetProcAddress(const char* proc)
1420 SDL_VideoDevice *video = current_video;
1421 SDL_VideoDevice *this = current_video;
1425 if ( video->GL_GetProcAddress ) {
1426 if ( video->gl_config.driver_loaded ) {
1427 func = video->GL_GetProcAddress(this, proc);
1429 SDL_SetError("No GL driver has been loaded");
1432 SDL_SetError("No dynamic GL support in video driver");
1437 /* Set the specified GL attribute for setting up a GL video mode */
1438 int SDL_GL_SetAttribute( SDL_GLattr attr, int value )
1441 SDL_VideoDevice *video = current_video;
1445 case SDL_GL_RED_SIZE:
1446 video->gl_config.red_size = value;
1448 case SDL_GL_GREEN_SIZE:
1449 video->gl_config.green_size = value;
1451 case SDL_GL_BLUE_SIZE:
1452 video->gl_config.blue_size = value;
1454 case SDL_GL_ALPHA_SIZE:
1455 video->gl_config.alpha_size = value;
1457 case SDL_GL_DOUBLEBUFFER:
1458 video->gl_config.double_buffer = value;
1460 case SDL_GL_BUFFER_SIZE:
1461 video->gl_config.buffer_size = value;
1463 case SDL_GL_DEPTH_SIZE:
1464 video->gl_config.depth_size = value;
1466 case SDL_GL_STENCIL_SIZE:
1467 video->gl_config.stencil_size = value;
1469 case SDL_GL_ACCUM_RED_SIZE:
1470 video->gl_config.accum_red_size = value;
1472 case SDL_GL_ACCUM_GREEN_SIZE:
1473 video->gl_config.accum_green_size = value;
1475 case SDL_GL_ACCUM_BLUE_SIZE:
1476 video->gl_config.accum_blue_size = value;
1478 case SDL_GL_ACCUM_ALPHA_SIZE:
1479 video->gl_config.accum_alpha_size = value;
1482 video->gl_config.stereo = value;
1484 case SDL_GL_MULTISAMPLEBUFFERS:
1485 video->gl_config.multisamplebuffers = value;
1487 case SDL_GL_MULTISAMPLESAMPLES:
1488 video->gl_config.multisamplesamples = value;
1490 case SDL_GL_ACCELERATED_VISUAL:
1491 video->gl_config.accelerated = value;
1493 case SDL_GL_SWAP_CONTROL:
1494 video->gl_config.swap_control = value;
1497 SDL_SetError("Unknown OpenGL attribute");
1504 /* Retrieve an attribute value from the windowing system. */
1505 int SDL_GL_GetAttribute(SDL_GLattr attr, int* value)
1508 SDL_VideoDevice* video = current_video;
1509 SDL_VideoDevice* this = current_video;
1511 if ( video->GL_GetAttribute ) {
1512 retval = this->GL_GetAttribute(this, attr, value);
1515 SDL_SetError("GL_GetAttribute not supported");
1520 /* Perform a GL buffer swap on the current GL context */
1521 void SDL_GL_SwapBuffers(void)
1523 SDL_VideoDevice *video = current_video;
1524 SDL_VideoDevice *this = current_video;
1526 if ( video->screen->flags & SDL_OPENGL ) {
1527 video->GL_SwapBuffers(this);
1529 SDL_SetError("OpenGL video mode has not been set");
1533 /* Update rects with locking */
1534 void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect *rects)
1537 SDL_GL_UpdateRects(numrects, rects);
1541 /* Update rects without state setting and changing (the caller is responsible for it) */
1542 void SDL_GL_UpdateRects(int numrects, SDL_Rect *rects)
1544 #if SDL_VIDEO_OPENGL
1545 SDL_VideoDevice *this = current_video;
1546 SDL_Rect update, tmp;
1549 for ( i = 0; i < numrects; i++ )
1553 for ( y = 0; y <= rects[i].h / 256; y++ )
1557 for ( x = 0; x <= rects[i].w / 256; x++ )
1564 if ( update.w > 256 )
1567 if ( update.h > 256 )
1571 this->glTexSubImage2D(
1578 this->is_32bit? GL_RGBA : GL_RGB,
1579 #ifdef GL_VERSION_1_2
1580 this->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
1584 (Uint8 *)this->screen->pixels +
1585 this->screen->format->BytesPerPixel * update.x +
1586 update.y * this->screen->pitch );
1590 * Note the parens around the function name:
1591 * This is because some OpenGL implementations define glTexCoord etc
1592 * as macros, and we don't want them expanded here.
1594 this->glBegin(GL_TRIANGLE_STRIP);
1595 (this->glTexCoord2f)( 0.0, 0.0 );
1596 (this->glVertex2i)( update.x, update.y );
1597 (this->glTexCoord2f)( (float)(update.w / 256.0), 0.0 );
1598 (this->glVertex2i)( update.x + update.w, update.y );
1599 (this->glTexCoord2f)( 0.0, (float)(update.h / 256.0) );
1600 (this->glVertex2i)( update.x, update.y + update.h );
1601 (this->glTexCoord2f)( (float)(update.w / 256.0), (float)(update.h / 256.0) );
1602 (this->glVertex2i)( update.x + update.w , update.y + update.h );
1615 /* Lock == save current state */
1618 #if SDL_VIDEO_OPENGL
1622 SDL_VideoDevice *this = current_video;
1624 this->glPushAttrib( GL_ALL_ATTRIB_BITS ); /* TODO: narrow range of what is saved */
1625 #ifdef GL_CLIENT_PIXEL_STORE_BIT
1626 this->glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
1629 this->glEnable(GL_TEXTURE_2D);
1630 this->glEnable(GL_BLEND);
1631 this->glDisable(GL_FOG);
1632 this->glDisable(GL_ALPHA_TEST);
1633 this->glDisable(GL_DEPTH_TEST);
1634 this->glDisable(GL_SCISSOR_TEST);
1635 this->glDisable(GL_STENCIL_TEST);
1636 this->glDisable(GL_CULL_FACE);
1638 this->glBindTexture( GL_TEXTURE_2D, this->texture );
1639 this->glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1640 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
1641 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
1642 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
1643 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
1645 this->glPixelStorei( GL_UNPACK_ROW_LENGTH, this->screen->pitch / this->screen->format->BytesPerPixel );
1646 this->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1647 (this->glColor4f)(1.0, 1.0, 1.0, 1.0); /* Solaris workaround */
1649 this->glViewport(0, 0, this->screen->w, this->screen->h);
1650 this->glMatrixMode(GL_PROJECTION);
1651 this->glPushMatrix();
1652 this->glLoadIdentity();
1654 this->glOrtho(0.0, (GLdouble) this->screen->w, (GLdouble) this->screen->h, 0.0, 0.0, 1.0);
1656 this->glMatrixMode(GL_MODELVIEW);
1657 this->glPushMatrix();
1658 this->glLoadIdentity();
1663 /* Unlock == restore saved state */
1664 void SDL_GL_Unlock()
1666 #if SDL_VIDEO_OPENGL
1670 SDL_VideoDevice *this = current_video;
1672 this->glPopMatrix();
1673 this->glMatrixMode(GL_PROJECTION);
1674 this->glPopMatrix();
1676 this->glPopClientAttrib();
1677 this->glPopAttrib();
1683 * Sets/Gets the title and icon text of the display window, if any.
1685 void SDL_WM_SetCaption (const char *title, const char *icon)
1687 SDL_VideoDevice *video = current_video;
1688 SDL_VideoDevice *this = current_video;
1692 if ( video->wm_title ) {
1693 SDL_free(video->wm_title);
1695 video->wm_title = SDL_strdup(title);
1698 if ( video->wm_icon ) {
1699 SDL_free(video->wm_icon);
1701 video->wm_icon = SDL_strdup(icon);
1703 if ( (title || icon) && (video->SetCaption != NULL) ) {
1704 video->SetCaption(this, video->wm_title,video->wm_icon);
1708 void SDL_WM_GetCaption (char **title, char **icon)
1710 SDL_VideoDevice *video = current_video;
1714 *title = video->wm_title;
1717 *icon = video->wm_icon;
1722 /* Utility function used by SDL_WM_SetIcon();
1723 * flags & 1 for color key, flags & 2 for alpha channel. */
1724 static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
1728 #define SET_MASKBIT(icon, x, y, mask) \
1729 mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
1731 colorkey = icon->format->colorkey;
1732 switch (icon->format->BytesPerPixel) {
1733 case 1: { Uint8 *pixels;
1734 for ( y=0; y<icon->h; ++y ) {
1735 pixels = (Uint8 *)icon->pixels + y*icon->pitch;
1736 for ( x=0; x<icon->w; ++x ) {
1737 if ( *pixels++ == colorkey ) {
1738 SET_MASKBIT(icon, x, y, mask);
1745 case 2: { Uint16 *pixels;
1746 for ( y=0; y<icon->h; ++y ) {
1747 pixels = (Uint16 *)icon->pixels +
1749 for ( x=0; x<icon->w; ++x ) {
1750 if ( (flags & 1) && *pixels == colorkey ) {
1751 SET_MASKBIT(icon, x, y, mask);
1752 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1753 SET_MASKBIT(icon, x, y, mask);
1761 case 4: { Uint32 *pixels;
1762 for ( y=0; y<icon->h; ++y ) {
1763 pixels = (Uint32 *)icon->pixels +
1765 for ( x=0; x<icon->w; ++x ) {
1766 if ( (flags & 1) && *pixels == colorkey ) {
1767 SET_MASKBIT(icon, x, y, mask);
1768 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1769 SET_MASKBIT(icon, x, y, mask);
1780 * Sets the window manager icon for the display window.
1782 void SDL_WM_SetIcon (SDL_Surface *icon, Uint8 *mask)
1784 SDL_VideoDevice *video = current_video;
1785 SDL_VideoDevice *this = current_video;
1787 if ( icon && video->SetIcon ) {
1788 /* Generate a mask if necessary, and create the icon! */
1789 if ( mask == NULL ) {
1790 int mask_len = icon->h*(icon->w+7)/8;
1792 mask = (Uint8 *)SDL_malloc(mask_len);
1793 if ( mask == NULL ) {
1796 SDL_memset(mask, ~0, mask_len);
1797 if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
1798 if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
1800 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
1802 video->SetIcon(video, icon, mask);
1805 video->SetIcon(this, icon, mask);
1811 * Grab or ungrab the keyboard and mouse input.
1812 * This function returns the final grab mode after calling the
1813 * driver dependent function.
1815 static SDL_GrabMode SDL_WM_GrabInputRaw(SDL_GrabMode mode)
1817 SDL_VideoDevice *video = current_video;
1818 SDL_VideoDevice *this = current_video;
1820 /* Only do something if we have support for grabs */
1821 if ( video->GrabInput == NULL ) {
1822 return(video->input_grab);
1825 /* If the final grab mode if off, only then do we actually grab */
1827 printf("SDL_WM_GrabInputRaw(%d) ... ", mode);
1829 if ( mode == SDL_GRAB_OFF ) {
1830 if ( video->input_grab != SDL_GRAB_OFF ) {
1831 mode = video->GrabInput(this, mode);
1834 if ( video->input_grab == SDL_GRAB_OFF ) {
1835 mode = video->GrabInput(this, mode);
1838 if ( mode != video->input_grab ) {
1839 video->input_grab = mode;
1840 if ( video->CheckMouseMode ) {
1841 video->CheckMouseMode(this);
1845 printf("Final mode %d\n", video->input_grab);
1848 /* Return the final grab state */
1849 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1850 mode -= SDL_GRAB_FULLSCREEN;
1854 SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode)
1856 SDL_VideoDevice *video = current_video;
1858 /* If the video isn't initialized yet, we can't do anything */
1860 return SDL_GRAB_OFF;
1863 /* Return the current mode on query */
1864 if ( mode == SDL_GRAB_QUERY ) {
1865 mode = video->input_grab;
1866 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1867 mode -= SDL_GRAB_FULLSCREEN;
1873 printf("SDL_WM_GrabInput(%d) ... ", mode);
1875 /* If the video surface is fullscreen, we always grab */
1876 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1877 mode -= SDL_GRAB_FULLSCREEN;
1879 if ( SDL_VideoSurface && (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
1880 mode += SDL_GRAB_FULLSCREEN;
1882 return(SDL_WM_GrabInputRaw(mode));
1884 static SDL_GrabMode SDL_WM_GrabInputOff(void)
1888 /* First query the current grab state */
1889 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
1891 /* Now explicitly turn off input grab */
1892 SDL_WM_GrabInputRaw(SDL_GRAB_OFF);
1894 /* Return the old state */
1899 * Iconify the window in window managed environments.
1900 * A successful iconification will result in an SDL_APPACTIVE loss event.
1902 int SDL_WM_IconifyWindow(void)
1904 SDL_VideoDevice *video = current_video;
1905 SDL_VideoDevice *this = current_video;
1909 if ( video->IconifyWindow ) {
1910 retval = video->IconifyWindow(this);
1916 * Toggle fullscreen mode
1918 int SDL_WM_ToggleFullScreen(SDL_Surface *surface)
1920 SDL_VideoDevice *video = current_video;
1921 SDL_VideoDevice *this = current_video;
1925 if ( SDL_PublicSurface && (surface == SDL_PublicSurface) &&
1926 video->ToggleFullScreen ) {
1927 if ( surface->flags & SDL_FULLSCREEN ) {
1928 toggled = video->ToggleFullScreen(this, 0);
1930 SDL_VideoSurface->flags &= ~SDL_FULLSCREEN;
1931 SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
1934 toggled = video->ToggleFullScreen(this, 1);
1936 SDL_VideoSurface->flags |= SDL_FULLSCREEN;
1937 SDL_PublicSurface->flags |= SDL_FULLSCREEN;
1940 /* Double-check the grab state inside SDL_WM_GrabInput() */
1942 SDL_WM_GrabInput(video->input_grab);
1949 * Get some platform dependent window manager information
1951 int SDL_GetWMInfo (SDL_SysWMinfo *info)
1953 SDL_VideoDevice *video = current_video;
1954 SDL_VideoDevice *this = current_video;
1956 if ( video && video->GetWMInfo ) {
1957 return(video->GetWMInfo(this, info));