19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
247891dd35fa2439a70f43ab8572778a398365bf24David 'Digit' Turner#define WIN32_LEAN_AND_MEAN 1
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Not yet in the mingw32 cross-compile headers */
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef CDS_FULLSCREEN
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CDS_FULLSCREEN	4
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gapidibvideo.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dibvideo.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_syswm_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_sysmouse_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dibevents_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../wincommon/SDL_wingl_c.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DM_DISPLAYORIENTATION
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DM_DISPLAYORIENTATION 0x00800000L
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DM_DISPLAYQUERYORIENTATION
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DM_DISPLAYQUERYORIENTATION 0x01000000L
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DMDO_0
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DMDO_0      0
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DMDO_90
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DMDO_90     1
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DMDO_180
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DMDO_180    2
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef DMDO_270
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DMDO_270    4
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NO_GETDIBITS
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NO_GAMMA_SUPPORT
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  #if _WIN32_WCE < 420
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #define NO_CHANGEDISPLAYSETTINGS
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  #else
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #define ChangeDisplaySettings(lpDevMode, dwFlags) ChangeDisplaySettingsEx(NULL, (lpDevMode), 0, (dwFlags), 0)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  #endif
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef WS_MAXIMIZE
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WS_MAXIMIZE	0
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef WS_THICKFRAME
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WS_THICKFRAME	0
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef SWP_NOCOPYBITS
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SWP_NOCOPYBITS	0
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef PC_NOCOLLAPSE
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PC_NOCOLLAPSE	0
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// defined and used in SDL_sysevents.c
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern HINSTANCE aygshell;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_SetColors(_THIS, int firstcolor, int ncolors,
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 SDL_Color *colors);
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_CheckGamma(_THIS);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_SwapGamma(_THIS);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_QuitGamma(_THIS);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_SetGammaRamp(_THIS, Uint16 *ramp);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_GetGammaRamp(_THIS, Uint16 *ramp);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_VideoQuit(_THIS);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_AllocHWSurface(_THIS, SDL_Surface *surface);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_LockHWSurface(_THIS, SDL_Surface *surface);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_FreeHWSurface(_THIS, SDL_Surface *surface);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Windows message handling functions */
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_GrabStaticColors(HWND window);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_ReleaseStaticColors(HWND window);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_Activate(_THIS, BOOL active, BOOL minimized);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_RealizePalette(_THIS);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_PaletteChanged(_THIS, HWND window);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_WinPAINT(_THIS, HDC hdc);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
118e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic void DIB_GetWinPos(_THIS, int* px, int *py);
119e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic void DIB_SetWinPos(_THIS, int  x, int  y);
120e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_IsWinVisible(_THIS, int recenter);
121e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_GetMonitorDPI(_THIS, int* xDpi, int *yDpi);
122e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_GetMonitorRect(_THIS, SDL_Rect*  rect);
123e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* helper fn */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_SussScreenDepth();
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DIB driver bootstrap functions */
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_Available(void)
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_DeleteDevice(SDL_VideoDevice *device)
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device->hidden ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( device->hidden->dibInfo ) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free( device->hidden->dibInfo );
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device->hidden);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device->gl_data ) {
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device->gl_data);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(device);
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *DIB_CreateDevice(int devindex)
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->hidden));
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(device->hidden){
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset(device->hidden, 0, (sizeof *device->hidden));
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			device->hidden->dibInfo = (DibInfo *)SDL_malloc((sizeof(DibInfo)));
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(device->hidden->dibInfo == NULL)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(device->hidden);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				device->hidden = NULL;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->gl_data = (struct SDL_PrivateGLData *)
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *device->gl_data));
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ||
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                 (device->gl_data == NULL) ) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_DeleteDevice(device);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden->dibInfo, 0, (sizeof *device->hidden->dibInfo));
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = DIB_VideoInit;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = DIB_ListModes;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = DIB_SetVideoMode;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateMouse = WIN_UpdateMouse;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = DIB_SetColors;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = NULL;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = DIB_VideoQuit;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = DIB_AllocHWSurface;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = NULL;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = DIB_LockHWSurface;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = DIB_UnlockHWSurface;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = NULL;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = DIB_FreeHWSurface;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetGammaRamp = DIB_SetGammaRamp;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetGammaRamp = DIB_GetGammaRamp;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_OPENGL
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_LoadLibrary = WIN_GL_LoadLibrary;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_GetProcAddress = WIN_GL_GetProcAddress;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_GetAttribute = WIN_GL_GetAttribute;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_MakeCurrent = WIN_GL_MakeCurrent;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GL_SwapBuffers = WIN_GL_SwapBuffers;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = WIN_SetWMCaption;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = WIN_SetWMIcon;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->IconifyWindow = WIN_IconifyWindow;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GrabInput = WIN_GrabInput;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = WIN_GetWMInfo;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeWMCursor = WIN_FreeWMCursor;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateWMCursor = WIN_CreateWMCursor;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ShowWMCursor = WIN_ShowWMCursor;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->WarpWMCursor = WIN_WarpWMCursor;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckMouseMode = WIN_CheckMouseMode;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = DIB_InitOSKeymap;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = DIB_PumpEvents;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
221e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	device->GetWindowPos = DIB_GetWinPos;
222e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	device->SetWindowPos = DIB_SetWinPos;
223e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	device->IsWindowVisible = DIB_IsWinVisible;
224e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	device->GetMonitorDPI = DIB_GetMonitorDPI;
225e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	device->GetMonitorRect = DIB_GetMonitorRect;
226e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the windows message handling functions */
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_Activate = DIB_Activate;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_RealizePalette = DIB_RealizePalette;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_PaletteChanged = DIB_PaletteChanged;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_WinPAINT = DIB_WinPAINT;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HandleMessage = DIB_HandleMessage;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = DIB_DeleteDevice;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're finally ready */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap WINDIB_bootstrap = {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"windib", "Win95/98/NT/2000/CE GDI",
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DIB_Available, DIB_CreateDevice
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int cmpmodes(const void *va, const void *vb)
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Rect *a = *(SDL_Rect **)va;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Rect *b = *(SDL_Rect **)vb;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( a->w == b->w )
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->h - a->h;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->w - a->w;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_AddMode(_THIS, int bpp, int w, int h)
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect *mode;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, index;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int next_mode;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if we already have this mode */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( bpp < 8 || bpp > 32 ) {  /* Not supported */
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	index = ((bpp+7)/8)-1;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_nummodes[index]; ++i ) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mode = SDL_modelist[index][i];
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (mode->w == w) && (mode->h == h) ) {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(0);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new video mode rectangle */
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mode == NULL ) {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->x = 0;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->y = 0;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->w = w;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->h = h;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new list of modes, and fill in the new mode */
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_mode = SDL_nummodes[index];
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index] = (SDL_Rect **)
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_modelist[index] == NULL ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_nummodes[index] = 0;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mode);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode] = mode;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode+1] = NULL;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_nummodes[index]++;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_CreatePalette(_THIS, int bpp)
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*	RJR: March 28, 2000
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	moved palette creation here from "DIB_VideoInit" */
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LOGPALETTE *palette;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ncolors;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ncolors = (1 << bpp);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	palette = (LOGPALETTE *)SDL_malloc(sizeof(*palette)+
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ncolors*sizeof(PALETTEENTRY));
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	palette->palVersion = 0x300;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	palette->palNumEntries = ncolors;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GetSystemPaletteEntries(hdc, 0, ncolors, palette->palPalEntry);
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen_pal = CreatePalette(palette);
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen_logpal = palette;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_VideoInit(_THIS, SDL_PixelFormat *vformat)
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *env = NULL;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_CHANGEDISPLAYSETTINGS
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEVMODE settings;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the window */
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DIB_CreateWindow(this) < 0 ) {
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !SDL_AUDIO_DISABLED
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DX5_SoundFocus(SDL_Window);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the screen depth */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = DIB_SussScreenDepth();
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (vformat->BitsPerPixel) {
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 15:
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Rmask = 0x00007c00;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Gmask = 0x000003e0;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Bmask = 0x0000001f;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->BitsPerPixel = 16;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 16:
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Rmask = 0x0000f800;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Gmask = 0x000007e0;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Bmask = 0x0000001f;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 24:
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 32:
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* GDI defined as 8-8-8 */
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Rmask = 0x00ff0000;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Gmask = 0x0000ff00;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vformat->Bmask = 0x000000ff;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* See if gamma is supported on this screen */
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DIB_CheckGamma(this);
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_CHANGEDISPLAYSETTINGS
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	settings.dmSize = sizeof(DEVMODE);
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	settings.dmDriverExtra = 0;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	settings.dmFields = DM_DISPLAYQUERYORIENTATION;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->supportRotation = ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL) == DISP_CHANGE_SUCCESSFUL;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Query for the desktop resolution */
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_desktop_mode.dmSize = sizeof(SDL_desktop_mode);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_desktop_mode.dmDriverExtra = 0;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &SDL_desktop_mode);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_w = SDL_desktop_mode.dmPelsWidth;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_h = SDL_desktop_mode.dmPelsHeight;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Query for the list of available video modes */
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; EnumDisplaySettings(NULL, i, &settings); ++i ) {
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_AddMode(this, settings.dmBitsPerPel,
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			settings.dmPelsWidth, settings.dmPelsHeight);
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( this->hidden->supportRotation )
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIB_AddMode(this, settings.dmBitsPerPel,
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				settings.dmPelsHeight, settings.dmPelsWidth);
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Sort the mode lists */
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_MODELISTS; ++i ) {
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_nummodes[i] > 0 ) {
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_qsort(SDL_modelist[i], SDL_nummodes[i], sizeof *SDL_modelist[i], cmpmodes);
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// WinCE and fullscreen mode:
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// We use only vformat->BitsPerPixel that allow SDL to
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// emulate other bpp (8, 32) and use triple buffer,
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// because SDL surface conversion is much faster than the WinCE one.
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Although it should be tested on devices with graphics accelerator.
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DIB_AddMode(this, vformat->BitsPerPixel,
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GetDeviceCaps(GetDC(NULL), HORZRES),
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GetDeviceCaps(GetDC(NULL), VERTRES));
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_CHANGEDISPLAYSETTINGS */
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Grab an identity palette if we are in a palettized mode */
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( vformat->BitsPerPixel <= 8 ) {
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*	RJR: March 28, 2000
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		moved palette creation to "DIB_CreatePalette" */
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_CreatePalette(this, vformat->BitsPerPixel);
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in some window manager capabilities */
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.wm_available = 1;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->origRotation = -1;
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allow environment override of screensaver disable. */
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( env ) {
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		allow_screensaver = SDL_atoi(env);
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		allow_screensaver = 0;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		allow_screensaver = 1;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We support any format at any dimension */
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return((SDL_Rect **)-1);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Helper fn to work out which screen depth windows is currently using.
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  15 bit mode is considered 555 format, 16 bit is 565.
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  returns 0 for unknown mode.
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  (Derived from code in sept 1999 Windows Developer Journal
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  http://www.wdj.com/code/archive.html)
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_SussScreenDepth()
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef NO_GETDIBITS
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int depth;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(depth);
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int depth;
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int dib_size;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    LPBITMAPINFOHEADER dib_hdr;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HDC hdc;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HBITMAP hbm;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allocate enough space for a DIB header plus palette (for
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * 8-bit modes) or bitfields (for 16- and 32-bit modes)
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dib_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dib_hdr = (LPBITMAPINFOHEADER) SDL_malloc(dib_size);
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(dib_hdr, 0, dib_size);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dib_hdr->biSize = sizeof(BITMAPINFOHEADER);
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get a device-dependent bitmap that's compatible with the
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       screen.
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hdc = GetDC(NULL);
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hbm = CreateCompatibleBitmap( hdc, 1, 1 );
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Convert the DDB to a DIB.  We need to call GetDIBits twice:
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * the first call just fills in the BITMAPINFOHEADER; the
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * second fills in the bitfields or palette.
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DeleteObject(hbm);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ReleaseDC(NULL, hdc);
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    depth = 0;
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch( dib_hdr->biBitCount )
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 8:     depth = 8; break;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 24:    depth = 24; break;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 32:    depth = 32; break;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 16:
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if( dib_hdr->biCompression == BI_BITFIELDS ) {
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* check the red mask */
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            switch( ((DWORD*)((char*)dib_hdr + dib_hdr->biSize))[0] ) {
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case 0xf800: depth = 16; break;   /* 565 */
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case 0x7c00: depth = 15; break;   /* 555 */
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(dib_hdr);
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return depth;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* NO_GETDIBITS */
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_ResizeWindow(_THIS, int width, int height, int prev_width, int prev_height, Uint32 flags)
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	RECT bounds;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x, y;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Resize the window */
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !SDL_windowid && !IsZoomed(SDL_Window) ) {
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !SDL_windowid ) {
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HWND top;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UINT swp_flags;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *window = NULL;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *center = NULL;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( width != prev_width || height != prev_height ) {
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			center = SDL_getenv("SDL_VIDEO_CENTERED");
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( window ) {
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) {
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_windowX = x;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_windowY = y;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( SDL_strcmp(window, "center") == 0 ) {
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					center = window;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.left = SDL_windowX;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.top = SDL_windowY;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.right = SDL_windowX+width;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bounds.bottom = SDL_windowY+height;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), (GetMenu(SDL_Window) != NULL), 0);
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// The bMenu parameter must be FALSE; menu bars are not supported
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), 0, 0);
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		width = bounds.right-bounds.left;
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		height = bounds.bottom-bounds.top;
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (flags & SDL_FULLSCREEN) ) {
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( center ) {
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( SDL_windowX || SDL_windowY || window ) {
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = bounds.left;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			y = bounds.top;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = y = -1;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			swp_flags |= SWP_NOMOVE;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flags & SDL_FULLSCREEN ) {
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			top = HWND_TOPMOST;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			top = HWND_NOTOPMOST;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags);
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !(flags & SDL_FULLSCREEN) ) {
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_windowX = SDL_bounds.left;
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_windowY = SDL_bounds.top;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( GetParent(SDL_Window) == NULL ) {
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetForegroundWindow(SDL_Window);
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current,
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *video;
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int prev_w, prev_h;
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 prev_flags;
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD style;
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const DWORD directstyle =
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(WS_POPUP);
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const DWORD windowstyle =
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const DWORD resizestyle =
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(WS_THICKFRAME|WS_MAXIMIZEBOX);
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int binfo_size;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BITMAPINFO *binfo;
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 Rmask, Gmask, Bmask;
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prev_w = current->w;
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prev_h = current->h;
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prev_flags = current->flags;
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Special case for OpenGL windows...since the app needs to call
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  SDL_SetVideoMode() in response to resize events to continue to
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  function, but WGL handles the GL context details behind the scenes,
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  there's no sense in tearing the context down just to rebuild it
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  to what it already was...tearing it down sacrifices your GL state
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  and uploaded textures. So if we're requesting the same video mode
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  attributes just resize the window and return immediately.
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Window &&
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     ((current->flags & ~SDL_ANYFORMAT) == (flags & ~SDL_ANYFORMAT)) &&
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (current->format->BitsPerPixel == bpp) &&
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (flags & SDL_OPENGL) &&
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     !(flags & SDL_FULLSCREEN) ) {  /* probably not safe for fs */
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->w = width;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->h = height;
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_resizing = 1;
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_ResizeWindow(this, width, height, prev_w, prev_h, flags);
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_resizing = 0;
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return current;
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up any GL context that may be hanging around */
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( current->flags & SDL_OPENGL ) {
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		WIN_GL_ShutDown(this);
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_resizing = 1;
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Recalculate the bitmasks if necessary */
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( bpp == current->format->BitsPerPixel ) {
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video = current;
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (bpp) {
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 15:
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 16:
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( DIB_SussScreenDepth() == 15 ) {
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* 5-5-5 */
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rmask = 0x00007c00;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gmask = 0x000003e0;
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bmask = 0x0000001f;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* 5-6-5 */
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rmask = 0x0000f800;
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gmask = 0x000007e0;
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bmask = 0x0000001f;
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 24:
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 32:
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* GDI defined as 8-8-8 */
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rmask = 0x00ff0000;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Gmask = 0x0000ff00;
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Bmask = 0x000000ff;
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rmask = 0x00000000;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Gmask = 0x00000000;
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Bmask = 0x00000000;
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video = SDL_CreateRGBSurface(SDL_SWSURFACE,
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					0, 0, bpp, Rmask, Gmask, Bmask, 0);
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( video == NULL ) {
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in part of the video surface */
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->flags = 0;	/* Clear flags */
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->w = width;
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->h = height;
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	video->pitch = SDL_CalculatePitch(video);
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Small fix for WinCE/Win32 - when activating window
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   SDL_VideoSurface is equal to zero, so activating code
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   is not called properly for fullscreen windows because
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   macros WINDIB_FULLSCREEN uses SDL_VideoSurface
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoSurface = video;
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE)
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flags & SDL_FULLSCREEN )
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video->flags |= SDL_FULLSCREEN;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_CHANGEDISPLAYSETTINGS
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set fullscreen mode if appropriate */
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DEVMODE settings;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BOOL changed;
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(&settings, 0, sizeof(DEVMODE));
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		settings.dmSize = sizeof(DEVMODE);
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		// try to rotate screen to fit requested resolution
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( this->hidden->supportRotation )
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DWORD rotation;
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			// ask current mode
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			settings.dmFields = DM_DISPLAYORIENTATION;
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL);
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			rotation = settings.dmDisplayOrientation;
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( (width > GetDeviceCaps(GetDC(NULL), HORZRES))
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				&& (height < GetDeviceCaps(GetDC(NULL), VERTRES)))
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch( rotation )
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case DMDO_0:
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					settings.dmDisplayOrientation = DMDO_90;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case DMDO_270:
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					settings.dmDisplayOrientation = DMDO_180;
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if( settings.dmDisplayOrientation != rotation )
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					// go to landscape
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					this->hidden->origRotation = rotation;
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ChangeDisplaySettingsEx(NULL,&settings,NULL,CDS_RESET,NULL);
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if( (width < GetDeviceCaps(GetDC(NULL), HORZRES))
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				&& (height > GetDeviceCaps(GetDC(NULL), VERTRES)))
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch( rotation )
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case DMDO_90:
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					settings.dmDisplayOrientation = DMDO_0;
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case DMDO_180:
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					settings.dmDisplayOrientation = DMDO_270;
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if( settings.dmDisplayOrientation != rotation )
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					// go to portrait
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					this->hidden->origRotation = rotation;
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					ChangeDisplaySettingsEx(NULL,&settings,NULL,CDS_RESET,NULL);
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		settings.dmBitsPerPel = video->format->BitsPerPixel;
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		settings.dmPelsWidth = width;
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		settings.dmPelsHeight = height;
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		settings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( width <= (int)SDL_desktop_mode.dmPelsWidth &&
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     height <= (int)SDL_desktop_mode.dmPelsHeight ) {
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			settings.dmDisplayFrequency = SDL_desktop_mode.dmDisplayFrequency;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			settings.dmFields |= DM_DISPLAYFREQUENCY;
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! changed && (settings.dmFields & DM_DISPLAYFREQUENCY) ) {
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			settings.dmFields &= ~DM_DISPLAYFREQUENCY;
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		changed = 1;
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( changed ) {
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			video->flags |= SDL_FULLSCREEN;
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_fullscreen_mode = settings;
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_CHANGEDISPLAYSETTINGS */
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Reset the palette and create a new one if necessary */
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( grab_palette ) {
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_ReleaseStaticColors(SDL_Window);
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		grab_palette = FALSE;
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_pal != NULL ) {
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*	RJR: March 28, 2000
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		delete identity palette if switching from a palettized mode */
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DeleteObject(screen_pal);
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_pal = NULL;
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_logpal != NULL ) {
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(screen_logpal);
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_logpal = NULL;
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( bpp <= 8 )
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*	RJR: March 28, 2000
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		create identity palette switching to a palettized mode */
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_CreatePalette(this, bpp);
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	style = GetWindowLong(SDL_Window, GWL_STYLE);
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	style &= ~(resizestyle|WS_MAXIMIZE);
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (video->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		style &= ~windowstyle;
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		style |= directstyle;
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_CHANGEDISPLAYSETTINGS
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (prev_flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ChangeDisplaySettings(NULL, 0);
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flags & SDL_NOFRAME ) {
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			style &= ~windowstyle;
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			style |= directstyle;
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			video->flags |= SDL_NOFRAME;
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			style &= ~directstyle;
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			style |= windowstyle;
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( flags & SDL_RESIZABLE ) {
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				style |= resizestyle;
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				video->flags |= SDL_RESIZABLE;
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if WS_MAXIMIZE && !defined(_WIN32_WCE)
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* DJM: Don't piss of anyone who has setup his own window */
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !SDL_windowid )
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWindowLong(SDL_Window, GWL_STYLE, style);
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Delete the old bitmap if necessary */
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_bmp != NULL ) {
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DeleteObject(screen_bmp);
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! (flags & SDL_OPENGL) ) {
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BOOL is16bitmode = (video->format->BytesPerPixel == 2);
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Suss out the bitmap info header */
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo_size = sizeof(*binfo);
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( is16bitmode ) {
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* 16bit modes, palette area used for rgb bitmasks */
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			binfo_size += 3*sizeof(DWORD);
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( video->format->palette ) {
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			binfo_size += video->format->palette->ncolors *
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							sizeof(RGBQUAD);
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo = (BITMAPINFO *)SDL_malloc(binfo_size);
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! binfo ) {
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( video != current ) {
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_FreeSurface(video);
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biWidth = video->w;
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biHeight = -video->h;	/* -ve for topdown bitmap */
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biPlanes = 1;
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biSizeImage = video->h * video->pitch;
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biXPelsPerMeter = 0;
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biYPelsPerMeter = 0;
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biClrUsed = 0;
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biClrImportant = 0;
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		binfo->bmiHeader.biBitCount = video->format->BitsPerPixel;
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( is16bitmode ) {
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* BI_BITFIELDS tells CreateDIBSection about the rgb masks in the palette */
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			binfo->bmiHeader.biCompression = BI_BITFIELDS;
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			((Uint32*)binfo->bmiColors)[0] = video->format->Rmask;
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			((Uint32*)binfo->bmiColors)[1] = video->format->Gmask;
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			((Uint32*)binfo->bmiColors)[2] = video->format->Bmask;
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			binfo->bmiHeader.biCompression = BI_RGB;	/* BI_BITFIELDS for 565 vs 555 */
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( video->format->palette ) {
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_memset(binfo->bmiColors, 0,
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					video->format->palette->ncolors*sizeof(RGBQUAD));
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Create the offscreen bitmap buffer */
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hdc = GetDC(SDL_Window);
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_bmp = CreateDIBSection(hdc, binfo, DIB_RGB_COLORS,
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					(void **)(&video->pixels), NULL, 0);
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseDC(SDL_Window, hdc);
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(binfo);
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_bmp == NULL ) {
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( video != current ) {
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_FreeSurface(video);
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't create DIB section");
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->UpdateRects = DIB_NormalUpdate;
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set video surface flags */
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_pal && (flags & (SDL_FULLSCREEN|SDL_HWPALETTE)) ) {
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			grab_palette = TRUE;
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_pal ) {
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* BitBlt() maps colors for us */
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			video->flags |= SDL_HWPALETTE;
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DIB_ResizeWindow(this, width, height, prev_w, prev_h, flags);
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_resizing = 0;
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up for OpenGL */
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flags & SDL_OPENGL ) {
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( WIN_GL_SetupWindow(this) < 0 ) {
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video->flags |= SDL_OPENGL;
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* JC 14 Mar 2006
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Flush the message loop or this can cause big problems later
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Especially if the user decides to use dialog boxes or assert()!
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIN_FlushMessageQueue();
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're live! */
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(video);
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces in the DIB driver */
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_AllocHWSurface(_THIS, SDL_Surface *surface)
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_FreeHWSurface(_THIS, SDL_Surface *surface)
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DIB_LockHWSurface(_THIS, SDL_Surface *surface)
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface)
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc, mdc;
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_pal ) {
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SelectPalette(hdc, screen_pal, FALSE);
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mdc = CreateCompatibleDC(hdc);
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SelectObject(mdc, screen_bmp);
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<numrects; ++i ) {
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BitBlt(hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					mdc, rects[i].x, rects[i].y, SRCCOPY);
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DeleteDC(mdc);
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int FindPaletteIndex(LOGPALETTE *pal, BYTE r, BYTE g, BYTE b)
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PALETTEENTRY *entry;
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int nentries = pal->palNumEntries;
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < nentries; ++i ) {
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry = &pal->palPalEntry[i];
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( entry->peRed == r && entry->peGreen == g && entry->peBlue == b ) {
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return i;
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL CheckPaletteEntry(LOGPALETTE *pal, int index, BYTE r, BYTE g, BYTE b)
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PALETTEENTRY *entry;
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BOOL moved = 0;
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	entry = &pal->palPalEntry[index];
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( entry->peRed != r || entry->peGreen != g || entry->peBlue != b ) {
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int found = FindPaletteIndex(pal, r, g, b);
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( found >= 0 ) {
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pal->palPalEntry[found] = *entry;
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry->peRed = r;
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry->peGreen = g;
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry->peBlue = b;
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		moved = 1;
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	entry->peFlags = 0;
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return moved;
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc, mdc;
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	RGBQUAD *pal;
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int moved_entries = 0;
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the display palette */
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_pal ) {
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PALETTEENTRY *entry;
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<ncolors; ++i ) {
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry = &screen_logpal->palPalEntry[firstcolor+i];
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry->peRed   = colors[i].r;
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry->peGreen = colors[i].g;
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry->peBlue  = colors[i].b;
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry->peFlags = PC_NOCOLLAPSE;
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(SYSPAL_NOSTATIC) && !defined(_WIN32_WCE)
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check to make sure black and white are in position */
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( GetSystemPaletteUse(hdc) != SYSPAL_NOSTATIC256 ) {
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			moved_entries += CheckPaletteEntry(screen_logpal, 0, 0x00, 0x00, 0x00);
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			moved_entries += CheckPaletteEntry(screen_logpal, screen_logpal->palNumEntries-1, 0xff, 0xff, 0xff);
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* FIXME:
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   If we don't have full access to the palette, what we
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   really want to do is find the 236 most diverse colors
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   in the desired palette, set those entries (10-245) and
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   then map everything into the new system palette.
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Copy the entries into the system palette */
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UnrealizeObject(screen_pal);
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetPaletteEntries(screen_pal, 0, screen_logpal->palNumEntries, screen_logpal->palPalEntry);
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SelectPalette(hdc, screen_pal, FALSE);
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		RealizePalette(hdc);
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Copy palette colors into DIB palette */
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pal = SDL_stack_alloc(RGBQUAD, ncolors);
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<ncolors; ++i ) {
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pal[i].rgbRed = colors[i].r;
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pal[i].rgbGreen = colors[i].g;
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pal[i].rgbBlue = colors[i].b;
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pal[i].rgbReserved = 0;
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the DIB palette and update the display */
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mdc = CreateCompatibleDC(hdc);
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SelectObject(mdc, screen_bmp);
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetDIBColorTable(mdc, firstcolor, ncolors, pal);
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( moved_entries || !grab_palette ) {
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BitBlt(hdc, 0, 0, this->screen->w, this->screen->h,
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       mdc, 0, 0, SRCCOPY);
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DeleteDC(mdc);
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_stack_free(pal);
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_CheckGamma(_THIS)
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_GAMMA_SUPPORT
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WORD ramp[3*256];
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we fail to get gamma, disable gamma control */
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! GetDeviceGammaRamp(hdc, ramp) ) {
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->GetGammaRamp = NULL;
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->SetGammaRamp = NULL;
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_GAMMA_SUPPORT */
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_SwapGamma(_THIS)
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_GAMMA_SUPPORT
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gamma_saved ) {
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hdc = GetDC(SDL_Window);
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* About to leave active state, restore gamma */
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDeviceGammaRamp(hdc, gamma_saved);
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* About to enter active state, set game gamma */
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GetDeviceGammaRamp(hdc, gamma_saved);
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDeviceGammaRamp(hdc, this->gamma);
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseDC(SDL_Window, hdc);
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_GAMMA_SUPPORT */
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_QuitGamma(_THIS)
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_GAMMA_SUPPORT
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gamma_saved ) {
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Restore the original gamma if necessary */
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HDC hdc;
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hdc = GetDC(SDL_Window);
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDeviceGammaRamp(hdc, gamma_saved);
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ReleaseDC(SDL_Window, hdc);
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Free the saved gamma memory */
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(gamma_saved);
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma_saved = 0;
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_GAMMA_SUPPORT */
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_SetGammaRamp(_THIS, Uint16 *ramp)
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef NO_GAMMA_SUPPORT
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("SDL compiled without gamma ramp support");
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BOOL succeeded;
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the ramp for the display */
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! gamma_saved ) {
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma_saved = (WORD *)SDL_malloc(3*256*sizeof(*gamma_saved));
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! gamma_saved ) {
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hdc = GetDC(SDL_Window);
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetDeviceGammaRamp(hdc, gamma_saved);
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseDC(SDL_Window, hdc);
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hdc = GetDC(SDL_Window);
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = SetDeviceGammaRamp(hdc, ramp);
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseDC(SDL_Window, hdc);
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = TRUE;
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return succeeded ? 0 : -1;
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_GAMMA_SUPPORT */
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DIB_GetGammaRamp(_THIS, Uint16 *ramp)
11729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef NO_GAMMA_SUPPORT
11749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("SDL compiled without gamma ramp support");
11759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
11769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
11779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
11789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BOOL succeeded;
11799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the ramp from the display */
11819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(SDL_Window);
11829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	succeeded = GetDeviceGammaRamp(hdc, ramp);
11839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(SDL_Window, hdc);
11849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return succeeded ? 0 : -1;
11859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !NO_GAMMA_SUPPORT */
11869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DIB_VideoQuit(_THIS)
11899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
11919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Destroy the window and everything associated with it */
11939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Window ) {
11949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Delete the screen bitmap (also frees screen->pixels) */
11959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this->screen ) {
11969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( grab_palette ) {
11979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				DIB_ReleaseStaticColors(SDL_Window);
11989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
11999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef NO_CHANGEDISPLAYSETTINGS
12009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( this->screen->flags & SDL_FULLSCREEN ) {
12019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ChangeDisplaySettings(NULL, 0);
12029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ShowWindow(SDL_Window, SW_HIDE);
12039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
12049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( this->screen->flags & SDL_OPENGL ) {
12069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				WIN_GL_ShutDown(this);
12079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
12089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			this->screen->pixels = NULL;
12099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_pal != NULL ) {
12119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DeleteObject(screen_pal);
12129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_pal = NULL;
12139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_logpal != NULL ) {
12159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(screen_logpal);
12169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_logpal = NULL;
12179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_bmp ) {
12199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DeleteObject(screen_bmp);
12209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_bmp = NULL;
12219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( screen_icn ) {
12239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DestroyIcon(screen_icn);
12249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_icn = NULL;
12259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_QuitGamma(this);
12279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_DestroyWindow(this);
12289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Window = NULL;
12309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_WIN32_WCE)
12329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Unload wince aygshell library to prevent leak
12349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if( aygshell )
12359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
12369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			FreeLibrary(aygshell);
12379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			aygshell = NULL;
12389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i < SDL_arraysize(SDL_modelist); ++i ) {
12439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !SDL_modelist[i] ) {
12449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			continue;
12459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( j=0; SDL_modelist[i][j]; ++j ) {
12479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_modelist[i][j]);
12489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(SDL_modelist[i]);
12509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_modelist[i] = NULL;
12519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_nummodes[i] = 0;
12529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Exported for the windows message loop only */
12569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_GrabStaticColors(HWND window)
12579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(SYSPAL_NOSTATIC) && !defined(_WIN32_WCE)
12599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
12609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(window);
12629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC256);
12639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( GetSystemPaletteUse(hdc) != SYSPAL_NOSTATIC256 ) {
12649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
12659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(window, hdc);
12679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_ReleaseStaticColors(HWND window)
12709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(SYSPAL_NOSTATIC) && !defined(_WIN32_WCE)
12729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC hdc;
12739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hdc = GetDC(window);
12759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetSystemPaletteUse(hdc, SYSPAL_STATIC);
12769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ReleaseDC(window, hdc);
12779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_Activate(_THIS, BOOL active, BOOL minimized)
12809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( grab_palette ) {
12829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !active ) {
12839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIB_ReleaseStaticColors(SDL_Window);
12849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIB_RealizePalette(this);
12859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( !minimized ) {
12869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIB_GrabStaticColors(SDL_Window);
12879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIB_RealizePalette(this);
12889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_RealizePalette(_THIS)
12929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_pal != NULL ) {
12949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HDC hdc;
12959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hdc = GetDC(SDL_Window);
12979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
12989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UnrealizeObject(screen_pal);
12999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
13009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SelectPalette(hdc, screen_pal, FALSE);
13019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( RealizePalette(hdc) ) {
13029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			InvalidateRect(SDL_Window, NULL, FALSE);
13039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
13049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseDC(SDL_Window, hdc);
13059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_PaletteChanged(_THIS, HWND window)
13089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( window != SDL_Window ) {
13109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DIB_RealizePalette(this);
13119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Exported for the windows message loop only */
13159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DIB_WinPAINT(_THIS, HDC hdc)
13169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HDC mdc;
13189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_pal ) {
13209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SelectPalette(hdc, screen_pal, FALSE);
13219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mdc = CreateCompatibleDC(hdc);
13239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SelectObject(mdc, screen_bmp);
13249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	BitBlt(hdc, 0, 0, SDL_VideoSurface->w, SDL_VideoSurface->h,
13259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							mdc, 0, 0, SRCCOPY);
13269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DeleteDC(mdc);
13279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic void DIB_GetWinPos(_THIS, int* px, int *py)
1330e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1331e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	RECT  rect;
1332e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	GetWindowRect(SDL_Window, &rect);
1333e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	*px = rect.left;
1334e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	*py = rect.top;
1335e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1336e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1337e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic void DIB_SetWinPos(_THIS, int  x, int  y)
1338e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1339e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    SetWindowPos(SDL_Window, HWND_TOPMOST,
1340e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                 x, y, 0, 0, SWP_NOSIZE|SWP_NOZORDER);
1341e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1342e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1343e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Halltypedef struct {
1344e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    int   result;
1345e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    int   first;
1346e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    RECT  wrect;
1347e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    RECT  primary;
1348e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall} VisibilityData;
1349e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1350e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1351e4c5d95ed37611acc6a186522315195b4ebfb9efJesse HallBOOL CALLBACK visibility_cb(HMONITOR  hMonitor,
1352e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                            HDC       hdcMonitor,
1353e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                            LPRECT    mrect,
1354e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                            LPARAM    dwData)
1355e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1356e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    VisibilityData*  data = (VisibilityData*)dwData;
1357e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1358e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if ( data->first ) {
1359e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        data->first   = 0;
1360e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        data->primary = mrect[0];
1361e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
1362e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1363e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if ( data->wrect.left   >= mrect->left   &&
1364e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall         data->wrect.right  <= mrect->right  &&
1365e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall         data->wrect.top    >= mrect->top    &&
1366e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall         data->wrect.bottom <= mrect->bottom )
1367e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    {
1368e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        data->result = 1;
1369e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        return FALSE;
1370e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
1371e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    return TRUE;
1372e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1373e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1374e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_IsWinVisible(_THIS, int  recenter)
1375e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1376e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    VisibilityData  data;
1377e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    data.result = 0;
1378e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    data.first  = 1;
1379e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1380e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    GetWindowRect(SDL_Window, &data.wrect);
1381e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1382e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    EnumDisplayMonitors(NULL, NULL, visibility_cb, (LPARAM)&data);
1383e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1384e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if ( !data.result && recenter ) {
1385e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        int  new_x = 10;
1386e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        int  new_y = 10;
1387e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1388e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        if ( !data.first ) {
1389e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            int  primary_w = data.primary.right  - data.primary.left;
1390e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            int  primary_h = data.primary.bottom - data.primary.top;
1391e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1392e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            new_x = data.primary.left + (primary_w - this->screen->w)/2;
1393e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall            new_y = data.primary.top  + (primary_h - this->screen->h)/2;
1394e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        }
1395e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        DIB_SetWinPos(this, new_x, new_y);
1396e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
1397e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    return  data.result;
1398e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1399e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1400e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_GetMonitorDPI(_THIS, int* xDpi, int *yDpi)
1401e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1402e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    HDC  displayDC = CreateDC( "DISPLAY", NULL, NULL, NULL );
1403e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    int  xdpi, ydpi;
1404e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1405e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if (displayDC == NULL) {
1406e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        return -1;
1407e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
1408e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    xdpi = GetDeviceCaps( displayDC, LOGPIXELSX );
1409e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    ydpi = GetDeviceCaps( displayDC, LOGPIXELSY );
1410e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1411e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    DeleteDC(displayDC);
1412e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1413e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    /* sanity checks */
1414e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if (xdpi < 20 || xdpi > 400 || ydpi < 20 || ydpi > 400) {
1415e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall        return -1;
1416e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    }
1417e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1418e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    *xDpi = xdpi;
1419e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    *yDpi = ydpi;
1420e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    return 0;
1421e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1422e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1423e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1424e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Halltypedef struct {
1425e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    int   first;
1426e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    RECT  wrect;
1427e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	long  bestArea;
1428e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	RECT  bestRect;
1429e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	RECT  primary;
1430e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall} ProximityData;
1431e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1432e4c5d95ed37611acc6a186522315195b4ebfb9efJesse HallBOOL CALLBACK proximity_cb(HMONITOR  hMonitor,
1433e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                           HDC       hdcMonitor,
1434e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                           LPRECT    mrect,
1435e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall                           LPARAM    dwData)
1436e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1437e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    ProximityData*  data = (ProximityData*)dwData;
1438e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    int   x1, y1, x2, y2, area;
1439e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1440e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	x1 = mrect->left;
1441e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	x2 = mrect->right;
1442e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	y1 = mrect->top;
1443e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	y2 = mrect->bottom;
1444e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1445e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (data->first) {
1446e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		data->primary = mrect[0];
1447e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	}
1448e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1449e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (x1 < data->wrect.left)
1450e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		x1 = data->wrect.left;
1451e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    if (x2 > data->wrect.right)
1452e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		x2 = data->wrect.right;
1453e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (y1 < data->wrect.top)
1454e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		y1 = data->wrect.top;
1455e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (y2 > data->wrect.bottom)
1456e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		y2 = data->wrect.bottom;
1457e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1458e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (x1 >= x2 || y1 >= y2)
1459e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		return TRUE;
1460e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1461e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	area = (x2-x1)*(y2-y1);
1462e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (data->first || area > data->bestArea) {
1463e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		data->first    = 0;
1464e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		data->bestRect = mrect[0];
1465e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		data->bestArea = area;
1466e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	}
1467e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    return TRUE;
1468e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1469e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1470e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hallstatic int  DIB_GetMonitorRect(_THIS, SDL_Rect*  rect)
1471e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall{
1472e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    ProximityData  data;
1473e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	RECT*          sr;
1474e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1475e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    data.first  = 1;
1476e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    GetWindowRect(SDL_Window, &data.wrect);
1477e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1478e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall    EnumDisplayMonitors(NULL, NULL, proximity_cb, (LPARAM)&data);
1479e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1480e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	if (data.first)
1481e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall		return -1;
1482e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1483e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	sr = &data.bestRect;
1484e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1485e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	rect->x = sr->left;
1486e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	rect->y = sr->top;
1487e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	rect->w = sr->right - sr->left;
1488e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	rect->h = sr->bottom - sr->top;
1489e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
1490e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall	return 0;
1491e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall}
1492e4c5d95ed37611acc6a186522315195b4ebfb9efJesse Hall
14939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stub in case DirectX isn't available */
14949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !SDL_AUDIO_DRIVER_DSOUND
14959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DX5_SoundFocus(HWND hwnd)
14969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
14979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
14989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1500