工程包含了三个文件,分别是 d3dUtility.h d3dUtility.cpp d3dInit.cpp
d3dUtility.h
#ifndef _d3dUtilityH_
#define _d3dUtilityH_
#include <d3dx9.h>
#include <string.h>
//初始化
bool InitD3D(HINSTANCE hInstance,int width,int height,bool windowed,
D3DDEVTYPE deviceType,IDirect3DDevice9 **device);
int EnterMsgLoop(bool (*ptr_display)(float timeDelt)); //主事件循环
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
template<class T> void Release(T t) //对象释放
{
if(t)
{
t->Release();
t=0;
}
}
template<class T> void Delete(T t) //删除对象
{
if(t)
{
delete t;
t=0;
}
}
#endif
d3dUtility.cpp 实现了d3dUtility.h中声明的方法
#include <Windows.h>
#include <memory>
#include "d3dUtility.h"
bool InitD3D(HINSTANCE hInstance,int width,int height,bool windowed,
D3DDEVTYPE deviceType,IDirect3DDevice9 **device)
{
//创建窗口
//Step 1 创建窗口类
WNDCLASSEX wc={0};
wc.style=CS_HREDRAW+CS_VREDRAW;
wc.cbClsExtra=0;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbWndExtra=0;
//wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.hbrBackground=GetSysColorBrush(COLOR_WINDOW);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wc.hIconSm=wc.hIcon;
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName=L"d3d9";
wc.lpszMenuName=NULL;
//Step 2 注册窗口类
if(!RegisterClassEx(&wc))
{
MessageBox(0,L"RegisterClass()-FAILED",0,0);
return false;
}
//窗口位置和大小
RECT rc={0,0,640,480};
/*RECT rc={GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
640,480}; GetSystemMetrics(SM_CXSCREEN) 获取屏幕宽度 */
AdjustWindowRect(&rc,WS_OVERLAPPEDWINDOW,false);
//Step 3 创建窗口
HWND hwnd=CreateWindowA("d3d9","D3D Game Development!",WS_EX_TOPMOST|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,rc.right-rc.left,rc.bottom-rc.top,NULL,NULL,hInstance,NULL);
if(!hwnd)
{
MessageBox(0,L"CreateWindowA()-FAILED",0,0);
return false;
}
//Step 4 显示及更新窗口
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
//初始化D3D
//Step 1 创建Idirec3D9对象
HRESULT hr=0;
IDirect3D9 *d3d9=0;
d3d9=Direct3DCreate9(D3D_SDK_VERSION);
if(!d3d9)
return false;
//Step 2 检查设备性能
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,deviceType,&caps);
int vp;
if(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//Step 3 填充结构D3DPRESENT_PARAMETERS
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = windowed;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
//Step 4 创建IDirect3DDevice9
hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,deviceType,
hwnd,vp,&d3dpp,device);
if(FAILED(hr))
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,deviceType,
hwnd,vp,&d3dpp,device);
if(FAILED(hr))
{
d3d9->Release();
MessageBox(0,L"CreateDevice()-FAILED",0,0);
return false;
}
}
d3d9->Release(); //释放d3d9对象
return true;
}
int EnterMsgLoop(bool (*ptr_display)(float timeDelta)) //主事件循环
{
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
static float lastTime=float(timeGetTime());
while (msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
float currTime=float(timeGetTime());
float timeDelta=(currTime-lastTime)*0.001f;
ptr_display(timeDelta);
lastTime=currTime;
}
}
return static_cast<int>(msg.wParam);
}
d3dInit.cpp
#include <Windows.h>
#include <memory>
#include "d3dUtility.h"
bool InitD3D(HINSTANCE hInstance,int width,int height,bool windowed,
D3DDEVTYPE deviceType,IDirect3DDevice9 **device)
{
//创建窗口
//Step 1 创建窗口类
WNDCLASSEX wc={0};
wc.style=CS_HREDRAW+CS_VREDRAW;
wc.cbClsExtra=0;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbWndExtra=0;
//wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.hbrBackground=GetSysColorBrush(COLOR_WINDOW);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wc.hIconSm=wc.hIcon;
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName=L"d3d9";
wc.lpszMenuName=NULL;
//Step 2 注册窗口类
if(!RegisterClassEx(&wc))
{
MessageBox(0,L"RegisterClass()-FAILED",0,0);
return false;
}
//窗口位置和大小
RECT rc={0,0,640,480};
/*RECT rc={GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
640,480}; GetSystemMetrics(SM_CXSCREEN) 获取屏幕宽度 */
AdjustWindowRect(&rc,WS_OVERLAPPEDWINDOW,false);
//Step 3 创建窗口
HWND hwnd=CreateWindowA("d3d9","D3D Game Development!",WS_EX_TOPMOST|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,rc.right-rc.left,rc.bottom-rc.top,NULL,NULL,hInstance,NULL);
if(!hwnd)
{
MessageBox(0,L"CreateWindowA()-FAILED",0,0);
return false;
}
//Step 4 显示及更新窗口
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
//初始化D3D
//Step 1 创建Idirec3D9对象
HRESULT hr=0;
IDirect3D9 *d3d9=0;
d3d9=Direct3DCreate9(D3D_SDK_VERSION);
if(!d3d9)
return false;
//Step 2 检查设备性能
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,deviceType,&caps);
int vp;
if(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//Step 3 填充结构D3DPRESENT_PARAMETERS
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = windowed;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
//Step 4 创建IDirect3DDevice9
hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,deviceType,
hwnd,vp,&d3dpp,device);
if(FAILED(hr))
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr=d3d9->CreateDevice(D3DADAPTER_DEFAULT,deviceType,
hwnd,vp,&d3dpp,device);
if(FAILED(hr))
{
d3d9->Release();
MessageBox(0,L"CreateDevice()-FAILED",0,0);
return false;
}
}
d3d9->Release(); //释放d3d9对象
return true;
}
int EnterMsgLoop(bool (*ptr_display)(float timeDelta)) //主事件循环
{
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
static float lastTime=float(timeGetTime());
while (msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
float currTime=float(timeGetTime());
float timeDelta=(currTime-lastTime)*0.001f;
ptr_display(timeDelta);
lastTime=currTime;
}
}
return static_cast<int>(msg.wParam);
}