Booting Direct3D

The first step any Direct3D application must perform is to create the Direct3D object, from which all other objects are generated. The Direct3D object is thus a bit like the factory design pattern. Once we have created it, we can extract new interfaces from it easily. Let's take a look at the code required to properly initialize Direct3D.

The structures we will need are as follows:

 LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device HWND hWnd;     // window handle, already initialized 

Now, let's review the calls required to actually create the Direct3D object. To do so, we need to pass the symbolic constant D3D_SDK_VERSION as a parameter, so Direct3D can make sure the application is built against the right set of header files. The code would be

 g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ); if (g_pD3D==NULL) return false; 

We now need to create the Direct3D-compatible device that will be used to generate the graphics. This is a three-step approach. First, we query the system so we can get information on the display mode, window characteristics, and so on. Second, we fill the D3DPRESENT_PARAMETERS structure with the settings we want to include in our device, and third, we create the device. The following code gets the current display mode:

 D3DDISPLAYMODE d3ddm; if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))         return false; 

Now, let's fill the presentation parameters structure, which is used to create the D3DDevice. Most parameters are unused and thus can be zeroed out. We set Windowed to TRUE, because we want to do D3D in a window, and then set the SwapEffect to discard, which is the most efficient method of presenting the back buffer to the display. We then request a back buffer format that matches the current desktop display format:

 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; 

Notice how we have requested a windowed application, which runs in the same graphics mode as the host. By doing so, Direct3D inherits the window size from the main application. Now we are ready to actually create the device with the CreateDevice call. Here we are using the default adapter (most systems only have one unless they have multiple graphics hardware cards installed) and requesting the Hardware Abstraction Layer (HAL) (which is specifying that we want the hardware device rather than a software device). Software vertex processing is specified because we know it will work on all cards. On cards that support hardware vertex processing, though, we would see a big performance gain by specifying hardware vertex processing.

 if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )    {    return false;    } 

The first parameter tells Direct3D we want to use the default graphics adapter. This is not important in most cases because we will probably only have one video card. But for systems with more than one video board, the first parameter is the ordinal number of the card: 0, 1, 2, and so on. The second parameter tells Direct3D to use the HAL. This means we will perform hardware rendering, which is the most common option. Other options are D3DDEVTYPE_REF for the reference rasterizer and D3DDEVTYPE_SW for software rendering. The third parameter is the window handle to the application. In the fourth parameter, we need to specify the kind of vertex processing will we use. We can choose between software vertex processing (as in the preceding example), hardware, or mixed. Right after the vertex processing options, we need to state which presentation parameters our device will be using. In the example, that data comes from the D3DPRESENT_PARAMETERS structure we just initialized. For completeness, here is the format of the structure:

 typedef struct _D3DPRESENT_PARAMETERS_ {     UINT                    BackBufferWidth;     UINT                    BackBufferHeight;     D3DFORMAT         BackBufferFormat;     UINT                    BackBufferCount;     D3DMULTISAMPLE_TYPE     MultiSampleType;     D3DSWAPEFFECT    SwapEffect;     HWND                    hDeviceWindow;     BOOL                     Windowed;     BOOL                     EnableAutoDepthStencil;     D3DFORMAT           AutoDepthStencilFormat;     DWORD                  Flags;     UINT                      FullScreen_RefreshRateInHz;     UINT                      FullScreen_PresentationInterval; } D3DPRESENT_PARAMETERS; 

As you can see, there's lots to explore and set, like initializing the stencil buffer or controlling the number of backbuffers. The call to CreateDevice ends the boot process and leaves us with a brand-new, ready to use Direct3D device where we can send our geometry. Here is the complete code so you can copy and paste as needed:

 LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device HWND hWnd;  // window handle, already initialized g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ); if (g_pD3D==NULL) return false; D3DDISPLAYMODE d3ddm; if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))         return false; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )    {    return false;    } 

Now, let's take care of some geometry.



Core Techniques and Algorithms in Game Programming2003
Core Techniques and Algorithms in Game Programming2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 261

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net