#ifndef CD3D_MANAGER
#define CD3D_MANAGER

#include <windows.h>
#include "stringc.h"
#include "CGraphicSystem.h"

#include "d3d8.h"
#include "d3dx8.h"
#include "CD3DImageManager.h"

#include "D3DFont.h"
#include <basetsd.h>

#include "D3DStructs.h"	//Defines some custom D3D Structures

struct LinePoint 
{
	float x, y, z, rhw;
	DWORD color;
};



class CD3DManager : CGraphicSystem
{

public:
	
	CD3DManager();
	virtual ~CD3DManager();

	virtual bool Init(int Width, int Height);
	virtual bool Shutdown();
	VOID    Cleanup3DEnvironment();

	virtual getWidth(){ return ScrWidth;}
	virtual getHeight(){ return ScrHeight; }
	
	//----------------------------------------------------------------//
	//	Window Creating Fullscreen/Windowed mode toggle, etc
	//----------------------------------------------------------------//

	virtual HRESULT Create( HINSTANCE hInstance );
	virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
	virtual INT     Run();

	//----------------------------------------------------------------//
	//	Frame prepping, etc
	//----------------------------------------------------------------//

	virtual void PrepFrame();		
	virtual void DrawFrame();		
	virtual void ReleaseFrame();	

	//----------------------------------------------------------------//
	//	Drawing Functions
	//----------------------------------------------------------------//

	virtual void DrawPixel(int, int);
	virtual void DrawLine(int x1, int y1, int x2, int y2, DWORD color);
	virtual void DrawHLine(int x1, int x2, int y);
	virtual void DrawVLine(int x, int y1, int y2);
	virtual void DrawRect(RECT r);

	virtual void DrawCImage(CImage* img,int x, int y);	
	virtual CImage* LoadCImage(STRINGC fileName);
	virtual D3DXIMAGE_INFO loadTexture(STRINGC fileName, LPDIRECT3DTEXTURE8& texture);


	virtual void FillScreen(int r, int g, int b);	
	virtual void PrintText(STRINGC txt, int x, int y);


protected:

	int ScrWidth, ScrHeight;    
    

    D3DCAPS8 *pd3dcaps; // Caps for the rendering device
	LPDIRECT3DTEXTURE8 Texture;

	CD3DFont* Font;
	CD3DImageManager ImageManager;

	//----------------------------------------------------------------//
	//	Internal D3D Functions
	//----------------------------------------------------------------//

	HRESULT Initialize3DEnvironment();
	virtual HRESULT AdjustWindowForChange();
	HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
	HRESULT BuildDeviceList();
	BOOL    FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType, D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );	
	virtual VOID    Pause( BOOL bPause );
	HRESULT ToggleFullscreen();
	HRESULT ForceWindowed();
	HRESULT Resize3DEnvironment();
	HRESULT UserSelectNewDevice();
	static INT_PTR CALLBACK SelectDeviceProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
	HRESULT CD3DManager::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,D3DFORMAT Format );

	//This will probably need to be changed
	
	virtual HRESULT InvalidateDeviceObjects();
	virtual HRESULT InitDeviceObjects();
	virtual HRESULT DeleteDeviceObjects();
    virtual HRESULT FinalCleanup()                             { return S_OK; }	
	virtual HRESULT RestoreDeviceObjects();

	// Main objects used for creating and rendering the 3D scene
    D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset
    HWND              m_hWnd;              // The main app window
    HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)
    LPDIRECT3D8       m_pD3D;              // The main D3D object	
    LPDIRECT3DDEVICE8 m_pd3dDevice;        // The D3D rendering device
    D3DCAPS8          m_d3dCaps;           // Caps for the device
    D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer
    DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing
    DWORD             m_dwWindowStyle;     // Saved window style for mode switches
    RECT              m_rcWindowBounds;    // Saved window bounds for mode switches
    RECT              m_rcWindowClient;    // Saved client area size for mode switches
	LPD3DXSPRITE	  m_pd3dxSprite;	   // Interface to Sprite routines

	// Internal variables used for timing
    BOOL              m_bFrameMoving;
    BOOL              m_bSingleStep;	
    FLOAT             m_fTime;             // Current time in seconds
    FLOAT             m_fElapsedTime;      // Time elapsed since last frame
    FLOAT             m_fFPS;              // Instanteous frame rate
    TCHAR             m_strDeviceStats[90];// String to hold D3D device stats
    TCHAR             m_strFrameStats[40]; // String to hold frame stats
	
	// Internal variables for the state of the app
    D3DAdapterInfo    m_Adapters[10];
    DWORD             m_dwNumAdapters;
    DWORD             m_dwAdapter;
    BOOL              m_bWindowed;
    BOOL              m_bActive;
    BOOL              m_bReady;

	// Overridable variables for the app
    TCHAR*            m_strWindowTitle;    // Title for the app's window
    BOOL              m_bUseDepthBuffer;   // Whether to autocreate depthbuffer
    DWORD             m_dwMinDepthBits;    // Minimum number of bits needed in depth buffer
    DWORD             m_dwMinStencilBits;  // Minimum number of bits needed in stencil buffer
    DWORD             m_dwCreationWidth;   // Width used to create window
    DWORD             m_dwCreationHeight;  // Height used to create window
    BOOL              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen

	//For line drawing
	LinePoint					 m_lineList[500];		//Thats room for 250 lines
	int							 m_lineIndex;			//What line index we are on
	LPDIRECT3DVERTEXBUFFER8		 m_D3DVertexBuffer;		// The Vertex Buffer





};







#endif