📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_3DTiles
//
// Description: Example code showing how to load and render 3D tiles.
//
// 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
// d3dfile.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("D3DFrame_3DTiles");
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
// Setup tile map and window
m_shTileMapWidth = 10;
m_shTileMapHeight = 10;
// Set this so the framework sizes the window properly
m_dwCreationWidth = 640;
m_dwCreationHeight = 480;
}
//-----------------------------------------------------------------------------
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::OneTimeSceneInit()
{
int i;
m_pStatsFont = new CD3DFont( _T("Arial"), 8, NULL );
if( m_pStatsFont == NULL )
return E_FAIL;
//---------------------------------------------
// INITIALIZE TILE MAP
//---------------------------------------------
//
// Allocate memory for the tiles
//
for( i = 0; i < g_iNumTiles; i++ ) {
m_pObject[ i ] = new CD3DMesh();
}
// Clear out the map with the 0 tile
memset( m_iTileMap, 0, (m_shTileMapWidth*m_shTileMapHeight) * sizeof( int ) );
// Randomly place rocks on the grass
// Seed the randomized
srand( timeGetTime() );
for( i = 0; i < 100; i++ ) {
if( rand()%5 == 3 )
m_iTileMap[ i ] = 1;
else
m_iTileMap[ i ] = 0;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FrameMove()
{
// Rotate the camera about the y-axis
D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, -20.0f, 20.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
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;
int iX, iY;
int iCurTile;
float fXPos;
float fYPos;
// Clear the viewport
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
for( iY = 0; iY < 10; iY++ ) {
// Horizontal
for( iX = 0; iX < 10; iX++ ) {
//---------------------------------------------
// RENDER THE GROUND LAYER
//---------------------------------------------
// Figure out which tile to display
iCurTile = m_iTileMap[ iX + ( iY * m_shTileMapWidth ) ];
// Calculate the position
fXPos = (-5.0f*iX)+22.5f;
fYPos = (-5.0f*iY)+32.5f;
// Set position of tile
D3DXMatrixTranslation( &matTranslation, fXPos, fYPos, 0.0f );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matTranslation );
// Render the tile
m_pObject[ iCurTile ]->Render( m_pd3dDevice );
}
}
// 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;
char szFileName[512];
// Initialize the font
if( FAILED( hr = m_pStatsFont->InitDeviceObjects( m_pd3dDevice ) ) )
return hr;
//---------------------------------------------
// LOAD 3D TILE INFORMATION
//---------------------------------------------
for( int i = 0; i < g_iNumTiles; i++ ) {
// Create the filename
sprintf( szFileName, "ground_tile%d.x", i+1 );
// Load the mesh
if( FAILED( m_pObject[ i ]->Create( m_pd3dDevice, _T( szFileName ) ) ) )
return D3DAPPERR_MEDIANOTFOUND;
// Set its vertex type
m_pObject[ i ]->SetFVF( m_pd3dDevice, D3DVERTEX::FVF );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::RestoreDeviceObjects()
{
D3DLIGHT9 d3dLight;
// Restore the fonts
m_pStatsFont->RestoreDeviceObjects();
// Restore device-memory objects for the mesh
for( int i = 0; i < g_iNumTiles; i++ ) {
m_pObject[ i ]->RestoreDeviceObjects( m_pd3dDevice );
}
// 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 );
// Set the world matrix
D3DXMATRIXA16 matIdentity;
D3DXMatrixIdentity( &matIdentity );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matIdentity );
// Set the projection matrix
D3DXMATRIXA16 matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// Clear out light structure
ZeroMemory( &d3dLight, sizeof(D3DLIGHT9) );
d3dLight.Type = D3DLIGHT_POINT;
d3dLight.Diffuse.r = 1.0f;
d3dLight.Diffuse.g = 1.0f;
d3dLight.Diffuse.b = 1.0f;
d3dLight.Position.x = 0.0f;
d3dLight.Position.y = -20.0f;
d3dLight.Position.z = 20.0f;
d3dLight.Attenuation0 = 1.0f;
d3dLight.Attenuation1 = 0.0f;
d3dLight.Range = 100.0f;
m_pd3dDevice->SetLight( 0, &d3dLight );
m_pd3dDevice->LightEnable( 0, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
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()
{
m_pStatsFont->InvalidateDeviceObjects();
for( int i = 0; i < g_iNumTiles; i++ ) {
m_pObject[ i ]->InvalidateDeviceObjects();
}
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()
{
m_pStatsFont->DeleteDeviceObjects();
for( int i = 0; i < g_iNumTiles; i++ ) {
m_pObject[ i ]->Destroy();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FinalCleanup()
{
SAFE_DELETE( m_pStatsFont );
for( int i = 0; i < g_iNumTiles; i++ ) {
SAFE_DELETE( m_pObject[ i ] );
}
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 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -