#include <xtl.h>
#include "xbinput.h"
extern int vSpace;
extern int screenOptions;
extern int screenResize;
extern int screenResizeP;
extern int screenResizeI;
extern int screenResizePB;
extern int screenResizeJ;
extern int screenResizeG;
extern int screenResizeB;
extern int screenResizeA;
extern int screenResizeA2;
extern int screenOptionsM;
extern int screenOptionsHelp;
extern int screenResizeS;
extern int screenControllerOptions;
extern int screenCheatOptions;
extern int screensoundOptions;
extern int screenSoundOptions;
extern int screenLightGunOptions;
extern int screenGeneralOptions;
extern int screenGeneralOptionsGG;
extern int screenGeneralOptionsGGText;
extern int screenGUIOptions;
extern int GameNameColor;
extern void loadGuide(char* p, char* n);
extern int gameGuideLines ;
extern int gameGuideColor ;
extern int gameGuideFontSize ;
extern void drawGuide();
extern int GameNameSelectedColor;
extern int FolderColor;
extern int zTopHT;
int guideHeight = 15;
extern void setHeight(int v);
extern int FontSizeGUI;
extern int guideLeft;
extern int zLeftHT;
extern int lC;
extern int rC;
extern int lCWidth;
extern int rCHeight;
extern int MenuOption;
extern int MenuOptionSelected;
extern int FontSizeGUI;
extern int FullNameColor;
extern double slow;
extern unsigned char rightAnalog;
extern unsigned char leftAnalog;
extern DWORD menuColor[5];
int showAvailMemory = 0;
// Global instance of custom gamepad devices
extern int DisableMenuSounds;
extern void playEffect(int i);
extern  void writeConfig();
#include <vector>
#include <string>
using namespace std;
extern vector<string> guide;
extern XBGAMEPAD g_Gamepads[4];
int map34 = 0;
#include <string>
#include <vector>
using namespace std;
struct guideDBEntry {
	string name;
	string path;
};
double slowGuide = 0;
extern int guideGameSelectedText;
int guideGameSelected = 0;
vector<guideDBEntry> guideDatabase;

extern "C" {
	unsigned char ShowInfo = 0;
	extern unsigned char KeyUsePlayer1234;
}
int map34Changed = 1;
int disableCombo = 0;
extern unsigned char * keys;
extern int firstVidRender;
extern int FlickerFilter;
extern "C" {
	extern unsigned char FPSAtStart;
	extern  unsigned char FPSOn;
	extern unsigned char RevStereo;
	extern unsigned char SPCDisable;
	extern unsigned char RewindStates;// = how many states 0 - 25
	extern unsigned char RewindFrames;// = how many half seconds between rewind states 0 - 40
	extern unsigned int per2exec;
	extern unsigned int cfgper2exec;
	extern void Setper2exec();
	extern int Bper2exec ;
	extern int Bcfgper2exec ;


}

float scrollSpeed = 1.0f;

extern int zLeftGM;
extern int zTopGM;

extern int zLeftCD;
extern int zTopCD;

extern int zLeftMM;
extern int zTopMM;

extern  int HardwareFilter;
extern unsigned char SoftwareFilter;
extern  int FlickerFilter;
extern  int ShowFPS;
extern  int Soften;
int generalOptionSelected = 0;
extern void setFlickerSoft();
extern int LowPassFilter;
extern int SoundFrequency;
extern int SoundInterpolation;
extern int SurroundSound;

extern "C" {
	extern void SetReverseStereo();
	extern void SetSPCEnable();
	extern void setFrequency();
	extern void setLowPassFilter();
	extern void setSoundInterpolation();
	extern void setSurroundSound();
}

extern int DisableMenuSounds;

extern "C" {
	extern unsigned char ForceROMTiming;
	extern unsigned char pl3contrl;
	extern unsigned char pl4contrl;
}

extern int Disable34;
extern int DisableSaveStates;

extern void processOptions();

extern int inGame;
int HideFileExtension = 1;
int ShowZipContents = 0;
int EnableAudioAdjust = 1;
extern int HighRes;
extern int screenShotSlot;
extern float XBINPUT_DEADZONE;
int ScreenshotSaving = 1;

int musicSpeed = 30;




