/*
 * 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_D3D9WINDOW_H
#define KEOS_D3D9WINDOW_H

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

namespace Keos 
{
    //-----------------------------------------------------------------------
    /** Direct3D9 IRenderWindow implementation. 
     */
    class CD3D9Window : public IRenderWindow
    {
    public:

        /** Constructor.
         */
        CD3D9Window();

        /** Constructor.
		 *	@param pD3D9 CD3D9 object from CD3D9RenderSystem.
		 */
		CD3D9Window(CD3D9* pD3D9);

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

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

        /** See IRenderWindow.
         */
        void Destroy(void);

        /** See IRenderWindow.
         */
        bool IsClosed(void) const;

        /** See IRenderWindow.
         */
        void Reposition(int nLeft, int nTop);

        /** See IRenderWindow.
         */
        void Resize(uint nWidth, uint nHeight);

        /** See IRenderWindow.
         */
        void SwapBuffers(bool bWaitForVSync);

        /** Return the Win32 window handle.
         */
		HWND GetWindowHandle() const { return m_HWnd; }
		
		/** Method for dealing with resize / move & 3d library.
         */
		virtual void WindowMovedOrResized(void);

		bool IsReady() const { return m_bReady; }
		void SetReady(bool bSet) { m_bReady = bSet; }
		void SetActive(bool bSet) { m_bActive = bSet; }

        /** Create (or recreate) the D3D device for this window.
		 */
		void CreateD3DResources();

		/** See IRenderTarget.
         */
		virtual void Update(void);

		/** Get the presentation parameters used with this window.
		 */
		D3DPRESENT_PARAMETERS* GetPresentationParameters(void) 
		{ return &m_d3dpp; }
	
	protected:

		/** Window procedure callback.
         */
		static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
		
        //! Win32 Window handle
		HWND	m_HWnd;	
        //! Is ready i.e. available for update
		bool	m_bReady;	
        //! Indicates whether the window has been closed by the user
		bool	m_bClosed;
		//! Indicates whether the window is (re)sizing
		bool	m_bSizing;

        // Direct3D9 Specific
		CD3D9* m_pD3D9;
		D3DPRESENT_PARAMETERS m_d3dpp;
		LPDIRECT3DSURFACE9 m_pRenderSurface;
		LPDIRECT3DSURFACE9 m_pRenderZBuffer;
		D3DMULTISAMPLE_TYPE m_FSAAType;
		DWORD m_FSAAQuality;
		bool m_bVSync;
    };

} // namespace Keos

#endif // KEOS_D3D9WINDOW_H
