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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_Isometric2DSpriteTiles
//
// Description: Example code showing how to create a simple tile map
//				using an array of integers that point to textures
//				in memory.  The tiles are drawn using the DXSprite
//				Interface.
//
// 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:
//		resourcedata.rc
//		main.cpp
//		main.h
//
// Original D3D Application Shell Code: 
//		Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"

int g_iNumTextures = 9;

//-----------------------------------------------------------------------------
// 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("2D Isometric Tile Example with Sprites");
    m_pStatsFont		= NULL;

	m_shWindowWidth		= 640;
	m_shWindowHeight	= 320;
	m_shTileMapWidth	= 10;
	m_shTileMapHeight	= 10;

	m_dwCreationWidth	= 640;
	m_dwCreationHeight	= 320;
}

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

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

	// Randomly place rocks on the grass
	
	// Seed the randomized
	srand( timeGetTime() );
	for( int i = 0; i < 100; i++ ) {
		
		// Populate the base layer with
		// grass, sand, or brick tiles
		if( rand()%10 == 3 ) 
			m_iTileMap[ i ][ 0 ] = 2;
		else if( rand()%10 == 4 ) 
			m_iTileMap[ i ][ 0 ] = 3;
		else
			m_iTileMap[ i ][ 0 ] = 4;

		// Populate the detail layer with
		// pillars or trees
		if( rand()%10 == 5 ) 
			m_iTileMap[ i ][ 1 ] = 6;
		else if( rand()%10 == 4 ) 
			m_iTileMap[ i ][ 1 ] = 5;
		else if( rand()%10 == 3 ) 
			m_iTileMap[ i ][ 1 ] = 8;
	}

    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;
	RECT				rectDest;

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

    // Begin the scene 
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) {
		// Start Drawing using the sprite device
		pd3dxSprite->Begin();
		// Vertical
		for( iY = 0; iY < m_shTileMapHeight; iY++ ) {
			// Horizontal
			for( iX = 0; iX < m_shTileMapWidth; iX++ ) {
				
				//---------------------------------------------
				//          RENDER THE BASE LAYER
				//---------------------------------------------
				
				// Figure out which tile to display
				iCurTile = m_iTileMap[ iX + ( iY * m_shTileMapWidth ) ][ 0 ];
				// Create the destination rectangle
				rectDest.left = (iX*32)-(iY*32)+290;
				rectDest.top = ((iY*16)+(iX*16))+20;
				BltSprite( &rectDest, m_pTexture[ iCurTile ], NULL );

				//---------------------------------------------
				//          RENDER THE DETAIL LAYER
				//---------------------------------------------
				
				iCurTile = m_iTileMap[ iX + ( iY * m_shTileMapWidth ) ][ 1 ];
				// Only draw the tile if other than 0
				if( iCurTile != 0 ) {
					if( iCurTile == 5 ) {
						rectDest.left = (iX*32)-(iY*32)+290;
						rectDest.top = ((iY*16)+(iX*16))-74;
					}
					else if( iCurTile == 6 ) {
						rectDest.left = (iX*32)-(iY*32)+290;
						rectDest.top = ((iY*16)+(iX*16))-64;
					}
					else if( iCurTile == 8 ) {
						rectDest.left = (iX*32)-(iY*32)+290;
						rectDest.top = ((iY*16)+(iX*16))-12;
					}
					BltSprite( &rectDest, m_pTexture[ iCurTile ], NULL );
				}
			}
		}
		// End the sprite device usage
		pd3dxSprite->End();
		
		// Show frame rate
        m_pStatsFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
		// Show video device information
        m_pStatsFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

        // 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, Identity;
		
	// Restore the fonts
    m_pStatsFont->RestoreDeviceObjects();

	// Create the sprite interface
	D3DXCreateSprite( m_pd3dDevice, &pd3dxSprite );

    // 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->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

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

	// Set alpha blending states
	// This is used to provide transparency/translucency
	m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,			D3DBLEND_SRCALPHA );
	m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND,			D3DBLEND_INVSRCALPHA );

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

	D3DXMatrixOrthoLH( &matproj, (float)m_shWindowWidth, (float)m_shWindowHeight, 0.0f, 1.0f );
	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
	//---------------------------------------------

	// Load the texture for the terrain
	for( int i = 0; i < g_iNumTextures; i++ ) {
		sprintf( szFileName,"tile%d.bmp", i );
		if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[ i ] ) ) ) {
			return S_OK;
		}
	}

	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
	for( int i = 0; i < g_iNumTextures; i++ ) {
		SAFE_RELEASE( m_pTexture[ i ] );
	}
	// Release the sprite device
	SAFE_RELEASE( pd3dxSprite );

    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
	for( int i = 0; i < g_iNumTextures; i++ ) {
		SAFE_RELEASE( m_pTexture[ i ] );
	}
	// Release the sprite device
	SAFE_RELEASE( pd3dxSprite );

    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
	for( int i = 0; i < g_iNumTextures; i++ ) {
		SAFE_DELETE( m_pTexture[ i ] );
	}
	// Delete the sprite device
	SAFE_DELETE( pd3dxSprite );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CD3DFramework::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

//-----------------------------------------------------------------------------
// Desc: Rendering function to render a sprite
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::BltSprite( RECT *pDestRect, LPDIRECT3DTEXTURE9 pSrcTexture, RECT *pSrcRect )
{
	D3DXVECTOR2 vecScaling(1,1);	// Scaling
	D3DXVECTOR2 vecRotCenter(0,0);	// Rotation center
	D3DXVECTOR2 vecTrans(0,0);		// Translation

	// Set the destination location
	vecTrans.x = (float) pDestRect->left;
	vecTrans.y = (float) pDestRect->top;

	// Display the sprite on-screen	
	pd3dxSprite->Draw( pSrcTexture, NULL, &vecScaling, &vecRotCenter, 0, &vecTrans, 0xFFFFFFFF );
	
	return S_OK;
}

⌨️ 快捷键说明

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