• Advertisement
Sign in to follow this  

Unable to create Direct3DDevice

This topic is 3346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to initialize my direct3DDevice through direct3D->CreateDevice() function and the last parameter (&direct3DDevice) is still NULL which in turn makes the program crash. I checked all over and I'm pretty sure my code's correct... Here's my code, first the function it's having problems at:
    HRESULT hResult;
    direct3D = Direct3DCreate9(D3D_SDK_VERSION);

    if (direct3D == NULL)
    {
        return false;
    }

    // Decide if windowed or fullscreen
    bool windowed = true;

    if (fullscreen)
    {
        windowed = false;
    }

    // Setup the present parameters
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed = windowed;    // Windowed or fullscreen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // Discards the previous frames
    d3dpp.BackBufferFormat = D3DFMT_R8G8B8;    // The display format
    d3dpp.BackBufferCount = 1;    // Number of back buffers
    d3dpp.BackBufferHeight = screenHeight;    // Height of the back buffer
    d3dpp.BackBufferWidth = screenWidth;    // Width of the back buffer
    d3dpp.hDeviceWindow = hwnd;    // Handle to our window
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    // The stencil format
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_RATE_DEFAULT;

    // Create the video device
    hResult = direct3D->CreateDevice(D3DADAPTER_DEFAULT,    // default adapter, primary display
                                     D3DDEVTYPE_HAL,    // Hardware Allocation Layer
                                     hwnd,    // Handle for the window
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING,    // Set hardware to vertex process
                                     &d3dpp,    // The present parameters
                                     &direct3DDevice);

    if (FAILED(hResult))
    {
        MessageBox(hwnd, L"Failed to create Direct3D device.", 
                   L"DirectX Error", MB_OK);
        return false;
    }

    direct3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);    // this normalizes the normal values

    return true;



Here's my constructor/destructor:
dxMgr::dxMgr(void)
{
    direct3D = NULL;    // The direct3D Object
    direct3DDevice = NULL;    // The Device Object
}

dxMgr::~dxMgr(void)
{
    if (direct3DDevice != NULL)
    {
        direct3DDevice->Release();
        direct3DDevice = NULL;
    }
    if (direct3D != NULL)
    {
        direct3D->Release();
        direct3D = NULL;
    }
}



After the CreateDevice() function, &direct3DDevice is still null, so it throws a message box saying it failed to create a device. Here's how direct3DDevice is initialized from the header:
private:
    LPDIRECT3D9 direct3D;    // This is the Direct3D object
    LPDIRECT3DDEVICE9 direct3DDevice;    // This is our device object



I'm thinking it's because in the constructor direct3DDevice is set to NULL, then never initialized, but I just don't know what to initialize it to or how... I hope this is enough for someone here to diagnose the problem. If you need a snippet from anywhere else, please let me know. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Try enabling the debug runtimes (go to the DirectX Control Panel and select "Use Debug Version of Direct3D 9"), you should get some output telling you why the call to IDirect3D0::CreateDevice is failing.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Try enabling the debug runtimes (go to the DirectX Control Panel and select "Use Debug Version of Direct3D 9"), you should get some output telling you why the call to IDirect3D0::CreateDevice is failing.


Thanks, that helped. Apparently:
d3dpp.BackBufferFormat = D3DFMT_R8G8B8;

is incorrect.

It should be:
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;


I'll have to remember the DX debugger!

Share this post


Link to post
Share on other sites
If in windowed mode then set the backbuffer format to D3DFMT_UNKNOWN and it will simply match the existing (working!) display mode format used by the desktop; it's better than guessing at a hard coded format.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rubik
...which in turn makes the program crash.
Your code should always be written in such a way that any D3D function can fail without bringing down the entire program that abruptly. CreateDevice() can fail for many reasons on many different hardware configs, you should always be able to report the error to the user and exit gracefully, rather than having your app crash.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement