📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_Isometric2DTiles
//
// 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"
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");
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 tiles
// 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;
float fTileX, fTileY;
// 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() ) ) {
// 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 ];
// Figure out the on-screen coordinates
fTileX = -32.0f+(iX*32.0f)-(iY*32.0f);
fTileY = 128.0f-((iY*16.0f)+(iX*16.0f));
// Display the tile
vDrawTile( fTileX, fTileY, 64.0f, 32.0f, iCurTile );
//---------------------------------------------
// RENDER THE DETAIL LAYER
//---------------------------------------------
// Figure out which tile to display
iCurTile = m_iTileMap[ iX + ( iY * m_shTileMapWidth ) ][ 1 ];
if( iCurTile != 0 ) {
// Figure out the on-screen coordinates
fTileX = -32.0f+(iX*32.0f)-(iY*32.0f);
fTileY = 128.0f-((iY*16.0f)+(iX*16.0f));
if( iCurTile == 5 )
vDrawTile( fTileX, fTileY, 64.0f, 125.0f, iCurTile );
else if( iCurTile == 6 )
vDrawTile( fTileX, fTileY, 67.0f, 109.0f, iCurTile );
else if( iCurTile == 8 )
vDrawTile( fTileX, fTileY, 64.0f, 64.0f, iCurTile );
}
}
}
// 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();
// 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;
}
}
//---------------------------------------------
// INITIALIZE 3D-2D TILE
//---------------------------------------------
vInitTileVB();
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 3D-2D tile
SAFE_RELEASE( m_pVBTile );
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 3D-2D tile
SAFE_RELEASE( m_pVBTile );
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 3D-2D tile
SAFE_DELETE( m_pVBTile );
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 );
}
void CD3DFramework::vInitTileVB( void )
{
TILEVERTEX* pVertices;
// 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();
};
void CD3DFramework::vDrawTile( float fXPos, float fYPos, float fXSize, float fYSize, int iTexture )
{
D3DXMATRIX matWorld;
D3DXMATRIX matRotation;
D3DXMATRIX matTranslation;
D3DXMATRIX matScale;
D3DXMATRIX matproj, matview;
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 );
// Set default position, scale, rotation
D3DXMatrixIdentity( &matWorld );
// Scale the tile
D3DXMatrixScaling( &matScale, fXSize, fYSize, 1.0f );
D3DXMatrixMultiply( &matWorld, &matWorld, &matScale );
// Rotate the tile
D3DXMatrixRotationZ( &matRotation, 0.0f );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotation );
// Move the tile
D3DXMatrixTranslation( &matTranslation, fXPos-0.5f, fYPos-0.5f, 0.0f );
D3DXMatrixMultiply( &matWorld, &matWorld, &matTranslation );
// 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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -