DirectX基础学习系列1

时间:2022-07-20 11:19:08

1.3 基础

1.3.1表面

表面接口:     IDirect3DSurface9

获得表面信息:GetDesc(D3DSURFACE_DESC)

获得表面接口指针 :LockRect( D3DLOCKED_RECT * pLockedRect,CONST RECT * pRect, DWORD Flags)

Un LockRect()

其中:D3DLOCKED_RECT {INT Pitch ;  void *pBits ;}

1.3.2  多重采样

D3DMULTISAMPLE_TYPE

{

D3D_MULTISAPLE_NONE

D3D_MULTISAMPLE_1....

}

使用函数IDirectX3D9::CheckDeviceMultiSampleType()检测

1.3.3 像素格式

D3DFORMAT

{

D3DFMT_R8G8B8

D3DFMT_X8R8G8B8

D3DFMT_A8R8G8B8

D3DFMT_A16B16G16R16F

D3DFMT_A32B32G32R32F

}

1.3.4内存池

D3DPOOL

{

D3DPOOL_DEFAULT

D3DPOOL_MANAGE

D3DPOOL_SYSTEMMEM

D3DPOOL_SCRATCH

}

1.3.5 交换链与页面置换

IDirect3dSwapChain9

1.3.6 深度缓存 z-buffer

判断像素的前后,D3DFMT_D32   D3DFMT_D24S8  D3DFMT_D24X8

1.3.6设备性能

D3DCAP9

1.4初始化过程

(1)获取IDirect3D9指针

IDirect3d9 * p =  Direct3DCreate9(D3D_SDK_VESION) ;

设备枚举,获得设备性能参数 以及创建显示接口

(2)检测设备性能

IDirect3D9::GetDeviceCaps(uint Adapter,D3DDETYPE DeviceType , D3DCAP9*pCaps)

(3)初始化D3DPRESENT_PARAMTERS参数

(4)创建IDirect3DDevic9

IDirect3d9::CreateDevice()

1.5 代码

注意:运行代码需要加入:d3d9.lib d3dx9.lib winmm.lib 库文件

// Step 1: Create the IDirect3D9 object.

IDirect3D9* d3d9 = 0;

    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

    if( !d3d9 )

{

::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);

return false;

}

// Step 2: Check for hardware vp.

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;

// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.

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: Create the device.

hr = d3d9->CreateDevice(

D3DADAPTER_DEFAULT, // primary adapter

deviceType,         // device type

hwnd,               // window associated with device

vp,                 // vertex processing

   &d3dpp,             // present parameters

   device);            // return created device

if( FAILED(hr) )

{

// try again using a 16-bit depth buffer

d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

hr = d3d9->CreateDevice(

D3DADAPTER_DEFAULT,

deviceType,

hwnd,

vp,

&d3dpp,

device);

if( FAILED(hr) )

{

d3d9->Release(); // done with d3d9 object

::MessageBox(0, "CreateDevice() - FAILED", 0, 0);

return false;

}

}

d3d9->Release(); // done with d3d9 object