#include "app.h"
#define DIRECTINPUT_VERSION 0x0800
#define DIRECTSOUND_VERSION 0x0800
#include "xbinput.h"
unsigned char Flicker=0;
extern float getVolume();
extern void setVolume();
extern void processOptions();
extern int checkSelection();
extern int checkA(); 
extern void loadHelp();
extern void clearHelp();
extern void buildGuideList();
extern void clearKeyboard();
extern int MapControllers;
extern int wasThere;
extern void analogScroll();
extern float getLightGunY();
extern float getLightGunX();
extern int triggerPulled();
extern void pauseVGMusic();
extern int musicMode;
extern FLOAT fLeft;//fOriginX;
extern FLOAT fRight;//fOriginX + ( fSizeY * fMovieWidth) / fMovieHeight;
extern FLOAT fTop;//fOriginY;
extern FLOAT fBottom;//fOriginY + fSizeY;
extern int vgplaying;
extern bool videoRender;
extern int mpOptionSelected;
extern int tvMode;
extern "C" {
extern void stopMovie();
}
extern void resumeVGMusic();
extern void drawCoords(int xTarget, int yTarget);
extern void drawLightgunInfo();
extern int screenResizeMovie;
extern int lightGunMode;
extern int lightgunOnScreen();
#include <string>
extern char currentSongName[512];
extern unsigned char supressCursor;
extern std::string romBasePath;
extern int controllerType;
extern int confirmDelete;
extern int favoriteAdd;
extern void drawCursor();
extern void checkMPOptions();
extern int screenGeneralOptionsMP;
extern void resetThemeSong();
extern unsigned char EnableGameClock;
extern int screenGeneralOptionsTGA;
extern void controllerTGADatabaseCheck();
extern void imageDelete();
extern void imageControl();
extern int TGADatabaseDelete;
extern int TGADatabasePick;
struct playerControl {
   int button_A      ;
   int button_B      ;
   int button_X      ;
   int button_Y      ;
   int button_Start  ;
   int button_Select ;
   int button_Left   ;
   int button_Right  ;
   int button_FastForward  ;
   int button_Rewind  ;
   int button_SaveState  ;
   int button_LoadState  ;
   int button_ShowFPS  ;
   int button_ExitGame  ;

};


extern unsigned char qStereoSound;
int saveStateLoad = 0;
int saveStateSave = 0;
extern int processStateOptions();

extern int ShowFPS;
extern playerControl controllers[4];
extern int EnableAudioAdjust;

extern "C" {
   extern unsigned char cfgSoundQuality;
   extern unsigned char antienab;
   extern unsigned char Mode7HiRes16b;
   extern unsigned char newgfx16b;
   extern unsigned char RevStereo;
   extern unsigned char OldGfxMode2;
   extern unsigned char Surround;
   extern DWORD SoundQuality;
   
   extern unsigned char *sram;
   extern unsigned char yesoutofmemory;
   extern unsigned int nmiprevaddrl, nmiprevaddrh, nmirept, nmiprevline, nmistatus;
   extern unsigned char spcnumread;
   extern unsigned char NextLineCache, sramsavedis, sndrot, regsbackup[3019];
   extern unsigned int curexecstate;
   void outofmemfix();
   void GUIDoReset();
}

#define u32 unsigned int
#define u8 unsigned char
#define uint32 unsigned int
#define uint16 unsigned short
#define uint8 unsigned char
extern void  createSaveStateDirectories();
extern void SaveSlot(int slot);
//extern unsigned char savePartition;
extern std::string SAVE_PATH;
extern std::string SRAM_PATH;
extern int lightGun;
extern HRESULT LightGunRender();
extern HRESULT ProcessLightGun();
extern void Normal2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
							int width, int height);
extern void Normal1x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
							int width, int height);
extern void AdMame2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */,
			  u8 *dstPtr, u32 dstPitch, int width, int height);

extern void AdvMame2x(const uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch,
							 int width, int height);

extern void SuperScale(uint8 *src, uint32 srcpitch, uint8 *,uint8 *dst, uint32 dstpitch, int width, int height);
extern void restoreResolution();
extern void startHDMode();
unsigned char SoftwareFilter = 17;
extern int optionSelected;

extern int optionSelectedM;
extern int HardwareFilter;
extern int FlickerFilter;
extern int SoundFrequency;
extern int Soften;
extern int SoundInterpolation;
extern int LowPassFilter;
extern int DisableSaveStates;
extern int inGame;
extern float zA;
extern int optionSelected;
extern int SurroundSound;
extern int screenControllerOptions;
extern int screenCheatOptions;
extern int romMove;
extern IDirect3DTexture8 *TexScreenShot;
extern "C" {
extern unsigned char GUIOn2;
extern unsigned char SoundInterpType;
extern unsigned char LowPassFilterType;
//extern unsigned char SoundQuality;
extern unsigned char cfgSoundQuality;
extern void AdjustFrequency();
extern unsigned char scanlines;
//extern unsigned char cfgscanlines;
extern unsigned char antienab;
extern unsigned char SPCDisable;

}
extern int ReverseStereo;
extern "C" {
extern int SPCEmulationEnabled;
}
//extern void setFrequency();
//extern void setLowPassFilter();
//extern void setSoundInterpolation();
//extern void setSurroundSound();

