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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CD3DFramework::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch ( uMsg ) {
		// Set the size of the window upon startup
		case WM_CREATE:
			SetWindowPos( 
				hWnd, 
				NULL, 
				0,					// X-Pos
				0,					// Y-Pos
				m_shWindowWidth,	// Width
				m_shWindowHeight,	// Height
				NULL);
			break;
		break;
	};

	return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

void CD3DFramework::vInitTileVB( void )
{
	TILEVERTEX* pVertices;

	//
	// TILE VERTEX BUFFER
	//

	//
	// Create a quad made up of 3 vertices (triangle strips)
	//
	// 1          3
	//  X--------X
	//  |x       |
	//  | x      |
	//  |  x     |
	//  |   x    |
	//  |    x   |
	//  |     x  |
	//  |      x |
	//  |       x|
	//  X--------X
	// 0          2
	//

	// Create the interface object buffer
	if( FAILED( m_pd3dDevice->CreateVertexBuffer( 
		4*sizeof(TILEVERTEX),
		0, 
		D3DFVF_TILEVERTEX,
		D3DPOOL_DEFAULT, 
		&m_pVBTile,
		NULL ) ) )
	{
		return;
	}

	// Lock the buffer for editing
	if( FAILED( m_pVBTile->Lock( 
		0, 
		0, 
		(void**)&pVertices, 
		0 ) ) ) {
		
		return;
	}

	// Create the vertices
	pVertices[0].position	= D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	pVertices[0].tu			= 0.0f;
	pVertices[0].tv			= 1.0f;
	pVertices[0].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[1].position	= D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
	pVertices[1].tu			= 0.0f;
	pVertices[1].tv			= 0.0f;
	pVertices[1].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[2].position	= D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
	pVertices[2].tu			= 1.0f;
	pVertices[2].tv			= 1.0f;
	pVertices[2].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[3].position	= D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
	pVertices[3].tu			= 1.0f;
	pVertices[3].tv			= 0.0f;
	pVertices[3].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	// Unlock the buffer, done editing
	m_pVBTile->Unlock();

	//
	// UNIT VERTEX BUFFER
	//

	// Create the interface object buffer
	if( FAILED( m_pd3dDevice->CreateVertexBuffer( 
		4*sizeof(TILEVERTEX),
		0, 
		D3DFVF_TILEVERTEX,
		D3DPOOL_DEFAULT, 
		&m_pVBUnit,
		NULL ) ) )
	{
		return;
	}

	// Lock the buffer for editing
	if( FAILED( m_pVBUnit->Lock( 
		0, 
		0, 
		(void**)&pVertices, 
		0 ) ) ) {
		
		return;
	}

	// Create the vertices
	pVertices[0].position	= D3DXVECTOR3( -0.5f, -0.5f, 0.0f );
	pVertices[0].tu			= 0.0f;
	pVertices[0].tv			= 1.0f;
	pVertices[0].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[1].position	= D3DXVECTOR3( -0.5f, 0.5f, 0.0f );
	pVertices[1].tu			= 0.0f;
	pVertices[1].tv			= 0.0f;
	pVertices[1].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[2].position	= D3DXVECTOR3( 0.5f, -0.5f, 0.0f );
	pVertices[2].tu			= 1.0f;
	pVertices[2].tv			= 1.0f;
	pVertices[2].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[3].position	= D3DXVECTOR3( 0.5f, 0.5f, 0.0f );
	pVertices[3].tu			= 1.0f;
	pVertices[3].tv			= 0.0f;
	pVertices[3].vecNorm	= D3DXVECTOR3(0.0f,0.0f,1.0f);

	// Unlock the buffer, done editing
	m_pVBUnit->Unlock();
};

