很长一段时间没写了,现在补上
博主使用的DirectX SDK版本为June 2010,以下以此版本为例,记录龙书上第二部分第一章中初始化Direct3D的API。
IDirect3D9指针获取,Direct3DCreate9函数原型:
/*
* DLL Function for creating a Direct3D9 object. This object supports
* enumeration and allows the creation of Direct3DDevice9 objects.
* Pass the value of the constant D3D_SDK_VERSION to this function, so
* that the run-time can validate that your application was compiled
* against the right headers.
*/
IDirect3D9 * WINAPI Direct3DCreate9(UINT SDKVersion);
获取IDirect3D9对象指针一般代码:
IDirect3D9* _d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
Direct3DCreate9函数执行失败返回空指针,使用时应注意异常处理。
获取设备能力接口IDirect3D9::GetDeviceCaps,函数原型:
STDMETHOD(GetDeviceCaps)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps) PURE;
注:PURE STDMETHOD这些都是微软自己定义的宏。
函数入参adapter指定需要获得哪个显卡特性,可使用宏D3DADAPTER_DEFAULT指定为当前显卡;D3DDEVTYPE为枚举变量,一般可指定硬件设备(D3DDEVTYPE_HAL),软件设备(D3DDEVTYPE_REF);D3DCAPS9为外部所需获得特性的结构,调用GetDeviceCaps后会把结果放入该指针所指结构。
创建IDirect3DDevice9对象接口IDirect3D9::CreateDevice,函数原型:
STDMETHOD(CreateDevice)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface) PURE;
其中比较重要的参数是D3DPRESENT_PARAMETERS* pPresentationParameters,该结构参数会指定所创建的Deivce对象的一些特性。
所以一般的Direct3D初始化包括IDirect3D9对象创建,显卡能力检查,IDirect3DDevice9对象创建,一般初始化代码大致为:
bool InitD3D(HWND hWnd)
{
IDirect3D9 * d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DCAPS9 d3dCaps;
d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
// check caps
D3DPRESENT_PARAMETERS d3dParam;
// d3dParam assignment
IDirect3DDevice9 * d3dDevice;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dParam, &d3dDevice);
}