Skip to main content
GameDev.net gamedev.net
🔒 Locked

No window created and D3DERR_DEVICELOST.

Started by ShadowPhoenix Mar 29, 2008 at 11:09 PM 4 replies 3.4k views
Original Post
ShadowPhoenix
ShadowPhoenix
For some reason this goes through fine, but I don't get to see my window, nor the DXD_Device is created, and all I get is DXD_Lost no matter how many tries I have. I can't stop having a feeling that I am making a basic mistake somewhere...

...
 dwExStyle=WS_EX_APPWINDOW;
    dwStyle=WS_POPUP|WS_VISIBLE;
    RECT		WindowRect;	
   	WindowRect.left=(long)0;
   	WindowRect.right=(long)resX;
    WindowRect.top=(long)0;	
    WindowRect.bottom=(long)resY;
    
    if(AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle)==0)
    logger << "[ERROR] Failed to adjust window. Error value:" << GetLastError() << endl;
     
    HWND hWnd;
    if (!(hWnd=CreateWindowEx(	dwExStyle,							// Extended Style For The Window
				               reinterpret_cast<LPCWSTR>(classTitle),							// Class Name
							  	L"blah",								// Window Title
								dwStyle, //|							// Defined Window Style
								//WS_CLIPSIBLINGS |					// Required Window Style
								//WS_CLIPCHILDREN,					// Required Window Style
								0, 0,								// Window Position
								WindowRect.right-WindowRect.left,	// Calculate Window Width
								WindowRect.bottom-WindowRect.top,	// Calculate Window Height
								NULL,								// No Parent Window
								NULL,								// No Menu
								GetModuleHandle(NULL),							// Instance
								(void *)this)))							// ?
	logger<< "[ERROR] Failed to create window. Error value:" << GetLastError() << endl;
	
	
   /*
		DirectX
   */

	    g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);

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

    PresentParams.Windowed = TRUE;//FALSE;
	/*PresentParams.BackBufferHeight=resY;
	PresentParams.BackBufferWidth=resX;

    PresentParams.SwapEffect = D3DSWAPEFFECT_FLIP;

	PresentParams.BackBufferFormat=D3DFMT_UNKNOWN;
	PresentParams.BackBufferCount=3; //Heh?
	PresentParams.hDeviceWindow=hWnd;
	PresentParams.EnableAutoDepthStencil=true;
*/
    int erT2=0;int tries=6;
		
	do{erT2=g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

                              D3DCREATE_HARDWARE_VERTEXPROCESSING, &PresentParams,

							  &g_pDirect3D_Device);Sleep(1000);
	if(tries--<=0)break;
	}while(erT2!=D3DERR_NOTAVAILABLE);

	if(erT2!=D3D_OK)
		{logger <<"[ERROR] D3D Creating Device error: "; //Blah
			switch(erT2)
			{
				case D3DERR_DEVICELOST :
					logger<<"DEVICE_LOST"<<endl;break;
				case D3DERR_INVALIDCALL :
					logger<<"DEVICE_LOST"<<endl;break;
				case D3DERR_NOTAVAILABLE: 
					logger<<"DEVICE_LOST"<<endl;break;
				case D3DERR_OUTOFVIDEOMEMORY:
					logger<<"DEVICE_LOST"<<endl;break;
			}
		}
	if(g_pDirect3D==NULL)
		logger << "[ERROR] Our Direct3D thing is broken"<<endl;
	if(g_pDirect3D_Device==NULL)
		logger << "[ERROR] Our DXD DEVICE is also broken. OH S***"<<endl;
	HDC hDC=GetDC(hWnd);
	
	ShowWindow(hWnd, SW_SHOWDEFAULT); //SW_SHOWNORMAL
    UpdateWindow(hWnd);
...

Khaos Dragon
Khaos Dragon
Not sure if this article would prove useful for you, but I will post a link to it just in case. Basically it shows how to get a direct3D window rendering a triangle in less than 100 lines of code without any of the messy WinAPI stuff (by ways of SDL).

The author admits its a little outdated, but it still provides enough information to just dive straight into direct3D without having to spend lots of time debugging how to create a simple window context that direct3D can render into.

http://www.gamedev.net/reference/articles/article2249.asp
ET3D
ET3D
I'd suggest that you try to debug the issues separately. Try to get a window displayed, then try to create a device on it.

