Trouble creating a Direct3D Device

Hello, I am writing a simple Direct3D program. Using message boxes I have isolated the point where my code fails. The compiler doesn't recognize any errors. My program crashes when it reaches the code to create a Direct3D Device, and I don't know why. Please take a look at it and let me know if you have any suggestions on how to fix it. I am using VS 2010 and DirectX June 2010.
1
2
3
4
5
6
7
	d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		window,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);
You are using DirextX 9 in your program if I'm not mistaken. What is the return value of CreateDevice() for your program tyler22? Possible values are D3D_OK (which is unlikely if this is the function causing the problem), D3DERR_DEVICELOST, D3DERR_INVALIDCALL, D3DERR_NOTAVAILABLE and D3DERR_OUTOFVIDEOMEMORY.

Also, since you don't get any compile errors, it is not unlikely you are supplying an improper value for one of your function parameters. You may want to double-check that all the parameters you give to CreateDevice() are correctly initialized and store the data they're supposed to store at that point in the program. You can use the debugger for keeping track of these and make sure it all runs smoothly up to the function call, no need to implement additional message boxes.

Also, CreateDevice() shouldn't be run during the handling of a WM_CREATE message.
Sorry, I am new to DirectX programming and Windows programming in general. How do I tell the return value of CreateDevice()? I am also fairly sure that my parameters are correct, yet I will post them anyhow. I am not using a WM_CREATE message in this program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (d3d = NULL)
	{
		MessageBox(window,"Error initializing Direct3D","Error",MB_OK);
		return 0;
	}

MessageBox(window,"Direct3D is initialized","Good",MB_OK);	

	//Direct3D Presentation Parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREENW;
	d3dpp.BackBufferHeight = SCREENH;
	d3dpp.hDeviceWindow = window;
In line 2 you write if (d3d = NULL) which (as I'm sure you know) effectively assigns NULL to d3d rather than testing the equality (you need if (d3d==NULL) ). Is that a typo while posting your code?

You could try something like this for checking the return value of CreateDevice():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
HRESULT CreateDeviceReturnValue;

CreateDeviceReturnValue = d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		window,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);
		
if(CreateDeviceReturnValue == D3DERR_INVALIDCALL)
{
	MessageBox(window,"Device not created, CreateDevice returned D3DERR_INVALIDCALL. ","Error",MB_OK);
}

else if(CreateDeviceReturnValue == D3DERR_NOTAVAILABLE)
{
 //etc.
}

//etc 
Last edited on
Topic archived. No new replies allowed.