OSDN Git Service

#32713 初コミット。SVNrev567時点での、ファイルはbranch/140707(ReBuild XGVersion)から移行したもの。
[dtxmaniaxg-verk/dtxmaniaxg-verk-git.git] / SlimDXc_Jun2010(VC++2008) / source / direct3d9 / KeyframedAnimationSet.cpp
1 #include "stdafx.h"
2 /*
3 * Copyright (c) 2007-2010 SlimDX Group
4
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23 #include <d3d9.h>
24 #include <d3dx9.h>
25 #include <vcclr.h>
26
27 #include "../stack_array.h"
28 #include "../ComObject.h"
29 #include "../DataStream.h"
30
31 #include "../Math/Vector3.h"
32
33 #include "Device.h"
34 #include "Mesh.h"
35 #include "ProgressiveMesh.h"
36 #include "PatchMesh.h"
37 #include "SkinInfo.h"
38 #include "AnimationFrame.h"
39 #include "KeyframedAnimationSet.h"
40 #include "Direct3D9Exception.h"
41
42 using namespace System;
43
44 namespace SlimDX
45 {
46 namespace Direct3D9
47 {
48         KeyframedAnimationSet::KeyframedAnimationSet( String^ name, double ticksPerSecond, SlimDX::Direct3D9::PlaybackType playbackType,
49                 int animationCount, array<CallbackKey>^ callbackKeys )
50         {
51                 LPD3DXKEYFRAMEDANIMATIONSET pointer;
52                 array<unsigned char>^ nameBytes = System::Text::ASCIIEncoding::ASCII->GetBytes( name );
53                 pin_ptr<unsigned char> pinnedName = &nameBytes[0];
54
55                 int count = callbackKeys->Length;
56
57                 stack_array<D3DXKEY_CALLBACK> keys = stackalloc( D3DXKEY_CALLBACK, count );
58                 for( int i = 0; i < count; i++ )
59                 {
60                         keys[i].Time = callbackKeys[i].Time;
61                         keys[i].pCallbackData = callbackKeys[i].Data.ToPointer();
62                 }
63
64                 HRESULT hr = D3DXCreateKeyframedAnimationSet( reinterpret_cast<LPCSTR>( pinnedName ), ticksPerSecond, static_cast<D3DXPLAYBACK_TYPE>( playbackType ),
65                         animationCount, count, &keys[0], &pointer );
66
67                 if( RECORD_D3D9( hr ).IsFailure )
68                         throw gcnew Direct3D9Exception( Result::Last );
69
70                 Construct(pointer);
71         }
72
73         DataStream^ KeyframedAnimationSet::Compress( float lossiness )
74         {
75                 LPD3DXBUFFER data;
76
77                 HRESULT hr = InternalPointer->Compress( D3DXCOMPRESS_DEFAULT, lossiness, NULL, &data);
78                 
79                 if( RECORD_D3D9( hr ).IsFailure )
80                         return nullptr;
81
82                 return gcnew DataStream( data );
83         }
84
85         DataStream^ KeyframedAnimationSet::Compress( float lossiness, Frame^ frame )
86         {
87                 LPD3DXBUFFER data;
88
89                 HRESULT hr = InternalPointer->Compress( D3DXCOMPRESS_DEFAULT, lossiness, frame->Pointer, &data);
90                 
91                 if( RECORD_D3D9( hr ).IsFailure )
92                         return nullptr;
93
94                 return gcnew DataStream( data );
95         }
96
97         CallbackKey KeyframedAnimationSet::GetCallbackKey( int animation )
98         {
99                 D3DXKEY_CALLBACK key;
100
101                 HRESULT hr = InternalPointer->GetCallbackKey( animation, &key );
102                 RECORD_D3D9( hr );
103
104                 return CallbackKey( key );
105         }
106
107         array<CallbackKey>^ KeyframedAnimationSet::GetCallbackKeys()
108         {
109                 int count = CallbackKeyCount;
110                 stack_array<D3DXKEY_CALLBACK> keys = stackalloc( D3DXKEY_CALLBACK, count );
111
112                 HRESULT hr = InternalPointer->GetCallbackKeys( &keys[0] );
113                 
114                 if( RECORD_D3D9( hr ).IsFailure )
115                 {
116                         return nullptr;
117                 }
118
119                 array<CallbackKey>^ results = gcnew array<CallbackKey>( count );
120                 for( int i = 0; i < count; i++ )
121                         results[i] = CallbackKey( keys[i] );
122
123                 return results;
124         }
125
126         Result KeyframedAnimationSet::SetCallbackKey( int animation, CallbackKey callbackKey )
127         {
128                 D3DXKEY_CALLBACK key;
129                 key.Time = callbackKey.Time;
130                 key.pCallbackData = callbackKey.Data.ToPointer();
131
132                 HRESULT hr = InternalPointer->SetCallbackKey( animation, &key );
133                 return RECORD_D3D9( hr );
134         }
135
136         RotationKey KeyframedAnimationSet::GetRotationKey( int animation, int key )
137         {
138                 D3DXKEY_QUATERNION qkey;
139
140                 HRESULT hr = InternalPointer->GetRotationKey( animation, key, &qkey );
141                 RECORD_D3D9( hr );
142
143                 return RotationKey( qkey );
144         }
145
146         array<RotationKey>^ KeyframedAnimationSet::GetRotationKeys( int animation )
147         {
148                 int count = GetRotationKeyCount( animation );
149                 stack_array<D3DXKEY_QUATERNION> keys = stackalloc( D3DXKEY_QUATERNION, count );
150
151                 HRESULT hr = InternalPointer->GetRotationKeys( animation, &keys[0] );
152                 
153                 if( RECORD_D3D9( hr ).IsFailure )
154                 {
155                         return nullptr;
156                 }
157
158                 array<RotationKey>^ results = gcnew array<RotationKey>( count );
159                 for( int i = 0; i < count; i++ )
160                         results[i] = RotationKey( keys[i] );
161
162                 return results;
163         }
164
165         Result KeyframedAnimationSet::SetRotationKey( int animation, int key, RotationKey callbackKey )
166         {
167                 D3DXKEY_QUATERNION qkey;
168                 qkey.Time = callbackKey.Time;
169                 qkey.Value = D3DXQUATERNION( callbackKey.Value.X, callbackKey.Value.Y, callbackKey.Value.Z, callbackKey.Value.W );
170
171                 HRESULT hr = InternalPointer->SetRotationKey( animation, key, &qkey );
172                 return RECORD_D3D9( hr );
173         }
174
175         Result KeyframedAnimationSet::UnregisterRotationKey( int animation, int key )
176         {
177                 HRESULT hr = InternalPointer->UnregisterRotationKey( animation, key );
178                 return RECORD_D3D9( hr );
179         }
180
181         int KeyframedAnimationSet::GetRotationKeyCount( int animation )
182         {
183                 return InternalPointer->GetNumRotationKeys( animation );
184         }
185
186         ScaleKey KeyframedAnimationSet::GetScaleKey( int animation, int key )
187         {
188                 D3DXKEY_VECTOR3 qkey;
189
190                 HRESULT hr = InternalPointer->GetScaleKey( animation, key, &qkey );
191                 RECORD_D3D9( hr );
192
193                 return ScaleKey( qkey );
194         }
195
196         array<ScaleKey>^ KeyframedAnimationSet::GetScaleKeys( int animation )
197         {
198                 int count = GetScaleKeyCount( animation );
199                 stack_array<D3DXKEY_VECTOR3> keys = stackalloc( D3DXKEY_VECTOR3, count );
200
201                 HRESULT hr = InternalPointer->GetScaleKeys( animation, &keys[0] );
202                 
203                 if( RECORD_D3D9( hr ).IsFailure )
204                         return nullptr;
205
206                 array<ScaleKey>^ results = gcnew array<ScaleKey>( count );
207                 for( int i = 0; i < count; i++ )
208                         results[i] = ScaleKey( keys[i] );
209
210                 return results;
211         }
212
213         Result KeyframedAnimationSet::SetScaleKey( int animation, int key, ScaleKey callbackKey )
214         {
215                 D3DXKEY_VECTOR3 qkey;
216                 qkey.Time = callbackKey.Time;
217                 qkey.Value = D3DXVECTOR3( callbackKey.Value.X, callbackKey.Value.Y, callbackKey.Value.Z );
218
219                 HRESULT hr = InternalPointer->SetScaleKey( animation, key, &qkey );
220                 return RECORD_D3D9( hr );
221         }
222
223         Result KeyframedAnimationSet::UnregisterScaleKey( int animation, int key )
224         {
225                 HRESULT hr = InternalPointer->UnregisterScaleKey( animation, key );
226                 return RECORD_D3D9( hr );
227         }
228
229         int KeyframedAnimationSet::GetScaleKeyCount( int animation )
230         {
231                 return InternalPointer->GetNumScaleKeys( animation );
232         }
233
234         TranslationKey KeyframedAnimationSet::GetTranslationKey( int animation, int key )
235         {
236                 D3DXKEY_VECTOR3 qkey;
237
238                 HRESULT hr = InternalPointer->GetTranslationKey( animation, key, &qkey );
239                 RECORD_D3D9( hr );
240
241                 return TranslationKey( qkey );
242         }
243
244         array<TranslationKey>^ KeyframedAnimationSet::GetTranslationKeys( int animation )
245         {
246                 int count = GetTranslationKeyCount( animation );
247                 stack_array<D3DXKEY_VECTOR3> keys = stackalloc( D3DXKEY_VECTOR3, count );
248
249                 HRESULT hr = InternalPointer->GetTranslationKeys( animation, &keys[0] );
250                 
251                 if( RECORD_D3D9( hr ).IsFailure )
252                 {
253                         return nullptr;
254                 }
255
256                 array<TranslationKey>^ results = gcnew array<TranslationKey>( count );
257                 for( int i = 0; i < count; i++ )
258                         results[i] = TranslationKey( keys[i] );
259
260                 return results;
261         }
262
263         Result KeyframedAnimationSet::SetTranslationKey( int animation, int key, TranslationKey callbackKey )
264         {
265                 D3DXKEY_VECTOR3 qkey;
266                 qkey.Time = callbackKey.Time;
267                 qkey.Value = D3DXVECTOR3( callbackKey.Value.X, callbackKey.Value.Y, callbackKey.Value.Z );
268
269                 HRESULT hr = InternalPointer->SetTranslationKey( animation, key, &qkey );
270                 return RECORD_D3D9( hr );
271         }
272
273         Result KeyframedAnimationSet::UnregisterTranslationKey( int animation, int key )
274         {
275                 HRESULT hr = InternalPointer->UnregisterTranslationKey( animation, key );
276                 return RECORD_D3D9( hr );
277         }
278
279         int KeyframedAnimationSet::GetTranslationKeyCount( int animation )
280         {
281                 return InternalPointer->GetNumTranslationKeys( animation );
282         }
283
284         int KeyframedAnimationSet::RegisterAnimationKeys( String^ name, array<ScaleKey>^ scaleKeys,
285                 array<RotationKey>^ rotationKeys, array<TranslationKey>^ translationKeys )
286         {
287                 DWORD result;
288                 int scaleCount = scaleKeys->Length;
289                 int rotateCount = rotationKeys->Length;
290                 int translateCount = translationKeys->Length;
291
292                 array<unsigned char>^ nameBytes = System::Text::ASCIIEncoding::ASCII->GetBytes( name );
293                 pin_ptr<unsigned char> pinnedName = &nameBytes[0];
294
295                 stack_array<D3DXKEY_VECTOR3> scales = stackalloc( D3DXKEY_VECTOR3, scaleCount );
296                 stack_array<D3DXKEY_QUATERNION> rotations = stackalloc( D3DXKEY_QUATERNION, rotateCount );
297                 stack_array<D3DXKEY_VECTOR3> translations = stackalloc( D3DXKEY_VECTOR3, translateCount );
298
299                 HRESULT hr = InternalPointer->RegisterAnimationSRTKeys( reinterpret_cast<LPCSTR>( pinnedName ), scaleCount, rotateCount,
300                         translateCount, &scales[0], &rotations[0], &translations[0], &result );
301                 RECORD_D3D9( hr );
302
303                 if( Result::Last.IsFailure )
304                         return 0;
305
306                 return result;
307         }
308
309         Result KeyframedAnimationSet::UnregisterAnimation( int animation )
310         {
311                 HRESULT hr = InternalPointer->UnregisterAnimation( animation );
312                 return RECORD_D3D9( hr );
313         }
314
315         int KeyframedAnimationSet::CallbackKeyCount::get()
316         {
317                 return InternalPointer->GetNumCallbackKeys();
318         }
319
320         SlimDX::Direct3D9::PlaybackType KeyframedAnimationSet::PlaybackType::get()
321         {
322                 return static_cast<SlimDX::Direct3D9::PlaybackType>( InternalPointer->GetPlaybackType() );
323         }
324
325         double KeyframedAnimationSet::SourceTicksPerSecond::get()
326         {
327                 return InternalPointer->GetSourceTicksPerSecond();
328         }
329 }
330 }