/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#ifndef KEOS_D3D9RENDERSYSTEM_H
#define KEOS_D3D9RENDERSYSTEM_H

#include "KeosD3D9Prerequisites.h"
#include "KeosRenderSystem.h"
#include "KeosRenderWindow.h"

class CD3D9; 

namespace Keos
{
    //-----------------------------------------------------------------------
    /** Class for Direct3D9 renderer system.
     *
     */
    class CD3D9RenderSystem : public IRenderSystem
    {
    public :

        /** Constructor.
         */
        CD3D9RenderSystem();

        /** Destructor.
         */
        ~CD3D9RenderSystem();

		/** See RenderSystem.
         */
        const String& GetName(void) const;

        /** See IRenderSystem.
         */
        virtual IRenderWindow* Initialise(bool bAutoCreateWindow, const String& strWindowTitle = "KEOS Render Window");

        /** See IRenderSystem.
         */
        virtual IRenderWindow* CreateRenderWindow(const String &strName, uint nWidth, uint nHeight, uint nColourDepth, bool bFullScreen);
       
        /** See IRenderSystem.
         */
        virtual void Reinitialise(void);

        /** See IRenderSystem.
         */
        virtual void Shutdown(void);

        /** Start a scene render.
         */
        virtual void BeginScene(void) const;

        /** End a scene render.
         */
        virtual void EndScene(void) const;

        /** Print information on the API.
         */
        virtual void PrintInfo(void) const;

        /** See IRenderSystem.
         */
        virtual IBufferBase* _CreateVB(ulong nSize, ulong nStride, ulong nFlags) const;

        /** See IRenderSystem.
         */
        virtual IBufferBase* _CreateIB(ulong nSize, ulong nStride, ulong nFlags) const;

        /** See IRenderSystem.
         */
        virtual IDeclaration* CreateDeclaration(const TDeclarationElement* pElements, size_t nCount) const;

        /** See IRenderSystem.
         */
        virtual void _SetVB(uint nStream, const IBufferBase* pBuffer, ulong nStride, ulong nMinVertex, ulong nMaxVertex);

        /** See IRenderSystem.
         */
        virtual void _SetIB(const IBufferBase* pBuffer, ulong nStride);

        /** See IRenderSystem.
         */
        virtual void SetDeclaration(const IDeclaration* pDeclaration);

        /** See IRenderSystem.
         */
        virtual void DrawPrimitives(TPrimitiveType Type, ulong nFirstVertex, ulong nCount) const;

        /** See IRenderSystem.
         */
        virtual void DrawIndexedPrimitives(TPrimitiveType Type, ulong nFirstIndex, ulong nCount) const;

        /** See IRenderSystem.
         */
        virtual void PushMatrix(TMatrixType Type);

        /** See IRenderSystem.
         */
        virtual void PopMatrix(TMatrixType Type);

        /** See IRenderSystem.
         */
        virtual void LoadMatrix(TMatrixType Type, const CMatrix4& Matrix);

        /** See IRenderSystem.
         */
        virtual void LoadMatrixMult(TMatrixType Type, const CMatrix4& Matrix);

        /** See IRenderSystem.
         */
        virtual void GetMatrix(TMatrixType Type, CMatrix4& Matrix) const;

        /** See IRenderSystem.
         */
        virtual ulong ConvertColor(const CColor& Color) const;

        /** See IRenderSystem.
         */
        virtual ITextureBase* CreateTexture(const TVector2I& Size, TPixelFormat Format, ulong nFlags = 0) const;
        
        /** See IRenderSystem.
         */
        virtual void SetTexture(uint nUnit, const ITextureBase* pTexture) const;

        /** See IRenderSystem.
         */
        virtual void SetupAlphaBlending(TBlend Src, TBlend Dest) const;

       /** See IRenderSystem.
         */
        virtual void SetupTextureUnit(uint nUnit, TTextureOp Op, TTextureArg Arg1, TTextureArg Arg2 = TXA_DIFFUSE, const CColor& Constant = 0x00) const;

        /** See IRenderSystem.
         */
        virtual void Enable(TRenderParameter Param, bool bValue) const;

        /** See IRenderSystem.
         */
        virtual IShaderBase* CreateShader(CGprogram Program, TShaderType Type) const;

        /** See IRenderSystem.
         */
        virtual void SetVertexShader(const IShaderBase* pShader);

        /** See IRenderSystem.
         */
        virtual void SetPixelShader(const IShaderBase* pShader);

		/** See IRenderSystem.
         */
		virtual void SetClearColor(const CColor& Color);

		/** D3D specific method to restore a lost device. 
		 */
		void RestoreLostDevice(void);
		
		/** D3D specific method to return whether the device has been lost. 
		 */
		bool IsDeviceLost(void);
		
		/** Notify that a device has been lost. 
		 */
		void _NotifyDeviceLost(void);

    private:

		/** Initialize capabilities.
		 */
        void InitCapabilities(void);

        /** Search vertex shaders capabilities.
         */
        void SearchVertexShaderCaps(D3DCAPS9& Caps);

        /** Search pixel shaders capabilities.
         */
        void SearchPixelShaderCaps(D3DCAPS9& Caps);

		/** Check if a pixel format is supported.
		 */
        bool CheckFormat(D3DFORMAT Format, D3DRESOURCETYPE ResourceType, ulong nUsage = 0);

		//! Direct3D9 variables
        CD3D9* m_pD3D9;

		//! Save of the minimum index of the next verticies to be rendered
        ulong m_nMinVertex;
        
		//! Save of the number of the next verticies to be rendered
		ulong m_nVertexCount; 
        
		//! Matrices stack
		std::vector<CMatrix4> m_Matrices[6];

		//! The device has been lost
		bool m_bDeviceLost;
    };

	/** D3D specific class with LPDIRECT3D9 and LPDIRECT3DDEVICE9.
	 */
	class CD3D9 
	{
	public:

		//! Direct3D
		LPDIRECT3D9			pD3D;
		//! Direct3D rendering device
		LPDIRECT3DDEVICE9	pD3DDevice;

		/** Constructor.
		*/
		CD3D9() 
		{ 
			pD3D = NULL;
			pD3DDevice = NULL;
		}

		/** Destructor.
		*/
		~CD3D9()
		{
			if (pD3D) pD3D->Release();
			if (pD3DDevice) pD3DDevice->Release();
		}

		/** Release D3D device.
		 */
		void ReleaseDevice(void)
		{
			if (pD3DDevice) pD3DDevice->Release();
			pD3DDevice = NULL;
		}
	};

} // namespace Keos


#endif // KEOS_D3D9RENDERSYSTEM_H
