/*********************************************************************************************/
/*                                                                                           */
/* User Input Code                                                                           */
/*                                                                                           */
/*********************************************************************************************/
#include "standard.h"
#include "global.h"
#include "winmain.h"
#include "console.h"
#include <math.h>
#include "template.h"
#include "input.h"
#include "di.h"
#include "display.h"
#include "frontend.h"
#include "init.h"


#include "../game/input.h"
#include "../game/invfunc.h"
#include "../game/laramisc.h"
#include "../game/camera.h"
#include "../game/lara.h"
#include "../game/objects.h"
#include "../game/gameflow.h"
#include "../game/demo.h"
#include "../game/title.h"



/********************************* GLOBAL VARIABLES ******************************************/


int bLaraOn;
int bRoomOn;
int bObjectOn;
int bAObjectOn;
int bEffectOn;
int bDraw;

defglobal bool GtDisableFullScreenWindowedToggle;

extern int fmv_lock;
extern void SetZNear(int);

#define JOY_SENSITIVITY 8	// by Dude	// was 8

unsigned char keymap[256];

int joy_x, joy_y, joy_fire;

int record_demo = 0;

sint32 input;

#define RANDOM_SEED 0xd371f947

short layout[2][15] = {
	{DIK_UP,      DIK_DOWN,    DIK_LEFT,    DIK_RIGHT,   DIK_DELETE,  DIK_PGDN,    DIK_RSHIFT,  DIK_RALT, DIK_RCONTROL,    DIK_SPACE,   DIK_SLASH,       DIK_NUMPAD0, DIK_END,     DIK_ESCAPE, DIK_DECIMAL},
	{DIK_NUMPAD8, DIK_NUMPAD2, DIK_NUMPAD4, DIK_NUMPAD6, DIK_NUMPAD7, DIK_NUMPAD9, DIK_NUMPAD1, DIK_ADD,  DIK_NUMPADENTER, DIK_NUMPAD3, DIK_NUMPADMINUS, DIK_NUMPAD0, DIK_NUMPAD5, DIK_DECIMAL, DIK_Z}
	};

int conflict[15];

#ifdef GERMAN
char *kname[] = {
	NULL,		"ESC",	"1",		"2",		"3",		"4",		"5",		"6",
		"7",		"8",		"9",		"0",		"=",		"`",		"DEL",	"TAB", 
		"Q",		"W",		"E",		"R",		"T",		"Z",		"U",		"I",
		"O",		"P",		"~u",		"x",		"RET",	"STRG",	"A",		"S", 
		"D",		"F",		"G",		"H",		"J",		"K",		"L",		"~o",
		"~a",		"( ",		"SHIFT",	"#",		"Y",		"X",		"C",		"V",
		"B",		"N",		"M",		",",		".",		"-",		"SHIFT",	"NUMx",
		"ALT",	"SPACE",	"CAPS",	NULL,		NULL,		NULL,		NULL,		NULL, 
		
		NULL,		NULL,		NULL,		NULL,		NULL,		"NMLK",	NULL,		"NUM7",
		"NUM8",	"NUM9",	"NUM-",	"NUM4",	"NUM5",	"NUM6",	"NUM+",	"NUM1", 
		"NUM2",	"NUM3",	"NUM0",	"NUM,",	NULL,		NULL,		">",		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		"ENTER",	"STRG",	NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		"SHIFT",	NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		"NUM/",	NULL,		NULL,
		"ALT",	NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		"POS1",
		"HOCH",	"BDAUF",	NULL,		"LINKS",	NULL,		"RECHT",	NULL,		"ENDE",
		"HERAB",	"BDAB",	"EINFG",	"ENTF",	NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		"JOY1", 	"JOY2",		"JOY3",		"JOY4", 	"JOY5",		"JOY6", 	"JOY7",		"JOY8",		
		"JOY9",		"JOY10",	"JOY11",	"JOY12",	"JOY13",	"JOY14",	"JOY15",	"JOY16"
	};
