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.

132 lines
4.1 KiB

  1. //========= Copyright � 1996-2013, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Provide custom material swapping for weapons (when switch from world to view or vice versa)
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "cs_custom_material_swap.h"
  9. #include "materialsystem/icustommaterial.h"
  10. CCSCustomMaterialSwapManager g_CSCustomMaterialSwapManager;
  11. //
  12. // global custom material swap manager
  13. // the game uses this to swap custom materials (after the new one is done)
  14. //
  15. CCSCustomMaterialSwapManager::CCSCustomMaterialSwapManager()
  16. {
  17. m_pPendingSwaps.EnsureCapacity( 4 );
  18. }
  19. CCSCustomMaterialSwapManager::~CCSCustomMaterialSwapManager()
  20. {
  21. ClearAllPendingSwaps();
  22. }
  23. // this is called at the end of each frame
  24. bool ProcessCustomMaterialSwapManager()
  25. {
  26. return g_CSCustomMaterialSwapManager.Process();
  27. }
  28. bool CCSCustomMaterialSwapManager::Init()
  29. {
  30. g_pMaterialSystem->AddEndFramePriorToNextContextFunc( ::ProcessCustomMaterialSwapManager );
  31. return true;
  32. }
  33. void CCSCustomMaterialSwapManager::Shutdown()
  34. {
  35. g_pMaterialSystem->RemoveEndFramePriorToNextContextFunc( ::ProcessCustomMaterialSwapManager );
  36. ClearAllPendingSwaps();
  37. }
  38. // handles swapping materials that are pending swap and ready
  39. bool CCSCustomMaterialSwapManager::Process()
  40. {
  41. for ( int i = m_pPendingSwaps.Count() - 1; i >= 0 ; i-- )
  42. {
  43. if ( m_pPendingSwaps[ i ].m_pNewCustomMaterial->IsValid() )
  44. {
  45. C_BaseAnimating* pOwner = ( m_pPendingSwaps[ i ].m_hOwner ) ? m_pPendingSwaps[ i ].m_hOwner->GetBaseAnimating() : NULL;
  46. if ( pOwner )
  47. {
  48. pOwner->SetCustomMaterial( m_pPendingSwaps[ i ].m_pNewCustomMaterial, m_pPendingSwaps[ i ].m_nCustomMaterialIndex );
  49. }
  50. m_pPendingSwaps[ i ].m_hOwner = NULL;
  51. m_pPendingSwaps[ i ].m_nCustomMaterialIndex = -1;
  52. m_pPendingSwaps[ i ].m_pNewCustomMaterial->Release();
  53. m_pPendingSwaps[ i ].m_pNewCustomMaterial = NULL;
  54. if ( m_pPendingSwaps[ i ].m_pOldCustomMaterial )
  55. {
  56. m_pPendingSwaps[ i ].m_pOldCustomMaterial->Release();
  57. m_pPendingSwaps[ i ].m_pOldCustomMaterial = NULL;
  58. }
  59. m_pPendingSwaps.Remove( i );
  60. }
  61. }
  62. return false;
  63. }
  64. void CCSCustomMaterialSwapManager::RequestMaterialSwap( EHANDLE hOwner, int nCustomMaterialIndex, ICustomMaterial *pNewCustomMaterialInterface )
  65. {
  66. int nSwapIndex = m_pPendingSwaps.AddToTail();
  67. CCSPendingCustomMaterialSwap_t &materialSwap = m_pPendingSwaps[ nSwapIndex ];
  68. materialSwap.m_hOwner = hOwner;
  69. materialSwap.m_nCustomMaterialIndex = nCustomMaterialIndex;
  70. materialSwap.m_pNewCustomMaterial = pNewCustomMaterialInterface;
  71. materialSwap.m_pNewCustomMaterial->AddRef();
  72. ICustomMaterial *pOldCustomMaterialInterface = hOwner->GetBaseAnimating()->GetCustomMaterial( nCustomMaterialIndex );
  73. materialSwap.m_pOldCustomMaterial = pOldCustomMaterialInterface;
  74. if ( materialSwap.m_pOldCustomMaterial )
  75. {
  76. materialSwap.m_pOldCustomMaterial->AddRef();
  77. }
  78. }
  79. void CCSCustomMaterialSwapManager::ClearPendingSwaps( EHANDLE hOwner, int nCustomMaterialIndex )
  80. {
  81. for ( int i = m_pPendingSwaps.Count() - 1; i >= 0 ; i-- )
  82. {
  83. if ( m_pPendingSwaps[ i ].m_hOwner == hOwner && m_pPendingSwaps[ i ].m_nCustomMaterialIndex == nCustomMaterialIndex )
  84. {
  85. m_pPendingSwaps[ i ].m_hOwner = NULL;
  86. m_pPendingSwaps[ i ].m_nCustomMaterialIndex = -1;
  87. m_pPendingSwaps[ i ].m_pNewCustomMaterial->Release();
  88. m_pPendingSwaps[ i ].m_pNewCustomMaterial = NULL;
  89. if ( m_pPendingSwaps[ i ].m_pOldCustomMaterial )
  90. {
  91. m_pPendingSwaps[ i ].m_pOldCustomMaterial->Release();
  92. m_pPendingSwaps[ i ].m_pOldCustomMaterial = NULL;
  93. }
  94. m_pPendingSwaps.Remove( i );
  95. }
  96. }
  97. }
  98. void CCSCustomMaterialSwapManager::ClearAllPendingSwaps( void )
  99. {
  100. for ( int i = 0; i < m_pPendingSwaps.Count(); ++i )
  101. {
  102. CCSPendingCustomMaterialSwap_t &materialSwap = m_pPendingSwaps[ i ];
  103. materialSwap.m_hOwner = NULL;
  104. materialSwap.m_nCustomMaterialIndex = -1;
  105. materialSwap.m_pNewCustomMaterial->Release();
  106. materialSwap.m_pNewCustomMaterial = NULL;
  107. if ( m_pPendingSwaps[ i ].m_pOldCustomMaterial )
  108. {
  109. materialSwap.m_pOldCustomMaterial->Release();
  110. materialSwap.m_pOldCustomMaterial = NULL;
  111. }
  112. }
  113. m_pPendingSwaps.RemoveAll();
  114. }