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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_UnitTemplate
//
// Description: Example code showing how to create a simple tile map
//				using an array of integers that point to textures
//				in memory.
//
// File Function: Main body of the function (main.cpp)
//
// Code: 
//		Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
//		d3d9.lib dxguid.lib d3dx9dt.lib d3dxof.lib comctl32.lib	winmm.lib
//
// DX Files Required:
//		d3dapp.cpp
//		d3denumeration.cpp
//		d3dfont.cpp
//		d3dsettings.cpp
//		d3dutil.cpp
//		dxutil.cpp
//
// Local Files Required:
//		main.cpp
//		main.h
//
// Original D3D Application Shell Code: 
//		Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"

//-----------------------------------------------------------------------------
// Desc: Entry point to the program. Initializes everything, and goes into a
//       message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
	// Instance of the framework application
    CD3DFramework d3dApp;

    InitCommonControls();
    
	// Create the application environment
	// ----------------------------------
	// This initializes the display and loads all appropriate data.
	//
	if( FAILED( d3dApp.Create( hInst ) ) )
        return 0;

	// Start up the application
	// ------------------------
	// Starts the program running by activating the message loop
	// and render functions.
	//
    return d3dApp.Run();
}

//-----------------------------------------------------------------------------
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CD3DFramework::CD3DFramework()
{
    m_strWindowTitle	= _T("Unit Template Example");
    m_pStatsFont		= NULL;

	m_shWindowWidth		= 480;
	m_shWindowHeight	= 480;
	m_shTileMapWidth	= 10;
	m_shTileMapHeight	= 10;
}

