//D3DCOLOR_XXXX macros are defined in d3d8types.h and will not be redefined here.
#ifndef D3DCOLOR_ARGB

#define D3DCOLOR_ARGB(a,r,g,b) (D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
#define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)
#define D3DCOLOR_XRGB(r,g,b)   D3DCOLOR_ARGB(0xff,r,g,b)
#define D3DCOLOR_COLORVALUE(r,g,b,a) D3DCOLOR_RGBA((DWORD)((r)*255.f),(DWORD)((g)*255.f),(DWORD)((b)*255.f),(DWORD)((a)*255.f))

#endif

#define TEXTALIGN_HORZ	0x0F
#define TEXTALIGN_LEFT	0x00
#define TEXTALIGN_RIGHT	0x01
#define TEXTALIGN_CENTER 0x02
#define TEXTALIGN_HCENTER 0x02

#define TEXTALIGN_VERT	0xF0
#define TEXTALIGN_TOP	0x00
#define TEXTALIGN_BOTTOM 0x10
#define TEXTALIGN_VCENTER 0x20

#define TEXTALIGN_ORG	(TEXTALIGN_VCENTER|TEXTALIGN_CENTER)
#define MAX_NUM_VERTICES 50*6


bool IsMultimonitorAvailable();

bool __cdecl DrawBitmapToSurface(LPDIRECTDRAWSURFACE7 pddsTexture,int x,int y,const char* strName );
int __cdecl CopyTargetImage(int stage,LPDIRECTDRAWSURFACE7 tex=NULL,int step=0);
LPVOID GetSurfacePointer(LPDIRECTDRAWSURFACE7 psurf);
void ReleaseSurfacePointer(LPDIRECTDRAWSURFACE7 psurf);

/*
enum AUX1STATE
{
	AUX1_TEAM,
	AUX1_FREE,
	AUX1_TEAM_MESSAGE,
	AUX1_FREE_MESSAGE
};
*/
#define AUX1_TEAM		0
#define	AUX1_FREE		1
#define	AUX1_TEAM_MESSAGE	2
#define AUX1_FREE_MESSAGE	3

//Create one instance per device per font type (typeface, size, bold, italic).
class CHSHFont
{
	//Used temporarily; no need per instance, so declared static.
	static D3DTLVERTEX				m_pVB[300];

	LPDIRECT3DDEVICE7		m_pd3dDevice;
	LPDIRECTDRAWSURFACE7	m_pTexture;
	DWORD	m_dwTexWidth;
	DWORD	m_dwTexHeight;
	float	m_fTexCoords[128-32][4];
	
	DWORD	m_dwSavedStateBlock;
	DWORD	m_dwDrawTextStateBlock;
	
	DWORD m_dwMaxWidth;//Width of the widest single character..
public:
	CHSHFont();
	~CHSHFont();

	void Init(LPDIRECTDRAW7 pDD, LPDIRECT3DDEVICE7 pd3dDevice ,const char* strFontName, DWORD dwHeight, bool fBold,bool fItalic);
	void Cleanup();

	HRESULT GetTextExtent(const char* strText, SIZE* pSize );
	HRESULT DrawText( float x, float y, DWORD dwColor,const char* strText);
	HRESULT DrawText( float x, float y, DWORD dwColor,const char* strText, DWORD dwFlags);
	HRESULT DrawTextMultiLine( float sx, float sy, DWORD dwColor,const char* strText,DWORD dwFlags);
	HRESULT DrawTextVerticalOrg( float sx, float sy, DWORD dwColor,const char* strText);
};

class CHSH_Device
{
public:
	CHSH_Device* m_pOtherHSHD;
public:
	LPDIRECTDRAW7		 pDD;
	LPDIRECTDRAWSURFACE7 pDDSFront; 			//Required for Flip..
	LPDIRECTDRAWSURFACE7 pDDSBack;				//Required for Drawing.
	LPDIRECTDRAWSURFACE7 pDDSTarget;			//The actual rendering texture.
	LPDIRECTDRAWSURFACE7 pDDSTexture;			//The actual rendering texture.
	LPDIRECT3DDEVICE7	 pD3DDevice;
	CHSHFont	pFont[4];
	SIZE size_back;
	SIZE size_target;
	float tw,th;//texture width,height
	CHSH_Device();

	bool InitFirst(int devicenum,DWORD resx,DWORD resy, CHSH_Device* pOtherHSHD = NULL);
	bool InitSecond(DWORD tresx,DWORD tresy);
	virtual bool Release();
	virtual bool BeginScene()=0;
	virtual bool EndScene()=0;

	//DrawingRoutines..
	void DrawQuad(int x1,int y1,int x2,int y2,DWORD dwColor);
	void DrawQuadList(int quadcount,RECT * prc,DWORD dwColor);
	void DrawLine(int x1,int y1,int x2,int y2,DWORD dwColor);
	void DrawLineStrip(int point_count,POINT* pts,DWORD dwColor);
	void DrawLineList(int line_count,POINT* pts,DWORD dwColor);
	void DrawFrame(int x1,int y1,int x2,int y2,DWORD dwColor);
	void DrawFrameList(int framecount,RECT * prc,DWORD dwColor);
	void DrawThickFrame(int x1,int y1,int x2,int y2,int t,DWORD dwColor);
	void DrawThickFrameList(int framecount,RECT* prc,int t,DWORD dwColor);

	void DrawTextureRotate(float u1,float v1,float u2,float v2,float tx,float ty,float x,float y,float e,DWORD color);
	void DrawTexture(float x1,float y1,float x2,float y2,DWORD dwColor, float u1,float v1,float u2,float v2);
	void DrawTexture2(float x,float y,float w,float h,DWORD dwColor, float u1,float v1,float u2,float v2);
	void DrawTexture(float x,float y,DWORD dwColor, float u1,float v1,float u2,float v2);
	void DrawTexture(float pt[4][2],DWORD dwColor,float tx1,float tx2,float ty1,float ty2);

	void SetRenderTargetBackbuffer();
	void SetRenderTargetTexture();

};

extern const char g_szRadarShutdown[];
extern const char g_szRadarStartup[];
extern const char g_szRadarJumpjet[];
extern const char g_szRadarZoom[];
extern char g_aaRadarText[12][32];

class CRadar_Device:public CHSH_Device
{
public:
	int recent_state[12];
// MSL 5.03 Mechview
    POINT m_ptMechViewDim;
    POINT m_ptMechViewSize;
    int m_nMechViewCurs;
    int m_nMechViewMax;

public:
	CRadar_Device();
	~CRadar_Device();
	bool InitFirst();
	bool InitSecond();
	LPDIRECTDRAWSURFACE7 pDDSBackground;		//one attatched image sruface
	LPDIRECTDRAWSURFACE7 pDDSRadarDamageTexture;//Mech damage texture..
// MSL 5.03 Mechview
    LPDIRECTDRAWSURFACE7 pDDSMechViewBkgnd;
    LPDIRECTDRAWSURFACE7 pDDSMechViewBeagle;
    LPDIRECTDRAWSURFACE7 pDDSMechViewEcm;
    LPDIRECTDRAWSURFACE7 pDDSMechViewAms;
    LPDIRECTDRAWSURFACE7 pDDSMechViewJumpjet;
    LPDIRECTDRAWSURFACE7 pDDSMechViewLightamp;
    LPDIRECTDRAWSURFACE7 m_pDDSMechView;    //m x n - tiled 3D MechView images

	virtual bool BeginScene();		//Rotates and outputs texture; pre-renders background image...
	virtual bool EndScene();
	virtual bool Release();
	bool LoadRadarDamageTexture(const char *mechtexturename);
	bool DrawBackText(int state[12]);
	bool MapDrawn;
};

class CMFD_Device:public CHSH_Device
{
public:
	//CHSHFont	SmallFont;
	LPDIRECTDRAWSURFACE7 pDDSMechTexture;//Texture for storing 3D mech image...
	LPDIRECTDRAWSURFACE7 pDDSDamageTexture;//Mech damage texture..
// MSL 5.03 Target Damage Display
	LPDIRECTDRAWSURFACE7 pDDSTargetTexture;//Target damage texture..

	DWORD ch;						//current channel....
	CHSH_Device* m_pRightDevice;	// mode 4: pointer to right 640x480 device (NULL in modes 1-3)
public:
	CMFD_Device();
	~CMFD_Device();
	bool InitFirst();
	bool InitSecond();
	bool BeginChannel(DWORD channel);// Select channel 0-4.
	bool EndChannel();
	virtual bool BeginScene();		//Performs normal BeginScene/EndScene.
	bool BeginSceneRight();		// mode 4: clear+grid on right device at sh_step==1
	virtual bool EndScene();		//Performs the Flip.
	virtual bool Release();
	bool LoadDamageTexture(const char *mechtexturename);
// MSL 5.03 Target Damage Display
	bool LoadTargetTexture(const char *mechtexturename);
    bool DrawMFDBackText(char text[8][32], const int* pnStates = NULL);
	//bool DrawMFDBackText(char text[8][32],RIOBUTTON * state);
	bool DrawMFDBackTextAux3(int state);
	bool DrawMFDBackGrid();
	bool DrawMFDDefaultBackAux1(int state);
};

// Mode 4 (split dual 640x480): right-side MFD device on its own 640x480 monitor.
// Same GPU as the left device (mfd_device), so VRAM textures are shared.
class CMFDRight_Device : public CHSH_Device
{
public:
	CMFDRight_Device() {}
	bool InitFirst();
	bool InitSecond();
	virtual bool BeginScene() { return true; }
	virtual bool EndScene()   { return true; }
	virtual bool Release()    { return CHSH_Device::Release(); }
};

class CMR_Device:public CHSH_Device
{
public:
	bool map_loaded;
public:
	CMR_Device();
	~CMR_Device();
	bool InitFirst();
	bool InitSecond();
	LPDIRECTDRAWSURFACE7 pDDSBackground;		//one attatched image sruface
	LPDIRECTDRAWSURFACE7 pDDSMapTexture;		//Texture for scrollable map image..
	LPDIRECTDRAWSURFACE7 pDDSMRTargetTexture;//Mission Review target texture..
	bool LoadMRTargetTexture(const char *mechtexturename);
	virtual bool BeginScene();		//Rotates and outputs texture; pre-renders background image...
	virtual bool EndScene();
	virtual bool Release();
};

void HSH_EnterFullScreen2();
void HSH_DirectDrawRelease2();

extern CRadar_Device radar_device;
extern CMFD_Device mfd_device;
extern CMFDRight_Device mfd_device_right;
extern CMR_Device mr_device;
extern bool use_shgui;
extern int sh_step;
extern bool hsh_initialized;
extern bool hsh_mrdev_initialized;
extern bool sh_isdeathmode;
extern bool g_f3dtarget;
