XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

IDirect3DDevice9 Creation failing please help!!!!

Last post 05-02-2008 8:22 AM by Wessam Bahnassi. 2 replies.
  • 03-12-2008 5:19 PM

    IDirect3DDevice9 Creation failing please help!!!!

     

    Okay, so I have been programming in C++ and OPENGL for about 10 years but I am just learning Direct3d so I need some help.  My program is compiling and linking fine but I am getting a  runtime error. Here is the source code and the Problem description follows:

     

    #include <afxwin.h>

    #include <d3d9.h>

    #include <d3dx9.h>

    #pragma comment (lib, "d3d9.lib")

     

    IDirect3D9 * pD3D;

    IDirect3DDevice9 * pD3DDevice;

    const char g_szClassName[] = "myWindowClass";

    // Step 4: the Window Procedure

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

    {

    switch(msg)

    {

    case WM_CLOSE:

    DestroyWindow(hwnd);

    break;

    case WM_DESTROY:

    PostQuitMessage(0);

    break;

    default:

    return DefWindowProc(hwnd, msg, wParam, lParam);

    }

    return 0;

    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

    LPSTR lpCmdLine, int nCmdShow)

    {

    WNDCLASSEX wc;

    HWND hwnd;

    MSG Msg;

    D3DPRESENT_PARAMETERS pDeviceParams;

    ZeroMemory(&pDeviceParams, sizeof(D3DPRESENT_PARAMETERS));

    if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))

    {

    MessageBox(NULL, "IDirect3D9 object creation failed", "Error", MB_ICONEXCLAMATION | MB_OK);

    return E_FAIL;

    }

    //Step 1: Registering the Window Class

    wc.cbSize = sizeof(WNDCLASSEX);

    wc.style = 0;

    wc.lpfnWndProc = WndProc;

    wc.cbClsExtra = 0;

    wc.cbWndExtra = 0;

    wc.hInstance = hInstance;

    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    wc.lpszMenuName = NULL;

    wc.lpszClassName = g_szClassName;

    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))

    {

    MessageBox(NULL, "Window Registration Failed!", "Error!",

    MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    // Step 2: Creating the Window

    hwnd = CreateWindowEx(

    WS_EX_CLIENTEDGE,

    g_szClassName,

    "The title of my window",

    WS_OVERLAPPEDWINDOW,

    CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,

    NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)

    {

    MessageBox(NULL, "Window Creation Failed!", "Error!",

    MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    ShowWindow(hwnd, nCmdShow);

    UpdateWindow(hwnd);

    pDeviceParams.Windowed = TRUE;

    pDeviceParams.hDeviceWindow = hwnd;

    pDeviceParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

    pDeviceParams.BackBufferFormat = D3DFMT_UNKNOWN;

    pDeviceParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;

    pDeviceParams.EnableAutoDepthStencil = TRUE;

    pDeviceParams.AutoDepthStencilFormat = D3DFMT_D16;

    if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pDeviceParams, &pD3DDevice)));

    {

    MessageBox(NULL, "Device Creation Failed\n", "Error", MB_ICONEXCLAMATION | MB_OK);

    return E_FAIL;

    }

    // Step 3: The Message Loop

    while(GetMessage(&Msg, NULL, 0, 0) > 0)

    {

    TranslateMessage(&Msg);

    DispatchMessage(&Msg);

    }

    return Msg.wParam;

    }

     

     

    In the above source code you should see an if statement that is larger than the rest, that is where the error is occuring.  For some reason it always fails when I try to create the IDirect3DDevice9 object.  I have tried switching from software, hardware, and mixed vertex processing. None of that works, and every tidbit of code for this that I could find on the internet seems to do it the same way.  Can someone please tell me how to fix this, as I said I am new to Direct3d. Thanks You.

     

  • 05-02-2008 5:55 AM In reply to

    Re: IDirect3DDevice9 Creation failing please help!!!!

    You can show the error using the HRESULT code returned by CreateDevice. There's an auxiliar functions (DXGetErrorString and DxGetErrorDescription) to return the error cause.

    HRESULT hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pDeviceParams, &pD3DDevice)

    if (FAILED(hr));

    {

    TCHAR szError[4096];

    swprintf_s(szError, "Device Creation Failed. %s - %s",  DXGetErrorString(hr), DXGetErrorDescription(hr))

    MessageBox(NULL, szError, "Error", MB_ICONEXCLAMATION | MB_OK);

    return E_FAIL;

    }


    Don't forget to include dxerr.h in the source code and dxerr.lib in the linker before to use DXGetErrorString and DXGetErrorDescription.
  • 05-02-2008 8:22 AM In reply to

    Re: IDirect3DDevice9 Creation failing please help!!!!

    Such errors are always accompanied by useful description messages in the debug output. I suggest you run your app with D3D debug info. Follow the steps here to get it up and running:

    http://www.inframez.com/papers/d3dforums.htm

    Then you will know exactly why your device creation is failing.

    Wessam Bahnassi
    Microsoft DirectX MVP
Page 1 of 1 (3 items) Previous Next