Platform_Win32_3D_D3D9_IndexBuffer.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D_D3D9_IndexBuffer.cpp *** 00002 00003 #include "Platform_Win32_3D_D3D9_IndexBuffer.h" 00004 #include "Debug.h" 00005 00006 #define WIN32_LEAN_AND_MEAN 00007 #define VC_EXTRALEAN 00008 #include <d3d9.h> 00009 00010 //*** Constructor *** 00011 00012 Platform_Win32_3D_D3D9_IndexBuffer::Platform_Win32_3D_D3D9_IndexBuffer(IDirect3DDevice9* device, int indexCount, bool dynamic): 00013 device_(device), 00014 indexCount_(indexCount ), 00015 dynamic_(dynamic), 00016 d3dIndexBuffer_(0), 00017 lockedData_(0) 00018 { 00019 DWORD usage=0; 00020 D3DPOOL pool=D3DPOOL_DEFAULT; 00021 if (dynamic) 00022 { 00023 usage=D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY; 00024 pool=D3DPOOL_DEFAULT; 00025 } 00026 else 00027 { 00028 usage=D3DUSAGE_WRITEONLY; 00029 pool=D3DPOOL_MANAGED; 00030 } 00031 HRESULT hr=device->CreateIndexBuffer(indexCount_*sizeof(unsigned short),usage,D3DFMT_INDEX16,pool,&d3dIndexBuffer_,0); 00032 Assert(SUCCEEDED(hr),"Failed to create index buffer"); 00033 } 00034 00035 00036 //*** Destructor *** 00037 00038 Platform_Win32_3D_D3D9_IndexBuffer::~Platform_Win32_3D_D3D9_IndexBuffer() 00039 { 00040 d3dIndexBuffer_->Release(); 00041 } 00042 00043 00044 //*** Lock *** 00045 00046 void Platform_Win32_3D_D3D9_IndexBuffer::Lock(int startIndex, int indexCount) 00047 { 00048 Assert(!lockedData_,"Index buffer is aready locked"); 00049 if (lockedData_) 00050 { 00051 return; 00052 } 00053 00054 DWORD flags=0; 00055 if (dynamic_) 00056 { 00057 flags=D3DLOCK_DISCARD|D3DLOCK_NOOVERWRITE; 00058 } 00059 HRESULT hr=d3dIndexBuffer_->Lock(0,0,reinterpret_cast<void**>(&lockedData_),flags); 00060 Assert(SUCCEEDED(hr),"Failed to lock vertex buffer"); 00061 } 00062 00063 00064 //*** Unlock *** 00065 00066 void Platform_Win32_3D_D3D9_IndexBuffer::Unlock() 00067 { 00068 Assert(lockedData_,"Index buffer is not locked"); 00069 if (!lockedData_) 00070 { 00071 return; 00072 } 00073 00074 HRESULT hr=d3dIndexBuffer_->Unlock(); 00075 Assert(SUCCEEDED(hr),"Failed to unlock vertex buffer"); 00076 lockedData_=0; 00077 } 00078 00079 00080 //*** GetIndexData *** 00081 00082 unsigned short* Platform_Win32_3D_D3D9_IndexBuffer::GetIndexData() 00083 { 00084 Assert(lockedData_,"Index buffer is not locked"); 00085 if (!lockedData_) 00086 { 00087 return 0; 00088 } 00089 00090 return reinterpret_cast<unsigned short*>(lockedData_); 00091 } 00092 00093 00094 //*** GetIndexStride *** 00095 00096 int Platform_Win32_3D_D3D9_IndexBuffer::GetIndexStride() 00097 { 00098 Assert(lockedData_,"Index buffer is not locked"); 00099 if (!lockedData_) 00100 { 00101 return 0; 00102 } 00103 00104 return sizeof(unsigned short); 00105 } 00106 00107 00108 //*** Bind *** 00109 00110 void Platform_Win32_3D_D3D9_IndexBuffer::Bind() 00111 { 00112 Assert(!lockedData_,"Index buffer is locked"); 00113 if (lockedData_) 00114 { 00115 return; 00116 } 00117 00118 device_->SetIndices(d3dIndexBuffer_); 00119 } 00120
Reproduction/republishing of any material on this site without permission is strictly prohibited.