void checkGeneralOptions() {
	if(g_Gamepads[0].Event == XBGAMEPAD_B) {
		if(DisableMenuSounds == 0) playEffect(1);
		screenGeneralOptions = 0;
		screenGeneralOptionsGG = 0;
		screenGeneralOptionsGGText = 0;
		screenGUIOptions = 0;
		screenOptions     = 1;
		screenResize = 0;
		screenResizeP = 0;
		screenResizeI = 0;
		screenResizePB = 0;
		screenResizeJ = 0;
		screenResizeG = 0;
		screenResizeB = 0;
		screenResizeA = 0;
		screenResizeA2 = 0;
		screenOptionsM = 0;
		screenOptionsHelp = 0;
		screenResizeS = 0;
		screenControllerOptions = 0;
		writeConfig();

		return;          
	} 

	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		generalOptionSelected++;

		if(inGame == 0) { //6 options 0-5
			if(generalOptionSelected > 15) generalOptionSelected = 0;
			if(generalOptionSelected < 0) generalOptionSelected = 15;   
		}  
		else { //5 options 0-4
			if(generalOptionSelected > 14) generalOptionSelected = 0;
			if(generalOptionSelected < 0) generalOptionSelected = 14;   
		}  
	}

	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		generalOptionSelected--; 
		if(inGame == 0) {
			if(generalOptionSelected > 15) generalOptionSelected = 0;
			if(generalOptionSelected < 0) generalOptionSelected = 15;   
		}  
		else {
			if(generalOptionSelected > 14) generalOptionSelected = 0;
			if(generalOptionSelected < 0) generalOptionSelected = 14;   
		}  
	}  

	if(g_Gamepads[0].Event == XBGAMEPAD_A) {
		if(DisableMenuSounds == 0) playEffect(2);
		if(inGame == 0) {
			if(generalOptionSelected == 0){ ForceROMTiming++; if(ForceROMTiming > 2) ForceROMTiming = 0; }
			if(generalOptionSelected == 1){ DisableSaveStates = (DisableSaveStates + 1) & 1;}
			if(generalOptionSelected == 2){ Disable34 = (Disable34 + 1) & 1; pl3contrl = Disable34; pl4contrl = Disable34; }
			if(generalOptionSelected == 3) {HideFileExtension++; if(HideFileExtension > 2) HideFileExtension = 0; }
			if(generalOptionSelected == 4) {ShowZipContents = (ShowZipContents + 1) & 1; }
			if(generalOptionSelected == 5) {EnableAudioAdjust = (EnableAudioAdjust + 1) & 1; }
			if(generalOptionSelected == 6) {RewindStates++; if(RewindStates > 25) RewindStates = 0; }
			if(generalOptionSelected == 7) {RewindFrames++; if(RewindFrames > 40) RewindFrames = 0; }
			if(generalOptionSelected == 8) {HighRes = (HighRes + 1) & 1; }
			if(generalOptionSelected == 9) {disableCombo = (disableCombo + 1) & 1; }
			if(generalOptionSelected == 10) { XBINPUT_DEADZONE+=0.01; }
			if(generalOptionSelected == 11){ShowInfo = (ShowInfo + 1) & 1;}
			if(generalOptionSelected == 12){ScreenshotSaving = (ScreenshotSaving + 1) & 1;}
			if(generalOptionSelected == 13){showAvailMemory = (showAvailMemory + 1) & 1;}
			if(generalOptionSelected == 14){scrollSpeed+=1; if(scrollSpeed > 50) scrollSpeed = 1; }
			if(generalOptionSelected == 15){screenShotSlot+=1; if(screenShotSlot > 8) screenShotSlot = 1; }
			//  if(generalOptionSelected == 16){musicSpeed+=1; if(musicSpeed > 60) musicSpeed = 1; }
		}
		else {
			if(generalOptionSelected == 0){ DisableSaveStates = (DisableSaveStates + 1) & 1;}
			if(generalOptionSelected == 1){ Disable34 = (Disable34 + 1) & 1; pl3contrl = Disable34; pl4contrl = Disable34; }
			if(generalOptionSelected == 2) {HideFileExtension++; if(HideFileExtension > 2) HideFileExtension = 0; }
			if(generalOptionSelected == 3) {ShowZipContents = (ShowZipContents + 1) & 1; }
			if(generalOptionSelected == 4) {EnableAudioAdjust = (EnableAudioAdjust + 1) & 1; }
			if(generalOptionSelected == 5) {RewindStates++; if(RewindStates > 25) RewindStates = 0; }
			if(generalOptionSelected == 6) {RewindFrames++; if(RewindFrames > 40) RewindFrames = 0; }
			if(generalOptionSelected == 7) {HighRes = (HighRes + 1) & 1; }
			if(generalOptionSelected == 8) {disableCombo = (disableCombo + 1) & 1; }
			if(generalOptionSelected == 9) { XBINPUT_DEADZONE+=0.01; }
			if(generalOptionSelected == 10){ ShowInfo = (ShowInfo + 1) & 1; }
			if(generalOptionSelected == 11){ScreenshotSaving = (ScreenshotSaving + 1) & 1;}
			if(generalOptionSelected == 12){map34 = (map34 + 1) & 1;  keys[KeyUsePlayer1234] = 1;}
			if(generalOptionSelected == 13){scrollSpeed+=1; if(scrollSpeed > 50) scrollSpeed = 1; }
			if(generalOptionSelected == 14){screenShotSlot+=1; if(screenShotSlot > 8) screenShotSlot = 1; }
			//  if(generalOptionSelected == 15){musicSpeed+=1; if(musicSpeed > 60) musicSpeed = 1; }

		}
	}

	if(g_Gamepads[0].Event == XBGAMEPAD_X) {
		if(DisableMenuSounds == 0) playEffect(2);
		if(inGame == 0) {
			if(generalOptionSelected == 0){ if(ForceROMTiming == 0) ForceROMTiming = 2; else ForceROMTiming--; }
			if(generalOptionSelected == 1){ DisableSaveStates = (DisableSaveStates + 1) & 1;}
			if(generalOptionSelected == 2){ Disable34 = (Disable34 + 1) & 1; pl3contrl = Disable34; pl4contrl = Disable34; }
			if(generalOptionSelected == 3) {HideFileExtension++; if(HideFileExtension > 2) HideFileExtension = 0; }
			if(generalOptionSelected == 4) {ShowZipContents = (ShowZipContents + 1) & 1; }
			if(generalOptionSelected == 5) {EnableAudioAdjust = (EnableAudioAdjust + 1) & 1; }
			if(generalOptionSelected == 6) { if(RewindStates == 0) RewindStates = 25; else RewindStates--; }
			if(generalOptionSelected == 7) { if(RewindFrames == 0) RewindFrames = 40; else RewindFrames--; }
			if(generalOptionSelected == 8) {HighRes = (HighRes + 1) & 1; }
			if(generalOptionSelected == 9) {disableCombo = (disableCombo + 1) & 1; }
			if(generalOptionSelected == 10) { XBINPUT_DEADZONE-=0.01; }
			if(generalOptionSelected == 11){ ShowInfo = (ShowInfo + 1) & 1;}
			if(generalOptionSelected == 12){ScreenshotSaving = (ScreenshotSaving + 1) & 1;}
			if(generalOptionSelected == 13){showAvailMemory = (showAvailMemory + 1) & 1;}
			if(generalOptionSelected == 14){scrollSpeed-=1; if(scrollSpeed < 1) scrollSpeed = 50; }
			if(generalOptionSelected == 15){screenShotSlot-=1; if(screenShotSlot < 1) screenShotSlot = 8; }
			//	  if(generalOptionSelected == 16){musicSpeed-=1; if(musicSpeed < 1) musicSpeed = 60; }
		}
		else {
			if(generalOptionSelected == 0){ DisableSaveStates = (DisableSaveStates + 1) & 1;}
			if(generalOptionSelected == 1){ Disable34 = (Disable34 + 1) & 1; pl3contrl = Disable34; pl4contrl = Disable34; }
			if(generalOptionSelected == 2) {HideFileExtension++; if(HideFileExtension > 2) HideFileExtension = 0; }
			if(generalOptionSelected == 3) {ShowZipContents = (ShowZipContents + 1) & 1; }
			if(generalOptionSelected == 4) {EnableAudioAdjust = (EnableAudioAdjust + 1) & 1; }
			if(generalOptionSelected == 5) { if(RewindStates == 0) RewindStates = 25; else RewindStates--; }
			if(generalOptionSelected == 6) { if(RewindFrames == 0) RewindFrames = 40; else RewindFrames--; }
			if(generalOptionSelected == 7) {HighRes = (HighRes + 1) & 1; }
			if(generalOptionSelected == 8) {disableCombo = (disableCombo + 1) & 1; }
			if(generalOptionSelected == 9) { XBINPUT_DEADZONE-=0.01; }
			if(generalOptionSelected == 10){ ShowInfo = (ShowInfo + 1) & 1;}
			if(generalOptionSelected == 11){ScreenshotSaving = (ScreenshotSaving + 1) & 1;}
			if(generalOptionSelected == 12){map34 = (map34 + 1) & 1; keys[KeyUsePlayer1234] = 1;}
			if(generalOptionSelected == 13){scrollSpeed-=1; if(scrollSpeed < 1) scrollSpeed = 50; }
			if(generalOptionSelected == 14){screenShotSlot-=1; if(screenShotSlot < 1) screenShotSlot = 8; }
			//	  if(generalOptionSelected == 15){musicSpeed-=1; if(musicSpeed < 1) musicSpeed = 60; }

		}
	}
	return;
} 

