Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
3.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "bsplightingthread.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. // --------------------------------------------------------------------------- //
  12. // Global functions.
  13. // --------------------------------------------------------------------------- //
  14. IBSPLightingThread* CreateBSPLightingThread( IVRadDLL *pDLL )
  15. {
  16. CBSPLightingThread *pRet = new CBSPLightingThread;
  17. if( pRet->Init( pDLL ) )
  18. {
  19. return pRet;
  20. }
  21. else
  22. {
  23. delete pRet;
  24. return 0;
  25. }
  26. }
  27. DWORD WINAPI ThreadMainLoop_Static( LPVOID lpParameter )
  28. {
  29. return ((CBSPLightingThread*)lpParameter)->ThreadMainLoop();
  30. }
  31. // --------------------------------------------------------------------------- //
  32. // Static helpers.
  33. // --------------------------------------------------------------------------- //
  34. class CCSLock
  35. {
  36. public:
  37. CCSLock( CRITICAL_SECTION *pCS )
  38. {
  39. EnterCriticalSection( pCS );
  40. m_pCS = pCS;
  41. }
  42. ~CCSLock()
  43. {
  44. LeaveCriticalSection( m_pCS );
  45. }
  46. CRITICAL_SECTION *m_pCS;
  47. };
  48. // --------------------------------------------------------------------------- //
  49. //
  50. // --------------------------------------------------------------------------- //
  51. CBSPLightingThread::CBSPLightingThread()
  52. {
  53. InitializeCriticalSection( &m_CS );
  54. m_hThread = 0;
  55. m_ThreadID = 0;
  56. m_ThreadCmd = THREADCMD_NONE;
  57. m_ThreadState = STATE_IDLE;
  58. }
  59. CBSPLightingThread::~CBSPLightingThread()
  60. {
  61. if( m_hThread )
  62. {
  63. // Stop the current lighting process if one is going on.
  64. Interrupt();
  65. // Tell the thread to exit.
  66. SetThreadCmd( THREADCMD_EXIT );
  67. DWORD dwCode;
  68. while( 1 )
  69. {
  70. if( GetExitCodeThread( m_hThread, &dwCode ) && dwCode == 0 )
  71. break;
  72. Sleep( 10 );
  73. }
  74. CloseHandle( m_hThread );
  75. }
  76. DeleteCriticalSection( &m_CS );
  77. }
  78. void CBSPLightingThread::Release()
  79. {
  80. delete this;
  81. }
  82. void CBSPLightingThread::StartLighting( char const *pVMFFileWithEntities )
  83. {
  84. // First, kill any lighting going on.
  85. Interrupt();
  86. // Store the VMF file data for the thread.
  87. int len = strlen( pVMFFileWithEntities ) + 1;
  88. m_VMFFileWithEntities.CopyArray( pVMFFileWithEntities, len );
  89. // Tell the thread to start lighting.
  90. SetThreadState( STATE_LIGHTING );
  91. SetThreadCmd( THREADCMD_LIGHT );
  92. }
  93. int CBSPLightingThread::GetCurrentState()
  94. {
  95. return GetThreadState();
  96. }
  97. void CBSPLightingThread::Interrupt()
  98. {
  99. if( GetThreadState() == STATE_LIGHTING )
  100. {
  101. m_pVRadDLL->Interrupt();
  102. while( GetThreadState() == STATE_LIGHTING )
  103. Sleep( 10 );
  104. }
  105. }
  106. float CBSPLightingThread::GetPercentComplete()
  107. {
  108. return m_pVRadDLL->GetPercentComplete();
  109. }
  110. bool CBSPLightingThread::Init( IVRadDLL *pDLL )
  111. {
  112. m_pVRadDLL = pDLL;
  113. m_hThread = CreateThread(
  114. NULL,
  115. 0,
  116. ThreadMainLoop_Static,
  117. this,
  118. 0,
  119. &m_ThreadID );
  120. if( !m_hThread )
  121. return false;
  122. SetThreadPriority( m_hThread, THREAD_PRIORITY_LOWEST );
  123. return true;
  124. }
  125. DWORD CBSPLightingThread::ThreadMainLoop()
  126. {
  127. while( 1 )
  128. {
  129. int cmd = GetThreadCmd();
  130. if( cmd == THREADCMD_NONE )
  131. {
  132. // Keep waiting for a new command.
  133. Sleep( 10 );
  134. }
  135. else if( cmd == THREADCMD_LIGHT )
  136. {
  137. if( m_pVRadDLL->DoIncrementalLight( m_VMFFileWithEntities.Base() ) )
  138. SetThreadState( STATE_FINISHED );
  139. else
  140. SetThreadState( STATE_IDLE );
  141. }
  142. else if( cmd == THREADCMD_EXIT )
  143. {
  144. return 0;
  145. }
  146. }
  147. }
  148. int CBSPLightingThread::GetThreadCmd()
  149. {
  150. CCSLock lock( &m_CS );
  151. int ret = m_ThreadCmd;
  152. m_ThreadCmd = THREADCMD_NONE;
  153. return ret;
  154. }
  155. void CBSPLightingThread::SetThreadCmd( int cmd )
  156. {
  157. CCSLock lock( &m_CS );
  158. m_ThreadCmd = cmd;
  159. }
  160. int CBSPLightingThread::GetThreadState()
  161. {
  162. CCSLock lock( &m_CS );
  163. return m_ThreadState;
  164. }
  165. void CBSPLightingThread::SetThreadState( int state )
  166. {
  167. CCSLock lock( &m_CS );
  168. m_ThreadState = state;
  169. }