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