//----------------------------------------------------------------------------- // Name: CBipedAnimInstance::RestoreDeviceObjects() // Desc: Reinitialize necessary objects //----------------------------------------------------------------------------- HRESULT CXFileAnimInstance::RestoreDeviceObjects() { // Compress the animation sets in the new animation controller SetupCallbacksAndCompression(); LPD3DXANIMATIONCONTROLLER pAC; m_pAI->GetAnimController( & pAC ); pAC->ResetTime(); pAC->AdvanceTime( m_dTimeCurrent, NULL ); // Initialize current track if( m_szASName[0] != '\0' ) { DWORD dwActiveSet = GetAnimIndex( m_szASName ); LPD3DXANIMATIONSET pAS = NULL; pAC->GetAnimationSet( dwActiveSet, &pAS ); pAC->SetTrackAnimationSet( m_dwCurrentTrack, pAS ); SAFE_RELEASE( pAS ); } pAC->SetTrackEnable( m_dwCurrentTrack, TRUE ); pAC->SetTrackWeight( m_dwCurrentTrack, 1.0f ); pAC->SetTrackSpeed( m_dwCurrentTrack, 1.0f ); SAFE_RELEASE( pAC ); // Call animate to initialize the tracks. Animate( 0.0 ); return S_OK; }
//----------------------------------------------------------------------------- // Name: CTiny::Setup // Desc: Initializes the class and readies it for animation //----------------------------------------------------------------------------- HRESULT CTiny::Setup(double dTimeCurrent, D3DXVECTOR3 pInitialPosition, CSoundManager *p_sound ) { HRESULT hr; WCHAR str[MAX_PATH]; // set the current and prev time m_dTimeCurrent = m_dTimePrev = dTimeCurrent; hr = m_pMA->CreateNewInstance( &m_dwMultiAnimIdx ); if( FAILED( hr ) ) return E_OUTOFMEMORY; m_pAI = m_pMA->GetInstance( m_dwMultiAnimIdx ); // set initial position m_vPos = pInitialPosition; m_fFacing = 0.0f; // set up anim indices m_dwAnimIdxLoiter = GetAnimIndex( "Loiter" ); m_dwAnimIdxWalk = GetAnimIndex( "Walk" ); m_dwAnimIdxJog = GetAnimIndex( "Jog" ); if( m_dwAnimIdxLoiter == ANIMINDEX_FAIL || m_dwAnimIdxWalk == ANIMINDEX_FAIL || m_dwAnimIdxJog == ANIMINDEX_FAIL ) return E_FAIL; // compute reorientation matrix based on default orientation and bounding radius D3DXMATRIX mx; float fScale = 1.f / m_pMA->GetBoundingRadius() / 2.5f; D3DXMatrixScaling( &mx, fScale, fScale, fScale ); m_mxOrientation = mx; D3DXMatrixRotationX( &mx, -D3DX_PI / 2.0f ); D3DXMatrixMultiply( &m_mxOrientation, &m_mxOrientation, &mx ); D3DXMatrixRotationY( &mx, D3DX_PI / 2.0f ); D3DXMatrixMultiply( &m_mxOrientation, &m_mxOrientation, &mx ); // set track to idle SetIdleKey(false); LPD3DXANIMATIONCONTROLLER pAC; m_pAI->GetAnimController( &pAC ); pAC->AdvanceTime( m_dTimeCurrent, NULL ); pAC->Release(); // Use D3DX to create a texture from a file based image DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"metalplate.wav" ); // load the sound hr = p_sound->Create(&m_pSound,str,DSBCAPS_CTRLVOLUME); return S_OK; }
//----------------------------------------------------------------------------- // Name: CBipedAnimInstance::InitDeviceObjects // Desc: Initializes the class and readies it for animation //----------------------------------------------------------------------------- HRESULT CXFileAnimInstance::InitDeviceObjects() { HRESULT hr; hr = m_pMA->CreateNewInstance( & m_dwMultiAnimIdx ); if( FAILED( hr ) ) return E_OUTOFMEMORY; m_pAI = m_pMA->GetInstance( m_dwMultiAnimIdx ); // set up anim indices GetAnimationIndex(); // set up footstep callbacks SetupCallbacksAndCompression(); // compute reorientation matrix based on default orientation and bounding radius Matrix4 mx; // Set the raduis of the object as it appears in the scene to 1/7.0f units float fScale = 1.f / m_pMA->GetBoundingRadius() / 7.f; ParaMatrixScaling( & mx, fScale, fScale, fScale ); m_mxOrientation = mx; // the following code is required by DirectX model trandsformation. ParaMatrixRotationX( & mx, -MATH_PI / 2.0f ); ParaMatrixMultiply( & m_mxOrientation, & m_mxOrientation, & mx ); ParaMatrixRotationY( & mx, MATH_PI / 2.0f ); ParaMatrixMultiply( & m_mxOrientation, & m_mxOrientation, & mx ); // default: play idle animation strcpy(m_szASNameTarget,"Loiter"); //ComputeFacingTarget(); PlayAnimation(false); //force loading the first animation LPD3DXANIMATIONCONTROLLER pAC; m_pAI->GetAnimController( & pAC ); pAC->AdvanceTime( m_dTimeCurrent, NULL ); pAC->Release(); return S_OK; }
//----------------------------------------------------------------------------- // Name: CTiny::Setup // Desc: Initializes the class and readies it for animation //----------------------------------------------------------------------------- HRESULT CTiny::Setup( CMultiAnim* pMA, vector <CTiny*>* pv_pChars, CSoundManager* pSM, double dTimeCurrent ) { m_pMA = pMA; m_pv_pChars = pv_pChars; m_pSM = pSM; m_dTimeCurrent = m_dTimePrev = dTimeCurrent; HRESULT hr; hr = m_pMA->CreateNewInstance( &m_dwMultiAnimIdx ); if( FAILED( hr ) ) return E_OUTOFMEMORY; m_pAI = m_pMA->GetInstance( m_dwMultiAnimIdx ); // set initial position bool bBlocked = true; DWORD dwAttempts; for( dwAttempts = 0; dwAttempts < 1000 && bBlocked; ++ dwAttempts ) { ChooseNewLocation( &m_vPos ); bBlocked = IsBlockedByCharacter( &m_vPos ); } m_fFacing = 0.0f; // set up anim indices m_dwAnimIdxLoiter = GetAnimIndex( "Loiter" ); m_dwAnimIdxWalk = GetAnimIndex( "Walk" ); m_dwAnimIdxJog = GetAnimIndex( "Jog" ); if( m_dwAnimIdxLoiter == ANIMINDEX_FAIL || m_dwAnimIdxWalk == ANIMINDEX_FAIL || m_dwAnimIdxJog == ANIMINDEX_FAIL ) return E_FAIL; // set up callback key data m_CallbackData[ 0 ].m_dwFoot = 0; m_CallbackData[ 0 ].m_pvTinyPos = &m_vPos; m_CallbackData[ 1 ].m_dwFoot = 1; m_CallbackData[ 1 ].m_pvTinyPos = &m_vPos; // set up footstep callbacks SetupCallbacksAndCompression(); m_pCallbackHandler = new CBHandlerTiny; if( m_pCallbackHandler == NULL ) return E_OUTOFMEMORY; // set up footstep sounds WCHAR sPath[ MAX_PATH ]; if( g_apSoundsTiny[ 0 ] == NULL ) { hr = DXUTFindDXSDKMediaFileCch( sPath, MAX_PATH, FOOTFALLSOUND00 ); if( FAILED( hr ) ) wcscpy_s( sPath, MAX_PATH, FOOTFALLSOUND00 ); hr = m_pSM->Create( &g_apSoundsTiny[ 0 ], sPath, DSBCAPS_CTRLVOLUME ); if( FAILED( hr ) ) { OutputDebugString( FOOTFALLSOUND00 L" not found; continuing without sound.\n" ); m_bPlaySounds = false; } } if( g_apSoundsTiny[ 1 ] == NULL ) { hr = DXUTFindDXSDKMediaFileCch( sPath, MAX_PATH, FOOTFALLSOUND01 ); if( FAILED( hr ) ) wcscpy_s( sPath, MAX_PATH, FOOTFALLSOUND01 ); hr = m_pSM->Create( &g_apSoundsTiny[ 1 ], sPath, DSBCAPS_CTRLVOLUME ); if( FAILED( hr ) ) { OutputDebugString( FOOTFALLSOUND01 L" not found; continuing without sound.\n" ); m_bPlaySounds = false; } } // compute reorientation matrix based on default orientation and bounding radius D3DXMATRIX mx; float fScale = 1.f / m_pMA->GetBoundingRadius() / 7.f; D3DXMatrixScaling( &mx, fScale, fScale, fScale ); m_mxOrientation = mx; D3DXMatrixRotationX( &mx, -D3DX_PI / 2.0f ); D3DXMatrixMultiply( &m_mxOrientation, &m_mxOrientation, &mx ); D3DXMatrixRotationY( &mx, D3DX_PI / 2.0f ); D3DXMatrixMultiply( &m_mxOrientation, &m_mxOrientation, &mx ); // set starting target SetSeekingState(); ComputeFacingTarget(); LPD3DXANIMATIONCONTROLLER pAC; m_pAI->GetAnimController( &pAC ); pAC->AdvanceTime( m_dTimeCurrent, NULL ); pAC->Release(); // Add this instance to the list try { pv_pChars->push_back( this ); } catch( ... ) { return E_OUTOFMEMORY; } return S_OK; }