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;
}
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;
}
The best way to debug D3D9 is by enabling the debug DLLs as mentioned in a previous answer. Unfortunately D3D9 debug DLLs are supported on Windows 7 and before. If you're on Windows 8 or later then your best bet is to move to D3D11 (you might want to do that anyways). From your description, you say the program shows the second MessageBox, meaning the call to CreateDevice has failed. Yet you mention that the debugger says that d3dObject is null. I'm going to assume that the location at which you got this reading was prior to calling Direct3DCreate9, otherwise your program would have crashed before the second message box appears.
In your particular case, CreateDevice is actually succeeding because HRESULT of 0 means success (S_OK), so as the previous answer said, use the SUCCEEDED or FAILED macros to check for HRESULT status instead of interpreting thrm as bools.
User contributions licensed under CC BY-SA 3.0