• Advertisement
Sign in to follow this  

Direct3dDevice Cannot Creating!!!

This topic is 4406 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

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


LPDIRECT3D9			d3d		=	NULL;
LPDIRECT3DDEVICE9	d3dd	=	NULL;
LPDIRECT3DVERTEXBUFFER9 d3d_vb = NULL;



struct CUSTOMVERTEX
{
	float x,y,z,rhw;
	DWORD color;


};




HRESULT Init3D(HWND hWnd)
{
if(FAILED(	d3d = Direct3DCreate9(  D3D_SDK_VERSION )))
{
	MessageBox(NULL, "Direct3dCreate9 HATA", "M", MB_OK);
}



	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;



if(FAILED(d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3dd)))
{
	MessageBox(NULL, "Direct3dDevice Cannot Creating!!!", "M", MB_OK);
	return E_FAIL;
}


return S_OK;
}


HRESULT InitGEO()
{
	CUSTOMVERTEX vertices[]=
	{
		{10.0f, 10.0f, 1.0f, 10.0f, D3DCOLOR_XRGB(255,0,0),},
		{250.0f, 400.0f, 1.0f, 10.0f, D3DCOLOR_XRGB(0,0,255),},
		{390.0f, 10.0f, 1.0f, 10.0f, D3DCOLOR_XRGB(0,255,0),},
	};





if(FAILED(d3dd->CreateVertexBuffer(3 * sizeof(vertices), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &d3d_vb, NULL)))

{
	MessageBox(NULL, "Direct3dVertexBuffer Cannot Creating!!! ERROR!", "M", MB_OK);
}
	
	
	
	

	
VOID* pVertices;



	d3d_vb->Lock(0, sizeof(vertices), &pVertices, 0);
	memcpy(pVertices, vertices, sizeof(vertices));
	d3d_vb->Unlock();



return S_OK;

}


VOID Clean()
{
	if(d3d != NULL) d3d->Release();
	if(d3dd != NULL) d3dd->Release();
	if(d3d_vb != NULL) d3d_vb->Release();
}
void Render()
{
	d3dd->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

	InitGEO();
	d3dd->BeginScene();

	d3dd->SetStreamSource(0, d3d_vb, sizeof(CUSTOMVERTEX), 0);
	d3dd->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
	d3dd->DrawPrimitive(D3DPT_TRIANGLEFAN, 0,0);

	d3dd->EndScene();

	d3dd->Present(NULL,NULL,NULL,NULL);


}


LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) 

{ 

    switch( msg ) 

    { 

        case WM_DESTROY: 

            Clean(); 

            PostQuitMessage( 0 ); 

           return 0;  

        case WM_PAINT: 

            Render(); 

            

            return 0; 

    } 


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

}
 
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )

{

    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Application", NULL }; 

    RegisterClassEx( &wc ); 

    HWND hWnd = CreateWindow( "D3D Application", "D3D Application 1", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL ); 

    if( SUCCEEDED( Init3D( hWnd ) ) ) 

    { 

        ShowWindow( hWnd, SW_SHOWDEFAULT ); 

        UpdateWindow( hWnd ); 

        MSG msg; 

        while( GetMessage( &msg, NULL, 0, 0 ) ) 

        { 

            TranslateMessage( &msg ); 

            DispatchMessage( &msg ); 

        } 

    } 

    UnregisterClass( "D3D Application", wc.hInstance ); 

    return 0; 

}

When i run this program i get an error: "Direct3dDevice Cannot Creating!!!" (I created an Error handling :D) What is the problem??? EDIT: 'source' tags should be used for lengthy fragments of code. [Edited by - jollyjeffers on January 27, 2006 5:30:21 AM]

Share this post


Link to post
Share on other sites
Advertisement

D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));

d3dpp.Windowed = TRUE;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferWidth = 300;
d3dpp.BackBufferHeight = 300;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;


Share this post


Link to post
Share on other sites
For the OP - you should run your application with the debug runtimes to get a better idea of why the call is failing. If you're not familiar with the debug runtimes, see the "DirectX Debugging" link in my signiture.

As a general note - CreateDevice() will typically fail if the hardware/driver cannot match the features that you have requested. You could try putting this line in after the declaration but before you fill it in:

ZeroMemory( &d3dpp, sizeof(d3dpp) );

Most samples call that to make sure there aren't any "garbage" values in the structure that trip up the CreateDevice() call.

Quote:
Original post by Flimflam
You can't specify an unknown backbuffer format.

Not so for windowed mode [smile]
Quote:
In fact, D3DFMT_UNKNOWN can be specified for the BackBufferFormat while in windowed mode. This tells the runtime to use the current display-mode format and eliminates the need to call GetDisplayMode.


hth
Jack

Share this post


Link to post
Share on other sites
yeah i forget it :D


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

Share this post


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

  • Advertisement