OSDN Git Service

1ac002ea47c13f7d77566ecfd5cf044babe14bdb
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / VertexBuffer.cpp
1 #pragma once
2 #include "mof/VertexBuffer.hpp"
3 #include "mof/private/GraphicsDeviceImpl.hpp"
4 #include <d3dx9.h>
5 #include "mof/VertexTypes.hpp"
6 #include "mof/private/VertexFVF.hpp"
7 #include <stdlib.h>
8
9 template class mof::VertexBuffer<mof::VertexXYZRHWCUV>;
10 template class mof::VertexBuffer<mof::VertexXYZRHWC>;
11 template class mof::VertexBuffer<mof::VertexXYZCUV>;
12 template class mof::VertexBuffer<mof::VertexXYZNUV>;
13 template class mof::VertexBuffer<mof::VertexXYZC>;
14
15
16 template <class T>
17 struct mof::VertexBuffer<T>::Impl{
18         IDirect3DVertexBuffer9* pBuffer;
19         mof::PRIMITIVE_TYPE primitiveType;
20         
21         
22         Impl(mof::PRIMITIVE_TYPE primitiveType_);
23         ~Impl();
24 };
25
26
27 template <class T>
28 mof::VertexBuffer<T>::Impl::Impl(mof::PRIMITIVE_TYPE primitiveType_ )
29 : pBuffer(NULL) , primitiveType(primitiveType_) 
30 {       
31 }
32
33 template <class T>
34 mof::VertexBuffer<T>::Impl::~Impl()
35 {       
36         if(pBuffer != NULL)pBuffer->Release();
37 }
38
39
40 template <class T>
41 mof::VertexBuffer<T>::VertexBuffer(const T& front , const T& back , mof::PRIMITIVE_TYPE primitiveType )
42 : m_pImpl(new Impl( primitiveType ))
43 {
44         int length = &back - &front + 1;
45         if(length <= 0)throw std::invalid_argument("\95s\90³\82È\92¸\93_\94z\97ñ\8ew\92è");
46         
47         const LPDIRECT3DDEVICE9 pDevice = mof::GraphicsDevice::getRawDevice();
48         HRESULT hr = pDevice->CreateVertexBuffer( sizeof(T) * length , 0 , mof::getFVF<T>() , D3DPOOL_DEFAULT , &m_pImpl->pBuffer , NULL);
49         if(FAILED(hr))throw std::runtime_error("Failed -- CreateVertexBuffer");
50         
51         T* pBuffer;
52         hr = m_pImpl->pBuffer->Lock( 0, 0 ,(LPVOID*)&pBuffer , 0);
53         if(FAILED(hr))throw std::invalid_argument(std::string("\92¸\93_\83o\83b\83t\83@\82Ì\83\8d\83b\83N\82É\8e¸\94s\82µ\82Ü\82µ\82½"));
54         
55         memcpy( pBuffer , &front , sizeof(T) * length );
56         m_pImpl->pBuffer->Unlock();
57 }
58
59
60
61 template<class T>
62 mof::VertexBuffer<T>::~VertexBuffer(){
63 }
64
65
66 template<class T>
67 int mof::VertexBuffer<T>::getLength() const{
68         D3DVERTEXBUFFER_DESC desc;
69         m_pImpl->pBuffer->GetDesc(&desc);
70         return desc.Size / sizeof(T);
71 }
72
73
74 template<class T>
75 mof::PRIMITIVE_TYPE mof::VertexBuffer<T>::getPrimitiveType() const{
76         return m_pImpl->primitiveType;
77 }
78
79
80 template<class T>
81 void  mof::VertexBuffer<T>::draw() const{
82         HRESULT hr = E_FAIL;
83
84         const LPDIRECT3DDEVICE9 pDevice = mof::GraphicsDevice::getRawDevice();
85
86         pDevice->SetFVF(mof::getFVF<T>());
87         pDevice->SetStreamSource(0 , m_pImpl->pBuffer , 0 , sizeof(T));
88
89         
90         int length = getLength();
91
92         if(m_pImpl->primitiveType == mof::PRIMITIVE_TYPE_TRIANGLESTRIP){
93                 hr = pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP , 0 , length -2 );
94         }
95         else if(m_pImpl->primitiveType == mof::PRIMITIVE_TYPE_TRIANGLELIST){
96                 hr = pDevice->DrawPrimitive( D3DPT_TRIANGLELIST , 0 , length /3 );
97         }
98         
99         if(FAILED(hr))throw std::runtime_error("Failed -- DrawPrimitive");
100
101 }
102