3 * Copyright (c) 2007-2010 SlimDX Group
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
27 #include "../stack_array.h"
28 #include "../ComObject.h"
29 #include "../DataStream.h"
31 #include "../Math/Vector3.h"
35 #include "ProgressiveMesh.h"
36 #include "PatchMesh.h"
38 #include "AnimationFrame.h"
39 #include "KeyframedAnimationSet.h"
40 #include "Direct3D9Exception.h"
42 using namespace System;
48 KeyframedAnimationSet::KeyframedAnimationSet( String^ name, double ticksPerSecond, SlimDX::Direct3D9::PlaybackType playbackType,
49 int animationCount, array<CallbackKey>^ callbackKeys )
51 LPD3DXKEYFRAMEDANIMATIONSET pointer;
52 array<unsigned char>^ nameBytes = System::Text::ASCIIEncoding::ASCII->GetBytes( name );
53 pin_ptr<unsigned char> pinnedName = &nameBytes[0];
55 int count = callbackKeys->Length;
57 stack_array<D3DXKEY_CALLBACK> keys = stackalloc( D3DXKEY_CALLBACK, count );
58 for( int i = 0; i < count; i++ )
60 keys[i].Time = callbackKeys[i].Time;
61 keys[i].pCallbackData = callbackKeys[i].Data.ToPointer();
64 HRESULT hr = D3DXCreateKeyframedAnimationSet( reinterpret_cast<LPCSTR>( pinnedName ), ticksPerSecond, static_cast<D3DXPLAYBACK_TYPE>( playbackType ),
65 animationCount, count, &keys[0], &pointer );
67 if( RECORD_D3D9( hr ).IsFailure )
68 throw gcnew Direct3D9Exception( Result::Last );
73 DataStream^ KeyframedAnimationSet::Compress( float lossiness )
77 HRESULT hr = InternalPointer->Compress( D3DXCOMPRESS_DEFAULT, lossiness, NULL, &data);
79 if( RECORD_D3D9( hr ).IsFailure )
82 return gcnew DataStream( data );
85 DataStream^ KeyframedAnimationSet::Compress( float lossiness, Frame^ frame )
89 HRESULT hr = InternalPointer->Compress( D3DXCOMPRESS_DEFAULT, lossiness, frame->Pointer, &data);
91 if( RECORD_D3D9( hr ).IsFailure )
94 return gcnew DataStream( data );
97 CallbackKey KeyframedAnimationSet::GetCallbackKey( int animation )
101 HRESULT hr = InternalPointer->GetCallbackKey( animation, &key );
104 return CallbackKey( key );
107 array<CallbackKey>^ KeyframedAnimationSet::GetCallbackKeys()
109 int count = CallbackKeyCount;
110 stack_array<D3DXKEY_CALLBACK> keys = stackalloc( D3DXKEY_CALLBACK, count );
112 HRESULT hr = InternalPointer->GetCallbackKeys( &keys[0] );
114 if( RECORD_D3D9( hr ).IsFailure )
119 array<CallbackKey>^ results = gcnew array<CallbackKey>( count );
120 for( int i = 0; i < count; i++ )
121 results[i] = CallbackKey( keys[i] );
126 Result KeyframedAnimationSet::SetCallbackKey( int animation, CallbackKey callbackKey )
128 D3DXKEY_CALLBACK key;
129 key.Time = callbackKey.Time;
130 key.pCallbackData = callbackKey.Data.ToPointer();
132 HRESULT hr = InternalPointer->SetCallbackKey( animation, &key );
133 return RECORD_D3D9( hr );
136 RotationKey KeyframedAnimationSet::GetRotationKey( int animation, int key )
138 D3DXKEY_QUATERNION qkey;
140 HRESULT hr = InternalPointer->GetRotationKey( animation, key, &qkey );
143 return RotationKey( qkey );
146 array<RotationKey>^ KeyframedAnimationSet::GetRotationKeys( int animation )
148 int count = GetRotationKeyCount( animation );
149 stack_array<D3DXKEY_QUATERNION> keys = stackalloc( D3DXKEY_QUATERNION, count );
151 HRESULT hr = InternalPointer->GetRotationKeys( animation, &keys[0] );
153 if( RECORD_D3D9( hr ).IsFailure )
158 array<RotationKey>^ results = gcnew array<RotationKey>( count );
159 for( int i = 0; i < count; i++ )
160 results[i] = RotationKey( keys[i] );
165 Result KeyframedAnimationSet::SetRotationKey( int animation, int key, RotationKey callbackKey )
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 );
171 HRESULT hr = InternalPointer->SetRotationKey( animation, key, &qkey );
172 return RECORD_D3D9( hr );
175 Result KeyframedAnimationSet::UnregisterRotationKey( int animation, int key )
177 HRESULT hr = InternalPointer->UnregisterRotationKey( animation, key );
178 return RECORD_D3D9( hr );
181 int KeyframedAnimationSet::GetRotationKeyCount( int animation )
183 return InternalPointer->GetNumRotationKeys( animation );
186 ScaleKey KeyframedAnimationSet::GetScaleKey( int animation, int key )
188 D3DXKEY_VECTOR3 qkey;
190 HRESULT hr = InternalPointer->GetScaleKey( animation, key, &qkey );
193 return ScaleKey( qkey );
196 array<ScaleKey>^ KeyframedAnimationSet::GetScaleKeys( int animation )
198 int count = GetScaleKeyCount( animation );
199 stack_array<D3DXKEY_VECTOR3> keys = stackalloc( D3DXKEY_VECTOR3, count );
201 HRESULT hr = InternalPointer->GetScaleKeys( animation, &keys[0] );
203 if( RECORD_D3D9( hr ).IsFailure )
206 array<ScaleKey>^ results = gcnew array<ScaleKey>( count );
207 for( int i = 0; i < count; i++ )
208 results[i] = ScaleKey( keys[i] );
213 Result KeyframedAnimationSet::SetScaleKey( int animation, int key, ScaleKey callbackKey )
215 D3DXKEY_VECTOR3 qkey;
216 qkey.Time = callbackKey.Time;
217 qkey.Value = D3DXVECTOR3( callbackKey.Value.X, callbackKey.Value.Y, callbackKey.Value.Z );
219 HRESULT hr = InternalPointer->SetScaleKey( animation, key, &qkey );
220 return RECORD_D3D9( hr );
223 Result KeyframedAnimationSet::UnregisterScaleKey( int animation, int key )
225 HRESULT hr = InternalPointer->UnregisterScaleKey( animation, key );
226 return RECORD_D3D9( hr );
229 int KeyframedAnimationSet::GetScaleKeyCount( int animation )
231 return InternalPointer->GetNumScaleKeys( animation );
234 TranslationKey KeyframedAnimationSet::GetTranslationKey( int animation, int key )
236 D3DXKEY_VECTOR3 qkey;
238 HRESULT hr = InternalPointer->GetTranslationKey( animation, key, &qkey );
241 return TranslationKey( qkey );
244 array<TranslationKey>^ KeyframedAnimationSet::GetTranslationKeys( int animation )
246 int count = GetTranslationKeyCount( animation );
247 stack_array<D3DXKEY_VECTOR3> keys = stackalloc( D3DXKEY_VECTOR3, count );
249 HRESULT hr = InternalPointer->GetTranslationKeys( animation, &keys[0] );
251 if( RECORD_D3D9( hr ).IsFailure )
256 array<TranslationKey>^ results = gcnew array<TranslationKey>( count );
257 for( int i = 0; i < count; i++ )
258 results[i] = TranslationKey( keys[i] );
263 Result KeyframedAnimationSet::SetTranslationKey( int animation, int key, TranslationKey callbackKey )
265 D3DXKEY_VECTOR3 qkey;
266 qkey.Time = callbackKey.Time;
267 qkey.Value = D3DXVECTOR3( callbackKey.Value.X, callbackKey.Value.Y, callbackKey.Value.Z );
269 HRESULT hr = InternalPointer->SetTranslationKey( animation, key, &qkey );
270 return RECORD_D3D9( hr );
273 Result KeyframedAnimationSet::UnregisterTranslationKey( int animation, int key )
275 HRESULT hr = InternalPointer->UnregisterTranslationKey( animation, key );
276 return RECORD_D3D9( hr );
279 int KeyframedAnimationSet::GetTranslationKeyCount( int animation )
281 return InternalPointer->GetNumTranslationKeys( animation );
284 int KeyframedAnimationSet::RegisterAnimationKeys( String^ name, array<ScaleKey>^ scaleKeys,
285 array<RotationKey>^ rotationKeys, array<TranslationKey>^ translationKeys )
288 int scaleCount = scaleKeys->Length;
289 int rotateCount = rotationKeys->Length;
290 int translateCount = translationKeys->Length;
292 array<unsigned char>^ nameBytes = System::Text::ASCIIEncoding::ASCII->GetBytes( name );
293 pin_ptr<unsigned char> pinnedName = &nameBytes[0];
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 );
299 HRESULT hr = InternalPointer->RegisterAnimationSRTKeys( reinterpret_cast<LPCSTR>( pinnedName ), scaleCount, rotateCount,
300 translateCount, &scales[0], &rotations[0], &translations[0], &result );
303 if( Result::Last.IsFailure )
309 Result KeyframedAnimationSet::UnregisterAnimation( int animation )
311 HRESULT hr = InternalPointer->UnregisterAnimation( animation );
312 return RECORD_D3D9( hr );
315 int KeyframedAnimationSet::CallbackKeyCount::get()
317 return InternalPointer->GetNumCallbackKeys();
320 SlimDX::Direct3D9::PlaybackType KeyframedAnimationSet::PlaybackType::get()
322 return static_cast<SlimDX::Direct3D9::PlaybackType>( InternalPointer->GetPlaybackType() );
325 double KeyframedAnimationSet::SourceTicksPerSecond::get()
327 return InternalPointer->GetSourceTicksPerSecond();