/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
////																					 ////
////																					 ////
////								  - HThread.cpp -   								 ////
////																					 ////
////																					 ////
////				Implémentation des fonctions SCOL de la librairie sonore			 ////
////									 Version  1.0									 ////
////																					 ////
////								  Hilaire Verschuere								 ////
////																					 ////
////																					 ////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////


#include "../Basic/ZooScene.h"


//-----------------------------------------------------------------------------
// Name: staticThreadProc()
// Thread routine
//-----------------------------------------------------------------------------
DWORD WINAPI staticThreadProc( LPVOID pThis )
{
	return ((HThread*)pThis)->ThreadProc();
}




//-----------------------------------------------------------------------------
// Name: HThread::HThread()
// Constructor
//-----------------------------------------------------------------------------
HThread::HThread()
{
	Thread			= NULL ;
	ThreadID		= 0 ;
	ThreadIsRunning = false ;
}


//-----------------------------------------------------------------------------
// Name: HThread::~HThread()
// Destructor
//-----------------------------------------------------------------------------
HThread::~HThread()
{
}



//-----------------------------------------------------------------------------
// Name: HThread::Start()
// Start the thread
//-----------------------------------------------------------------------------
HRESULT HThread::Start()
{
	if( ThreadIsRunning ) return ERRMSG("Thread is running") ;  // just to be safe. 
	
	// Start the thread.
	ThreadIsRunning = true ;
	Thread = CreateThread( NULL, 0, staticThreadProc, this, 0, &ThreadID );
	if( Thread == NULL ) return ERRMSG("Create Thread Failed") ;

	return 0 ;
}



//-----------------------------------------------------------------------------
// Name: HThread::Stop()
// Stop the thread
//-----------------------------------------------------------------------------
void HThread::Stop()
{
	while( ThreadIsRunning )
	{
		PostThreadMessage( ThreadID, WM_QUIT, 0, 0 ) ;
        WaitForSingleObject( Thread, 100 ) ;
	}
	SAFE_CLOSE_HANDLE( Thread ) ;
}