void CD3DFramework::vDrawTile( float fXPos, float fYPos, float fXSize, float fYSize, int iTexture )
{
	D3DXMATRIX	matWorld;
	D3DXMATRIX	matRotation;
	D3DXMATRIX	matTranslation;
	D3DXMATRIX	matScale;
	
	// Set default position,scale,rotation
	D3DXMatrixIdentity( &matTranslation );
	// Scale the tile
	D3DXMatrixScaling( &matScale, fXSize, fYSize, 1.0f );
	D3DXMatrixMultiply( &matTranslation, &matTranslation, &matScale );
	// Rotate the tile
	D3DXMatrixRotationZ( &matRotation, 0.0f );
	D3DXMatrixMultiply( &matWorld, &matTranslation, &matRotation );
	
	// Move the tile
	matWorld._41 = fXPos-0.5f;	// X-Pos
	matWorld._42 = fYPos+0.5f;	// Y-Pos
	
	// Set matrix
	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	// Set the texture to use
	m_pd3dDevice->SetTexture( 0, m_pTexture[ iTexture ] );
	// Use the tile vertex buffer
	m_pd3dDevice->SetStreamSource( 0, m_pVBTile, 0, sizeof(TILEVERTEX) );
	// Use the tile vertex format
	m_pd3dDevice->SetFVF( D3DFVF_TILEVERTEX );
	// Display the quad
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
	// Dereference texture
	m_pd3dDevice->SetTexture( 0, NULL);
}

void CD3DFramework::vDrawUnit( float fXPos, float fYPos, float fXSize, float fYSize, float fRot, CUnitAnimation *animObj, int iTexture, int iOwner )
{
	D3DXMATRIX	matWorld;
	D3DXMATRIX	matRotation;
	D3DXMATRIX	matTranslation;
	D3DXMATRIX	matScale;
	
	// Set default position,scale,rotation
	D3DXMatrixIdentity( &matTranslation );
	// Scale the tile
	D3DXMatrixScaling( &matScale, fXSize, fYSize, 1.0f );
	D3DXMatrixMultiply( &matTranslation, &matTranslation, &matScale );
	// Rotate the tile
	D3DXMatrixRotationZ( &matRotation, (float)DegToRad( -fRot ) );
	D3DXMatrixMultiply( &matWorld, &matTranslation, &matRotation );
	
	// Move the tile
	matWorld._41 = fXPos-0.5f;	// X-Pos
	matWorld._42 = fYPos+0.5f;	// Y-Pos
	
	// Set matrix
	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	// Use the tile vertex buffer
	m_pd3dDevice->SetStreamSource( 0, m_pVBUnit, 0, sizeof(TILEVERTEX) );
	// Use the tile vertex format
	m_pd3dDevice->SetFVF( D3DFVF_TILEVERTEX );
	
	//
	// Render the base graphic
	//
	
	// Set the texture to use
	m_pd3dDevice->SetTexture( 0, animObj->m_Textures[ iTexture ].m_pTexture );
	// Display the quad
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	//
	// Render the owner color overlay
	//

	// Set the texture to use
	m_pd3dDevice->SetTexture( 0, animObj->m_Textures[ iTexture+iOwner+1 ].m_pTexture );
	// Display the quad
	m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	// Dereference texture
	m_pd3dDevice->SetTexture( 0, NULL);
}

void CD3DFramework::vInitializeUnits( void )
{
	int iUnit;
	
	//
	// RESET MANAGER
	//
	m_UnitManager.vReset();

	//
	// SET DIRECT3D DEVICE
	//
	m_UnitManager.vSetRenderDevice ( m_pd3dDevice );

	//
	// IMPORT UNIT BASE DATA
	//
	m_UnitManager.iLoadBaseTypes(	"UnitData\\BaseType_Defense.csv", 
									"UnitData\\BaseType_Offense.csv", 
									"UnitData\\BaseType_Movement.csv",
									"UnitData\\BaseType_Unit.csv", 
									"UnitData\\BaseType_Animation.csv" );

	// Add some units to the "game"
	iUnit = m_UnitManager.iAddUnit( "Apache Attack Helicopter", 0 );
	m_UnitManager.m_UnitObjs[ iUnit ].vSetPosition( -180.0f, -80.0f );

	iUnit = m_UnitManager.iAddUnit( "Apache Attack Helicopter", 1 );
	m_UnitManager.m_UnitObjs[ iUnit ].vSetPosition( -70.0f, -80.0f );

	iUnit = m_UnitManager.iAddUnit( "Spirit Scout Helicopter", 2 );
	m_UnitManager.m_UnitObjs[ iUnit ].vSetPosition( 50.0f, -80.0f );

	iUnit = m_UnitManager.iAddUnit( "Spirit Scout Helicopter", 3 );
	m_UnitManager.m_UnitObjs[ iUnit ].vSetPosition( 180.0f, -80.0f );
}