extern void saveBookMark(char*);

void checkGeneralOptionsGGText() {
	if(g_Gamepads[0].Event == XBGAMEPAD_B) {
		if(DisableMenuSounds == 0) playEffect(1);
		screenGeneralOptionsGG = 1;
		
		setHeight(FontSizeGUI);

		//free the buffer that holds the text file.
		guide.clear();
		saveBookMark((char*)guideDatabase[guideGameSelected].name.c_str());
		screenGeneralOptionsGGText = 0;
		
		return;          
	} 

	//if (g_Gamepads[0].fY1 > 0) {if(zTopHT < (rC - 130))zTopHT+=5;   }// Up
	//if (g_Gamepads[0].fY1 < 0) {zTopHT-=5;   } // Down
	//if (g_Gamepads[0].fX1 < 0) {zLeftHT-=1;  } // Left
	//if (g_Gamepads[0].fX1 > 0) {zLeftHT+=1;  }// Right
	
	
	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {
		if(guideGameSelectedText < 7) guideGameSelectedText = 7;
		guideGameSelectedText++; if(guideGameSelectedText >=guide.size()) guideGameSelectedText = guide.size()- 1;

	}
	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		guideGameSelectedText--; if(guideGameSelectedText < 7) guideGameSelectedText = 7;
	}  

	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_RIGHT)  {
		if(guideGameSelectedText < 7) guideGameSelectedText = 7;
		guideGameSelectedText+=gameGuideLines; if(guideGameSelectedText >=guide.size()) guideGameSelectedText = guide.size()- 1;

	}
	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_LEFT)  {  
		guideGameSelectedText-=gameGuideLines; if(guideGameSelectedText < 7) guideGameSelectedText = 7;
	}  


	if( g_Gamepads[0].bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) {
		if(slowGuide >= 1) { 
				slowGuide = 0; 
			} 

			if(rightAnalog == 255)
				slowGuide = scrollSpeed;
			else
				slowGuide += ((float)rightAnalog/255.0f);
			guideGameSelectedText+=((int)slowGuide); 
			if(guideGameSelectedText >= guide.size()) 
				guideGameSelectedText = guide.size() - 1;
	
	}

	if (g_Gamepads[0].fX1 < 0) {guideLeft-=2;  } // Left
	if (g_Gamepads[0].fX1 > 0) {guideLeft+=2;  }// Right

	if( g_Gamepads[0].bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) {
		if(slowGuide >= 1)  { 
				slowGuide = 0;
		}

			if(leftAnalog == 255)
				slowGuide = scrollSpeed;
			else
				slowGuide += ((float)leftAnalog/255.0f);
			//slow += (scrollSpeed / 10.0f);
			guideGameSelectedText-=((int)slowGuide); 
			if(guideGameSelectedText < 7)
				guideGameSelectedText = 7;
	
		
	}

	return;
} 

