⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c3danimation.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
#include "C3DAnimation.h"

//-----------------------------------------------------------------------------
//
// Constructor
//
//-----------------------------------------------------------------------------
C3DAnimation::C3DAnimation()
{
	int i, j;
	
	// Set number of objects
	m_iNumObjects = 0;
	// Set number of frames
	m_iNumFrames = 0;
	// Set the starting animation state
	m_iCurFrame = 0;
	m_lCurTime = 0;

	// Initialize object, name, and key information
	for( i = 0; i < g_iMaxObjects; i++ ) {
		// Objects
		m_objObject[ i ] = NULL;
		// Names
		strcpy( &m_szObjectName[ i ][ 0 ], "" );
		// Keys
		for( j = 0; j < g_iMaxKeys; j++ ) {
			m_keyFrames[ i ][ j ] = NULL;
		}
	}
}

//-----------------------------------------------------------------------------
//
// Destructor
//
//-----------------------------------------------------------------------------
C3DAnimation::~C3DAnimation()
{
	// Clear up memory
	vReset();
}

//-----------------------------------------------------------------------------
//
// Resets the contents of the animation to the starting state
//
//-----------------------------------------------------------------------------
void C3DAnimation::vReset( void )
{
	int i, j;
	
	// Release objects
	for( i = 0; i < m_iNumObjects; i++ ) {
		if( m_objObject[ i ] ) {
			delete m_objObject[ i ];
			m_objObject[ i ] = NULL;
		}
	}

	// Release key frame data
	for( i = 0; i < m_iNumObjects; i++ ) {
		for( j = 0; j < m_iNumFrames; j++ ) {
			if( m_keyFrames[ i ][ j ] ) {
				delete m_keyFrames[ i ][ j ];
				m_keyFrames[ i ][ j ] = NULL;
			}
		}
	}

	// Set number of objects
	m_iNumObjects = 0;
	// Set number of frames
	m_iNumFrames = 0;
	// Set the starting animation state
	m_iCurFrame = 0;
	m_lCurTime = 0;
	
	// Reset names
	for( i = 0; i < g_iMaxObjects; i++ ) {
		// Names
		strcpy( &m_szObjectName[ i ][ 0 ], "" );
	}
}

//-----------------------------------------------------------------------------
//
// Add a new frame for translation, rotation, and scale
// A frame is created for each object in the scene.
//
//-----------------------------------------------------------------------------
void C3DAnimation::vNewFrame( void )
{
	int			iFrame = 0;
	stKeyFrame	*ptrFrame;
	stKeyFrame	*ptrPrevFrame;
	
	// Increment the number of frames in the anim
	m_iNumFrames++;

	// Get the key frame index
	iFrame = m_iNumFrames-1;

	//
	// Create a new frame for each object
	// in the animation.
	//
	for( int iObj = 0; iObj < m_iNumObjects; iObj++ ) {

		// Allocate memory for the frame
		m_keyFrames[ iObj ][ iFrame ] = new stKeyFrame;

		// Get a pointer to the new frame
		ptrFrame = m_keyFrames[ iObj ][ iFrame ];

		// Give first frame "default" values
		if( iFrame == 0 ) {
			ptrFrame->m_vecScale = D3DXVECTOR3( 1.0, 1.0, 1.0 );
			ptrFrame->m_vecRot = D3DXVECTOR3( 0.0, 0.0f, 0.0 );
			ptrFrame->m_vecTrans = D3DXVECTOR3( 0.0, 0.0, 0.0 );
			ptrFrame->m_lTimeDelay = 10;
		}
		// Not the first frame, so set its values to the previous
		// frame.
		else {
			// Get pointer to the previous frame
			ptrPrevFrame = m_keyFrames[ iObj ][ (iFrame-1) ];

			// Set new frame values to prev frame values
			ptrFrame->m_vecScale = ptrPrevFrame->m_vecScale;
			ptrFrame->m_vecRot = ptrPrevFrame->m_vecRot;
			ptrFrame->m_vecTrans = ptrPrevFrame->m_vecTrans;
			ptrFrame->m_lTimeDelay = ptrPrevFrame->m_lTimeDelay;
		}
	}
	// Set the current frame to the temp frame var
	m_iCurFrame = iFrame;
}

