Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualcozzie

Posted 25 December 2012 - 11:02 AM

Hi,

I'm trying to make sure I'm calling create, testcooperativelevel, reset and release on my d3d device, from the main thread (same one as the window messages are handled). I'm not sure I understand the definition of a thread, I believe I only have one thread. Here's a bit of my code to give you a better view.

I'm doing the following calls to create, reset, release and testcooperative level:

Create => part of private function in d3d class, class includes mHwnd and d3ddev vars
Reset => part of private function 'ResetDevice' in d3d class, triggered from winproc of d3d window/hwnd
TestCooperativelevel => both from the winproc handler of d3d window/hwnd as within several member functions of d3d class
(also from main program loop, through 'CheckDevice' function, member of d3d class)
Release => in the destructor of my d3d class, in which d3ddev is a public member

CD3d		_d3d; // object of own d3d wrapper/class including mHwnd and md3ddev membersint WINAPI WinMain(HINSTANCE hInstance,				   HINSTANCE hPrevInstance,				   LPSTR lpCmdLine,				   int nCmdShow){        _d3d.CreateWindow();        _d3d.InitD3ddevice();        // load scene etc.	PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);	while(msg.message != WM_QUIT)	{		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))			{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		else		{			if(active)			{				if(_d3d.CheckDevice())				{					_timer.CalcFps();					SetWindowTextA(_d3d.mHwnd, _timer.mStrFramerate);										// render 3d scene					if(!_d3d.RenderFrame(&_d3dscene, &_d3dcam)) 					{						MessageBox(_d3d.mHwnd, err_render.c_str(), err_windowtext.c_str(), MB_ICONERROR);							return (INT)msg.wParam;					}				}			}		}	}	return (INT)msg.wParam;}// windowproc, which is linked to the mHwnd in the _d3d objectLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	int sleeptime = 20;	switch(message)		{		case WM_ACTIVATE:			if(LOWORD(wParam) == WA_ACTIVE || WA_CLICKACTIVE)			{				active = true;				Sleep(sleeptime);				break;			}			else				active = false;				Sleep(sleeptime);				break;			break; 		case WM_ACTIVATEAPP:			if(!wParam)			{				active = false;				Sleep(sleeptime);			}			if(wParam)			{				if(_d3d.mD3ddev)				{					if(!_d3d.CheckDevice())					{						{							while(1)							{								Sleep(sleeptime);								switch (_d3d.mD3ddev->TestCooperativeLevel())								{								case D3D_OK:									return 0;								case D3DERR_DEVICELOST:									break;								case D3DERR_DEVICENOTRESET:									{										_d3d.OnLostDevice();										_d3dscene.OnLostDevice();										if(!_d3d.ResetDevice()) break;										else										{											_d3d.OnResetDevice(&_d3dscene, &_d3dcam);											_d3dscene.OnResetDevice(&_scene, &_d3d.mD3ddev);										}										active = true;										break;									}								case D3DERR_DRIVERINTERNALERROR:									PostQuitMessage(WM_QUIT);								}							}						}					}				}				else active = true;				Sleep(sleeptime);			}			break; 		case WM_DESTROY:				PostQuitMessage(WM_QUIT);			break;		default:			return DefWindowProc(hWnd, message, wParam, lParam);	}	return 0;}

Does anyone know if I'm using just one thread and if I follow the MSDN statement that the d3d device calls are executed from the main thread?
(or can I only call Create, Reset, Testcooperativelevel, Release from the Wndproc function of the d3d window?)


#1cozzie

Posted 23 December 2012 - 11:32 AM

Hi,

I'm trying to make sure I'm calling create, testcooperativelevel, reset and release on my d3d device, from the main thread (same one as the window messages are handled). I'm not sure I understand the definition of a thread, I believe I only have one thread. Here's a bit of my code to give you a better view.

I'm doing the following calls to create, reset, release and testcooperative level:

Create => part of private function in d3d class, class includes mHwnd and d3ddev vars
Reset => part of private function 'ResetDevice' in d3d class, triggered from winproc of d3d window/hwnd
TestCooperativelevel => both from the winproc handler of d3d window/hwnd as within several member functions of d3d class
(also from main program loop, through 'CheckDevice' function, member of d3d class)
Release => in the destructor of my d3d class, in which d3ddev is a public member

 

CD3d		_d3d; // object of own d3d wrapper/class including mHwnd and md3ddev members

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
        _d3d.CreateWindow();
        _d3d.InitD3ddevice();
        // load scene etc.
	PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))	
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			if(active)
			{
				if(_d3d.CheckDevice())
				{
					_timer.CalcFps();
					SetWindowTextA(_d3d.mHwnd, _timer.mStrFramerate);
					
					// render 3d scene
					if(!_d3d.RenderFrame(&_d3dscene, &_d3dcam)) 
					{
						MessageBox(_d3d.mHwnd, err_render.c_str(), err_windowtext.c_str(), MB_ICONERROR);	
						return (INT)msg.wParam;
					}
				}
			}
		}
	}
	return (INT)msg.wParam;
}

// windowproc, which is linked to the mHwnd in the _d3d object

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int sleeptime = 20;
	switch(message)	
	{
		case WM_ACTIVATE:
			if(LOWORD(wParam) == WA_ACTIVE || WA_CLICKACTIVE)
			{
				active = true;
				Sleep(sleeptime);
				break;
			}
			else
				active = false;
				Sleep(sleeptime);
				break;

			break; 

		case WM_ACTIVATEAPP:
			if(!wParam)
			{
				active = false;
				Sleep(sleeptime);
			}
			if(wParam)
			{
				if(_d3d.mD3ddev)
				{
					if(!_d3d.CheckDevice())
					{
						{
							while(1)
							{
								Sleep(sleeptime);
								switch (_d3d.mD3ddev->TestCooperativeLevel())
								{
								case D3D_OK:
									return 0;

								case D3DERR_DEVICELOST:
									break;

								case D3DERR_DEVICENOTRESET:
									{
										_d3d.OnLostDevice();
										_d3dscene.OnLostDevice();

										if(!_d3d.ResetDevice()) break;
										else
										{
											_d3d.OnResetDevice(&_d3dscene, &_d3dcam);
											_d3dscene.OnResetDevice(&_scene, &_d3d.mD3ddev);
										}
										active = true;
										break;
									}
								case D3DERR_DRIVERINTERNALERROR:
									PostQuitMessage(WM_QUIT);
								}
							}
						}
					}
				}
				else active = true;
				Sleep(sleeptime);
			}
			break; 

		case WM_DESTROY:	
			PostQuitMessage(WM_QUIT);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}



 

Does anyone know if I'm using just one thread and if I follow the MSDN statement that the d3d device calls are executed from the main thread?
(or can I only call Create, Reset, Testcooperativelevel, Release from the Wndproc function of the d3d window?)

 


PARTNERS