📄 dx9_initialization.cpp
字号:
//-----------------------------------------------------------------------------
// Name: dx9_initialization.cpp
// Author: Kevin Harris
// Last Modified: 05/27/05
// Description: This sample demonstrates how to initialize Direct3D
//-----------------------------------------------------------------------------
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include"Common.h"
#include"CHud.h"
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
//LPDIRECT3DSURFACE9 pSourSurf = NULL; // HUD图片表面
//LPDIRECT3DTEXTURE9 g_pTexture;
//LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;
//LPDIRECT3DSURFACE9 pRTSurf = NULL; // 纹理表面
const int Width = 800; // 窗口宽度
const int Height = 600; // 窗口高度
double g_dCurTime; // 当前时间
double g_dLastTime; // 上帧时间
LPD3DXFONT g_pd3dxFont = NULL; // 字体
bool g_IsWireFrame = FALSE; // 是否线框渲染
CHud *pHud;
//struct Vertex
//{
// float x, y, z;
// float tu, tv;
//};
//
//Vertex g_quadVertices[] =
//{
// {-1.0f, 0.5f, 0.0f, 0.0f,0.0f },
// { 1.0f, 0.5f, 0.0f, 1.0f,0.0f },
// {-1.0f,-0.5f, 0.0f, 0.0f,1.0f },
// { 1.0f,-0.5f, 0.0f, 1.0f,1.0f }
//};
//bool ComputeBoundingBox(ID3DXMesh* mesh,D3DXMATRIX matW, BoundingBox* box)
//{
// HRESULT hr = 0;
//
// float *v = 0;
// float *buffer, *Buffer_start;
//
// int VerSize = D3DXGetFVFVertexSize(mesh->GetFVF());
// int Vernum = mesh->GetNumVertices();
// int totalSize = VerSize * Vernum;
//
// buffer = (float*)malloc( totalSize );
// ZeroMemory( buffer, totalSize );
// mesh->LockVertexBuffer( 0, (void**)&v );
// memcpy( buffer, v, totalSize );
// mesh->UnlockVertexBuffer();
// Buffer_start = buffer;
//
// int i = 0;
// while (i<Vernum)
// {
// D3DXVec3TransformCoord( (D3DXVECTOR3*)buffer,(D3DXVECTOR3*)v, &matW );
// buffer += 8;
// v += 8;
// i++;
// }
//
// hr = D3DXComputeBoundingBox(
// (D3DXVECTOR3*)Buffer_start,
// Vernum,
// VerSize,
// &box->min,
// &box->max);
//
// free( Buffer_start );
// Buffer_start = NULL;
// buffer = NULL;
//
// if( FAILED(hr) )
// return false;
//
// return true;
//}
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void init(void);
void shutDown(void);
void render(void);
void createFont( void );
void RendText( int x, int y, char* fps);
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
int a = sizeof(D3DXVECTOR3);
WNDCLASSEX winClass;
MSG uMsg;
memset(&uMsg,0,sizeof(uMsg));
winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if( !RegisterClassEx(&winClass) )
return E_FAIL;
int AppLeft = (GetSystemMetrics(SM_CXFULLSCREEN) - Width) / 2;
int AppTop = (GetSystemMetrics(SM_CYFULLSCREEN) - Height) / 2;
g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS",
"ZZ_DirectX 框架程序",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,// WS_MINIMIZEBOX | WS_VISIBLE | WS_SYSMENU,
AppLeft, AppTop, Width, Height, NULL, NULL, hInstance, NULL );
if( g_hWnd == NULL )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );
init(); // 重要部分
while( uMsg.message != WM_QUIT )
{
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
render();
}
shutDown();
UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );
return uMsg.wParam;
}
//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}
break;
case WM_CLOSE:
{
PostQuitMessage(0);
}
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
{
return DefWindowProc( hWnd, msg, wParam, lParam );
}
break;
}
return 0;
}
//-----------------------------------------------------------------------------
// Name: init()
// Desc:
//-----------------------------------------------------------------------------
void init( void )
{
// 创建D3D接口
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
if( g_pD3D == NULL )
{
// TO DO: Respond to failure of Direct3DCreate9
return;
}
// 取得当前显示模式
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
{
// TO DO: Respond to failure of GetAdapterDisplayMode
return;
}
// 检查设备格式
HRESULT hr;
if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
{
if( hr == D3DERR_NOTAVAILABLE )
// POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
return;
}
// 如果不支持硬件顶点处理,就使用软件
D3DCAPS9 d3dCaps;
if( FAILED( g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, &d3dCaps ) ) )
{
// TO DO: Respond to failure of GetDeviceCaps
return;
}
DWORD dwBehaviorFlags = 0;
if( d3dCaps.VertexProcessingCaps != 0 )
dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//
// Everything checks out - create a simple, windowed device.
//
D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // 最有效的交换方式
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // 立即交换
d3dpp.BackBufferWidth = Width; // 不设置这2个成员,会导致全屏化会失败!
d3dpp.BackBufferHeight = Height;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
dwBehaviorFlags, &d3dpp, &g_pd3dDevice ) ) )
{
// TO DO: Respond to failure of CreateDevice
return;
}
// 创建字体
createFont();
pHud = new CHud;
pHud->init( g_pd3dDevice, g_pd3dxFont, "ABCDEFG" );
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 90.0f ),
640.0f / 480.0f, 0.1f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0, 0, -5 ),
&D3DXVECTOR3(0, 0, 0 ),
&D3DXVECTOR3(0, 1, 0 ));
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}
//-----------------------------------------------------------------------------
// Name: render()
// Desc:
//-----------------------------------------------------------------------------
int FrameCnt = 0;
float TimeElapsed = 0.0f;
void render( void )
{
static char strFPS[10]; // strFPS字符串
static char allFPS[255]; // allFPS字符串
char* pstrFPS = strFPS;
char* pallFPS = allFPS;
sprintf( pallFPS, "帧率:" );
g_dCurTime = timeGetTime();
float timeDelta = (float) ((g_dCurTime - g_dLastTime)*0.001f);
g_dLastTime = g_dCurTime;
//增加帧数
FrameCnt++;
//增加时间
TimeElapsed += timeDelta;
USHORT fps;
if(TimeElapsed >= 1.0f) // 当时间超过一秒时开始计算每秒帧数
{
fps = (float)FrameCnt / TimeElapsed;
itoa( fps, pstrFPS, 10 );
TimeElapsed = 0.0f;
FrameCnt = 0;
};
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.7f,0.7f,0.7f,1.0f), 1.0f, 0 ); // D3DCOLOR_COLORVALUE 初始化颜色(D3DCOLOR_RGBA)的宏
g_pd3dDevice->BeginScene();
// 在这里渲染图形
strcat( pallFPS, pstrFPS );
RendText( 10, 10, pallFPS );
pHud->render( D3DXVECTOR3(0,0,0) );
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc:
//-----------------------------------------------------------------------------
void shutDown( void )
{
/*if( g_pVertexBuffer != NULL )
g_pVertexBuffer->Release();
if( g_pTexture != NULL )
g_pTexture->Release();
if( pSourSurf != NULL )
pSourSurf->Release();
if( pRTSurf != NULL )
pRTSurf->Release();*/
SAFE_DELETE( pHud );
SAFE_RELEASE( g_pd3dxFont );
SAFE_RELEASE( g_pd3dDevice );
SAFE_RELEASE( g_pD3D );
}
void createFont( void )
{
HRESULT hr;
HDC hDC;
int nHeight;
int nPointSize = 9;
hDC = GetDC( NULL );
// GetDeviceCaps(hDC, LOGPIXELSY) 用于取得每英寸有多少像素
// MulDiv函数(被乘数, 分子, 分母) = nPointSize * GetDeviceCaps(hDC, LOGPIXELSY) / 72
nHeight = ( MulDiv(nPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72) );
ReleaseDC( NULL, hDC );
hr = D3DXCreateFont(
g_pd3dDevice,
nHeight,
0, // Weight
0,
true,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
TEXT("宋体"),
&g_pd3dxFont
);
};
void RendText( int x, int y, char* fps)
{
RECT destRect;
SetRect( &destRect, x, y, 100, 100 );
g_pd3dxFont->DrawText( NULL, fps, -1, &destRect, DT_NOCLIP,
D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -