DirectX 9.0 学习笔记1
--Flyli
今天第一次接触directx9的编程,其实也是第一次接触directx编程,于是把自己的笔记记录下来和大家分享,呵呵~
我从网上下载了direct9 2007年的那个sdk,安装后,开始了我第一次编程,其函数原身来源与sdk的Tutorial 1: CreateDevice ,关于这个Tutorial1(指导1),其很大一部分内容都是关于windows窗体的创建,而不是我们的主题。
以下是sdk上第一个程序的完整代码,进过我的习惯性修改,并添加了我的部分注释:
#include <d3d9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
//-----------------------------------------------------------------------------
//Clobal function declaration
//-----------------------------------------------------------------------------
//这个是我的个人习惯哈,喜欢所有的函数声明放到main前面,而把函数体放main后面
HRESULT InitD3D( HWND hWnd );
VOID Cleanup();
VOID Render();
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
//这里是窗口注册的过程
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL };
RegisterClassEx( &wc );
// Create the application's window
//这里是窗口创建的过程
HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice2",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
//*****************************
//以上都是windows窗口创建的过程
//*****************************
// Initialize Direct3D
// 初始化函数的过程
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT ); //hWnd要显示的窗口句柄,后面宏表示显示方式
UpdateWindow( hWnd );
// Enter the message loop
//windows是用消息驱动的,下面就是著名的消息循环了。呵呵
MSG msg; //定义个消息类型
while( GetMessage( &msg, NULL, 0, 0 ) )
{
//简单的说一下我对消息循环的理解哈,translatemessage将虚拟键消息转换为字符消息以
//便于处理,而dispatchmessage是用于将窗口消息提取出来交给MsgProc处理
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
UnregisterClass( L"D3D Tutorial", wc.hInstance );
return 0;
}
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;//这个判断其实是告诉directx正确的头文件版本,至于作用嘛,良好的编程习惯
//嘛,假如头文件版本变了就需要重新编译,另外就是对我们的d3d设备进行初始
//化
//---------------------------------------------------
//下面对d3dpp进行了填充
//---------------------------------------------------
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
//用direct设备创建一个渲染设备
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;
// Clear the backbuffer to a blue color
//将direct9的设备背景清为蓝色
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
/*Clear 的函数原型为
HRESULT Clear(
DWORD Count,
CONST D3DRECT * pRects,
DWORD Flags,
D3DCOLOR Color,
float Z,
DWORD Stencil);*/
//其每个函数的具体含义见sdk中的说明哈
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Rendering of scene objects can happen here
//在这里进行渲染
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
//下面这句是用来现实刚才渲染的结果的,其具体含义还是需要
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY: //当收到退出消息时
Cleanup(); //释放所创建的direct9的对象
PostQuitMessage( 0 );
return 0;
case WM_PAINT: //收到重绘消息时
Render(); //进行渲染
ValidateRect( hWnd, NULL ); //是句柄为hWnd的窗口全部无效(个人觉这句没有必要)
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam ); //假如这个消息本窗口不需要处理这交付给
//windows自动处理
}