#else
char *kname[] = {
	NULL,		"ESC",	"1",		"2",		"3",		"4",		"5",		"6",
		"7",		"8",		"9",		"0",		"-",		"+",		"BKSP",	"TAB", 
		"Q",		"W",		"E",		"R",		"T",		"Y",		"U",		"I",
		"O",		"P",		"<",		">",		"RET",	"CTRL",	"A",		"S", 
		"D",		"F",		"G",		"H",		"J",		"K",		"L",		";",
		"'",		"`",		"SHIFT",	"#",		"Z",		"X",		"C",		"V",
		"B",		"N",		"M",		",",		".",		"/",		"SHIFT",	"PADx",
		"ALT",	"SPACE",	"CAPS",	NULL,		NULL,		NULL,		NULL,		NULL, 
		
		NULL,		NULL,		NULL,		NULL,		NULL,		"NMLK",	NULL,		"PAD7",
		"PAD8",	"PAD9",	"PAD-",	"PAD4",	"PAD5",	"PAD6",	"PAD+",	"PAD1", 
		"PAD2",	"PAD3",	"PAD0",	"PAD.",	NULL,		NULL,		"\\",		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		"ENTER",	"CTRL",	NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		"SHIFT",	NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		"PAD/",	NULL,		NULL,
		"ALT",	NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		"HOME",
		"UP",		"PGUP",	NULL,		"LEFT",	NULL,		"RIGHT",	NULL,		"END",
		"DOWN",	"PGDN",	"INS",	"DEL",	NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,		NULL,
		
		"JOY1", 	"JOY2",		"JOY3",		"JOY4", 	"JOY5",		"JOY6", 	"JOY7",		"JOY8",		
		"JOY9",		"JOY10",	"JOY11",	"JOY12",	"JOY13",	"JOY14",	"JOY15",	"JOY16"
	};
#endif
/*********************************** FUNCTION CODE *******************************************/

int Key(int number)
	{
	sint16 key;
	
	/* Check user key */
	key = layout[1][number];
	
	/* Check joystick buttons */
	if (key >= 256)
		{
		if ((1<<(key & 0xff)) & joy_fire)
			return (1);
		}
	else
		{
		if (rawkey(key))
			return (1);
		
		if (key == DIK_RCONTROL)
			return (rawkey(DIK_LCONTROL));
		if (key == DIK_LCONTROL)
			return (rawkey(DIK_RCONTROL));
		if (key == DIK_RSHIFT)
			return (rawkey(DIK_LSHIFT));
		if (key == DIK_LSHIFT)
			return (rawkey(DIK_RSHIFT));
		if (key == DIK_RMENU)
			return (rawkey(DIK_LMENU));
		if (key == DIK_LMENU)
			return (rawkey(DIK_RMENU));
		}
	
	// Don't check default key if conflict with user defined keys
	if (conflict[number])
		return (0);
	
	// Check default
	key = layout[0][number];
	
	if (rawkey(key))
		return (1);
	
	if (key == DIK_RCONTROL)
		return (rawkey(DIK_LCONTROL));
	if (key == DIK_LCONTROL)
		return (rawkey(DIK_RCONTROL));
	if (key == DIK_RSHIFT)
		return (rawkey(DIK_LSHIFT));
	if (key == DIK_LSHIFT)
		return (rawkey(DIK_RSHIFT));
	if (key == DIK_RMENU)
		return (rawkey(DIK_LMENU));
	if (key == DIK_LMENU)
		return (rawkey(DIK_RMENU));
	
	return (0);
	}





#define FOGLIMIT 0.35f
#define FOGMUL	(256.0f*(1.0f/FOGLIMIT))
#define FOGSH	24

extern float foglimit;
extern float fogmul;