//-----------------------------------------------------------------------------
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::OneTimeSceneInit()
{
    m_pStatsFont = new CD3DFont( _T("Arial"), 8, NULL );
    if( m_pStatsFont == NULL )
        return E_FAIL;

	//---------------------------------------------
	//          INITIALIZE TILE MAP
	//---------------------------------------------
	
	// Clear out the map with the grass tile
	memset( m_iTileMap, 0, (m_shTileMapWidth*m_shTileMapHeight) * sizeof( int ) );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Called once per frame, the call is the entry point for 3d
//       rendering. This function sets up render states, clears the
//       viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::Render()
{
	D3DXMATRIX			matTranslation;
	D3DXMATRIX			matRotation;
	D3DXMATRIX			matRotation2;
	D3DXMATRIX			matScale;
	int					iX;
	int					iY;
	int					iCurTile;
	float				fTileX, fTileY;
	CUnit				*ptrUnit;
	static DWORD		dwLastUpdateTime = 0;

	// Update the units
	if( timeGetTime() > dwLastUpdateTime ) {
		vUpdateUnits();
		dwLastUpdateTime = timeGetTime()+33;
	}

    // Clear the viewport
    m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(120,120,120), 1.0f, 0L );

    // Begin the scene 
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) {
		// Vertical
		for( iY = 0; iY < m_shTileMapHeight; iY++ ) {
			// Horizontal
			for( iX = 0; iX < m_shTileMapWidth; iX++ ) {
				// Figure out which tile to display
				iCurTile = m_iTileMap[ iX + ( iY * m_shTileMapWidth ) ];
				// Figure out the on-screen coordinates
				fTileX = -240.0f+(iX*48.0f);
				fTileY = 192.0f-(iY*48.0f);
				// Display the tile
				vDrawTile( fTileX, fTileY, 48.0f, 48.0f, iCurTile );
			} 
		}

		//
		// RENDER UNITS
		//

		// Turn on transparency
		m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );
		m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,			D3DBLEND_SRCALPHA );
		m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND,			D3DBLEND_INVSRCALPHA );

		// 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 ) {

				// Draw the unit
				vDrawUnit( 
					ptrUnit->m_fXPos, 
					ptrUnit->m_fYPos,
					ptrUnit->m_fScale*128.0f, 
					ptrUnit->m_fScale*128.0f,
					ptrUnit->m_fRot,
					ptrUnit->m_Animation,
					ptrUnit->m_iCurAnimFrame,
					ptrUnit->m_iOwner
				);
			}
		}

		// Turn off transparency
		m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   FALSE );

		// Show frame rate
        m_pStatsFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
		// Show video device information
        m_pStatsFont->DrawText( 2, 15, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

		//
		// Display Stats
		//
		char szStats[128];

		// Memory information
		sprintf( szStats, "Total Unit Types Loaded = %d", m_UnitManager.m_iTotalUnitBaseObjs );
		m_pStatsFont->DrawText( 2, 35, D3DCOLOR_ARGB(255,255,255,255), szStats );
		sprintf( szStats, "Total Textures Loaded   = %d (%ld MBytes)", 
			m_UnitManager.iCountTotalTextures(),
			m_UnitManager.iCountTotalTextures() * 128 * 128 * 4/1000000 );
		m_pStatsFont->DrawText( 2, 50, D3DCOLOR_ARGB(255,255,255,255), szStats );
		
		// Unit stats
		sprintf( szStats, "0] Unit Name____________%s", m_UnitManager.m_UnitBaseObjs[0].m_szName );
		m_pStatsFont->DrawText( 2, 70, D3DCOLOR_ARGB(255,255,200,0), szStats );
		sprintf( szStats, "0] Unit Offense Type 1____%s", m_UnitManager.m_UnitBaseObjs[0].m_Offense1->m_szName );
		m_pStatsFont->DrawText( 2, 85, D3DCOLOR_ARGB(255,255,200,0), szStats );
		if( m_UnitManager.m_UnitBaseObjs[0].m_Offense2 ) {
			sprintf( szStats, "0] Unit Offense Type 2____%s", m_UnitManager.m_UnitBaseObjs[0].m_Offense2->m_szName );
		}
		else {
			sprintf( szStats, "0] Unit Offense Type 2____None" );
		}
		m_pStatsFont->DrawText( 2, 100, D3DCOLOR_ARGB(255,255,200,0), szStats );
		
		if( m_UnitManager.m_UnitBaseObjs[0].m_Offense3 ) {
			sprintf( szStats, "0] Unit Offense Type 3____%s", m_UnitManager.m_UnitBaseObjs[0].m_Offense3->m_szName );
		}
		else {
			sprintf( szStats, "0] Unit Offense Type 3____None" );
		}
		m_pStatsFont->DrawText( 2, 115, D3DCOLOR_ARGB(255,255,200,0), szStats );
		sprintf( szStats, "0] Unit Defense Type_____%s", m_UnitManager.m_UnitBaseObjs[0].m_Defense->m_szName );
		m_pStatsFont->DrawText( 2, 130, D3DCOLOR_ARGB(255,255,200,0), szStats );
		sprintf( szStats, "0] Unit Move Type________%s", m_UnitManager.m_UnitBaseObjs[0].m_Movement->m_szName );
		m_pStatsFont->DrawText( 2, 145, D3DCOLOR_ARGB(255,255,200,0), szStats );

        // End the scene
        m_pd3dDevice->EndScene();
    }

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InitDeviceObjects()
{
    HRESULT hr;

    if( FAILED( hr = m_pStatsFont->InitDeviceObjects( m_pd3dDevice ) ) )
        return hr;

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::RestoreDeviceObjects()
{
	char		szFileName[512];
	D3DXMATRIX	matproj,matview;
		
	// Restore the fonts
    m_pStatsFont->RestoreDeviceObjects();

    // Set up the textures
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );

    // Set miscellaneous render states
    m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
    m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x00888888 );

	// Setup the 3D View
	D3DXMatrixIdentity(&matview);
	m_pd3dDevice->SetTransform(D3DTS_VIEW, &matview);

	D3DXMatrixOrthoLH(&matproj, (float)m_shWindowWidth, (float)m_shWindowHeight, 0, 1);
	m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matproj);
	m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
	m_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);

    // Setup a material
    D3DMATERIAL9 mtrl;
    D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
    m_pd3dDevice->SetMaterial( &mtrl );

	//---------------------------------------------
	// INITIALIZE TILE MAP TEXTURES & UNIT BITMAPS
	//---------------------------------------------

	// Load the texture for the terrain
	sprintf(szFileName,"Textures\\grass00.bmp");
	if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[0] ) ) ) {
		return S_OK;
	}

	//---------------------------------------------
	//         INITIALIZE 3D-2D TILE
	//---------------------------------------------
	vInitTileVB();

	vInitializeUnits();

	return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InvalidateDeviceObjects()
{
	// Invalidate the font
    m_pStatsFont->InvalidateDeviceObjects();
	// Release textures
	SAFE_RELEASE( m_pTexture[ 0 ] );
	m_UnitManager.vClearMem();
	// Release 3D-2D tile
	SAFE_RELEASE( m_pVBTile );
	SAFE_RELEASE( m_pVBUnit );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::DeleteDeviceObjects()
{
	// Delete the font
    m_pStatsFont->DeleteDeviceObjects();
	// Release textures
	SAFE_RELEASE( m_pTexture[ 0 ] );
	m_UnitManager.vClearMem();
	// Release 3D-2D tile
	SAFE_RELEASE( m_pVBTile );
	SAFE_RELEASE( m_pVBUnit );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FinalCleanup()
{
	// Delete the font
    SAFE_DELETE( m_pStatsFont );
	// Delete textures
	SAFE_DELETE( m_pTexture[ 0 ] );
	m_UnitManager.vClearMem();
	// Delete 3D-2D tile
	SAFE_DELETE( m_pVBTile );
	SAFE_RELEASE( m_pVBUnit );

⌨️ 快捷键说明

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