Platform_Win32_3D_IndexBuffer.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D_IndexBuffer.cpp *** 00002 00003 #include "Platform_Win32_3D_IndexBuffer.h" 00004 #include "Platform_Win32_3D_Technology.h" 00005 #include "Platform_Win32_3D.h" 00006 #include "StandardLibrary.h" 00007 #include "Debug.h" 00008 00009 00010 //*** Constructor *** 00011 00012 Platform_Win32_3D_IndexBuffer::Platform_Win32_3D_IndexBuffer(Platform_Win32_3D* win32_3d, int indexCount, bool dynamic): 00013 win32_3d_(win32_3d), 00014 indexCount_(indexCount), 00015 dynamic_(dynamic), 00016 indexData_(0), 00017 locked_(false), 00018 technologyIndexBuffer_(0) 00019 { 00020 indexData_=new unsigned short[indexCount_]; 00021 } 00022 00023 00024 //*** Destructor *** 00025 00026 Platform_Win32_3D_IndexBuffer::~Platform_Win32_3D_IndexBuffer() 00027 { 00028 delete[] indexData_; 00029 00030 if (technologyIndexBuffer_) 00031 { 00032 delete technologyIndexBuffer_; 00033 technologyIndexBuffer_=0; 00034 } 00035 00036 win32_3d_->IndexBufferDeleted(this); 00037 } 00038 00039 00040 //*** Reset *** 00041 00042 void Platform_Win32_3D_IndexBuffer::Reset(Platform_Win32_3D_Technology* technology) 00043 { 00044 bool copyData=false; 00045 00046 if (technologyIndexBuffer_) 00047 { 00048 delete technologyIndexBuffer_; 00049 technologyIndexBuffer_=0; 00050 copyData=true; // If the buffer was recreated rather than created initially, we need to copy the data to the new buffer 00051 } 00052 00053 technologyIndexBuffer_=technology->CreateIndexBuffer(indexCount_,dynamic_); 00054 if (copyData) 00055 { 00056 // Locking and unlocking will force a rewrite of the buffer data 00057 Lock(); 00058 Unlock(); 00059 } 00060 } 00061 00062 00063 //*** Lock *** 00064 00065 void Platform_Win32_3D_IndexBuffer::Lock(int startIndex, int indexCount) 00066 { 00067 Assert(!locked_,"Index buffer is already locked"); 00068 Assert(technologyIndexBuffer_,"No technology instance!"); 00069 if (locked_ || !technologyIndexBuffer_) 00070 { 00071 return; 00072 } 00073 00074 technologyIndexBuffer_->Lock(); 00075 locked_=true; 00076 } 00077 00078 00079 //*** Unlock *** 00080 00081 void Platform_Win32_3D_IndexBuffer::Unlock() 00082 { 00083 Assert(locked_,"Index buffer is not locked"); 00084 Assert(technologyIndexBuffer_,"No technology instance!"); 00085 if (!locked_ || !technologyIndexBuffer_) 00086 { 00087 return; 00088 } 00089 00090 // Copy the data to the actual index buffer 00091 int indexStride=technologyIndexBuffer_->GetIndexStride(); 00092 if (indexStride==sizeof(unsigned short)) 00093 { 00094 MemCpy(technologyIndexBuffer_->GetIndexData(),indexData_,sizeof(unsigned short)*indexCount_); 00095 } 00096 else 00097 { 00098 unsigned char* target=reinterpret_cast<unsigned char*>(technologyIndexBuffer_->GetIndexData()); 00099 unsigned short* source=indexData_; 00100 for (int i=0; i<indexCount_; i++) 00101 { 00102 unsigned short* indexBuffer=reinterpret_cast<unsigned short*>(target); 00103 *indexBuffer=*source; 00104 source++; 00105 target+=indexStride; 00106 } 00107 } 00108 00109 technologyIndexBuffer_->Unlock(); 00110 locked_=false; 00111 } 00112 00113 00114 //*** GetIndexData *** 00115 00116 unsigned short* Platform_Win32_3D_IndexBuffer::GetIndexData() 00117 { 00118 Assert(locked_,"Index buffer is not locked"); 00119 Assert(technologyIndexBuffer_,"No technology instance!"); 00120 if (!locked_ || !technologyIndexBuffer_) 00121 { 00122 return 0; 00123 } 00124 00125 return indexData_; 00126 } 00127 00128 00129 //*** GetIndexStride *** 00130 00131 int Platform_Win32_3D_IndexBuffer::GetIndexStride() 00132 { 00133 Assert(locked_,"Index buffer is not locked"); 00134 Assert(technologyIndexBuffer_,"No technology instance!"); 00135 if (!locked_ || !technologyIndexBuffer_) 00136 { 00137 return 0; 00138 } 00139 00140 return sizeof(unsigned short); 00141 } 00142 00143 00144 //*** Bind *** 00145 00146 void Platform_Win32_3D_IndexBuffer::Bind() 00147 { 00148 Assert(!locked_,"Index buffer is locked"); 00149 Assert(technologyIndexBuffer_,"No technology instance!"); 00150 if (locked_ || !technologyIndexBuffer_) 00151 { 00152 return; 00153 } 00154 00155 technologyIndexBuffer_->Bind(); 00156 00157 win32_3d_->IndexBufferBound(this); 00158 } 00159
Reproduction/republishing of any material on this site without permission is strictly prohibited.
