Search code examples
c++directxdirectx-9

Can't create d3d device


I am trying to initialize directX, but for some reason it can't create d3d device. It compiles properly, but shows last messagebox ("Can't create D3D device")

While debugging:
- d3dObject 0x00000000 IDirect3D9 *
- IUnknown {...} IUnknown
__vfptr CXX0030: Error: expression cannot be evaluated

#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>

HWND hMainWindow;

IDirect3D9 *d3dObject = 0;
IDirect3DDevice9 *d3dDevice = 0;

bool InitDirectX()
{
    d3dObject = Direct3DCreate9(D3D_SDK_VERSION);
    if( !d3dObject )
    {
        MessageBox(0, "Can't create Direct3D Object", "Error", MB_ICONSTOP);
        PostQuitMessage(0);
    }

    D3DDISPLAYMODE mode;
    d3dObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);
    d3dObject->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mode.Format, mode.Format, true);
    d3dObject->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, false);

    D3DCAPS9 caps;
    d3dObject->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

    DWORD devBehaviorFlags = 0;
    if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
          devBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
          devBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    // If pure device and HW T&L supported
    if( caps.DevCaps & D3DDEVCAPS_PUREDEVICE &&
          devBehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING)
                devBehaviorFlags |= D3DCREATE_PUREDEVICE;

    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.BackBufferWidth            = 800;
    d3dpp.BackBufferHeight           = 600;
    d3dpp.BackBufferFormat           = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount            = 1;
    d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality         = 0;
    d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow              = hMainWindow;
    d3dpp.Windowed                   = true;
    d3dpp.EnableAutoDepthStencil     = true;
    d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
    d3dpp.Flags                      = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;


    if(!d3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWindow, devBehaviorFlags, &d3dpp, &d3dDevice))
    {
        ::MessageBox(0, "Can't create D3D device", "Error", MB_ICONSTOP);
        return false;
    }

    return true;
}

Solution

  • IDirect3D9::CreateDevice returns a HRESULT not a bool. The correct way to check if the device has been created is to use the SUCCEEDED or FAILED macros:

    HRESULT hr = d3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWindow, devBehaviorFlags, &d3dpp, &d3dDevice);
    if( FAILED(hr) )
    {
        // hr will be one of the D3DERR_ values
        ::MessageBox(0, "Can't create D3D device", "Error", MB_ICONSTOP);
        return false;
    }