extern int screenGeneralOptions;
extern int screenGUIOptions;
extern int screenVideoOptions;
extern int screenSoundOptions;
extern int screenMPOptions;
extern int screenLightGunOptions;
extern int screenResizeOptions;
extern void checkVideoOptions();
extern void checkSoundOptions();
extern void checkGeneralOptions();
extern void processCheatOptions();
extern void checkGUIOptions();

extern IDirect3DVertexBuffer8 *VertexBuffer;
extern IDirect3DVertexBuffer8 *VertexBufferPreview;
extern IDirect3DVertexBuffer8 *VertexBufferPreviewGP;
extern IDirect3DVertexBuffer8 *VertexBufferMP;
extern IDirect3DVertexBuffer8 *VertexBufferMPL;
extern IDirect3DVertexBuffer8 *VertexBufferPreviewBorder;
extern IDirect3DVertexBuffer8 *VertexBufferPreviewB;
extern IDirect3DVertexBuffer8 *VertexBufferPreview2;
extern IDirect3DVertexBuffer8 *VertexBufferLinear;
extern IDirect3DVertexBuffer8 *VertexBufferResize;
extern IDirect3DVertexBuffer8 *VertexBufferController;
extern IDirect3DVertexBuffer8 *VertexBufferOptions;
struct CustomVertex
{
  float x,y,z; // Vertex cordinates
  unsigned int colour;
  float u,v; // Texture coordinates
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
extern CustomVertex VertexList[4];
extern CustomVertex AnimatedVertexList[4];
extern CustomVertex StationaryAnimatedVertexList[4];
extern CustomVertex VertexListPreview[4];
extern CustomVertex VertexListPreviewGP[4];
extern CustomVertex VertexListPreviewB[4];
extern CustomVertex VertexListPreviewBorder[4];
extern CustomVertex VertexListPreview2[4];
extern CustomVertex VertexListLinear[4];
extern CustomVertex VertexListResize[4];
extern CustomVertex VertexListController[4];
extern CustomVertex VertexListOptions[4];
extern void freeTexture();
extern int screenResize;
extern int screenResizeP;
extern int screenResizeI;
extern int screenResizePB;
extern int screenResizeJ;
extern int screenResizeG;
extern int screenResizeE;

extern int screenResizeB;
extern int screenResizeA;
extern int screenResizeA2;
extern int screenOptionsM;
extern int screenOptionsHelp;
extern int screenResizeS;
extern int screenController;
extern int screenOptions;
extern  float zBottom;
extern  float zTop;
extern  float zLeft;
extern  float zRight;
extern  float zBottomP;
extern  float zTopP;
extern  float zLeftP;
extern  float zRightP;


extern  float zBottomGP;
extern  float zTopGP;
extern  float zLeftGP;
extern  float zRightGP;

extern  float zBottomMPL;
extern  float zTopMPL;
extern  float zLeftMPL;
extern  float zRightMPL;

extern void resetRumbleTimer();
extern bool rumbleOver() ;
bool rumbleActive = false;
extern int activeRumble;
extern  float zBottomPB;
extern  float zTopPB;
extern  float zLeftPB;
extern  float zRightPB;

extern int screenResizeGP;
extern  int zTopHT;
extern  int zLeftHT;
extern float zTopJ ;
extern float zLeftJ ;
extern float zBottomJ ;
extern float zRightJ ;

extern int fontResizeR;
extern int fontResizeEO;
extern int fontResizeCO;
extern int fontResizeCC;
extern int fontResizeClip;
extern int fontResizeTO;
extern int fontResizeCCO;
extern int fontResizeF;
extern int zLeftEO;
extern int zTopEO;

extern int zLeftCO;
extern int zTopCO;

extern int zLeftTO;
extern int zTopTO;

extern int zLeftCCO;
extern int zTopCCO;

extern int fadeColor;
extern int fadeColor2;
extern int fadeOption;

extern float zTopG ;
extern float zLeftG ;
extern float zBottomG ;
extern float zRightG ;
extern int guiOptionSelected;
extern int gFont;
extern float zTopB ;
extern float zLeftB ;


 extern int zTopBR;
extern int zLeftBR;