extern int LoopMode ;
extern int fadeColor;
extern int fadeColor2;
extern int mpOptionSelected;
void checkGeneralOptionsGG() {
	if(g_Gamepads[0].Event == XBGAMEPAD_B) {
		if(DisableMenuSounds == 0) playEffect(1);

		if(screenOptionsHelp == 1) {
			if(DisableMenuSounds == 0) playEffect(1);
			screenOptionsHelp = 0; 
			LoopMode = 2;
			//writeConfig();
			screenOptionsM = 1;
			fadeColor = 255;
			fadeColor2 = 255;
			//mpOptionSelected = 0;
			screenGeneralOptionsGG = 0;
			return;
		}
		screenGeneralOptions = 0;
		screenGeneralOptionsGG = 0;
		screenGUIOptions = 0;
		screenOptions     = 1;
		screenResize = 0;
		screenResizeP = 0;
		screenResizeI = 0;
		screenResizePB = 0;
		screenResizeJ = 0;
		screenResizeG = 0;
		screenResizeB = 0;
		screenResizeA = 0;
		screenResizeA2 = 0;
		screenOptionsM = 0;
		screenOptionsHelp = 0;
		screenResizeS = 0;
		screenControllerOptions = 0;
		return;          
	} 

	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {  
		if(DisableMenuSounds == 0) playEffect(0);
		guideGameSelected++; 
		if(guideGameSelected >= guideDatabase.size()) 
			guideGameSelected = guideDatabase.size() - 1;

	}

	if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		guideGameSelected--; 
		if(guideGameSelected < 0) 
			guideGameSelected = 0;
	}  

	if(g_Gamepads[0].Event == XBGAMEPAD_A) {
		if(DisableMenuSounds == 0) playEffect(2);

		string p = guideDatabase[guideGameSelected].path;
		string p2 = guideDatabase[guideGameSelected].name;

		screenGeneralOptionsGG = 0;
		screenGeneralOptionsGGText = 1;
		setHeight(gameGuideFontSize);
		loadGuide((char*)p.c_str(), (char*)p2.c_str());
		//display the file selected.
		return;

	}

	if( g_Gamepads[0].bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) {
			if(slow >= 1) { 
				slow = 0; 
			} 
			if(rightAnalog == 255)
				slow = scrollSpeed;
			else
				slow += ((float)rightAnalog/255.0f);
			guideGameSelected+=((int)slow); 
			if(guideGameSelected >= guideDatabase.size()) 
				guideGameSelected = guideDatabase.size() - 1;
	}

	if( g_Gamepads[0].bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) {
		
			if(slow >= 1)  { 
				slow = 0;
				//if(DisableMenuSounds == 0 && cheatDBGameSelected != 0) 
				//	playEffect(0);
			}

			if(leftAnalog == 255)
				slow = scrollSpeed;
			else
				slow += ((float)leftAnalog/255.0f);
			//slow += (scrollSpeed / 10.0f);
			guideGameSelected-=((int)slow); if(guideGameSelected < 0) guideGameSelected = 0;
	}

	return;
} 