int S_UpdateInput(void)
	{
	/* Get input from keyboard/joypad or whatever, and put into global variable 'input' */
	sint32 linput;
	static int medi_used=0;
	static bool tDebounceF3=false;
	static bool tDebounceF4=false;
	static bool tDebounceF7=false;
	static bool tDebounceF8=false;
	static bool tDebounceF11=false;

	DD_SpinMessageLoop();
	
	// Allow Direct Input to fill out keyboard layout
	DI_ReadKeyboard(keymap);
	joy_fire=DI_ReadJoystick(joy_x,joy_y);
/*	
	
	// TODO : Temp Fog Stuff
	if(rawkey(DIK_Q))
	{
		foglimit -= 0.01f;
		if(foglimit < 0.0f) foglimit = 0.01f;
		fogmul = 255.0f*(1.0f/foglimit);	
	}

	if(rawkey(DIK_W))
	{
		foglimit += 0.01f;
		if(foglimit > 1.0f) foglimit = 1.0f;
		fogmul = 255.0f*(1.0f/foglimit);	
	}
*/	
	linput = 0;
	
	if (joy_x < -JOY_SENSITIVITY)
		linput |= IN_LEFT;
	else if (joy_x > JOY_SENSITIVITY)
		linput |= IN_RIGHT;
	
	if (joy_y > JOY_SENSITIVITY)
		linput |= IN_BACK;
	else if (joy_y < -JOY_SENSITIVITY)
		linput |= IN_FORWARD;
	
	if (Key(0))
		linput |= IN_FORWARD;
	if (Key(1))
		linput |= IN_BACK;
	if (Key(2))
		linput |= IN_LEFT;
	if (Key(3))
		linput |= IN_RIGHT;
	//if (Key(4))
	//	linput |= IN_STEPL;
	//if (Key(5))
	//	linput |= IN_STEPR;
	if (Key(6))
		linput |= IN_SLOW;
	if (Key(7))
		linput |= IN_JUMP;
	if (Key(8))
		linput |= IN_ACTION;
	if (Key(9))
		linput |= IN_DRAW;
	if (Key(10))
		linput |= IN_F;
	if (Key(11))
		linput |= IN_LOOK;
	if (Key(12))
		linput |= IN_ROLL;
	if (Key(14))
		linput |= IN_DUCK;
	
	if (linput & IN_SLOW)
	{
		if (!(linput & (IN_FORWARD|IN_BACK)))
		{
			if (linput & IN_LEFT)
				linput = (linput & ~IN_LEFT) | IN_STEPL;
			else if (linput & IN_RIGHT)
				linput = (linput & ~IN_RIGHT) | IN_STEPR;
		}
	}


	if(rawkey(DIK_ADD))
		IncreaseScreenSize();

	if(rawkey(DIK_SUBTRACT))
		DecreaseScreenSize();


#ifdef GAMEDEBUG

	bLaraOn = 1;
	if(rawkey(DIK_Q))
		bLaraOn = 0;

	bRoomOn = 1;
	if(rawkey(DIK_W))
		bRoomOn = 0;

	bObjectOn = 1;
	if(rawkey(DIK_E))
		bObjectOn = 0;

	bAObjectOn = 1;
	if(rawkey(DIK_R))
		bAObjectOn = 0;

	bEffectOn = 1;
	if(rawkey(DIK_T))
		bEffectOn = 0;

	bDraw = 1;
	if(rawkey(DIK_D))
		bDraw = 0;


#endif

	/* Lock out option keys during cinematic camera excursions */
	if (Key(13) && camera.type != CINEMATIC_CAMERA)
		linput |= IN_OPTION;

	if ( linput&IN_FORWARD && linput&IN_BACK )
		linput |= IN_ROLL;
	
	if (rawkey(DIK_RETURN) || (linput & IN_ACTION))
		linput |= IN_SELECT;
	if (rawkey(DIK_ESCAPE))
		linput |= IN_DESELECT;
	
	/* Make left+right cancel */
	if ((linput & (IN_LEFT|IN_RIGHT)) == (IN_LEFT|IN_RIGHT))
		linput -= (IN_LEFT|IN_RIGHT);

/*	
	if (rawkey(DIK_Z))
		linput |= IN_DUCK;
*/
	
	
	// quite enough info if in FMV mode
/*
	if (fmv_lock)
	{
		input = linput;
		return (int)GtWindowClosed;
	}

 */// RF

	/* Weapon select keys */
	if (rawkey(DIK_1) && Inv_RequestItem(GUN_OPTION))
		lara.request_gun_type = LG_PISTOLS;
	else if (rawkey(DIK_2) && Inv_RequestItem(SHOTGUN_OPTION))
		lara.request_gun_type = LG_SHOTGUN;
	else if (rawkey(DIK_3) && Inv_RequestItem(MAGNUM_OPTION))
		lara.request_gun_type = LG_MAGNUMS;
	else if (rawkey(DIK_4) && Inv_RequestItem(UZI_OPTION))
		lara.request_gun_type = LG_UZIS;
	else if (rawkey(DIK_5) && Inv_RequestItem(HARPOON_OPTION))
		lara.request_gun_type = LG_HARPOON;
	else if (rawkey(DIK_6) && Inv_RequestItem(M16_OPTION))
		lara.request_gun_type = LG_M16;
	else if (rawkey(DIK_7) && Inv_RequestItem(ROCKET_OPTION))
		lara.request_gun_type = LG_ROCKET;
	else if (rawkey(DIK_0) && Inv_RequestItem(FLAREBOX_OPTION))
		lara.request_gun_type = LG_FLARE;

	/* Medi pack select keys */
	if (rawkey(DIK_8) && Inv_RequestItem(MEDI_OPTION))
	{
		if (!medi_used)
		{
			UseItem(MEDI_OPTION);
			medi_used = 15; // debounce please
		}
	}
	else if (rawkey(DIK_9) && Inv_RequestItem(BIGMEDI_OPTION))
	{
		if (!medi_used)
		{
			UseItem(BIGMEDI_OPTION);
			medi_used = 15;
		}
	}
	else if (medi_used)
		medi_used--;



#ifdef GAMEDEBUG
	/* Internal keys */
	if ( rawkey(DIK_A) )
		linput |= IN_A;
	if ( rawkey(DIK_B) )
		linput |= IN_B;
	if ( rawkey(DIK_C) )
		linput |= IN_C;
	if ( rawkey(DIK_D) )
		linput |= IN_D;
	if ( rawkey(DIK_E) )
		linput |= IN_E;
	
	if (rawkey(DIK_L))
		level_complete = 1;
/*

	if (rawkey(DIK_Q) && record_demo==0)
	{
		democount = 0;
		SaveLaraDemoPos();
		SeedRandomDraw(RANDOM_SEED);
		SeedRandomControl(RANDOM_SEED);
		record_demo = 1;
	}
	
	if ( rawkey(DIK_W) )							// debugging aids
	{
		if (record_demo>0) 						// Save Demo if we have to...
			S_SaveDemoData( CurrentLevel );
		S_ExitSystem("");
	}
*/
  

	//	if ( rawkey(DIK_F) )
	//		FlipMap();
	
	
//	if (rawkey(DIK_E))
//		GtWireFrameMode=false;
//	if (rawkey(DIK_W))
//		GtWireFrameMode=true;

	if (rawkey(DIK_G))
		show_game_malloc_totals();

	
#else
#if !defined(NO_FLY_CHEAT) || defined(ALWAYS_ENABLE_DOZY_CHEAT)
	if (gameflow.cheat_enable)
	{
		if ( rawkey(DIK_C) )
			linput |= IN_C;
		if ( rawkey(DIK_D) )
			linput |= IN_D;
	}
#endif
#endif


/*	
	gameflow.cheat_enable = true;
	gameflow.dozy_cheat_enabled =false;
	if ( rawkey(DIK_C) )
		linput |= IN_C;
	if ( rawkey(DIK_D) )
		linput |= IN_D;
*/	

	if (rawkey(DIK_S))
		SaveDDBuffer(App.lpFrontBuffer);
	
	
	if (gameflow.dozy_cheat_enabled)
		{
		// NOTE: ALLOW CHEAT MODE TO BE ENABLED BY TYPING "DOZY"
		static int cheat_code=0;
		
		if (linput)
			cheat_code = 0;
		
		switch (cheat_code)
			{
			case 0:
				if (rawkey(DIK_D))
					cheat_code=1;
				break;
			case 1:
				if (rawkey(DIK_O))
					cheat_code=2;
				break;
			case 2:
				if (rawkey(DIK_Z))
					cheat_code=3;
				break;
			case 3:
				if (rawkey(DIK_Y))
					linput=IN_C;
				break;
			}
		}
	
	bool tShift=rawkey(DIK_LSHIFT)||rawkey(DIK_RSHIFT);
	
	/*
	
	if (G_AppSettings.nRenderMode!=RENDERER_INTERNAL)
		{
		if (rawkey(DIK_F7))
			{
			if (!tDebounceF7)
				{
				tDebounceF7=true;
				AppSettings newSettings=G_AppSettings;
				if (tShift)
					{
					if (G_AppSettings.tFullScreen)
						{
						newSettings.tTripleBuffering=!G_AppSettings.tTripleBuffering;
						DX_ChangeSettings(newSettings);
						}
					}
				else
					{
					newSettings.tZBuffer=!G_AppSettings.tZBuffer;
					DX_ChangeSettings(newSettings);
					}
				}
			}
		else
			tDebounceF7=false;
		
		if (rawkey(DIK_F8))
			{
			if (!tDebounceF8)
				{
				tDebounceF8=true;
				AppSettings newSettings=G_AppSettings;
				if (tShift)
					newSettings.tPerspectiveCorrect=!G_AppSettings.tPerspectiveCorrect;
				else
					newSettings.tBilinearFiltering=!G_AppSettings.tBilinearFiltering;
				DX_ChangeSettings(newSettings);
				}
			}
		else
			tDebounceF8=false;
		
		if (rawkey(DIK_F11))
			{
			if (!tDebounceF11)
				{
				tDebounceF11=true;
				AppSettings newSettings=G_AppSettings;
				newSettings.tDither=!G_AppSettings.tDither;
				DX_ChangeSettings(newSettings);
				}
			}
		else
			tDebounceF11=false;
		}
	else
		{
		// internal renderer: toggle perspective correctness
		if (rawkey(DIK_F7))
			{
			if (!tDebounceF7)
				{
				tDebounceF7=true;
				if (tShift)
					{
					if (G_AppSettings.tFullScreen)
						{
						AppSettings newSettings=G_AppSettings;
						newSettings.tTripleBuffering=!G_AppSettings.tTripleBuffering;
						DX_ChangeSettings(newSettings);
						}
					}
				else
					{
					G_AppSettings.tPerspectiveCorrect = !G_AppSettings.tPerspectiveCorrect;
					perspective_distance = (G_AppSettings.tPerspectiveCorrect)? ((6*WALL_L)<<W2V_SHIFT) : ((3*WALL_L)<<W2V_SHIFT);
					Log("PERSP %d", perspective_distance);
					}
				}
			}
		else
			tDebounceF7=false;
		}

	*/
	/*
	
	if (!GtDisableFullScreenWindowedToggle)
		{
		if (rawkey(DIK_F12))
			{
			bool tChange=false;
			AppSettings newSettings=G_AppSettings;
			if (tShift)
				{
				if (!Inventory_Displaying)
					{
					if (G_AppSettings.nRenderMode==RENDERER_INTERNAL)
						{
						if (!G_DisplayAdapter.HAL_Modes.IsEmpty())
							{
							newSettings.nRenderMode=RENDERER_HAL;
							newSettings.FullScreenMode=G_DisplayAdapter.HAL_Modes.Find(ScreenMode(G_RenderInfo.nWidth,G_RenderInfo.nHeight,16,0));
							newSettings.tFullScreen=true;
							tChange=true;
							}
						}
					else
						{
						if (!G_DisplayAdapter.Internal_Modes.IsEmpty())
							{
							newSettings.nRenderMode=RENDERER_INTERNAL;
							newSettings.FullScreenMode=G_DisplayAdapter.Internal_Modes.Find(ScreenMode(G_RenderInfo.nWidth,G_RenderInfo.nHeight,8,0));
							newSettings.tFullScreen=true;
							tChange=true;
							}
						}
					}
				}
			else
				{
				newSettings.tFullScreen=!G_AppSettings.tFullScreen;
				if (newSettings.tFullScreen)
					{
					List<ScreenMode>& rModeList=(G_AppSettings.nRenderMode==RENDERER_HAL)?G_DisplayAdapter.HAL_Modes:G_DisplayAdapter.Internal_Modes;
					if (!rModeList.IsEmpty())
						{
						newSettings.FullScreenMode=rModeList.Find(ScreenMode(G_RenderInfo.nWidth,G_RenderInfo.nHeight,G_RenderInfo.nBPP,0));
						tChange=true;
						}
					}
				else
					{
	//				newSettings.nWindowHeight=G_RenderInfo.nHeight;
	//				newSettings.nWindowWidth=DD_CalcAspectAdjustedWidth(G_RenderInfo.nWidth,G_RenderInfo.nHeight);
					int nW=phd_winwidth; if (nW<320) nW=320;
					int nH=phd_winheight; if (nH<240) nH=240;
					newSettings.nWindowHeight=nH;
					newSettings.nWindowWidth=DD_CalcAspectAdjustedWidth(nW,nH);
					newSettings.tTripleBuffering=false;
					if (DX_ChangeSettings(newSettings))
						{
						game_sizer=screen_sizer=1.0;
						setup_screen_size();
						}
					}
				}
			if (tChange)
				DX_ChangeSettings(newSettings);
			}
		}
	
	*/

/*

	if (!mode_lock && camera.type != CINEMATIC_CAMERA)
		{
		if ( !gameflow.screensizing_disabled )
			{
			if (rawkey(DIK_F1))
				{
				if (G_AppSettings.tFullScreen)
					{
					Position posNewMode;
					if (G_AppSettings.nRenderMode==RENDERER_HAL)
						{
						ScreenMode& rCurMode=G_DisplayAdapter.HAL_Modes[G_AppSettings.FullScreenMode];
						posNewMode=G_AppSettings.FullScreenMode;
						while (posNewMode=G_DisplayAdapter.HAL_Modes.GetPrev(posNewMode))
							{
							ScreenMode& rNewMode=G_DisplayAdapter.HAL_Modes[posNewMode];
							if (tShift)
								{
								if ((rNewMode.nWidth==rCurMode.nWidth) &&
									(rNewMode.nHeight==rCurMode.nHeight) &&
									(rNewMode.nType==rCurMode.nType) &&
									(rNewMode.nBPP<rCurMode.nBPP))
									break;
								}
							else
								{
								if ((rNewMode.nType==rCurMode.nType) &&
									(rNewMode.nBPP==rCurMode.nBPP))
									break;
								}
							}
						}
					else
						posNewMode=G_DisplayAdapter.Internal_Modes.GetPrev(G_AppSettings.FullScreenMode);
					if (posNewMode)
						{
						AppSettings newSettings=G_AppSettings;
						newSettings.FullScreenMode=posNewMode;
						DX_ChangeSettings(newSettings);
						}
					}
				}
			if (rawkey(DIK_F2))
				{
				if (G_AppSettings.tFullScreen)
					{
					Position posNewMode;
					if (G_AppSettings.nRenderMode==RENDERER_HAL)
						{
						ScreenMode& rCurMode=G_DisplayAdapter.HAL_Modes[G_AppSettings.FullScreenMode];
						posNewMode=G_AppSettings.FullScreenMode;
						while (posNewMode=G_DisplayAdapter.HAL_Modes.GetNext(posNewMode))
							{
							ScreenMode& rNewMode=G_DisplayAdapter.HAL_Modes[posNewMode];
							if (tShift)
								{
								if ((rNewMode.nWidth==rCurMode.nWidth) &&
									(rNewMode.nHeight==rCurMode.nHeight) &&
									(rNewMode.nType==rCurMode.nType) &&
									(rNewMode.nBPP>rCurMode.nBPP))
									break;
								}
							else
								{
								if ((rNewMode.nType==rCurMode.nType) &&
									(rNewMode.nBPP==rCurMode.nBPP))
									break;
								}
							}
						}
					else
						posNewMode=G_DisplayAdapter.Internal_Modes.GetNext(G_AppSettings.FullScreenMode);
					if (posNewMode)
						{
						AppSettings newSettings=G_AppSettings;
						newSettings.FullScreenMode=posNewMode;
						DX_ChangeSettings(newSettings);
						}
					}
				}
			if (rawkey(DIK_F3))
				{
				if (!tDebounceF3)
					{
					tDebounceF3=true;
					if (G_AppSettings.tFullScreen)
						DecreaseScreenSize();
					}
				}
			else
				tDebounceF3=false;

			if (rawkey(DIK_F4))
				{
				if (!tDebounceF4)
					{
					tDebounceF4=true;
					if (G_AppSettings.tFullScreen)
						IncreaseScreenSize();
					}
				}
			else
				tDebounceF4=false;
			}		
		}
	
	*/  
	

	
/*	
	if ( !gameflow.loadsave_disabled )
		{
		if ( rawkey(DIK_F5) )
			linput |= IN_SAVE;
		else if ( rawkey(DIK_F6) )
			linput |= IN_LOAD;
		}
		
*/
  
	
	  input = linput;
	
#ifdef GAMEDEBUG
	/* This is a PC only ability */
	if (record_demo>0)
		SaveDemoData();
#endif
	return (int)GtWindowClosed;
}