 extern int zTopV;
extern int zLeftV;
 extern int zTopRM;
extern int zLeftRM;
 extern int zTopSM;
extern int zLeftSM;

 extern int zTopGM;
extern int zLeftGM;

 extern int zTopMM;
extern int zLeftMM;

 extern int zTopBF;
extern int zLeftBF;

 extern int zTopGT;
extern int zLeftGT;

extern int zTopIT;
extern int zLeftIT;

extern int zTopCC;
extern int zLeftCC;
extern float zTopA;
extern float zLeftA ;
extern float zBottomA ;
extern float zRightA ;
extern float zTopA2;
extern float zLeftA2 ;
extern float zBottomA2 ;
extern float zRightA2 ;

extern int sWidth;
extern int sHeight;
extern int currentStationaryFrame;

extern float zTopAR;
extern float zLeftAR ;
extern float zBottomAR ;
extern float zRightAR ;
extern float zTopA2R;
extern float zLeftA2R ;
extern float zBottomA2R ;
extern float zRightA2R ;


extern float zTopS;
extern float zLeftS ;
extern float zBottomS ;
extern float zRightS ;

extern void makeVertexListLinear();
extern void makeVertexListResize();
extern void makeVertexListController();
extern void makeVertexListOptions();
extern void checkControllerConfig();
extern void setTextureMode();
extern void setVertex();
extern float zTop;
extern float zLeft;
extern float zBottom;
extern float zRight;

extern float zTopP;
extern float zLeftP;
extern float zBottomP;
extern float zRightP;


extern int screenResizeMPL;

extern float zTopPB;
extern float zLeftPB;

extern float zTopMP;
extern float zLeftMP;

extern int zTopHT;
extern int zLeftHT;

extern float zBottomPB;
extern float zRightPB;
 extern  int zTopBR;
extern int zLeftBR;
extern  int zTopV;
extern int zLeftV;
extern  int zTopSM;
extern int zLeftSM;

extern  int zTopGM;
extern int zLeftGM;

extern  int zTopCD;
extern int zLeftCD;

extern  int zTopMM;
extern int zLeftMM;
extern int mouseDist;
 extern  int zTopBF;
extern int zLeftBF;

 extern  int zTopGT;
extern int zLeftGT;

extern int Codes[15][8+1+27];
extern void  writeConfig();
extern float zBottomB;
 extern float zRightB;
extern int DisableMenuSounds;
extern int Disable34;
extern int fontResizeV;
extern int fontResizeRumble;
extern int fontResizeS;
extern int fontResizeGT;
extern int fontResizeIT;
extern int fontResizeG;
extern int fontResizeCD;
extern int lC;
extern int rC;
extern int lCWidth;
extern int rCHeight;
extern int fontResizeM;
int oldmX = 0;
int oldmY = 0;

extern "C" {
extern unsigned char guioff;
extern bool actRaiser2;
}


extern void readInput();
//extern "C" {

extern XINPUT_STATE g_InputStates[4];
// Global instance of custom gamepad devices
extern XBGAMEPAD g_Gamepads[4];
//}



extern int favorites;
extern void saveFavorites();
extern void saveFavoritesExit();
extern void restoreRomBrowser();
extern int backupChanged;
extern void buildFavorites();
extern void addToFavorites();
extern void removeFromFavorites();

extern void loadAnimation();
extern void clearAnimation();
extern  float leftMove;
extern  float rightMove;
extern  float topMove;
extern  float bottomMove;
extern float zoomSpeed;
extern int zoomOver;

extern  float    zLeftScale;
extern float 	zRightScale;
extern float 	zTopScale;
extern float 	zBottomScale;

extern void SwitchFullScreen(void);
extern  void pauseMusic();
extern void pauseMusic();
extern void resumeMusic();
extern void updateSound();
extern void setFlickerSoft();
extern void setTexture();
extern void showMemory();
extern int showAvailMemory;
extern void drawTime();
//struct Input ZsnesInp;
//HANDLE ZsnesGamePad=NULL;
//XINPUT_STATE ZsnesPad;
void outofmemory();
extern int Disable34;

extern unsigned char NewGrfx;

//void init();
unsigned char * keys;
extern int DisableMenuSounds;
extern "C" void allocmem();

//struct Input ZsnesInp2;
//HANDLE ZsnesGamePad2=NULL;
//XINPUT_STATE ZsnesPad2;

//struct Input ZsnesInp3;
//HANDLE ZsnesGamePad3=NULL;
//XINPUT_STATE ZsnesPad3;

//struct Input ZsnesInp4;
//HANDLE ZsnesGamePad4=NULL;
//XINPUT_STATE ZsnesPad4;

extern  void playEffect(int i); 
extern  void updateEffect(); 
extern int MenuRender();
extern void loadCheat();

extern "C" void init();
extern "C" unsigned char fname[512];
extern "C" unsigned char fnames[512];
extern "C" unsigned char fnamest[512];
extern "C" unsigned char *romdata;;
class FileMenu
{
public:
  FileMenu();
  int init();
  int scan();
  void exit();
  int render();
  int scroll(int amount);
  int getFilePath(char *name);


