Creating the Direct3D Object

The CreateDirect3D function creates the DirectDraw (Direct3D) driver objects and retrieves the COM interfaces for communicating with these objects. This function calls three crucial API elements: DirectDrawCreate, to create the DirectDraw object, IDirectDraw2::SetCooperativeLevel, to determine whether the application will run in full-screen or windowed mode, and IDirectDraw::QueryInterface, to retrieve a pointer to the Direct3D interface.

static HRESULT 
CreateDirect3D(HWND hwnd) 
{ 
    HRESULT hRes; 
 
    ASSERT(NULL == lpdd); 
    ASSERT(NULL == lpd3d); 
 
    // Create the DirectDraw/3D driver object and get the DirectDraw 
    // interface to that object. 
 
    hRes = DirectDrawCreate(NULL, &lpdd, NULL); 
    if (FAILED(hRes)) 
        return hRes; 
 
    // Since we are running in a window, set the cooperative level to 
    // normal. Also, to ensure that the palette is realized correctly, 
    // we need to pass the window handle of the main window. 
 
    hRes = lpdd->lpVtbl->SetCooperativeLevel(lpdd, hwnd, DDSCL_NORMAL); 
    if (FAILED(hRes)) 
        return hRes; 
 
        // Retrieve the Direct3D interface to the DirectDraw/3D driver 
        // object. 
 
    hRes = lpdd->lpVtbl->QueryInterface(lpdd, &IID_IDirect3D, &lpd3d); 
    if (FAILED(hRes)) 
        return hRes; 
 
    return DD_OK; 
}