📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_2DTiles
//
// 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"
LPD3DXBUFFER pCode;
LPD3DXBUFFER pErrors;
//-----------------------------------------------------------------------------
// 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 Tile 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 ) );
// Fill the second half with beach tile
for( int i = 0; i < 50; i++ ) {
m_iTileMap[ i+50 ] = 3;
}
// Randomly place rocks on the grass
// Seed the randomized
srand( timeGetTime() );
for( i = 0; i < 50; i++ ) {
// Place rock tile if random 0-10 = 5
if( rand()%10 == 5 )
m_iTileMap[ i ] = 1;
}
// Place the grass edging along the beach tiles
for( i = 50; i < 60; i++ ) {
m_iTileMap[ i ] = 2;
}
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(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 );
}
}
// 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;
// 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
//---------------------------------------------
// Load the texture for the terrain
sprintf(szFileName,"grass00.bmp");
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[0] ) ) ) {
return S_OK;
}
sprintf(szFileName,"grass_rocks.bmp");
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[1] ) ) ) {
return S_OK;
}
sprintf(szFileName,"grass_edging_bottom.bmp");
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[2] ) ) ) {
return S_OK;
}
sprintf(szFileName,"beach.bmp");
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &m_pTexture[3] ) ) ) {
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
SAFE_RELEASE( m_pTexture[ 0 ] );
SAFE_RELEASE( m_pTexture[ 1 ] );
SAFE_RELEASE( m_pTexture[ 2 ] );
SAFE_RELEASE( m_pTexture[ 3 ] );
// 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
SAFE_RELEASE( m_pTexture[ 0 ] );
SAFE_RELEASE( m_pTexture[ 1 ] );
SAFE_RELEASE( m_pTexture[ 2 ] );
SAFE_RELEASE( m_pTexture[ 3 ] );
// 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
SAFE_DELETE( m_pTexture[ 0 ] );
SAFE_DELETE( m_pTexture[ 1 ] );
SAFE_DELETE( m_pTexture[ 2 ] );
SAFE_DELETE( m_pTexture[ 3 ] );
// 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 )
{
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;
// 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;
// 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);
}
class CUnitDefense
{
private:
unsigned int m_iMissileArmorRating;
unsigned int m_iBulletArmorRating;
unsigned int m_iLaserArmorRating;
unsigned int m_iMeleeArmorRating;
int m_iHitPoints;
int m_iRegenRate;
public:
CUnitDefense();
~CUnitDefense();
};
class CUnitOffense
{
private:
unsigned int m_iMissileDamageRating;
unsigned int m_iBulletDamageRating;
unsigned int m_iLaserDamageRating;
unsigned int m_iMeleeDamageRating;
unsigned int m_iSplashRadius;
unsigned int m_iRateOfFire;
unsigned int m_iProjectileSpeed;
public:
CUnitOffense();
~CUnitOffense();
};
class CUnitMovement
{
private:
unsigned int m_iSpeed;
unsigned int m_iMovementType;
unsigned int m_iAcceleration;
unsigned int m_iDeacceleration;
public:
CUnitMovement();
~CUnitMovement();
};
class CUnit
{
private:
CUnitDefense m_Defense;
CUnitOffense m_Offense;
CUnitMovement m_Movement;
public:
CUnit();
~CUnit();
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -