src/video/windib/SDL_dibvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Oct 2002 20:25:05 +0000
changeset 514 1080bfc4aa96
parent 453 a6fa62b1be09
child 515 230b156829ed
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <malloc.h>
    31 #include <windows.h>
    32 #ifdef HAVE_AYGSHELL
    33 #include <aygshell.h>
    34 #endif
    35 
    36 /* Not yet in the mingw32 cross-compile headers */
    37 #ifndef CDS_FULLSCREEN
    38 #define CDS_FULLSCREEN	4
    39 #endif
    40 
    41 #include "SDL.h"
    42 #include "SDL_mutex.h"
    43 #include "SDL_syswm.h"
    44 #include "SDL_sysvideo.h"
    45 #include "SDL_sysevents.h"
    46 #include "SDL_events_c.h"
    47 #include "SDL_pixels_c.h"
    48 #include "SDL_dibvideo.h"
    49 #include "SDL_syswm_c.h"
    50 #include "SDL_sysmouse_c.h"
    51 #include "SDL_dibevents_c.h"
    52 #include "SDL_wingl_c.h"
    53 
    54 #ifdef _WIN32_WCE
    55 #define NO_GETDIBITS
    56 #define NO_CHANGEDISPLAYSETTINGS
    57 #define NO_GAMMA_SUPPORT
    58 #endif
    59 #ifndef WS_MAXIMIZE
    60 #define WS_MAXIMIZE		0
    61 #endif
    62 #ifndef SWP_NOCOPYBITS
    63 #define SWP_NOCOPYBITS	0
    64 #endif
    65 #ifndef PC_NOCOLLAPSE
    66 #define PC_NOCOLLAPSE	0
    67 #endif
    68 
    69 /* Initialization/Query functions */
    70 static int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat);
    71 static SDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    72 SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    73 static int DIB_SetColors(_THIS, int firstcolor, int ncolors,
    74 			 SDL_Color *colors);
    75 static void DIB_CheckGamma(_THIS);
    76 void DIB_SwapGamma(_THIS);
    77 void DIB_QuitGamma(_THIS);
    78 int DIB_SetGammaRamp(_THIS, Uint16 *ramp);
    79 int DIB_GetGammaRamp(_THIS, Uint16 *ramp);
    80 static void DIB_VideoQuit(_THIS);
    81 
    82 /* Hardware surface functions */
    83 static int DIB_AllocHWSurface(_THIS, SDL_Surface *surface);
    84 static int DIB_LockHWSurface(_THIS, SDL_Surface *surface);
    85 static void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface);
    86 static void DIB_FreeHWSurface(_THIS, SDL_Surface *surface);
    87 
    88 /* Windows message handling functions */
    89 static void DIB_RealizePalette(_THIS);
    90 static void DIB_PaletteChanged(_THIS, HWND window);
    91 static void DIB_WinPAINT(_THIS, HDC hdc);
    92 
    93 /* helper fn */
    94 static int DIB_SussScreenDepth();
    95 
    96 /* DIB driver bootstrap functions */
    97 
    98 static int DIB_Available(void)
    99 {
   100 	return(1);
   101 }
   102 
   103 static void DIB_DeleteDevice(SDL_VideoDevice *device)
   104 {
   105 	if ( device ) {
   106 		if ( device->hidden ) {
   107 			free(device->hidden);
   108 		}
   109 		if ( device->gl_data ) {
   110 			free(device->gl_data);
   111 		}
   112 		free(device);
   113 	}
   114 }
   115 
   116 static SDL_VideoDevice *DIB_CreateDevice(int devindex)
   117 {
   118 	SDL_VideoDevice *device;
   119 
   120 	/* Initialize all variables that we clean on shutdown */
   121 	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   122 	if ( device ) {
   123 		memset(device, 0, (sizeof *device));
   124 		device->hidden = (struct SDL_PrivateVideoData *)
   125 				malloc((sizeof *device->hidden));
   126 		device->gl_data = (struct SDL_PrivateGLData *)
   127 				malloc((sizeof *device->gl_data));
   128 	}
   129 	if ( (device == NULL) || (device->hidden == NULL) ||
   130 		                 (device->gl_data == NULL) ) {
   131 		SDL_OutOfMemory();
   132 		DIB_DeleteDevice(device);
   133 		return(NULL);
   134 	}
   135 	memset(device->hidden, 0, (sizeof *device->hidden));
   136 	memset(device->gl_data, 0, (sizeof *device->gl_data));
   137 
   138 	/* Set the function pointers */
   139 	device->VideoInit = DIB_VideoInit;
   140 	device->ListModes = DIB_ListModes;
   141 	device->SetVideoMode = DIB_SetVideoMode;
   142 	device->UpdateMouse = WIN_UpdateMouse;
   143 	device->SetColors = DIB_SetColors;
   144 	device->UpdateRects = NULL;
   145 	device->VideoQuit = DIB_VideoQuit;
   146 	device->AllocHWSurface = DIB_AllocHWSurface;
   147 	device->CheckHWBlit = NULL;
   148 	device->FillHWRect = NULL;
   149 	device->SetHWColorKey = NULL;
   150 	device->SetHWAlpha = NULL;
   151 	device->LockHWSurface = DIB_LockHWSurface;
   152 	device->UnlockHWSurface = DIB_UnlockHWSurface;
   153 	device->FlipHWSurface = NULL;
   154 	device->FreeHWSurface = DIB_FreeHWSurface;
   155 	device->SetGammaRamp = DIB_SetGammaRamp;
   156 	device->GetGammaRamp = DIB_GetGammaRamp;
   157 #ifdef HAVE_OPENGL
   158 	device->GL_LoadLibrary = WIN_GL_LoadLibrary;
   159 	device->GL_GetProcAddress = WIN_GL_GetProcAddress;
   160 	device->GL_GetAttribute = WIN_GL_GetAttribute;
   161 	device->GL_MakeCurrent = WIN_GL_MakeCurrent;
   162 	device->GL_SwapBuffers = WIN_GL_SwapBuffers;
   163 #endif
   164 	device->SetCaption = WIN_SetWMCaption;
   165 	device->SetIcon = WIN_SetWMIcon;
   166 	device->IconifyWindow = WIN_IconifyWindow;
   167 	device->GrabInput = WIN_GrabInput;
   168 	device->GetWMInfo = WIN_GetWMInfo;
   169 	device->FreeWMCursor = WIN_FreeWMCursor;
   170 	device->CreateWMCursor = WIN_CreateWMCursor;
   171 	device->ShowWMCursor = WIN_ShowWMCursor;
   172 	device->WarpWMCursor = WIN_WarpWMCursor;
   173 	device->CheckMouseMode = WIN_CheckMouseMode;
   174 	device->InitOSKeymap = DIB_InitOSKeymap;
   175 	device->PumpEvents = DIB_PumpEvents;
   176 
   177 	/* Set up the windows message handling functions */
   178 	WIN_RealizePalette = DIB_RealizePalette;
   179 	WIN_PaletteChanged = DIB_PaletteChanged;
   180 	WIN_WinPAINT = DIB_WinPAINT;
   181 	HandleMessage = DIB_HandleMessage;
   182 
   183 	device->free = DIB_DeleteDevice;
   184 
   185 	/* We're finally ready */
   186 	return device;
   187 }
   188 
   189 VideoBootStrap WINDIB_bootstrap = {
   190 	"windib", "Win95/98/NT/2000 GDI",
   191 	DIB_Available, DIB_CreateDevice
   192 };
   193 
   194 #ifndef NO_CHANGEDISPLAYSETTINGS
   195 
   196 static int cmpmodes(const void *va, const void *vb)
   197 {
   198     SDL_Rect *a = *(SDL_Rect **)va;
   199     SDL_Rect *b = *(SDL_Rect **)vb;
   200     if(a->w > b->w)
   201         return -1;
   202     return b->h - a->h;
   203 }
   204 
   205 static int DIB_AddMode(_THIS, int bpp, int w, int h)
   206 {
   207 	SDL_Rect *mode;
   208 	int i, index;
   209 	int next_mode;
   210 
   211 	/* Check to see if we already have this mode */
   212 	if ( bpp < 8 ) {  /* Not supported */
   213 		return(0);
   214 	}
   215 	index = ((bpp+7)/8)-1;
   216 	for ( i=0; i<SDL_nummodes[index]; ++i ) {
   217 		mode = SDL_modelist[index][i];
   218 		if ( (mode->w == w) && (mode->h == h) ) {
   219 			return(0);
   220 		}
   221 	}
   222 
   223 	/* Set up the new video mode rectangle */
   224 	mode = (SDL_Rect *)malloc(sizeof *mode);
   225 	if ( mode == NULL ) {
   226 		SDL_OutOfMemory();
   227 		return(-1);
   228 	}
   229 	mode->x = 0;
   230 	mode->y = 0;
   231 	mode->w = w;
   232 	mode->h = h;
   233 
   234 	/* Allocate the new list of modes, and fill in the new mode */
   235 	next_mode = SDL_nummodes[index];
   236 	SDL_modelist[index] = (SDL_Rect **)
   237 	       realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
   238 	if ( SDL_modelist[index] == NULL ) {
   239 		SDL_OutOfMemory();
   240 		SDL_nummodes[index] = 0;
   241 		free(mode);
   242 		return(-1);
   243 	}
   244 	SDL_modelist[index][next_mode] = mode;
   245 	SDL_modelist[index][next_mode+1] = NULL;
   246 	SDL_nummodes[index]++;
   247 
   248 	return(0);
   249 }
   250 
   251 #endif /* !NO_CHANGEDISPLAYSETTINGS */
   252 
   253 static HPALETTE DIB_CreatePalette(int bpp)
   254 {
   255 /*	RJR: March 28, 2000
   256 	moved palette creation here from "DIB_VideoInit" */
   257 
   258 	HPALETTE handle = NULL;
   259 	
   260 	if ( bpp <= 8 )
   261 	{
   262 		LOGPALETTE *palette;
   263 		HDC hdc;
   264 		int ncolors;
   265 		int i;
   266 
   267 		ncolors = 1;
   268 		for ( i=0; i<bpp; ++i ) {
   269 			ncolors *= 2;
   270 		}
   271 		palette = (LOGPALETTE *)malloc(sizeof(*palette)+
   272 					ncolors*sizeof(PALETTEENTRY));
   273 		palette->palVersion = 0x300;
   274 		palette->palNumEntries = ncolors;
   275 		hdc = GetDC(SDL_Window);
   276 		GetSystemPaletteEntries(hdc, 0, ncolors, palette->palPalEntry);
   277 		ReleaseDC(SDL_Window, hdc);
   278 		handle = CreatePalette(palette);
   279 		free(palette);
   280 	}
   281 	
   282 	return handle;
   283 }
   284 
   285 int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat)
   286 {
   287 #ifndef NO_CHANGEDISPLAYSETTINGS
   288 	int i;
   289 	DEVMODE settings;
   290 #endif
   291 
   292 	/* Create the window */
   293 	if ( DIB_CreateWindow(this) < 0 ) {
   294 		return(-1);
   295 	}
   296 #ifndef DISABLE_AUDIO
   297 	DX5_SoundFocus(SDL_Window);
   298 #endif
   299 
   300 	/* Determine the screen depth */
   301 	vformat->BitsPerPixel = DIB_SussScreenDepth();
   302 	switch (vformat->BitsPerPixel) {
   303 		case 15:
   304 			vformat->Rmask = 0x00007c00;
   305 			vformat->Gmask = 0x000003e0;
   306 			vformat->Bmask = 0x0000001f;
   307 			vformat->BitsPerPixel = 16;
   308 			break;
   309 		case 16:
   310 			vformat->Rmask = 0x0000f800;
   311 			vformat->Gmask = 0x000007e0;
   312 			vformat->Bmask = 0x0000001f;
   313 			break;
   314 		case 24:
   315 		case 32:
   316 			/* GDI defined as 8-8-8 */
   317 			vformat->Rmask = 0x00ff0000;
   318 			vformat->Gmask = 0x0000ff00;
   319 			vformat->Bmask = 0x000000ff;
   320 			break;
   321 		default:
   322 			break;
   323 	}
   324 
   325 	/* See if gamma is supported on this screen */
   326 	DIB_CheckGamma(this);
   327 
   328 #ifndef NO_CHANGEDISPLAYSETTINGS
   329 	/* Query for the list of available video modes */
   330 	for ( i=0; EnumDisplaySettings(NULL, i, &settings); ++i ) {
   331 		DIB_AddMode(this, settings.dmBitsPerPel,
   332 			settings.dmPelsWidth, settings.dmPelsHeight);
   333 	}
   334 	/* Sort the mode lists */
   335 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   336 		if ( SDL_nummodes[i] > 0 ) {
   337 			qsort(SDL_modelist[i], SDL_nummodes[i], sizeof *SDL_modelist[i], cmpmodes);
   338 		}
   339 	}
   340 #endif /* !NO_CHANGEDISPLAYSETTINGS */
   341 
   342 	/* Grab an identity palette if we are in a palettized mode */
   343 	if ( vformat->BitsPerPixel <= 8 ) {
   344 	/*	RJR: March 28, 2000
   345 		moved palette creation to "DIB_CreatePalette" */
   346 		screen_pal = DIB_CreatePalette(vformat->BitsPerPixel);
   347 	}
   348 
   349 	/* Fill in some window manager capabilities */
   350 	this->info.wm_available = 1;
   351 
   352 	/* We're done! */
   353 	return(0);
   354 }
   355 
   356 /* We support any format at any dimension */
   357 SDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   358 {
   359 #ifdef NO_CHANGEDISPLAYSETTINGS
   360 	return((SDL_Rect **)-1);
   361 #else
   362 	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   363 		return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
   364 	} else {
   365 		return((SDL_Rect **)-1);
   366 	}
   367 #endif
   368 }
   369 
   370 
   371 /*
   372   Helper fn to work out which screen depth windows is currently using.
   373   15 bit mode is considered 555 format, 16 bit is 565.
   374   returns 0 for unknown mode.
   375   (Derived from code in sept 1999 Windows Developer Journal
   376   http://www.wdj.com/code/archive.html)
   377 */
   378 static int DIB_SussScreenDepth()
   379 {
   380 #ifdef NO_GETDIBITS
   381 	int depth;
   382 	HDC hdc;
   383 
   384 	hdc = GetDC(SDL_Window);
   385 	depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
   386 	ReleaseDC(SDL_Window, hdc);
   387 #ifndef _WIN32_WCE
   388 	// AFAIK 16 bit CE devices have indeed RGB 565
   389 	if ( depth == 16 ) {
   390 		depth = 15;	/* GDI defined as RGB 555 */
   391 	}
   392 #endif
   393 	return(depth);
   394 #else
   395     int dib_size;
   396     LPBITMAPINFOHEADER dib_hdr;
   397     HDC hdc;
   398     HBITMAP hbm;
   399 
   400     /* Allocate enough space for a DIB header plus palette (for
   401      * 8-bit modes) or bitfields (for 16- and 32-bit modes)
   402      */
   403     dib_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
   404     dib_hdr = (LPBITMAPINFOHEADER) malloc(dib_size);
   405     memset(dib_hdr, 0, dib_size);
   406     dib_hdr->biSize = sizeof(BITMAPINFOHEADER);
   407     
   408     /* Get a device-dependent bitmap that's compatible with the
   409        screen.
   410      */
   411     hdc = GetDC(NULL);
   412     hbm = CreateCompatibleBitmap( hdc, 1, 1 );
   413 
   414     /* Convert the DDB to a DIB.  We need to call GetDIBits twice:
   415      * the first call just fills in the BITMAPINFOHEADER; the 
   416      * second fills in the bitfields or palette.
   417      */
   418     GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
   419     GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
   420     DeleteObject(hbm);
   421     ReleaseDC(NULL, hdc);
   422 
   423     switch( dib_hdr->biBitCount )
   424     {
   425     case 8:     return 8;
   426     case 24:    return 24;
   427     case 32:    return 32;
   428     case 16:
   429         if( dib_hdr->biCompression == BI_BITFIELDS ) {
   430             /* check the red mask */
   431             switch( ((DWORD*)((char*)dib_hdr + dib_hdr->biSize))[0] ) {
   432                 case 0xf800: return 16;    /* 565 */
   433                 case 0x7c00: return 15;    /* 555 */
   434             }
   435         }
   436     }
   437     return 0;    /* poo. */
   438 #endif /* NO_GETDIBITS */
   439 }
   440 
   441 
   442 /* Various screen update functions available */
   443 static void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
   444 
   445 SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current,
   446 				int width, int height, int bpp, Uint32 flags)
   447 {
   448 	SDL_Surface *video;
   449 	Uint32 prev_flags;
   450 	DWORD style;
   451 	const DWORD directstyle =
   452 			(WS_POPUP);
   453 	const DWORD windowstyle = 
   454 			(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX);
   455 	const DWORD resizestyle =
   456 			(WS_THICKFRAME|WS_MAXIMIZEBOX);
   457 	int binfo_size;
   458 	BITMAPINFO *binfo;
   459 	HDC hdc;
   460 	RECT bounds;
   461 	int x, y;
   462 	BOOL was_visible;
   463 	Uint32 Rmask, Gmask, Bmask;
   464 
   465 	/* See whether or not we should center the window */
   466 	was_visible = IsWindowVisible(SDL_Window);
   467 
   468 	/* Clean up any GL context that may be hanging around */
   469 	if ( current->flags & SDL_OPENGL ) {
   470 		WIN_GL_ShutDown(this);
   471 	}
   472 
   473 	/* Recalculate the bitmasks if necessary */
   474 	if ( bpp == current->format->BitsPerPixel ) {
   475 		video = current;
   476 	} else {
   477 		switch (bpp) {
   478 			case 15:
   479 			case 16:
   480 				if ( DIB_SussScreenDepth() == 15 ) {
   481 					/* 5-5-5 */
   482 					Rmask = 0x00007c00;
   483 					Gmask = 0x000003e0;
   484 					Bmask = 0x0000001f;
   485 				} else {
   486 					/* 5-6-5 */
   487 					Rmask = 0x0000f800;
   488 					Gmask = 0x000007e0;
   489 					Bmask = 0x0000001f;
   490 				}
   491 				break;
   492 			case 24:
   493 			case 32:
   494 				/* GDI defined as 8-8-8 */
   495 				Rmask = 0x00ff0000;
   496 				Gmask = 0x0000ff00;
   497 				Bmask = 0x000000ff;
   498 				break;
   499 			default:
   500 				Rmask = 0x00000000;
   501 				Gmask = 0x00000000;
   502 				Bmask = 0x00000000;
   503 				break;
   504 		}
   505 		video = SDL_CreateRGBSurface(SDL_SWSURFACE,
   506 					0, 0, bpp, Rmask, Gmask, Bmask, 0);
   507 		if ( video == NULL ) {
   508 			SDL_OutOfMemory();
   509 			return(NULL);
   510 		}
   511 	}
   512 
   513 	/* Fill in part of the video surface */
   514 	prev_flags = video->flags;
   515 	video->flags = 0;	/* Clear flags */
   516 	video->w = width;
   517 	video->h = height;
   518 	video->pitch = SDL_CalculatePitch(video);
   519 
   520 #ifdef HAVE_AYGSHELL
   521 	 /* Stuff to hide that $#!^%#$ WinCE taskbar in fullscreen... */
   522 	if ( flags & SDL_FULLSCREEN ) {
   523 		if ( !(prev_flags & SDL_FULLSCREEN) ) {
   524 			SHFullScreen(SDL_Window, SHFS_HIDETASKBAR);
   525 			SHFullScreen(SDL_Window, SHFS_HIDESIPBUTTON);
   526 			ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_HIDE);
   527 		}
   528 		video->flags |= SDL_FULLSCREEN;
   529 	} else {
   530 		if ( prev_flags & SDL_FULLSCREEN ) {
   531 			SHFullScreen(SDL_Window, SHFS_SHOWTASKBAR);
   532 			SHFullScreen(SDL_Window, SHFS_SHOWSIPBUTTON);
   533 			ShowWindow(FindWindow(TEXT("HHTaskBar"),NULL),SW_SHOWNORMAL);
   534 		}
   535 	}
   536 #endif
   537 #ifndef NO_CHANGEDISPLAYSETTINGS
   538 	/* Set fullscreen mode if appropriate */
   539 	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   540 		DEVMODE settings;
   541 
   542 		memset(&settings, 0, sizeof(DEVMODE));
   543 		settings.dmSize = sizeof(DEVMODE);
   544 		settings.dmBitsPerPel = video->format->BitsPerPixel;
   545 		settings.dmPelsWidth = width;
   546 		settings.dmPelsHeight = height;
   547 		settings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
   548 		if ( ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL ) {
   549 			video->flags |= SDL_FULLSCREEN;
   550 			SDL_fullscreen_mode = settings;
   551 		}
   552 	}
   553 #endif /* !NO_CHANGEDISPLAYSETTINGS */
   554 
   555 	/* Reset the palette and create a new one if necessary */
   556 	if ( screen_pal != NULL ) {
   557 	/*	RJR: March 28, 2000
   558 		delete identity palette if switching from a palettized mode */
   559 		DeleteObject(screen_pal);
   560 		screen_pal = NULL;
   561 	}
   562 	if ( bpp <= 8 )
   563 	{
   564 	/*	RJR: March 28, 2000
   565 		create identity palette switching to a palettized mode */
   566 		screen_pal = DIB_CreatePalette(bpp);
   567 	}
   568 
   569 	style = GetWindowLong(SDL_Window, GWL_STYLE);
   570 	style &= ~(resizestyle|WS_MAXIMIZE);
   571 	if ( (video->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   572 		style &= ~windowstyle;
   573 		style |= directstyle;
   574 	} else {
   575 #ifndef NO_CHANGEDISPLAYSETTINGS
   576 		if ( (prev_flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   577 			ChangeDisplaySettings(NULL, 0);
   578 		}
   579 #endif
   580 		if ( flags & SDL_NOFRAME ) {
   581 			style &= ~windowstyle;
   582 			style |= directstyle;
   583 			video->flags |= SDL_NOFRAME;
   584 		} else {
   585 			style &= ~directstyle;
   586 			style |= windowstyle;
   587 			if ( flags & SDL_RESIZABLE ) {
   588 				style |= resizestyle;
   589 				video->flags |= SDL_RESIZABLE;
   590 			}
   591 		}
   592 #if WS_MAXIMIZE
   593 		if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
   594 #endif
   595 	}
   596 
   597 	/* DJM: Don't piss of anyone who has setup his own window */
   598 	if (!SDL_windowid)
   599 		SetWindowLong(SDL_Window, GWL_STYLE, style);
   600 
   601 	/* Delete the old bitmap if necessary */
   602 	if ( screen_bmp != NULL ) {
   603 		DeleteObject(screen_bmp);
   604 	}
   605 	if ( ! (flags & SDL_OPENGL) ) {
   606 		BOOL is16bitmode = (video->format->BytesPerPixel == 2);
   607 
   608 		/* Suss out the bitmap info header */
   609 		binfo_size = sizeof(*binfo);
   610 		if( is16bitmode ) {
   611 			/* 16bit modes, palette area used for rgb bitmasks */
   612 			binfo_size += 3*sizeof(DWORD);
   613 		} else if ( video->format->palette ) {
   614 			binfo_size += video->format->palette->ncolors *
   615 							sizeof(RGBQUAD);
   616 		}
   617 		binfo = (BITMAPINFO *)malloc(binfo_size);
   618 		if ( ! binfo ) {
   619 			if ( video != current ) {
   620 				SDL_FreeSurface(video);
   621 			}
   622 			SDL_OutOfMemory();
   623 			return(NULL);
   624 		}
   625 
   626 		binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
   627 		binfo->bmiHeader.biWidth = video->w;
   628 		binfo->bmiHeader.biHeight = -video->h;	/* -ve for topdown bitmap */
   629 		binfo->bmiHeader.biPlanes = 1;
   630 		binfo->bmiHeader.biSizeImage = video->h * video->pitch;
   631 		binfo->bmiHeader.biXPelsPerMeter = 0;
   632 		binfo->bmiHeader.biYPelsPerMeter = 0;
   633 		binfo->bmiHeader.biClrUsed = 0;
   634 		binfo->bmiHeader.biClrImportant = 0;
   635 		binfo->bmiHeader.biBitCount = video->format->BitsPerPixel;
   636 
   637 		if ( is16bitmode ) {
   638 			/* BI_BITFIELDS tells CreateDIBSection about the rgb masks in the palette */
   639 			binfo->bmiHeader.biCompression = BI_BITFIELDS;
   640 			((Uint32*)binfo->bmiColors)[0] = video->format->Rmask;
   641 			((Uint32*)binfo->bmiColors)[1] = video->format->Gmask;
   642 			((Uint32*)binfo->bmiColors)[2] = video->format->Bmask;
   643 		} else {
   644 			binfo->bmiHeader.biCompression = BI_RGB;	/* BI_BITFIELDS for 565 vs 555 */
   645 			if ( video->format->palette ) {
   646 				memset(binfo->bmiColors, 0,
   647 					video->format->palette->ncolors*sizeof(RGBQUAD));
   648 			}
   649 		}
   650 
   651 		/* Create the offscreen bitmap buffer */
   652 		hdc = GetDC(SDL_Window);
   653 		screen_bmp = CreateDIBSection(hdc, binfo, DIB_RGB_COLORS,
   654 					(void **)(&video->pixels), NULL, 0);
   655 		ReleaseDC(SDL_Window, hdc);
   656 		free(binfo);
   657 		if ( screen_bmp == NULL ) {
   658 			if ( video != current ) {
   659 				SDL_FreeSurface(video);
   660 			}
   661 			SDL_SetError("Couldn't create DIB section");
   662 			return(NULL);
   663 		}
   664 		this->UpdateRects = DIB_NormalUpdate;
   665 
   666 		/* Set video surface flags */
   667 		if ( bpp <= 8 ) {
   668 			/* BitBlt() maps colors for us */
   669 			video->flags |= SDL_HWPALETTE;
   670 		}
   671 	}
   672 
   673 	/* Resize the window */
   674 	if ( SDL_windowid == NULL ) {
   675 		HWND top;
   676 		UINT swp_flags;
   677 
   678 		SDL_resizing = 1;
   679 		bounds.left = 0;
   680 		bounds.top = 0;
   681 		bounds.right = video->w;
   682 		bounds.bottom = video->h;
   683 		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
   684 		width = bounds.right-bounds.left;
   685 		height = bounds.bottom-bounds.top;
   686 		x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   687 		y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   688 		if ( y < 0 ) { /* Cover up title bar for more client area */
   689 			y -= GetSystemMetrics(SM_CYCAPTION)/2;
   690 		}
   691 		swp_flags = (SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
   692 		if ( was_visible && !(flags & SDL_FULLSCREEN) ) {
   693 			swp_flags |= SWP_NOMOVE;
   694 		}
   695 		if ( flags & SDL_FULLSCREEN ) {
   696 			top = HWND_TOPMOST;
   697 		} else {
   698 			top = HWND_NOTOPMOST;
   699 		}
   700 		SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags);
   701 		SDL_resizing = 0;
   702 		SetForegroundWindow(SDL_Window);
   703 	}
   704 
   705 	/* Set up for OpenGL */
   706 	if ( flags & SDL_OPENGL ) {
   707 		if ( WIN_GL_SetupWindow(this) < 0 ) {
   708 			return(NULL);
   709 		}
   710 		video->flags |= SDL_OPENGL;
   711 	}
   712 
   713 	/* We're live! */
   714 	return(video);
   715 }
   716 
   717 /* We don't actually allow hardware surfaces in the DIB driver */
   718 static int DIB_AllocHWSurface(_THIS, SDL_Surface *surface)
   719 {
   720 	return(-1);
   721 }
   722 static void DIB_FreeHWSurface(_THIS, SDL_Surface *surface)
   723 {
   724 	return;
   725 }
   726 static int DIB_LockHWSurface(_THIS, SDL_Surface *surface)
   727 {
   728 	return(0);
   729 }
   730 static void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface)
   731 {
   732 	return;
   733 }
   734 
   735 static void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
   736 {
   737 	HDC hdc, mdc;
   738 	int i;
   739 
   740 	hdc = GetDC(SDL_Window);
   741 	if ( screen_pal ) {
   742 		SelectPalette(hdc, screen_pal, FALSE);
   743 	}
   744 	mdc = CreateCompatibleDC(hdc);
   745 	SelectObject(mdc, screen_bmp);
   746 	for ( i=0; i<numrects; ++i ) {
   747 		BitBlt(hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
   748 					mdc, rects[i].x, rects[i].y, SRCCOPY);
   749 	}
   750 	DeleteDC(mdc);
   751 	ReleaseDC(SDL_Window, hdc);
   752 }
   753 
   754 int DIB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   755 {
   756 	RGBQUAD *pal;
   757 	int i;
   758 #ifndef _WIN32_WCE
   759 	HDC hdc, mdc;
   760 #else
   761 	HDC hdc;
   762 #endif
   763 
   764 	/* Update the display palette */
   765 	hdc = GetDC(SDL_Window);
   766 	if ( screen_pal ) {
   767 		PALETTEENTRY *entries;
   768 
   769 		entries = (PALETTEENTRY *)alloca(ncolors*sizeof(PALETTEENTRY));
   770 		for ( i=0; i<ncolors; ++i ) {
   771 			entries[i].peRed   = colors[i].r;
   772 			entries[i].peGreen = colors[i].g;
   773 			entries[i].peBlue  = colors[i].b;
   774 			entries[i].peFlags = PC_NOCOLLAPSE;
   775 		}
   776 		SetPaletteEntries(screen_pal, firstcolor, ncolors, entries);
   777 		SelectPalette(hdc, screen_pal, FALSE);
   778 		RealizePalette(hdc);
   779 	}
   780 
   781 	/* Copy palette colors into DIB palette */
   782 	pal = (RGBQUAD *)alloca(ncolors*sizeof(RGBQUAD));
   783 	for ( i=0; i<ncolors; ++i ) {
   784 		pal[i].rgbRed = colors[i].r;
   785 		pal[i].rgbGreen = colors[i].g;
   786 		pal[i].rgbBlue = colors[i].b;
   787 		pal[i].rgbReserved = 0;
   788 	}
   789 
   790 	/* Set the DIB palette and update the display */
   791 #ifndef _WIN32_WCE
   792 	mdc = CreateCompatibleDC(hdc);
   793 	SelectObject(mdc, screen_bmp);
   794 	SetDIBColorTable(mdc, firstcolor, ncolors, pal);
   795 	BitBlt(hdc, 0, 0, this->screen->w, this->screen->h,
   796 	       mdc, 0, 0, SRCCOPY);
   797 	DeleteDC(mdc);
   798 #endif
   799 	ReleaseDC(SDL_Window, hdc);
   800 	return(1);
   801 }
   802 
   803 static void DIB_CheckGamma(_THIS)
   804 {
   805 #ifndef NO_GAMMA_SUPPORT
   806 	HDC hdc;
   807 	WORD ramp[3*256];
   808 
   809 	/* If we fail to get gamma, disable gamma control */
   810 	hdc = GetDC(SDL_Window);
   811 	if ( ! GetDeviceGammaRamp(hdc, ramp) ) {
   812 		this->GetGammaRamp = NULL;
   813 		this->SetGammaRamp = NULL;
   814 	}
   815 	ReleaseDC(SDL_Window, hdc);
   816 #endif /* !NO_GAMMA_SUPPORT */
   817 }
   818 void DIB_SwapGamma(_THIS)
   819 {
   820 #ifndef NO_GAMMA_SUPPORT
   821 	HDC hdc;
   822 
   823 	if ( gamma_saved ) {
   824 		hdc = GetDC(SDL_Window);
   825 		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   826 			/* About to leave active state, restore gamma */
   827 			SetDeviceGammaRamp(hdc, gamma_saved);
   828 		} else {
   829 			/* About to enter active state, set game gamma */
   830 			GetDeviceGammaRamp(hdc, gamma_saved);
   831 			SetDeviceGammaRamp(hdc, this->gamma);
   832 		}
   833 		ReleaseDC(SDL_Window, hdc);
   834 	}
   835 #endif /* !NO_GAMMA_SUPPORT */
   836 }
   837 void DIB_QuitGamma(_THIS)
   838 {
   839 #ifndef NO_GAMMA_SUPPORT
   840 	if ( gamma_saved ) {
   841 		/* Restore the original gamma if necessary */
   842 		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   843 			HDC hdc;
   844 
   845 			hdc = GetDC(SDL_Window);
   846 			SetDeviceGammaRamp(hdc, gamma_saved);
   847 			ReleaseDC(SDL_Window, hdc);
   848 		}
   849 
   850 		/* Free the saved gamma memory */
   851 		free(gamma_saved);
   852 		gamma_saved = 0;
   853 	}
   854 #endif /* !NO_GAMMA_SUPPORT */
   855 }
   856 
   857 int DIB_SetGammaRamp(_THIS, Uint16 *ramp)
   858 {
   859 #ifdef NO_GAMMA_SUPPORT
   860 	SDL_SetError("SDL compiled without gamma ramp support");
   861 	return -1;
   862 #else
   863 	HDC hdc;
   864 	BOOL succeeded;
   865 
   866 	/* Set the ramp for the display */
   867 	if ( ! gamma_saved ) {
   868 		gamma_saved = (WORD *)malloc(3*256*sizeof(*gamma_saved));
   869 		if ( ! gamma_saved ) {
   870 			SDL_OutOfMemory();
   871 			return -1;
   872 		}
   873 		hdc = GetDC(SDL_Window);
   874 		GetDeviceGammaRamp(hdc, gamma_saved);
   875 		ReleaseDC(SDL_Window, hdc);
   876 	}
   877 	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   878 		hdc = GetDC(SDL_Window);
   879 		succeeded = SetDeviceGammaRamp(hdc, ramp);
   880 		ReleaseDC(SDL_Window, hdc);
   881 	} else {
   882 		succeeded = TRUE;
   883 	}
   884 	return succeeded ? 0 : -1;
   885 #endif /* !NO_GAMMA_SUPPORT */
   886 }
   887 
   888 int DIB_GetGammaRamp(_THIS, Uint16 *ramp)
   889 {
   890 #ifdef NO_GAMMA_SUPPORT
   891 	SDL_SetError("SDL compiled without gamma ramp support");
   892 	return -1;
   893 #else
   894 	HDC hdc;
   895 	BOOL succeeded;
   896 
   897 	/* Get the ramp from the display */
   898 	hdc = GetDC(SDL_Window);
   899 	succeeded = GetDeviceGammaRamp(hdc, ramp);
   900 	ReleaseDC(SDL_Window, hdc);
   901 	return succeeded ? 0 : -1;
   902 #endif /* !NO_GAMMA_SUPPORT */
   903 }
   904 
   905 static void FlushMessageQueue()
   906 {
   907 	MSG  msg;
   908 	while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) {
   909 		if ( msg.message == WM_QUIT ) break;
   910 		TranslateMessage( &msg );
   911 		DispatchMessage( &msg );
   912 	}
   913 }
   914 
   915 void DIB_VideoQuit(_THIS)
   916 {
   917 	/* Destroy the window and everything associated with it */
   918 	if ( SDL_Window ) {
   919 		/* Delete the screen bitmap (also frees screen->pixels) */
   920 		if ( this->screen ) {
   921 #ifndef NO_CHANGEDISPLAYSETTINGS
   922 			if ( this->screen->flags & SDL_FULLSCREEN ) {
   923 				ChangeDisplaySettings(NULL, 0);
   924 				ShowWindow(SDL_Window, SW_HIDE);
   925 			}
   926 #endif
   927 			if ( this->screen->flags & SDL_OPENGL ) {
   928 				WIN_GL_ShutDown(this);
   929 			}
   930 			this->screen->pixels = NULL;
   931 		}
   932 		if ( screen_bmp ) {
   933 			DeleteObject(screen_bmp);
   934 			screen_bmp = NULL;
   935 		}
   936 		if ( screen_icn ) {
   937 			DestroyIcon(screen_icn);
   938 			screen_icn = NULL;
   939 		}
   940 		DIB_QuitGamma(this);
   941 		DIB_DestroyWindow(this);
   942 		FlushMessageQueue();
   943 
   944 		SDL_Window = NULL;
   945 	}
   946 }
   947 
   948 /* Exported for the windows message loop only */
   949 static void DIB_FocusPalette(_THIS, int foreground)
   950 {
   951 	if ( screen_pal != NULL ) {
   952 		HDC hdc;
   953 
   954 		hdc = GetDC(SDL_Window);
   955 		SelectPalette(hdc, screen_pal, FALSE);
   956 		if ( RealizePalette(hdc) )
   957 			InvalidateRect(SDL_Window, NULL, FALSE);
   958 		ReleaseDC(SDL_Window, hdc);
   959 	}
   960 }
   961 static void DIB_RealizePalette(_THIS)
   962 {
   963 	DIB_FocusPalette(this, 1);
   964 }
   965 static void DIB_PaletteChanged(_THIS, HWND window)
   966 {
   967 	if ( window != SDL_Window ) {
   968 		DIB_FocusPalette(this, 0);
   969 	}
   970 }
   971 
   972 /* Exported for the windows message loop only */
   973 static void DIB_WinPAINT(_THIS, HDC hdc)
   974 {
   975 	HDC mdc;
   976 
   977 	if ( screen_pal ) {
   978 		SelectPalette(hdc, screen_pal, FALSE);
   979 	}
   980 	mdc = CreateCompatibleDC(hdc);
   981 	SelectObject(mdc, screen_bmp);
   982 	BitBlt(hdc, 0, 0, SDL_VideoSurface->w, SDL_VideoSurface->h,
   983 							mdc, 0, 0, SRCCOPY);
   984 	DeleteDC(mdc);
   985 }
   986 
   987 /* Stub in case DirectX isn't available */
   988 #ifndef ENABLE_DIRECTX
   989 void DX5_SoundFocus(HWND hwnd)
   990 {
   991 	return;
   992 }
   993 #endif