//-----------------------------------------------------------------------------
//
// Advance to the next frame in the animation
//
//-----------------------------------------------------------------------------
int C3DAnimation::iNextFrame( void )
{
	// Go to next frame
	m_iCurFrame++;

	// If frame past the end, loop to beginning
	if( m_iCurFrame >= m_iNumFrames ) {
		m_iCurFrame = 0;
	}
	// Return the frame #
	return( m_iCurFrame );
}

//-----------------------------------------------------------------------------
//
// Back up to the previous frame
//
//-----------------------------------------------------------------------------
int C3DAnimation::iPrevFrame( void )
{
	// Go to previous frame
	m_iCurFrame--;

	// If less than frame zero, go to the 
	// last frame.  If there is no last frame,
	// go to zero.
	if( m_iCurFrame < 0 ) {
		// Check if there are frames
		if( m_iNumFrames ) {
			// Go to the last one
			m_iCurFrame = m_iNumFrames-1;
		}
		// No frames
		else {
			// Go to zero frame
			m_iCurFrame = 0;
		}
	}
	// Return the frame #
	return( m_iCurFrame );
}

//-----------------------------------------------------------------------------
//
// Jump to the first frame in the animation
//
//-----------------------------------------------------------------------------
int C3DAnimation::iStartFrame( void )
{
	// Go to the first frame
	m_iCurFrame = 0;

	// Return the frame #
	return( m_iCurFrame );
}

//-----------------------------------------------------------------------------
//
// Add a 3D object to the animation
//
//-----------------------------------------------------------------------------
int C3DAnimation::iNewObj( char *szObjName )
{
	char szFileName[ 512 ];

	// Create the filename
	sprintf( szFileName, "Data\\3DObjects\\%s.x", szObjName );
	
	// Set object pointer
	m_objObject[ m_iNumObjects ] = new Object3DClass;
	m_objObject[ m_iNumObjects ]->hLoad( szFileName, m_pd3dDevice );

	// Store the name for later
	strcpy( &m_szObjectName[ m_iNumObjects ][ 0 ], szObjName );

	// Increment internal count
	m_iNumObjects++;

	// Return # of objects
	return( m_iNumObjects );
}

//-----------------------------------------------------------------------------
//
// Add the passed in vector to the translation vector
//
//-----------------------------------------------------------------------------
void C3DAnimation::vUpdateTrans( int iObj, int iKey, D3DXVECTOR3 vecTrans )
{
	// Check if object and key is valid
	if( iObj < m_iNumObjects && iObj >= 0 && iKey < m_iNumFrames && iKey >= 0 ) {
		// Update the vector
		m_keyFrames[ iObj ][ iKey ]->m_vecTrans += vecTrans;
	}
}

//-----------------------------------------------------------------------------
//
// Add the passed in vector to the rotation vector
//
//-----------------------------------------------------------------------------
void C3DAnimation::vUpdateRot( int iObj, int iKey, D3DXVECTOR3 vecRot )
{
	// Check if object and key is valid
	if( iObj < m_iNumObjects && iObj >= 0 && iKey < m_iNumFrames && iKey >= 0 ) {
		// Update the vector
		m_keyFrames[ iObj ][ iKey ]->m_vecRot += vecRot;
	}
}

//-----------------------------------------------------------------------------
//
// Add the passed in vector to the scale vector
//
//-----------------------------------------------------------------------------
void C3DAnimation::vUpdateScale( int iObj, int iKey, D3DXVECTOR3 vecScale )
{
	// Check if object and key is valid
	if( iObj < m_iNumObjects && iObj >= 0 && iKey < m_iNumFrames && iKey >= 0 ) {
		// Update the vector
		m_keyFrames[ iObj ][ iKey ]->m_vecScale += vecScale;
	}
}