extern int FontSetColour(unsigned int colour);
extern int FontText(WCHAR *,int,int, int);

//*************************************************************************
//DRAW
int yG = 0;
int drawG_Count = 0;

void drawGDText(char* text) {
	DWORD color = menuColor[MenuOption];

	// WCHAR text2345678_[80]={0};
	// wsprintfW(text2345678_,  L"Option Selected: %d, ShowFPS: %d", controllerConfigSelected, B[0].Button_ShowFPS);
	// FontSetColour(0xffff00);
	// FontText(text2345678_,50,300, 600);



	if(drawG_Count == generalOptionSelected) color = menuColor[MenuOptionSelected]; 

	//Draw the text string
	WCHAR text23456789[80]={0};
	//wsprintfW(text2345678_, (LPCWSTR)text);

	for(int i = 0; i < 1000; i++) {
		if(text[i] == '\0') {
			text23456789[i] = '\0';
			break;
		}
		text23456789[i] = text[i];
	}
	FontSetColour(color);
	FontText(text23456789,340+zLeftGM,yG+zTopGM, 600);
	yG += vSpace;
	drawG_Count++;
}

void drawGText(char* text) {
	DWORD color  = menuColor[MenuOption];

	// WCHAR text2345678_[80]={0};
	// wsprintfW(text2345678_,  L"Option Selected: %d, ShowFPS: %d", controllerConfigSelected, B[0].Button_ShowFPS);
	// FontSetColour(0xffff00);
	// FontText(text2345678_,50,300, 600);



	if(drawG_Count == generalOptionSelected) color = menuColor[MenuOptionSelected]; 

	//Draw the text string
	WCHAR text23456789[80]={0};
	//wsprintfW(text2345678_, (LPCWSTR)text);

	for(int i = 0; i < 1000; i++) {
		if(text[i] == '\0') {
			text23456789[i] = '\0';
			break;
		}
		text23456789[i] = text[i];
	}
	FontSetColour(color);
	FontText(text23456789,125+zLeftGM,yG+zTopGM, 600);
	//yG += 20;
	//drawG_Count++;
}
extern int sWidth;
#include <sstream>