  int nameReset();
  int nameFind(char *path);
  int nameAdd(char *entry);
  int nameSizeUp();
  int nameOffset(int index);

  char currentPath[260];
  char *nameList;
  int nameSize,nameMax;
  int nameCount;

  int choiceFocus;
};

extern void switchSongs();
extern FileMenu FileMenu;
extern bool a;
extern bool b;
extern HRESULT  stInitialize();
extern int FileMenuSelected;
#include <vector>
#include <string>
using namespace std;
struct fileDisplay {
	std::string name;
	std::string path;
	bool isAGame;
	bool isARoot;
	bool isADirectory;
	bool topLevel;
	int soundTrack;
	int song;
	int archive;
};

struct archType {
	std::string name;
	std::string path;
};


extern vector<fileDisplay> fileList;
extern unsigned char getRumbleValue(unsigned short address, unsigned char bank) ;
extern int archiveMode;
extern void processArchiveList();
extern void releaseMP();
extern int mpType;
extern int errorFound;
extern void drawXKeyboard();
extern bool checkStateK();
struct rumbleSetting { 
	
	bool isActive; 
	unsigned short address; 
	unsigned char bank; 
	int rumbleType; 
	unsigned char currentValue; 
	unsigned char previousValue; 
	std::string description; 
	int motorOneStrength; 
	int motorTwoStrength; 
	float motorOneTime; 
	float motorTwoTime; 
	int controller; 
	int rumbleCount;
	int rumbleTarget;
}; 
extern int displayRumbleConfig;

extern vector<rumbleSetting> rumbleList; 
extern void stopRumble();
extern unsigned char leftRumble;
extern unsigned char rightRumble;
extern unsigned char getMemoryLo(unsigned short address, unsigned char bank, int bankSize);
extern unsigned char getMemoryHi(unsigned short address, unsigned char bank, int bankSize);
extern int musicType;
extern void changePreview();
extern void updateZipFilename();
extern unsigned char leftAnalog;
extern unsigned char rightAnalog;
extern float scrollSpeed;
extern double slow;
extern void  searchDirectory(std::string, bool);
extern int automaticScreenShotSwitching;
extern int analogStill;
extern float lastTimeScreenShotSwitch ;
extern float currentTimeScreenShotSwitch ;
extern LARGE_INTEGER ticksPerSecondScreenShotSwitch;
extern LARGE_INTEGER tickScreenShotSwitch;   // A point in time
extern LARGE_INTEGER timeScreenShotSwitch;   // For converting tick into real time
extern int correctAppState();
extern void changePreviewUp();
extern void changePreviewDown();
extern   int oldScreenShot ;
extern int screenShotSlotAuto;
struct Turbo { 
  int turboA ; 
  int turboB ; 
  int turboX ; 
  int turboY ; 
  int turboL ; 
  int turboR ; 
}; 
extern int keyBoardDraw;
extern void drawKeyboard();
extern int map34Changed;
extern int map34;
extern int fastTurbo;
extern Turbo tButtons[4];
WAVEFORMATEX wfx;
extern "C" {
    extern unsigned char FPSAtStart;
    volatile int SPCSize;



	extern unsigned char wramdataa[65536];
	extern unsigned char wramdata[65536];
	extern unsigned char ram7fa[65536];
    

	extern unsigned int pl1selk;
    extern unsigned int pl1startk;
    extern unsigned int pl1upk;
    extern unsigned int pl1Ak;
    extern unsigned int pl1Bk;
    extern unsigned int pl1Lk;
    extern unsigned int pl1Rk;
    extern unsigned int pl1Xk;
    extern unsigned int pl1Yk;
    extern unsigned int pl1downk;
    extern unsigned int pl1leftk;
    extern unsigned int pl1rightk;
    extern unsigned int pl2selk;
    extern unsigned int pl2startk;
    extern unsigned int pl2upk;
    extern unsigned int pl2Ak;
    extern unsigned int pl2Bk;
    extern unsigned int pl2Lk;
    extern unsigned int pl2Rk;
    extern unsigned int pl2Xk;
    extern unsigned int pl2Yk;
    extern unsigned int pl2downk;
    extern unsigned int pl2leftk;
    extern unsigned int pl2rightk;
    extern unsigned int pl3selk;
    extern unsigned int pl3startk;
    extern unsigned int pl3upk;
    extern unsigned int pl3Ak;
    extern unsigned int pl3Bk;
    extern unsigned int pl3Lk;
    extern unsigned int pl3Rk;
    extern unsigned int pl3Xk;
    extern unsigned int pl3Yk;
    extern unsigned int pl3downk;
    extern unsigned int pl3leftk;
    extern unsigned int pl3rightk;
    
    extern unsigned int pl4selk;
    extern unsigned int pl4startk;
    extern unsigned int pl4upk;
    extern unsigned int pl4Ak;
    extern unsigned int pl4Bk;
    extern unsigned int pl4Lk;
    extern unsigned int pl4Rk;
    extern unsigned int pl4Xk;
    extern unsigned int pl4Yk;
    extern unsigned int pl4downk;
    extern unsigned int pl4leftk;
    extern unsigned int pl4rightk;



	extern unsigned int pl1Xtk; 
	extern unsigned int pl1Ytk; 
	extern unsigned int pl1Atk; 
	extern unsigned int pl1Btk; 
	extern unsigned int pl1Ltk; 
	extern unsigned int pl1Rtk;

	extern unsigned int pl2Xtk; 
	extern unsigned int pl2Ytk; 
	extern unsigned int pl2Atk; 
	extern unsigned int pl2Btk; 
	extern unsigned int pl2Ltk; 
	extern unsigned int pl2Rtk;

	extern unsigned int pl3Xtk; 
	extern unsigned int pl3Ytk; 
	extern unsigned int pl3Atk; 
	extern unsigned int pl3Btk; 
	extern unsigned int pl3Ltk; 
	extern unsigned int pl3Rtk;

	extern unsigned int pl4Xtk; 
	extern unsigned int pl4Ytk; 
	extern unsigned int pl4Atk; 
	extern unsigned int pl4Btk; 
	extern unsigned int pl4Ltk; 
	extern unsigned int pl4Rtk;


	extern unsigned char Turbo30hz;
    
    
    extern unsigned char pl1contrl;
    extern unsigned char pl2contrl;
    extern unsigned char pl3contrl;
    extern unsigned char pl4contrl;
    extern unsigned char KeyFastFrwrd;
    extern unsigned char KeyRewind;
    extern unsigned char KeySaveState;
    extern unsigned char KeyLoadState;
    extern unsigned char KeyNewGfxSwt;
	extern unsigned char KeyQuickRst;
    extern unsigned char KeyUsePlayer1234;
    extern void statesaver();
    extern void loadstate();
	extern void StartGUI();
	extern unsigned char KeyVolUp;
    extern unsigned char KeyVolDown;
	extern void ProcessRewind();
	extern unsigned char SnowOn;
	extern void membank0w16(unsigned short, unsigned char);
	extern unsigned int snesmmap[256];
	extern unsigned char srama[65536];
	extern unsigned char romtype;
    extern unsigned char TwelveHourClock = 1; 
	void ClockOutput(); 
}	

extern int firstVidRender;
extern float BGVol;
extern "C" {
   #include <stdio.h>
   #include <time.h>
	extern unsigned char cfgper2exec;
extern unsigned char per2exec;
}
#include <math.h>
#include <dsound.h>
#include "resource.h"

extern LPDIRECTSOUND8  m_pDSound;
extern int SetupMatrices();
extern int firstExit;
D3DLOCKED_RECT lockScreen;
extern void StartRumble(int slot);
extern void ProcessRumble();
extern bool rumbleOneInProgress ;
extern bool rumbleTwoInProgress ;

extern void LoadRumbleFile(std::string cSum);
extern void loadZRumble();
#ifdef __GNUC__ //MinGW

//Simple start and end structure, set as volatile so perhaps we can use -O1+ later
#define ASM_BEGIN asm volatile ( \
ASM_COMMAND(pushad)
#define ASM_END ASM_COMMAND(popad) \
);
//All commands need quotes and a newline and tab. C vars are _ prefixed
#define ASM_COMMAND(line) #line"\n\t"
#define ASM_COMMAND2(line, part2) #line", "#part2"\n\t"
#define ASM_COMMAND3(line, var) #line", _"#var"\n\t"
//Just for the prefix
#define ASM_CALL(func) ASM_COMMAND(call _ ## func)
//A function call is a simple register backup, call, restore
#define asm_call(func) ASM_BEGIN \
ASM_COMMAND(pushad) \
ASM_CALL(func) \
ASM_COMMAND(popad) \
ASM_END

