Getting Started with DirectX: Initializing Direct3D and Frequently Asked Questions

This article mainly explains how to initialize Direct3D. After the successful initialization of Direct3D, we will receive a C representing the graphics card+ Object (type: IDrect3DDevice9).

Initialization flowchart:


.

Complete code:


//global variable 
IDirect3DDevice9 *device = 0 ;
// Step 1: Create the IDirect3D9 object.
IDirect3D9* d3d9 = 0;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d9)
{
 return false;
}
// Step 2: Check for hardware vp.
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &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 = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = false;
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.(device representing physical graphics cards) 
//////////////////////////////
 HRESULT hr = d3d9->CreateDevice(
 D3DADAPTER_DEFAULT, // primary adapter
 D3DDEVTYPE_HAL,         // device type
 hWnd,               // window associated with device
 vp,                 // vertex processing
 &d3dpp,             // present parameters
 &device);            // return created device
 if (FAILED(hr))
 {
     d3d9->Release(); // done with d3d9 object
     return false;
 }
 else {
     ::MessageBox(0, L"successfully created graphics card object",0,0);
 }
}
d3d9->Release(); // done with d3d9 object
Possible errors encountered:

Error type: .

 unresolved external symbols Direct3DCreate9@4


Solution 1:
In VS Add the following file name to the project/properties/configuration properties/connectors/inputs/additional dependencies in NET, d3dx9.lib d3dxof.lib d3d9.lib winmm.lib dxguid.lib comctl32.lib
Solution 2: .

    #pragma comment(lib,"d3d9.lib") 
#pragma comment(lib,"winmm.lib") 
#pragma comment(lib,"d3dx9.lib")