//-----------------------------------------------------------------------------
//
// Save the animation
//
//-----------------------------------------------------------------------------
void C3DAnimation::vSave( char *szFileName )
{
	FILE	*fp;
	int		i, j;
	char	szFullFileName[ 512 ];

	// Create qualified file name
	sprintf( szFullFileName, "Data\\Anims\\%s.anim", szFileName );
	
	// Open the file for output
	fp = fopen( szFullFileName, "wb" );
	if( fp == NULL ) {
		return;
	}

	//
	// Output the header
	//

	// Number of object
	fwrite( &m_iNumObjects, 1, sizeof( int ), fp );
	// Number of frames
	fwrite( &m_iNumFrames, 1, sizeof( int ), fp );

	// Output object names
	for( i = 0; i < m_iNumObjects; i++ ) {
		fwrite( &m_szObjectName[ i ][ 0 ], 32, sizeof( char ), fp );
	}

	// Output key frame information
	for( i = 0; i < m_iNumObjects; i++ ) {
		for( j = 0; j < m_iNumFrames; j++ ) {
			// Delay
			fwrite( &m_keyFrames[ i ][ j ]->m_lTimeDelay, 1, sizeof( long ), fp );
			// Rotation
			fwrite( &m_keyFrames[ i ][ j ]->m_vecRot, 1, sizeof( D3DXVECTOR3 ), fp );
			// Scale
			fwrite( &m_keyFrames[ i ][ j ]->m_vecScale, 1, sizeof( D3DXVECTOR3 ), fp );
			// Translation
			fwrite( &m_keyFrames[ i ][ j ]->m_vecTrans, 1, sizeof( D3DXVECTOR3 ), fp );
		}
	}

	// Close the animation file
	fclose( fp );

	// Store animation name
	strcpy( m_szAnimName, szFileName );
}

//-----------------------------------------------------------------------------
//
// Load the animation
//
//-----------------------------------------------------------------------------
void C3DAnimation::vLoad( char *szFileName )
{
	FILE	*fp;
	int		i, j;
	int		iNumObjs;
	int		iNumFrames;
	char	szFullFileName[ 512 ];
	
	// Create qualified file name
	sprintf( szFullFileName, "Data\\Anims\\%s.anim", szFileName );
	
	// Open the file for input
	fp = fopen( szFullFileName, "rb" );
	if( fp == NULL ) {
		return;
	}

	// Reset the object in case its in use
	vReset();

	//
	// Read the header
	//

	// Number of object
	fread( &iNumObjs, 1, sizeof( int ), fp );
	// Number of frames
	fread( &iNumFrames, 1, sizeof( int ), fp );

	// Load object information
	for( i = 0; i < iNumObjs; i++ ) {
		// Read object name
		fread( &m_szObjectName[ i ][ 0 ], 32, sizeof( char ), fp );
		// Load the objects
		iNewObj( &m_szObjectName[ i ][ 0 ] );
	}

	// Allocate frame memory
	for( i = 0; i < iNumFrames; i++ ) {
		vNewFrame();
	}

	// Read key frame information
	for( i = 0; i < m_iNumObjects; i++ ) {
		for( j = 0; j < m_iNumFrames; j++ ) {
			// Delay
			fread( &m_keyFrames[ i ][ j ]->m_lTimeDelay, 1, sizeof( long ), fp );
			// Rotation
			fread( &m_keyFrames[ i ][ j ]->m_vecRot, 1, sizeof( D3DXVECTOR3 ), fp );
			// Scale
			fread( &m_keyFrames[ i ][ j ]->m_vecScale, 1, sizeof( D3DXVECTOR3 ), fp );
			// Translation
			fread( &m_keyFrames[ i ][ j ]->m_vecTrans, 1, sizeof( D3DXVECTOR3 ), fp );
		}
	}

	// Close the animation file
	fclose( fp );	

	// Store animation name
	strcpy( m_szAnimName, szFileName );
}

//-----------------------------------------------------------------------------
//
// Sets the internal Direct3D pointer
//
//-----------------------------------------------------------------------------
void C3DAnimation::vSet3DDevice( LPDIRECT3DDEVICE9 pd3dDevice )
{
	// Set the internal pointer.  The pointer is needed in order
	// to load Direct3D X-Files.
	m_pd3dDevice = pd3dDevice;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -