#ifndef RMWIN_H
#define RMWIN_H


struct videomode
{
	DWORD width, height, depth;
};

const int MAXDISPLAYMODES=100;

class RMWin : public CFrameWnd
{
public:
	RMWin();
	RMWin( int w, int h );
	BOOL Create( const CString& sTitle, int icon, int menu);
	void SetColorModel( D3DCOLORMODEL cm )  { colormodel=cm; }
	virtual void Render() = 0;
protected:
	int GetNumDisplayModes()  { return totaldisplaymodes; }
	BOOL ActivateDisplayMode( int index );
	int GetCurDisplayMode()  { return curdisplaymode; }
	BOOL GetDisplayModeDims( int index, DWORD& w, DWORD& h, DWORD& d );
	BOOL GetCurDisplayModeDims( DWORD& w, DWORD& h, DWORD& d );
	static void CheckResult( HRESULT );
	static void CheckDirectDrawResult( HRESULT );
	virtual void OnIdle(LONG)  { }
	static int GetMouseX()    { return mousex; }
	static int GetMouseY()    { return mousey; }
	D3DVALUE ScaleMesh( LPDIRECT3DRMMESHBUILDER, D3DVALUE );
	void UsePalette( CString filename );
	void UsePalette( PALETTEENTRY* );
	LPDIRECTDRAWSURFACE CreateSurface( DWORD w, DWORD h );
	BOOL ClearSurface(LPDIRECTDRAWSURFACE surf, DWORD clr);
	void SaveSurface( LPDIRECTDRAWSURFACE surf, int number );
protected:
	//{{AFX_MSG(RMWin)
	afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );
	afx_msg void OnDestroy();
	afx_msg void OnMouseMove( UINT state, CPoint point );
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	BOOL InitMainSurfaces();
	BOOL InitDisplayMode();
	BOOL ActivateDisplayMode(DWORD,DWORD,DWORD);
	void Initvars();
	virtual BOOL CreateScene() = 0;
	BOOL CreateDevice();
	GUID* GetGUID();
	BOOL InstallPalette();
	BOOL InstallPaletteFromFile();
	BOOL InstallPaletteFromArray();
	static HRESULT WINAPI DisplayModeAvailable(LPDDSURFACEDESC, LPVOID);
	static int CompareModes( const void *arg1, const void *arg2 );
protected:
	LPDIRECTDRAW ddraw;
	LPDIRECTDRAWSURFACE primsurf;
	LPDIRECTDRAWSURFACE backsurf;
	LPDIRECTDRAWSURFACE zbufsurf;
	LPDIRECTDRAWPALETTE palette;
	static LPDIRECT3DRM d3drm;
	static LPDIRECT3DRMFRAME scene;
	static LPDIRECT3DRMFRAME camera;
	static LPDIRECT3DRMDEVICE device;
	static LPDIRECT3DRMVIEWPORT viewport;
private:
	static DWORD modewidth, modeheight, modedepth;
	D3DCOLORMODEL colormodel;
	CRect winrect;
	LPDIRECTDRAWCLIPPER clipper;
	static int mousex;
	static int mousey;
	static UINT mousestate;
	static int totaldisplaymodes;
	static videomode displaymode[MAXDISPLAYMODES];
	static int curdisplaymode;
	CString palettefile;
	PALETTEENTRY* palettearray;
};

inline D3DCOLOR COLORREF_2_D3DCOLOR(COLORREF cref)
{
	D3DVALUE r=D3DVALUE(GetRValue(cref)/D3DVALUE(255));
	D3DVALUE g=D3DVALUE(GetGValue(cref)/D3DVALUE(255));
	D3DVALUE b=D3DVALUE(GetBValue(cref)/D3DVALUE(255));
	return D3DRMCreateColorRGB( r, g, b );
}

inline COLORREF D3DCOLOR_2_COLORREF(D3DCOLOR d3dclr)
{
	D3DVALUE red=D3DVALUE(255)*D3DRMColorGetRed(d3dclr);
	D3DVALUE green=D3DVALUE(255)*D3DRMColorGetGreen( d3dclr );
	D3DVALUE blue=D3DVALUE(255)*D3DRMColorGetBlue( d3dclr );
	return RGB((int)red,(int)green,(int)blue);
}


#endif
