OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / direct3d9 / MeshContainer.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
6 * of this software and associated documentation files (the "Software"), to deal\r
7 * in the Software without restriction, including without limitation the rights\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9 * copies of the Software, and to permit persons to whom the Software is\r
10 * furnished to do so, subject to the following conditions:\r
11\r
12 * The above copyright notice and this permission notice shall be included in\r
13 * all copies or substantial portions of the Software.\r
14\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21 * THE SOFTWARE.\r
22 */\r
23 #include <d3d9.h>\r
24 #include <d3dx9.h>\r
25 #include <vcclr.h>\r
26 \r
27 #include "../ComObject.h"\r
28 #include "../DataStream.h"\r
29 #include "../Utilities.h"\r
30 \r
31 #include "Device.h"\r
32 #include "Mesh.h"\r
33 #include "MeshData.h"\r
34 #include "MeshContainer.h"\r
35 #include "PatchMesh.h"\r
36 #include "ProgressiveMesh.h"\r
37 #include "SkinInfo.h"\r
38 \r
39 using namespace System;\r
40 \r
41 namespace SlimDX\r
42 {\r
43 namespace Direct3D9\r
44 {\r
45         MeshContainerShim::MeshContainerShim( SlimDX::Direct3D9::MeshContainer ^container ) : D3DXMESHCONTAINER( )\r
46         {\r
47                 m_Container = container;\r
48         }\r
49 \r
50         MeshContainer::MeshContainer()\r
51         {\r
52                 // Manual Allocation: ugly, but necessary\r
53                 // we clean up in both the destructor and the finalizer\r
54                 Pointer = new MeshContainerShim(this);\r
55                 Pointer->Name = NULL;\r
56                 Pointer->MeshData = D3DXMESHDATA();\r
57                 Pointer->pMaterials = NULL;\r
58                 Pointer->pEffects = NULL;\r
59                 Pointer->NumMaterials = 0;\r
60                 Pointer->pAdjacency = NULL;\r
61                 Pointer->pSkinInfo = NULL;\r
62                 Pointer->pNextMeshContainer = NULL;\r
63 \r
64                 m_Name = String::Empty;\r
65                 m_NextContainer = nullptr;\r
66                 m_SkinInfo = nullptr;\r
67         }\r
68 \r
69         MeshContainer::~MeshContainer()\r
70         {\r
71                 Destruct();\r
72         }\r
73 \r
74         MeshContainer::!MeshContainer()\r
75         {\r
76                 Destruct();\r
77         }\r
78 \r
79         void MeshContainer::Destruct()\r
80         {\r
81                 if( Pointer != NULL )\r
82                 {\r
83                         if( Pointer->pMaterials != NULL )\r
84                         {\r
85                                 for( UINT i = 0; i < Pointer->NumMaterials; i++ )\r
86                                         Utilities::FreeNativeString( Pointer->pMaterials[i].pTextureFilename );\r
87 \r
88                                 delete[] Pointer->pMaterials;\r
89                                 Pointer->pMaterials = NULL;\r
90                         }\r
91 \r
92                         delete[] Pointer->pAdjacency;\r
93                         Pointer->pAdjacency = NULL;\r
94 \r
95                         Utilities::FreeNativeString( Pointer->Name );\r
96 \r
97                         if( Pointer->pEffects != NULL )\r
98                         {\r
99                                 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )\r
100                                 {\r
101                                         for( UINT j = 0; j < Pointer->pEffects[i].NumDefaults; j++ )\r
102                                                 Utilities::FreeNativeString( Pointer->pEffects[i].pDefaults[j].pParamName );\r
103 \r
104                                         delete[] Pointer->pEffects[i].pDefaults;\r
105                                 }\r
106 \r
107                                 delete[] Pointer->pEffects;\r
108                                 Pointer->pEffects = NULL;\r
109                         }\r
110 \r
111                         if(m_MeshData != nullptr )\r
112                         {\r
113                                 delete m_MeshData->Mesh;\r
114                                 delete m_MeshData->PatchMesh;\r
115                                 delete m_MeshData->ProgressiveMesh;\r
116                                 m_MeshData = nullptr;\r
117                         }\r
118 \r
119                         delete m_SkinInfo;\r
120 \r
121                         delete Pointer;\r
122                         Pointer = NULL;\r
123                 }\r
124         }\r
125 \r
126         array<ExtendedMaterial>^ MeshContainer::GetMaterials()\r
127         {\r
128                 array<ExtendedMaterial>^ results = gcnew array<ExtendedMaterial>( Pointer->NumMaterials );\r
129 \r
130                 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )\r
131                         results[i] = ExtendedMaterial::FromUnmanaged( Pointer->pMaterials[i] );\r
132 \r
133                 return results;\r
134         }\r
135 \r
136         void MeshContainer::SetMaterials( array<ExtendedMaterial>^ materials )\r
137         {\r
138                 if( Pointer->pMaterials != NULL )\r
139                 {\r
140                         for( UINT i = 0; i < Pointer->NumMaterials; i++ )\r
141                                 Utilities::FreeNativeString( Pointer->pMaterials[i].pTextureFilename );\r
142                         delete[] Pointer->pMaterials;\r
143                 }\r
144 \r
145                 Pointer->NumMaterials = materials->Length;\r
146                 // Manual Allocation: ugly, but necessary\r
147                 // we clean up whenever a new set of materials is given to us, and in\r
148                 // both the destructor and finalizer\r
149                 Pointer->pMaterials = new D3DXMATERIAL[Pointer->NumMaterials];\r
150 \r
151                 for( int i = 0; i < materials->Length; i++ )\r
152                         Pointer->pMaterials[i] = ExtendedMaterial::ToUnmanaged( materials[i] );\r
153         }\r
154 \r
155         array<EffectInstance>^ MeshContainer::GetEffects()\r
156         {\r
157                 array<EffectInstance>^ results = gcnew array<EffectInstance>( Pointer->NumMaterials );\r
158 \r
159                 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )\r
160                         results[i] = EffectInstance::FromUnmanaged( Pointer->pEffects[i] );\r
161 \r
162                 return results;\r
163         }\r
164 \r
165         void MeshContainer::SetEffects( array<EffectInstance>^ effects )\r
166         {\r
167                 if( Pointer->pEffects != NULL )\r
168                 {\r
169                         for( UINT i = 0; i < Pointer->NumMaterials; i++ )\r
170                         {\r
171                                 for( UINT j = 0; j < Pointer->pEffects[i].NumDefaults; j++ )\r
172                                         Utilities::FreeNativeString( Pointer->pEffects[i].pDefaults[j].pParamName );\r
173 \r
174                                 delete[] Pointer->pEffects[i].pDefaults;\r
175                         }\r
176 \r
177                         delete[] Pointer->pEffects;\r
178                 }\r
179 \r
180                 Pointer->NumMaterials = effects->Length;\r
181                 // Manual Allocation: ugly, but necessary\r
182                 // we clean up whenever a new set of effects is given, and\r
183                 // in both the destructor and finalizer\r
184                 Pointer->pEffects = new D3DXEFFECTINSTANCE[effects->Length];\r
185 \r
186                 for( int i = 0; i < effects->Length; i++ )\r
187                         Pointer->pEffects[i] = EffectInstance::ToUnmanaged( effects[i] );\r
188         }\r
189 \r
190         array<int>^ MeshContainer::GetAdjacency()\r
191         {\r
192                 int count = 0;\r
193 \r
194                 if( MeshData->Type == MeshDataType::Mesh )\r
195                         count = MeshData->Mesh->FaceCount;\r
196                 else if( MeshData->Type == MeshDataType::ProgressiveMesh )\r
197                         count = MeshData->ProgressiveMesh->FaceCount;\r
198                 else if( MeshData->Type == MeshDataType::PatchMesh )\r
199                         count = MeshData->PatchMesh->PatchCount;\r
200                 count *= 3;\r
201 \r
202                 array<int>^ results = gcnew array<int>( count );\r
203 \r
204                 for( int i = 0; i < count; i++ )\r
205                         results[i] = Pointer->pAdjacency[i];\r
206 \r
207                 return results;\r
208         }\r
209 \r
210         void MeshContainer::SetAdjacency( array<int>^ adjacency )\r
211         {\r
212                 if( Pointer->pAdjacency != NULL )\r
213                         delete[] Pointer->pAdjacency;\r
214 \r
215                 int count = adjacency->Length;\r
216                 // Manual Allocation: ugly, but necessary\r
217                 // we clean up whenever a new set is given to us, and\r
218                 // in both the destructor and finalizer\r
219                 Pointer->pAdjacency = new DWORD[count];\r
220 \r
221                 for( int i = 0; i < count; i++ )\r
222                         Pointer->pAdjacency[i] = adjacency[i];\r
223         }\r
224 \r
225         String^ MeshContainer::Name::get()\r
226         {\r
227                 return m_Name;\r
228         }\r
229 \r
230         void MeshContainer::Name::set( String^ value )\r
231         {\r
232                 m_Name = value;\r
233 \r
234                 Utilities::FreeNativeString( Pointer->Name );\r
235                 Pointer->Name = Utilities::AllocateNativeString( value );\r
236         }\r
237 \r
238         MeshData^ MeshContainer::MeshData::get()\r
239         {\r
240                 return m_MeshData;\r
241         }\r
242 \r
243         void MeshContainer::MeshData::set( SlimDX::Direct3D9::MeshData^ value )\r
244         {\r
245                 m_MeshData = value;\r
246 \r
247                 Pointer->MeshData.Type = static_cast<D3DXMESHDATATYPE>( value->Type );\r
248                 \r
249                 if( Pointer->MeshData.Type == D3DXMESHTYPE_MESH )\r
250                         Pointer->MeshData.pMesh = value->Mesh->InternalPointer;\r
251                 else if( Pointer->MeshData.Type == D3DXMESHTYPE_PMESH )\r
252                         Pointer->MeshData.pPMesh = value->ProgressiveMesh->InternalPointer;\r
253                 else if( Pointer->MeshData.Type == D3DXMESHTYPE_PATCHMESH )\r
254                         Pointer->MeshData.pPatchMesh = value->PatchMesh->InternalPointer;\r
255         }\r
256 \r
257         SlimDX::Direct3D9::SkinInfo^ MeshContainer::SkinInfo::get()\r
258         {\r
259                 return m_SkinInfo;\r
260         }\r
261 \r
262         void MeshContainer::SkinInfo::set( SlimDX::Direct3D9::SkinInfo^ value )\r
263         {\r
264                 m_SkinInfo = value;\r
265 \r
266                 if( value == nullptr )\r
267                         Pointer->pSkinInfo = NULL;\r
268                 else\r
269                 {\r
270                         Pointer->pSkinInfo = value->InternalPointer;\r
271                 }\r
272         }\r
273 \r
274         MeshContainer^ MeshContainer::NextMeshContainer::get()\r
275         {\r
276                 return m_NextContainer;\r
277         }\r
278 \r
279         void MeshContainer::NextMeshContainer::set( MeshContainer^ value )\r
280         {\r
281                 m_NextContainer = value;\r
282 \r
283                 if( value == nullptr )\r
284                         Pointer->pNextMeshContainer = NULL;\r
285                 else\r
286                         Pointer->pNextMeshContainer = value->Pointer;\r
287         }\r
288 }\r
289 }\r