Initalizing Direct3D

I have a question about the code I found from Windows tutorial about initalizing
DirectX. How to put the following code into a procedure, included by a header file
I managed to build the program only only, if I had it in my main source file.


if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )



I tried something like that, but it didn't work:

void InitD3D(LPDIRECT3D9 *g_pD3D, HWND hWnd, LPDIRECT3DDEVICE9 g_pd3dDevice)
{
if( NULL == ( (*g_pD3D) = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;


if( FAILED( (*g_pD3D)->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
}

I put the line in my main program:
InitD3D(&g_pD3D, hWnd, g_pd3dDevice);


What did I do wrong





Answer this question

Initalizing Direct3D

  • geliser131

    One thing that does not look right is the g_pd3dDevice parameter. It's a pointer but it should be a pointer to pointer much like g_pD3D for InitD3D to be able to return the pointer to the created device.

    So the function should look like this:

    void InitD3D(LPDIRECT3D9 *g_pD3D, HWND hWnd, LPDIRECT3DDEVICE9 *g_pd3dDevice)
    {
    ...

    if( FAILED( (*g_pD3D)->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    &d3dpp, g_pd3dDevice ) ) ) // no & needed for g_pd3dDevice

    }

    And the call to InitD3D shoud look like this:

    InitD3D(&g_pD3D, hWnd, &g_pd3dDevice); // here you need & for g_pd3dDevice

    If you have other problems please be more explicit about what does not work.


  • FedorSteeman

    Thanks, It works now!

  • Initalizing Direct3D