You can also try to compare your code to the Direct3D tutorials. Their code isn't a great example, but it does include what you want (in fact, that's Tutorial 1).

When debugging Direct3D, you can turn on the debug runtime in the DirectX control panel (which is an application installed under the DirectX SDK entry in the start menu).
Evil Steve
Evil Steve
1. This is sounding very loud alarm bells for me:
reinterpret_cast(classTitle)
If classTitle isn't a wide character string already, then you can't cast it like that and expect it to work. If it is, then the cast shouldn't be needed. I suggest reading This.

2. You shouldn't be looping over CreateDevice like that. If it fails the first time, there's no reason it should succeed the next 5 times. All you're doing is causing a 6 second delay if there's an error, by which time an impatient user could have killed your task with ctrl+alt+del and decided the app is broken and never tried it again.

3. Never test against D3D_OK directly. Use the SUCCEEDED and FAILED macros. There's 2 billion possible return codes that mean "no error" besides D3D_OK. Admittedly, most of D3D only returns D3D_OK for success, but there are some functions that return other codes (S_FALSE is a success code for instance).

3. Use DXGetErrorString() to convert a HRESULT code (Or indeed a Windows error code from GetLastError()) to a string that you can log. That'll mean less code for you, and it also catches every possible error code.

4. You check if your D3D pointer is null after you've already tried to create the device. If your D3D pointer was null, you'd get an access violation on the CreateDevice call, and in release mode your app would just crash with a "The application has encountered a problem and needs to close" message box.


As for the "The device isn't created" problem, going by your testing for D3DERR_NOTAVAILABLE, what SDK are you using? The November 2007 (I think that was the one anyway) has a bug in the DirectX control panel, where the "Enable hardware acceleration" checkbox has an inverted meaning. Make sure this checkbox is unchecked if you're using the November 2007 SDK, or checked if you're using any other SDK.

Finally, the Debug Runtimes will give you an exact reason for any errors you have in D3D.
ShadowPhoenix
ShadowPhoenix
Quote:
Original post by Evil Steve
1. This is sounding very loud alarm bells for me:
reinterpret_cast(classTitle)
If classTitle isn't a wide character string already, then you can't cast it like that and expect it to work. If it is, then the cast shouldn't be needed. I suggest reading This.

2. You shouldn't be looping over CreateDevice like that. If it fails the first time, there's no reason it should succeed the next 5 times. All you're doing is causing a 6 second delay if there's an error, by which time an impatient user could have killed your task with ctrl+alt+del and decided the app is broken and never tried it again.

3. Never test against D3D_OK directly. Use the SUCCEEDED and FAILED macros. There's 2 billion possible return codes that mean "no error" besides D3D_OK. Admittedly, most of D3D only returns D3D_OK for success, but there are some functions that return other codes (S_FALSE is a success code for instance).

3. Use DXGetErrorString() to convert a HRESULT code (Or indeed a Windows error code from GetLastError()) to a string that you can log. That'll mean less code for you, and it also catches every possible error code.

4. You check if your D3D pointer is null after you've already tried to create the device. If your D3D pointer was null, you'd get an access violation on the CreateDevice call, and in release mode your app would just crash with a "The application has encountered a problem and needs to close" message box.


As for the "The device isn't created" problem, going by your testing for D3DERR_NOTAVAILABLE, what SDK are you using? The November 2007 (I think that was the one anyway) has a bug in the DirectX control panel, where the "Enable hardware acceleration" checkbox has an inverted meaning. Make sure this checkbox is unchecked if you're using the November 2007 SDK, or checked if you're using any other SDK.

Finally, the Debug Runtimes will give you an exact reason for any errors you have in D3D.

OMGOMGOMGOMGOMGOMGOMG I GOT IT WORKING! :D

Thank you so much Evil Steve! I gotz itz working! The problem was in my PresentParams, I was missing BackBufferFormat.

Is there a linky or a place where I can find out more about D3DPRESENT_PARAMETERS? Some of these parameters look really tasty, like the SwapEffect looks extremely inviting to change to something. For example, what is the difference between a D3DSWAPEFFECT_DISCARD, D3DSWAPEFFECT_FLIP and D3DSWAPEFFECT_COPY?

As for ASCII vs Unicode, I just defaulted back to ASCII. I do not think that I need more support than UTF8. And I fixed all the other problems.


Buckeye
Buckeye
Take a look at the help file in the SDK. It describes what each of those parameter choices does.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.