Skip to main content
gamedev.net
🔒 Locked

Initialization error

Started by robleetin Feb 14, 2007 at 10:19 AM 4 replies 1.6k views
Original Post
robleetin
robleetin
Hello ! I've a problem with this code : #include #define SAFE_RELEASE(p) if (p != NULL) { p->Release(); } else return E_FAIL; class DIRECTXMANAGEMENT { private: LPDIRECT3D9 m_D3DObject; LPDIRECT3DDEVICE9 m_D3DDevice; HWND m_MainWindowHandle; public: DIRECTXMANAGEMENT(); ~DIRECTXMANAGEMENT(); void InitWindow(); void GameLoop(); HRESULT InitD3D(); HRESULT RenderScene(); HRESULT DestroyD3DObjects(); }; LRESULT WINAPI MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wParam, lParam); } DIRECTXMANAGEMENT::DIRECTXMANAGEMENT() { InitWindow(); InitD3D(); GameLoop(); } void DIRECTXMANAGEMENT::GameLoop() { MSG msg; ZeroMemory(&msg, sizeof(msg)); PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE); while (msg.message != WM_QUIT) { ShowWindow(m_MainWindowHandle, SW_SHOWDEFAULT); UpdateWindow(m_MainWindowHandle); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else RenderScene(); } } DIRECTXMANAGEMENT::~DIRECTXMANAGEMENT() { DestroyD3DObjects(); } void DIRECTXMANAGEMENT::InitWindow() { WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "Direct3DA", NULL}; RegisterClassEx(&wc); m_MainWindowHandle = CreateWindow("Direct3DA", "D3DApp", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL); } HRESULT DIRECTXMANAGEMENT::InitD3D() { if ((m_D3DObject = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if (FAILED(m_D3DObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_MainWindowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_D3DDevice))) return E_FAIL; return S_OK; } HRESULT DIRECTXMANAGEMENT::RenderScene() { m_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); if (SUCCEEDED(m_D3DDevice->BeginScene())) { // Geometric objects here m_D3DDevice->EndScene(); } m_D3DDevice->Present(NULL, NULL, NULL, NULL); return S_OK; } HRESULT DIRECTXMANAGEMENT::DestroyD3DObjects() { SAFE_RELEASE(m_D3DObject); SAFE_RELEASE(m_D3DDevice); return S_OK; } int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int) { DIRECTXMANAGEMENT dxm; ZeroMemory(&dxm, sizeof(dxm)); dxm.GameLoop(); dxm.DestroyD3DObjects(); return 0; } At the execution, the program dumps and the following window displayed : http://img57.imageshack.us/img57/8080/snap1qj2.png Thank for help
Programmer101
Programmer101
I'm not completely sure about exactly why it is saying that. That error usually means that a com has not been correctly initialized and I noticed that you were missing quite a few present parameters such as the backbuffer size. This is what I use to initialize:

       //Initialize DX	if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)		return FALSE;		D3DDISPLAYMODE d3ddm;	//For Windowed	if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))		return FALSE;	D3DPRESENT_PARAMETERS d3dpp; 	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));	//For Windowed	d3dpp.Windowed = FALSE;	d3dpp.Windowed = TRUE;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	d3dpp.BackBufferFormat = d3ddm.Format;	d3dpp.BackBufferWidth = 1024;	d3dpp.BackBufferHeight = 768;	d3dpp.hDeviceWindow = hWnd;	d3dpp.BackBufferCount = 1;	d3dpp.MultiSampleQuality = 0;	d3dpp.EnableAutoDepthStencil = true;	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;	d3dpp.Flags = 0;	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;        if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_MIXED_VERTEXPROCESSING,&d3dpp,&g_pD3DDevice)))			return FALSE;


hope this helps!
robleetin
robleetin
Quote:
Original post by Programmer101
I'm not completely sure about exactly why it is saying that. That error usually means that a com has not been correctly initialized and I noticed that you were missing quite a few present parameters such as the backbuffer size. This is what I use to initialize:

*** Source Snippet Removed ***

hope this helps!

Thank you for help but the error is staying !

She is situated at the line 119 :
m_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255),
1.0f, 0);

I don't understand. :(
Evil Steve
Evil Steve
You check for errors in your InitD3D function, but never do anything about them. If something goes wrong and you can't create the device, your app just carries on anyway. This is why I never put initialisation in the constructor. Either throw an exception if InitD3D fails, or have some mechanism for checking if it failed.

As it is, CreateDevice() fails, so you end up with a NULL device pointer, which you then try to use in the Clear() call.
robleetin
robleetin
Quote:
Original post by Evil Steve
You check for errors in your InitD3D function, but never do anything about them. If something goes wrong and you can't create the device, your app just carries on anyway. This is why I never put initialisation in the constructor. Either throw an exception if InitD3D fails, or have some mechanism for checking if it failed.

As it is, CreateDevice() fails, so you end up with a NULL device pointer, which you then try to use in the Clear() call.
Thank you very much !

I added the following lines before the Clear() method :
if (m_D3DDevice == NULL)	return E_FAIL;



Now, there's no error but if the pointer m_D3DDevice is NULL, Clear() isn't called no ? So, the app's background is blue. Where's error ?

Evil Steve
Evil Steve
Quote:
Original post by robleetin
Now, there's no error but if the pointer m_D3DDevice is NULL, Clear() isn't called no ? So, the app's background is blue. Where's error ?
If the background is blue, then the device is not null, and everything is working correctly. You should be able to put some breakpoints in and see that.
If the background isn't blue, then Clear() isn't being called, which means your device is null.

You should install the debug runtimes, which will tell you if anything goes wrong, as well as why. I belive there's a bit in the forum FAQ about how to use the debug runtimes.

Topic Locked

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

Sign in to reply to this topic.