src/video/windib/SDL_dibvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 15:48:44 +0000
changeset 766 ed57c876700d
parent 515 230b156829ed
child 833 31fa08b36380
permissions -rw-r--r--
Date: Wed, 26 Nov 2003 01:52:02 +0800
From: "Andy Pfiffer"
Subject: [SDL] patch: PocketPC 2000 diffs for release_1_2_6

I was making a backup of my current workspace of SDL for PocketPC,
and thought I would send out my diffs.

Apologies in advance if the patch has become whitespace mangled.

These diffs are relative to CVS tag release_1_2_6, and contain (I think)
all changes that have been mentioned on the mailing list in the last
few months.

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