extern int FontSmallText(WCHAR *,int,int, int);
void drawGeneralOptions() {
	/*WCHAR text2345[280]={0};
	wsprintfW(text2345,L"Press \"B\" to exit.");
	FontSetColour(0xffff00);
	FontSmallText(text2345,sWidth/2,70, 600);	*/
	drawG_Count = 0;
	yG = 110;
	if(inGame == 0) {
		if(ForceROMTiming == 0) { drawGText("Region: "); drawGDText("Auto");  }
		if(ForceROMTiming == 1) { drawGText("Region: "); drawGDText("NTSC");	}
		if(ForceROMTiming == 2) { drawGText("Region: "); drawGDText("PAL");  }
	}

	if(DisableSaveStates == 0) { drawGText("Disable Savestates: "); drawGDText("Off");  }
	if(DisableSaveStates == 1) { drawGText("Disable Savestates: "); drawGDText("On");  }	

	if(Disable34 == 0) { drawGText("Player 3 and 4: "); drawGDText("Off");  }
	if(Disable34 == 1) { drawGText("Player 3 and 4: "); drawGDText("On");  }

	if(HideFileExtension == 0) { drawGText("Game Display: "); drawGDText("Full");  }
	if(HideFileExtension == 1) { drawGText("Game Display: "); drawGDText("No Extension");  }
	if(HideFileExtension == 2) { drawGText("Game Display: "); drawGDText("Only Game Name");  }

	if(ShowZipContents == 0) { drawGText("Show Zipfile Contents: "); drawGDText("Off");  }
	if(ShowZipContents == 1) { drawGText("Show Zipfile Contents: "); drawGDText("On");  }

	if(EnableAudioAdjust == 0) { drawGText("In-Game Audio Adjust: "); drawGDText("Off");  }
	if(EnableAudioAdjust == 1) { drawGText("In-Game Audio Adjust: "); drawGDText("On");  }


	if(RewindStates == 0) { drawGText("# of Rewind States: "); drawGDText("0");  }
	if(RewindStates == 1) { drawGText("# of Rewind States: "); drawGDText("1");  }
	if(RewindStates == 2) { drawGText("# of Rewind States: "); drawGDText("2");  }
	if(RewindStates == 3) { drawGText("# of Rewind States: "); drawGDText("3");  }
	if(RewindStates == 4) { drawGText("# of Rewind States: "); drawGDText("4");  }
	if(RewindStates == 5) { drawGText("# of Rewind States: "); drawGDText("5");  }
	if(RewindStates == 6) { drawGText("# of Rewind States: "); drawGDText("6");  }
	if(RewindStates == 7) { drawGText("# of Rewind States: "); drawGDText("7");  }
	if(RewindStates == 8) { drawGText("# of Rewind States: "); drawGDText("8");  }
	if(RewindStates == 9) { drawGText("# of Rewind States: "); drawGDText("9");  }
	if(RewindStates == 10) { drawGText("# of Rewind States: "); drawGDText("10");  }
	if(RewindStates == 11) { drawGText("# of Rewind States: "); drawGDText("11");  }
	if(RewindStates == 12) { drawGText("# of Rewind States: "); drawGDText("12");  }
	if(RewindStates == 13) { drawGText("# of Rewind States: "); drawGDText("13");  }
	if(RewindStates == 14) { drawGText("# of Rewind States: "); drawGDText("14");  }
	if(RewindStates == 15) { drawGText("# of Rewind States: "); drawGDText("15");  }
	if(RewindStates == 16) { drawGText("# of Rewind States: "); drawGDText("16");  }
	if(RewindStates == 17) { drawGText("# of Rewind States: "); drawGDText("17");  }
	if(RewindStates == 18) { drawGText("# of Rewind States: "); drawGDText("18");  }
	if(RewindStates == 19) { drawGText("# of Rewind States: "); drawGDText("19");  }
	if(RewindStates == 20) { drawGText("# of Rewind States: "); drawGDText("20");  }
	if(RewindStates == 21) { drawGText("# of Rewind States: "); drawGDText("21");  }
	if(RewindStates == 22) { drawGText("# of Rewind States: "); drawGDText("22");  }
	if(RewindStates == 23) { drawGText("# of Rewind States: "); drawGDText("23");  }
	if(RewindStates == 24) { drawGText("# of Rewind States: "); drawGDText("24");  }
	if(RewindStates == 25) { drawGText("# of Rewind States: "); drawGDText("25");  }

	if(RewindFrames == 0) { drawGText("Rewind 1/2 Seconds: "); drawGDText("0");  }
	if(RewindFrames == 1) { drawGText("Rewind 1/2 Seconds: "); drawGDText("1");  }
	if(RewindFrames == 2) { drawGText("Rewind 1/2 Seconds: "); drawGDText("2");  }
	if(RewindFrames == 3) { drawGText("Rewind 1/2 Seconds: "); drawGDText("3");  }
	if(RewindFrames == 4) { drawGText("Rewind 1/2 Seconds: "); drawGDText("4");  }
	if(RewindFrames == 5) { drawGText("Rewind 1/2 Seconds: "); drawGDText("5");  }
	if(RewindFrames == 6) { drawGText("Rewind 1/2 Seconds: "); drawGDText("6");  }
	if(RewindFrames == 7) { drawGText("Rewind 1/2 Seconds: "); drawGDText("7");  }
	if(RewindFrames == 8) { drawGText("Rewind 1/2 Seconds: "); drawGDText("8");  }
	if(RewindFrames == 9) { drawGText("Rewind 1/2 Seconds: "); drawGDText("9");  }
	if(RewindFrames == 10) { drawGText("Rewind 1/2 Seconds: "); drawGDText("10");  }
	if(RewindFrames == 11) { drawGText("Rewind 1/2 Seconds: "); drawGDText("11");  }
	if(RewindFrames == 12) { drawGText("Rewind 1/2 Seconds: "); drawGDText("12");  }
	if(RewindFrames == 13) { drawGText("Rewind 1/2 Seconds: "); drawGDText("13");  }
	if(RewindFrames == 14) { drawGText("Rewind 1/2 Seconds: "); drawGDText("14");  }
	if(RewindFrames == 15) { drawGText("Rewind 1/2 Seconds: "); drawGDText("15");  }
	if(RewindFrames == 16) { drawGText("Rewind 1/2 Seconds: "); drawGDText("16");  }
	if(RewindFrames == 17) { drawGText("Rewind 1/2 Seconds: "); drawGDText("17");  }
	if(RewindFrames == 18) { drawGText("Rewind 1/2 Seconds: "); drawGDText("18");  }
	if(RewindFrames == 19) { drawGText("Rewind 1/2 Seconds: "); drawGDText("19");  }
	if(RewindFrames == 20) { drawGText("Rewind 1/2 Seconds: "); drawGDText("20");  }
	if(RewindFrames == 21) { drawGText("Rewind 1/2 Seconds: "); drawGDText("21");  }
	if(RewindFrames == 22) { drawGText("Rewind 1/2 Seconds: "); drawGDText("22");  }
	if(RewindFrames == 23) { drawGText("Rewind 1/2 Seconds: "); drawGDText("23");  }
	if(RewindFrames == 24) { drawGText("Rewind 1/2 Seconds: "); drawGDText("24");  }
	if(RewindFrames == 25) { drawGText("Rewind 1/2 Seconds: "); drawGDText("25");  }
	if(RewindFrames == 26) { drawGText("Rewind 1/2 Seconds: "); drawGDText("26");  }
	if(RewindFrames == 27) { drawGText("Rewind 1/2 Seconds: "); drawGDText("27");  }
	if(RewindFrames == 28) { drawGText("Rewind 1/2 Seconds: "); drawGDText("28");  }
	if(RewindFrames == 29) { drawGText("Rewind 1/2 Seconds: "); drawGDText("29");  }
	if(RewindFrames == 30) { drawGText("Rewind 1/2 Seconds: "); drawGDText("30");  }
	if(RewindFrames == 31) { drawGText("Rewind 1/2 Seconds: "); drawGDText("31");  }
	if(RewindFrames == 32) { drawGText("Rewind 1/2 Seconds: "); drawGDText("32");  }
	if(RewindFrames == 33) { drawGText("Rewind 1/2 Seconds: "); drawGDText("33");  }
	if(RewindFrames == 34) { drawGText("Rewind 1/2 Seconds: "); drawGDText("34");  }
	if(RewindFrames == 35) { drawGText("Rewind 1/2 Seconds: "); drawGDText("35");  }
	if(RewindFrames == 36) { drawGText("Rewind 1/2 Seconds: "); drawGDText("36");  }
	if(RewindFrames == 37) { drawGText("Rewind 1/2 Seconds: "); drawGDText("37");  }
	if(RewindFrames == 38) { drawGText("Rewind 1/2 Seconds: "); drawGDText("38");  }
	if(RewindFrames == 39) { drawGText("Rewind 1/2 Seconds: "); drawGDText("39");  }
	if(RewindFrames == 40) { drawGText("Rewind 1/2 Seconds: "); drawGDText("40");  }

	if(HighRes == 0) { drawGText("Screenshot Resolution: "); drawGDText("256x224 (Non Filtered)");  }
	if(HighRes == 1) { drawGText("Screenshot Resolution: "); drawGDText("512x448 (Filtered)");  }

	if(disableCombo == 0) { drawGText("Disable Key Combo: "); drawGDText("Off");  }
	if(disableCombo == 1) { drawGText("Disable Key Combo: "); drawGDText("On");  }

	std::ostringstream q;
	q << XBINPUT_DEADZONE;
	std::string w = q.str();
	drawGText("Analog Deadzone: "); drawGDText((char*)w.c_str());
	if(ShowInfo == 0) { drawGText("ROM Info on Game Start: "); drawGDText("Off");  }
	if(ShowInfo == 1) { drawGText("ROM Info on Game Start: "); drawGDText("On");  }

	if(ScreenshotSaving == 0) { drawGText("Screenshot Saving: "); drawGDText("Disabled");  }
	if(ScreenshotSaving == 1) { drawGText("Screenshot Saving: "); drawGDText("Enabled");  }

	if(inGame == 1) {
		if(map34 == 0) { drawGText("Map P1, P2 to P3, P4: "); drawGDText("Disabled");  }
		if(map34 == 1) { drawGText("Map P1, P2 to P3, P4: "); drawGDText("Enabled");  }
	}

	if(inGame == 0) {
		if(showAvailMemory == 0) { drawGText("Display Free Memory: "); drawGDText("Off");  }
		if(showAvailMemory == 1) { drawGText("Display Free Memory: "); drawGDText("On");  }
	}

	std::ostringstream qf;
	qf << scrollSpeed;
	std::string wf = qf.str();
	drawGText("Max Analog Scroll Speed: "); drawGDText((char*)wf.c_str());

	if(screenShotSlot == 1) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 1"); }
	if(screenShotSlot == 2) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 2"); }
	if(screenShotSlot == 3) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 3"); }
	if(screenShotSlot == 4) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 4"); }
	if(screenShotSlot == 5) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 5"); }
	if(screenShotSlot == 6) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 6"); }
	if(screenShotSlot == 7) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 7"); }
	if(screenShotSlot == 8) { drawGText("Screenshot Save Slot: "); drawGDText("Slot 8"); }
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


bool operator<(const guideDBEntry &l, const guideDBEntry &r) {
			std::string a = l.name;
			std::string b = r.name;
			transform(a.begin(), a.end(), a.begin(), tolower);
			transform(b.begin(), b.end(), b.begin(), tolower);
			if(a < b) return true; else return false;
}
	
void searchDirectoryGuide(std::string pathToSearch) {
	std::string path1 = pathToSearch;// + "*";
	std::string path2("*.txt");
	std::string path = path1 + path2;
	HANDLE find=NULL;
	WIN32_FIND_DATA wfd;
	memset(&wfd,0,sizeof(wfd));
	find=FindFirstFile(path.c_str(),&wfd);

	if (find ==INVALID_HANDLE_VALUE){ //Empty directory
		guideDBEntry t, t2;
		t.name = "No text files were found in D:\\guides";
		t.path = "C:\\Test";
		guideDatabase.push_back(t);
		t2.name = "Game guides can be found at http:\\\\www.gamefaqs.com";
		t2.path = "C:\\Test";
		guideDatabase.push_back(t2);	
		return;
	}
	do {
		if(!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
			//Not a folder		 
			std::string fileFound(wfd.cFileName);
			guideDBEntry f;
			f.name = fileFound;
			for(int i = 0; i < f.name.size(); i++) {
				if((   (f.name.c_str())[i] == '.' 
					&& (f.name.c_str())[i+1] == 't' 
					&& (f.name.c_str())[i+2] == 'x' 
					&& (f.name.c_str())[i+3] == 't' 
					&& (f.name.c_str())[i+4] == '\0') 
					) {
						f.name[i] = '\0';
						break;
				}
			}
			f.path = pathToSearch + fileFound;
			guideDatabase.push_back(f);	
		}
	} while (FindNextFile(find,&wfd) !=0);
	//sort(guideDatabase.begin(), guideDatabase.end());
	sort(guideDatabase.begin(), guideDatabase.end());
}
extern int screenOptionsHelp;
void buildGuideList() {
	
	guideDatabase.clear();
	if(screenOptionsHelp == 1) {
		guideGameSelected = 0;
		searchDirectoryGuide("D:\\docs\\");
	}
	else {
		searchDirectoryGuide("D:\\guides\\");
	}
}

void drawGameGuideList() {
	int yDB = 110;
	DWORD color;
	int start = 0;

	if(guideGameSelected > 7 && (guideDatabase.size() > 15)) {
		start += (guideGameSelected  - 7);
	}

	if((guideGameSelected > ((guideDatabase.size()-1) - 7)) && (guideDatabase.size() > 15)) {
		start = (guideDatabase.size() - 15);
	}

	for(int i = start; i <  start +15; i++) {
		if(i == guideDatabase.size()) return;
		if(i ==guideGameSelected) color = 0xffff00; else color = 0xffffff; 
		WCHAR atext2345[1024]={0};

		std::string t = guideDatabase[i].name;
		for(int r = 0; r < 1024; r++) {
			if((t.c_str())[r] == '\0') {
				atext2345[r] = '\0';
				break;
			}
			atext2345[r] = (t.c_str())[r];
		}
		FontSetColour(color);
		FontSmallText(atext2345,sWidth/2,yDB, 600);
		yDB+=20;
	}
}


void drawGeneralOptionsGG() { //draw list of text files found
	/*WCHAR text2345[280]={0};
	wsprintfW(text2345,L"Press \"B\" to exit.");
	FontSetColour(0xffff00);
	FontSmallText(text2345,sWidth/2,70, 600);*/
	drawGameGuideList();
}



void drawGeneralOptionsGGText() { //draw the text file
	/*WCHAR text2345[280]={0};
	wsprintfW(text2345,L"Press \"B\" to exit.");
	FontSetColour(0xffff00);
	FontSmallText(text2345,sWidth/2,70, 600);*/
	//drawGameGuideList();

	drawGuide();
}