程序源码如下:
http://download.csdn.net/detail/smilestone322/7664705
在DirectDraw 7. 0 里, IDirectDraw7对象相当重要, 因为IDirectDraw7对象被当成一个
“工厂( factory) ”来创建其他的DirectDraw对象, 包括表面( surface)、裁剪器( clipper ) 以及调色板( palette)。
在Directx 9.0中Direct3D的应用步骤
1) 创建IDirect3D9对象;
IDirect3D9 * WINAPIDirect3DCreate9(UINTSDKVersion);
Parameters
SDKVersion
Type: UINT
The value of this parameter should be D3D_SDK_VERSION. See Remarks.
返回值:If successful, this function returns a pointer to anIDirect3D9 interface; otherwise, aNULL pointer is returned.
调用例子:
LPDIRECT3D9 g_pD3D = NULL;
if( NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
std::stringgetGPUstring(void) {// DirectX 9c, XP SP3 or better
DWORD vendorID = 0;
DWORD deviceID = 0;
std::stringtemp;
IDirect3D9* direct3D9 = Direct3DCreate9(D3D_SDK_VERSION);
if (0 != direct3D9) {
D3DADAPTER_IDENTIFIER9 adapterIdentifier;
HRESULT hr = direct3D9->GetAdapterIdentifier(0, 0, &adapterIdentifier);
if (S_OK ==hr) {
vendorID = adapterIdentifier.VendorId;
deviceID = adapterIdentifier.DeviceId;
temp = adapterIdentifier.Description;
}
direct3D9->Release();
}
return temp;
}
2) 检查IDirect3D9对象的性能并选择一个合适的设备类型;
GetAdapterCount:
UINT GetAdapterCount();
返回值:系统可用的显示适配器数量。如果函数的返回值为1,就很好选择, 因为只有一个显示适配器可供选择。
GetAdapterIdentifier:
HRESULT GetAdapterIdentifier(
[in] UINT Adapter,
[in] DWORD Flags,
[out] D3DADAPTER_IDENTIFIER9 *pIdentifier
);
函数功能: Describes the physical display adapters present in the system when theIDirect3D9 interface was instantiated.
参数:
Parameters
Adapter [in]
Type: UINT
Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter. The minimum value for this parameter is 0, and the maximum value for this parameter is one less than the value returned byGetAdapterCount.
Flags [in]
Type: DWORD
Flags sets the WHQLLevel member of D3DADAPTER_IDENTIFIER9. Flags can be set to either 0 or D3DENUM_WHQL_LEVEL. If D3DENUM_WHQL_LEVEL is specified, this call can connect to the Internet to download new Microsoft Windows Hardware Quality Labs (WHQL) certificates.
Differences between Direct3D 9 and Direct3D 9Ex:
D3DENUM_WHQL_LEVEL is deprecated for Direct3D9Ex running on Windows Vista, Windows Server 2008, Windows 7, and Windows Server 2008 R2 (or more current operating system). Any of these operating systems return 1 in theWHQLLevel member of D3DADAPTER_IDENTIFIER9 without checking the status of the driver.
pIdentifier [out]
Type: D3DADAPTER_IDENTIFIER9*
Pointer to a D3DADAPTER_IDENTIFIER9 structure to be filled with information describing this adapter. IfAdapter is greater than or equal to the number of adapters in the system, this structure will be zeroed.
返回值:
If the method succeeds, the return value is D3D_OK. D3DERR_INVALIDCALL is returned if Adapter is out of range, if Flags contains unrecognized parameters, or if pIdentifier isNULL or points to unwriteable memory.
例子同上:
GetDeviceCaps:
HRESULT GetDeviceCaps(
[in] UINT Adapter,
[in] D3DDEVTYPE DeviceType,
[out] D3DCAPS9 *pCaps
);
函数功能:检查系统每个显示适配器上的每个设备的性能
参数:
Adapter [in]
Type: UINT
Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter.
DeviceType [in]
Type: D3DDEVTYPE
Member of the D3DDEVTYPE enumerated type. Denotes the device type.
pCaps [out]
Type: D3DCAPS9*
Pointer to a D3DCAPS9 structure to be filled with information describing the capabilities of the device.
返回值:
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_INVALIDDEVICE, D3DERR_OUTOFVIDEOMEMORY, and D3DERR_NOTAVAILABLE.
3) 为适配器选择一个显示适配器和显示模式;
查询适配器显示模式:
从一个IDirect3D9 对象中得到显示模式信息,前后需要使用两个函数, 第一个函数是:
UINT GetAdapterModeCount(
[in] UINT Adapter,
[in] D3DFORMAT Format
);
函数功能:
这个函数的返回值是一个UINT, 这个值包含了当前正在查询的适配器所有可用的显示模式。该函数只有一个参数, 这个参数是当前正在查询的适配器的数;
Adapter [in]
Type: UINT
Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter.
Format [in]
Type: D3DFORMAT
Identifies the format of the surface type using D3DFORMAT. Use EnumAdapterModes to see the valid formats.
返回值:
This method returns the number of display modes on this adapter or zero if Adapter is greater than or equal to the number of adapters on the system.
HRESULT EnumAdapterModes(
[in] UINT Adapter,
[in] D3DFORMAT Format,
[in] UINT Mode,
[out] D3DDISPLAYMODE *pMode
);
函数功能:Queries the device to determine whether the specified adapter supports the requested format and display mode. This method could be used in a loop to enumerate all the available adapter modes.
参数:
Adapter [in]
Type: UINT
Ordinal number denoting the display adapter to enumerate. D3DADAPTER_DEFAULT is always the primary display adapter. This method returns D3DERR_INVALIDCALL when this value equals or exceeds the number of display adapters in the system.
Format [in]
Type: D3DFORMAT
Allowable pixel formats. See Remarks.
Mode [in]
Type: UINT
Represents the display-mode index which is an unsigned integer between zero and the value returned byGetAdapterModeCount minus one.
pMode [out]
Type: D3DDISPLAYMODE*
A pointer to the available display mode of type D3DDISPLAYMODE. See Remarks.
Allowed formats (which are members of D3DFORMAT) are as follows:
D3DFMT_A1R5G5B5
D3DFMT_A2R10G10B10
D3DFMT_A8R8G8B8
D3DFMT_R5G6B5
D3DFMT_X1R5G5B5
D3DFMT_X8R8G8B8
HRESULT GetAdapterDisplayMode(
[in] UINT Adapter,
[in, out] D3DDISPLAYMODE *pMode
);
函数功能:
Retrieves the current display mode of the adapter.
参数:
Adapter [in]
Type: UINT
Ordinal number that denotes the display adapter to query. D3DADAPTER_DEFAULT is always the primary display adapter.
pMode [in, out]
Type: D3DDISPLAYMODE*
Pointer to a D3DDISPLAYMODE structure, to be filled with information describing the current adapter's mode.
4) 创建并初始化IDirect3DDevice9对象;
创建ID i r e c t 3 D D e v ic e 9 对象函数如下:
HRESULT CreateDevice(
[in] UINT Adapter,
[in] D3DDEVTYPE DeviceType,
[in] HWND hFocusWindow,
[in] DWORD BehaviorFlags,
[in, out] D3DPRESENT_PARAMETERS *pPresentationParameters,
[out, retval] IDirect3DDevice9 **ppReturnedDeviceInterface
);
参数:
Adapter [in]
Type: UINT
Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter.
DeviceType [in]
Type: D3DDEVTYPE
Member of the D3DDEVTYPE enumerated type that denotes the desired device type. If the desired device type is not available, the method will fail.
hFocusWindow [in]
Type: HWND
The focus window alerts Direct3D when an application switches from foreground mode to background mode. See Remarks.
For full-screen mode, the window specified must be a top-level window.
For windowed mode, this parameter may be NULL only if the hDeviceWindow member ofpPresentationParameters is set to a valid, non-NULL value.
BehaviorFlags [in]
Type: DWORD
Combination of one or more options that control device creation. For more information, seeD3DCREATE.
pPresentationParameters [in, out]
Type: D3DPRESENT_PARAMETERS*
Pointer to a D3DPRESENT_PARAMETERS structure, describing the presentation parameters for the device to be created. If BehaviorFlags specifiesD3DCREATE_ADAPTERGROUP_DEVICE, pPresentationParameters is an array. Regardless of the number of heads that exist, only one depth/stencil surface is automatically created.
For Windows 2000 and Windows XP, the full-screen device display refresh rate is set in the following order:
1.User-specified nonzero ForcedRefreshRate registry key, if supported by the device.
2.Application-specified nonzero refresh rate value in the presentation parameter.
3.Refresh rate of the latest desktop, if supported by the device.
4.75ertz if supported by the device.
5.60ertz if supported by the device.
6.Device default.
An unsupported refresh rate will default to the closest supported refresh rate below it. For example, if the application specifies 63 hertz, 60 hertz will be used. There are no supported refresh rates below 57 hertz.
pPresentationParameters is both an input and an output parameter. Calling this method may change several members including:
1)If BackBufferCount, BackBufferWidth, and BackBufferHeight are 0 before the method is called, they will be changed when the method returns.
2)If BackBufferFormat equals D3DFMT_UNKNOWN before the method is called, it will be changed when the method returns.
ppReturnedDeviceInterface [out, retval]
Type: IDirect3DDevice9**
Address of a pointer to the returned IDirect3DDevice9 interface, which represents the created device.
例子:
HRESULT CDirect3D9MFCView::InitD3D(HWND hWnd, int width, intheight, bool windowed, D3DDEVTYPE deviceType )
{
HRESULT hr = 0;
IDirect3D9 *d3d9 = 0;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION );
if( !d3d9 )
{
::MessageBox( 0, _T( "InitD3D( ) - Failed!" ), 0, 0 );
return E_FAIL;
}
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps );
int vp = 0;
if( caps.DevCaps &D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
_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.Windowed =windowed;
_d3dpp.hDeviceWindow =hWnd;
_d3dpp.EnableAutoDepthStencil =true;
_d3dpp.AutoDepthStencilFormat =D3DFMT_D24S8;
_d3dpp.Flags = 0;
_d3dpp.FullScreen_RefreshRateInHz =D3DPRESENT_RATE_DEFAULT;
_d3dpp.PresentationInterval =D3DPRESENT_INTERVAL_IMMEDIATE;
hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, deviceType, hWnd, vp,
&_d3dpp, &_device );
if( FAILED(hr ) )
{
_d3dpp.AutoDepthStencilFormat =D3DFMT_D24S8;
hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, deviceType, hWnd, vp,
&_d3dpp, &_device );
if( FAILED(hr ) )
{
d3d9->Release( );
::MessageBox( 0, _T("CreateDevice( ) - Failed!"), 0, 0 );
return hr;
}
}
d3d9->Release( );
return S_OK;
}
3) 将图像加载到纹理上;
HRESULT CDirect3D9MFCView::Render()
{
if( _device )
{
_device->Clear( 0, 0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );
_device->BeginScene( );
_teapot->DrawSubset( 0 );
_device->EndScene( );
_device->Present( 0, 0, 0, 0 );
}
return S_OK;
}