void CD3DFramework::vUpdateUnits( void )
{
	CUnit	*ptrUnit;

	// Set the action of the temporary units
	// Action #2 = fire weapon
	//
	m_iAction = 2;

	// Loop through all avail units
	for( int i = 0; i < m_UnitManager.m_iTotalUnitObjs; i++ ) {

		// Set a pointer to the unit
		ptrUnit = &m_UnitManager.m_UnitObjs[ i ];

		// Check if active
		if( ptrUnit->m_bActive ) {
			// Fidget
			if( m_iAction == 3 || m_iAction == 0 ) {
				ptrUnit->m_iCurStillFrame++;
				if( ptrUnit->m_iCurStillFrame >= ptrUnit->m_Animation->m_iNumStillFrames ) {
					ptrUnit->m_iCurStillFrame = 0;
				}
				ptrUnit->m_iCurAnimFrame = 
					ptrUnit->m_Animation->m_iStartStillFrames +
					(ptrUnit->m_iCurStillFrame*(UNITMANAGER_MAXOWNERS+1));
			}
			// Rotate
			else if( m_iAction == 1 ) {
				ptrUnit->m_fRot += ptrUnit->m_Movement->m_fTurnSpeed;
				// If spun completely around, reset
				if( ptrUnit->m_fRot > 360.0f ) 
					ptrUnit->m_fRot -= 360.0f;
			}
			// Fire the units
			else if( m_iAction == 2 ) {
				ptrUnit->m_iCurAttackFrame++;
				if( ptrUnit->m_iCurAttackFrame >= ptrUnit->m_Animation->m_iNumAttackFrames ) {
					ptrUnit->m_iCurAttackFrame = 0;
				}
				ptrUnit->m_iCurAnimFrame = 
					ptrUnit->m_Animation->m_iStartAttackFrames +
					(ptrUnit->m_iCurAttackFrame*(UNITMANAGER_MAXOWNERS+1));
			}
			// Die
			else if( m_iAction == 4 ) {
				ptrUnit->m_iCurDieFrame++;
				if( ptrUnit->m_iCurDieFrame >= ptrUnit->m_Animation->m_iNumDieFrames ) {
					ptrUnit->m_iCurDieFrame = 0;
				}
				ptrUnit->m_iCurAnimFrame = 
					ptrUnit->m_Animation->m_iStartDieFrames +
					(ptrUnit->m_iCurDieFrame*(UNITMANAGER_MAXOWNERS+1));
			}
			// Move
			else if( m_iAction == 5 ) {
				ptrUnit->m_iCurMoveFrame++;
				if( ptrUnit->m_iCurMoveFrame >= ptrUnit->m_Animation->m_iNumMoveFrames ) {
					ptrUnit->m_iCurMoveFrame = 0;
				}
				ptrUnit->m_iCurAnimFrame = 
					ptrUnit->m_Animation->m_iStartMoveFrames +
					(ptrUnit->m_iCurMoveFrame*(UNITMANAGER_MAXOWNERS+1));

				// Move the unit
				ptrUnit->m_fYPos += ptrUnit->m_Movement->m_fMovementSpeed;
				// If out top of screen, put at bottom
				if( ptrUnit->m_fYPos > 360.0f ) 
					ptrUnit->m_fYPos = -360.0f;
			}
		}
	}	
}

⌨️ 快捷键说明

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