/*
 * 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_D3D9BUFFER_H
#define KEOS_D3D9BUFFER_H

#include "KeosD3D9Prerequisites.h"
#include "KeosBufferBase.h"
#include "KeosD3D9Enum.h"

struct IDirect3DVertexBuffer9;
struct IDirect3DIndexBuffer9;

namespace Keos
{
    //-----------------------------------------------------------------------
    /** Vertex/Index buffer Direct3D9 implementation. 
     */
    template <class T>
    class CD3D9Buffer : public IBufferBase
    {
    public :

        /** Constructor.
         *  @param nCount Buffer size.
         *  @param pBuffer D3D9 buffer pointer.
         */
        CD3D9Buffer(ulong nCount, T* pBuffer);

        /** Return a pointer on the D3D9 buffer.
         */
        T* GetBuffer() const;

    private :

        /** See IBufferBase.
         */
        virtual void* Lock(ulong nOffset, ulong nSize, ulong nFlags);

        /** See IBufferBase.
         */
        virtual void Unlock();

        //! D3D9 buffer pointer
        CSmartPtr<T, CCOMRefCounted> m_pBuffer;
    };

    // Index and vertex buffer definition from CD3D9Buffer template
    typedef CD3D9Buffer<IDirect3DVertexBuffer9> CD3D9VertexBuffer;
    typedef CD3D9Buffer<IDirect3DIndexBuffer9>  CD3D9IndexBuffer;



    //=======================================================================
    // CD3D9Buffer inline functions
    //=======================================================================
    
    //-----------------------------------------------------------------------  
    template <class T>
    inline CD3D9Buffer<T>::CD3D9Buffer(ulong nCount, T* pBuffer) :
    IBufferBase(nCount),
    m_pBuffer  (pBuffer)
    {

    }

    //-----------------------------------------------------------------------  
    template <class T>
    inline T* CD3D9Buffer<T>::GetBuffer() const
    {
        return m_pBuffer;
    }

    //-----------------------------------------------------------------------  
    template <class T>
    inline void* CD3D9Buffer<T>::Lock(ulong nOffset, ulong nSize, ulong nFlags)
    {
        void* pData = NULL;
        m_pBuffer->Lock(nOffset, nSize, &pData, CD3D9Enum::LockFlags(nFlags));

        return pData;
    }

    //-----------------------------------------------------------------------  
    template <class T>
    inline void CD3D9Buffer<T>::Unlock()
    {
        m_pBuffer->Unlock();
    }

} // namespace Keos

#endif // KEOS_D3D9BUFFER_H