#else //MSVC

#define ASM_BEGIN _asm {
#define ASM_END };

//MSVC is all straight foward about these
#define ASM_COMMAND(line) line
#define ASM_COMMAND2(line, part2) line, part2
#define ASM_COMMAND3(line, var) ASM_COMMAND2(line, var)
//Next is not really special either
#define ASM_CALL(func) ASM_COMMAND(call func)
//Using this weird style because of MSVCs bad parsing
#define asm_call(func) _asm pushad \
_asm call func \
_asm popad

#endif

DWORD WindowWidth = 256;
DWORD WindowHeight = 224;
DWORD FullScreen=0;
DWORD Moving=0;
DWORD SoundBufferSize=1024*18;
DWORD FirstSound=1;

int SoundEnabled=1;
int UsePrimaryBuffer=0;

DWORD FirstActivate = 1;

#define BYTE   unsigned char
#define WORD   unsigned short
#define DWORD  unsigned long

HANDLE debugWindow = 0;

extern "C"
{
HINSTANCE hInst;
unsigned char KitchenSync = 0;
unsigned char Force60hz = 0;
extern unsigned char mousexloc;
extern unsigned char mouseyloc;
}
extern void gunOnScreen();
extern int Environment;

extern int musicPlayer;
extern int playingMusic;
//extern int vgplaying;
extern int InitVGMusic(char *path);
extern int animateFramevg();
extern int animateFramevg2();
extern void songIsOver();
extern void previousTrack();
extern void nextTrack();
extern void stopMusic();
extern void updateVGStream();
extern int flipScreen;


LPDIRECTSOUNDBUFFER     lpPrimaryBuffer = NULL;
DSBUFFERDESC dsbd;

LPVOID lpvPtr1;
DWORD dwBytes1;
LPVOID lpvPtr2;
DWORD dwBytes2;
RECT                    rcWindow;

DWORD                   X1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   X2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   Y1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   Y2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   Z1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   Z2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RX1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RX2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RY1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RY2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RZ1Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   RZ2Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   S01Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   S02Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   S11Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   S12Disable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   POVDisable[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   NumPOV[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
DWORD                   NumBTN[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

DWORD                   CurrentJoy=0;

DWORD                   BitDepth;
DWORD                   GBitMask;
BYTE                    BackColor=0;

float                   MouseMinX=0;
float                   MouseMaxX=256;
float                   MouseMinY=0;
float                   MouseMaxY=223;
float                   MouseX;
float                   MouseY;
float                   MouseMoveX;
float                   MouseMoveY;
int                     MouseMove2X;
int                     MouseMove2Y;
BYTE                    MouseButtonPressed;

BYTE                    IsActivated=1;

WORD                    PrevRes=0;
RECT                    BlitArea;
BYTE                    AltSurface=0;
extern "C" {
DWORD                   MouseButton;
DWORD                   SurfaceX=0;
DWORD                   SurfaceY=0;
}

HANDLE hLock, hThread;
DWORD dwThreadId, dwThreadParam, semaphore_run;

extern "C" {
    int  SemaphoreMax = 5;
    void InitSemaphore();
    void ShutdownSemaphore();
    void DisplayWIPDisclaimer();
    void InitDebugger();
    
    void Clear2xSaIBuffer();
    void clear_display();
	void DrawWater();
	extern unsigned char GUIEffect;
}



static HMODULE hM_ddraw = NULL, hM_dsound = NULL,hM_dinput8 = NULL;

typedef HRESULT (WINAPI* lpDirectInput8Create)(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID *ppvOut, LPUNKNOWN punkOuter);
static lpDirectInput8Create pDirectInput8Create;

typedef HRESULT (WINAPI* lpDirectDrawCreateEx)( GUID FAR * lpGuid, LPVOID  *lplpDD, REFIID  iid,IUnknown FAR *pUnkOuter );
static lpDirectDrawCreateEx pDirectDrawCreateEx;

typedef HRESULT (WINAPI* lpDirectSoundCreate8)(LPCGUID pcGuidDevice, LPDIRECTSOUND8 *ppDS8, LPUNKNOWN pUnkOuter);
static lpDirectSoundCreate8 pDirectSoundCreate8;

#define UPDATE_TICKS_GAME (1000.0/59.948743718592964824120603015060)
#define UPDATE_TICKS_GAMEPAL (20.0)
#define UPDATE_TICKS_GUI (1000.0/36.0)
#define UPDATE_TICKS_UDP (1000.0/60.0)

double start, end, freq, update_ticks_pc, start2, end2, update_ticks_pc2;

extern "C" BYTE vsyncon;
extern "C" BYTE KitchenSync;
extern "C" BYTE TripleBufferWin;
extern "C" BYTE PauseFocusChange;

extern "C" {
    extern BYTE GUIOn;
    extern BYTE GUIOn2;
}
DWORD InputEn=1;
extern int disableCombo ;
extern int lastChoice;
extern int y2;
extern int root;
extern "C" {
    void initwinvideo();
    void DosExit(void);
    extern BYTE cfgsoundon;
    extern BYTE StereoSound;
    
    extern BYTE HighPriority;
    extern BYTE AlwaysOnTop;
    extern BYTE SaveMainWindowPos;
    extern BYTE AllowMultipleInst;
    extern BYTE DisableScreenSaver;
    extern BYTE TrapMouseCursor;
    extern signed short int MainWindowX;
    extern signed short int MainWindowY;
    extern int CurKeyPos;
    extern int CurKeyReadPos;
    extern int KeyBuffer[16];
    extern BYTE debugger;
}

BYTE PrevStereoSound;
DWORD PrevSoundQuality;

extern "C" DWORD converta;
extern "C" unsigned int BitConv32Ptr;
extern "C" unsigned int RGBtoYUVPtr;
extern "C" unsigned char cvidmode;
extern "C" unsigned char hqFilter;
extern "C" unsigned short resolutn;

DWORD FirstVid=1;
DWORD FirstFull=1;
DWORD DMode=0;
DWORD SMode=0;
DWORD DSMode=0;
DWORD NTSCMode=0;
DWORD prevHQMode=~0;
DWORD prevNTSCMode=0;
DWORD prevScanlines=~0;
WORD Refresh = 0;
extern "C" BYTE GUIWFVID[];
extern "C" BYTE GUIDSIZE[];
extern "C" BYTE GUISMODE[];
extern "C" BYTE GUIDSMODE[];
extern "C" BYTE GUIHQ2X[];
extern "C" BYTE GUIHQ3X[];
extern "C" BYTE GUIHQ4X[];
extern "C" BYTE GUINTVID[];

extern "C" void FreeDirectX(){}
extern "C" void DXLoadError(){FreeDirectX();}
extern "C" void ImportDirectX(){}
void ReleaseDirectDraw();
void ReleaseDirectSound();
void ReleaseDirectInput();
int InitDirectDraw();
int ReInitSound();

extern "C"
{
   void drawscreenwin(void);
   DWORD LastUsedPos=0;
   DWORD CurMode=~0;
   void initDirectDraw(){InitDirectDraw();}
   void reInitSound(){ReInitSound();}
}

void DDrawError(){}
void DrawScreen(){}
extern "C" void MultiMouseInit();
extern "C" void MultiMouseShutdown();

BOOL InputAcquire(void){InputEn = 1;return TRUE;}
BOOL InputDeAcquire(void){InputEn = 0;return TRUE;}

extern "C" void CheckPriority(){}
extern "C" void CheckAlwaysOnTop(){}
extern "C" void CheckScreenSaver(){}
extern "C" void MinimizeWindow(){IsActivated = 0;}
extern "C" BYTE MouseWheel;
BOOL InputRead(void){return TRUE;}
extern "C" void SaveSramData(void);
extern "C" void GUISaveVars(void);
extern "C" void createnewcfg(void);
extern void applyCheatCodesLoROM();
extern void applyCheatCodesHiROM();
void ExitFunction(){}
LRESULT CALLBACK Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){return 0;}
int RegisterWinClass(void){return TRUE;}
extern "C" { extern unsigned char PrimaryBuffer; }

BOOL ReInitSound(){return TRUE;}
void ReleaseDirectInput(){}
void ReleaseDirectSound(){}
void ReleaseDirectDraw(){}
void DInputError(){}
bool InitInput(){return TRUE;}
void TestJoy(){}
int InitDirectDraw(){return TRUE;}
void initCheatCodes();
BYTE* SurfBuf;
DWORD LockSurface(){return 0;}
void UnlockSurface(){}
int mX = 640/2;
int mY = 480/2;

extern "C" {
	extern unsigned char opexec268 ;	// # of opcodes/scanline in 2.68Mhz mode
extern unsigned char opexec358 ;	// # of opcodes/scanline in 3.58Mhz mode (228/180)
extern unsigned char opexec268cph;	// # of opcodes/hblank in 2.68Mhz mode
extern unsigned char opexec358cph;	// # of opcodes/hblank in 3.58Mhz mode (56/50)
extern unsigned char opexec268b;	// # of opcodes/scanline in 2.68Mhz mode
extern unsigned char opexec358b;	// # of opcodes/scanline in 3.58Mhz mode (228/180)
extern unsigned char opexec268cphb;	// # of opcodes/hblank in 2.68Mhz mode
extern unsigned char opexec358cphb;	
	extern unsigned char cfgStereoSound;
	extern unsigned char mousebuttons;
	extern int Bper2exec ;
	extern int Bcfgper2exec;
	extern unsigned char scanline;
	extern unsigned char cfgscanline;
    void WinUpdateDevices();
    short Buffer[1800*2];
    int X, Y;
    DWORD pitch;
	extern unsigned char ForceROMTiming;
    DWORD SurfBufD;
    int count, x,count2;
    HRESULT hr;
    int i;
    short *Sound;
    DWORD CurrentPos;
    DWORD WritePos;
    DWORD T60HZEnabled=0;
    DWORD T36HZEnabled=0;  
    extern unsigned int pressed;
    extern unsigned char romispal;  
    void Stop36HZ(void){T36HZEnabled=0;}
    char WinMessage[256];
    void clearwin();
    char WinName[]={"ZSNESW\0"};
    extern unsigned int vidbuffer;
    extern void SoundProcess();
	extern void UpdateVolume();
    extern int DSPBuffer;
    int * DSPBuffer1;
    DWORD ScreenPtr;
    DWORD ScreenPtr2;
    extern void GUI36hzcall(void);
	extern void Setper2exec();
    extern void Game60hzcall(void); 
    extern unsigned char MMXSupport;
    extern "C" {volatile int buffer_ptr;}
    int DataNeeded;    
    extern unsigned char curblank;
    extern DWORD AddEndBytes;
    extern DWORD NumBytesPerLine;
    extern unsigned char * WinVidMemStart;
    extern void copy640x480x16bwin(void);
    extern void hq2x_16b(void);
    extern void hq2x_32b(void);
    extern void hq3x_16b(void);
    extern void hq3x_32b(void);
    extern void hq4x_16b(void);
    extern void hq4x_32b(void);
    extern unsigned char NGNoTransp;
    extern unsigned char newengen;
    extern void ClearWin16();
    extern void ClearWin32();
    void clearwin(){}
    void clear_display(){}
    extern void DrawWin256x224x16();
    extern void DrawWin256x224x32();
    extern void DrawWin320x240x16();
    volatile __int64 copymaskRB = 0x001FF800001FF800LL;
    volatile __int64 copymaskG = 0x0000FC000000FC00LL;
    volatile __int64 copymagic = 0x0008010000080100LL;
    volatile __int64 coef = 0x0066009a0066009aLL;
    
    //extern BYTE MotionBlur;
    extern WORD totlines;
    extern unsigned char FPSAtStart;
    extern unsigned char FPSOn;
    extern unsigned char En2xSaI; 
    extern void GUIDoReset();
	extern void clearmem();
    extern void clearvidsound();
    int ZsnesModeUpdate(){return 0;}
    int gpInit = 0;
    int m = 0;




		//snesmouse = 0 = Controller
	//snesmouse = 1 = mouse1
	//snesmouse = 2 = mouse2
	//snesmouse = 3 = Super Scope
	//snesmouse = 4 = Lethal Enforcer
	
	//mousebuttons = 0x0000 0001 = Button 1 pressed
	//mousebuttons = 0x0000 0000 = Button 1 not pressed
	//mousebuttons = 0x0000 0010 = Button 2 pressed
	//mousebuttons = 0x0000 0000 = Button 2 not pressed
	int GetMouseX(void){
		//if(lightGunMode == 0) {
			return mX;
		//}
		//else {
		//	return 0;//fix
		//}
	}
    
	int GetMouseY(void){
	//	if(lightGunMode == 0) {
			return mY;
	//	}
	//	else {
		//	return 0;
	//	}
	}

    int GetMouseMoveX(void){
		//if(lightGunMode == 0) {
			 return mX - oldmX;
		//}
		//else {
		//	return 0;
		//}
	}
    
	int GetMouseMoveY(void){
		//if(lightGunMode == 0) {
			 return mY - oldmY;
		//}
		//else {
		//	return 0;
		//}
	}
    
	int GetMouseButton(void){
		//if(lightGunMode == 0) {
			return mousebuttons;
		//}
		//else {
		//	return 0;
		//}
	}



    void SetMouseMinX(int MinX){MouseMinX = (float) MinX;}
    void SetMouseMaxX(int MaxX){MouseMaxX = (float) MaxX;}
    void SetMouseMinY(int MinY){MouseMinY = (float) MinY;}
    void SetMouseMaxY(int MaxY){MouseMaxY = (float) MaxY;}
    void SetMouseX(int X){MouseX = (float) X;}
    void SetMouseY(int Y){MouseY = (float) Y;}
    void ZsnesPage(){MouseX = 0;MouseY = 0;}
    void DocsPage(){MouseX = 0;MouseY = 0;}
    extern "C" signed int NumberOfOpcodes;
    void WriteLine(){char buf[50];sprintf(buf, "%d\n", NumberOfOpcodes);}
    void InitDebugger(){}
    int CheckBattery(){return 0;}
    int CheckBatteryTime(){return 0;}
    int CheckBatteryPercent(){return 0;}
    void DoSleep(){Sleep(1);}
    extern "C" unsigned int PrevBuildNum;
    void DisplayWIPDisclaimer(){}
}

extern "C" void InitPreGame();
extern "C" void DeInitPostGame();
extern "C" void InitSPC();
extern "C" void setnoise();
extern "C" void SystemInit();

