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.

159 lines
4.8 KiB

  1. //========== Copyright � 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "pch_materialsystem.h"
  7. #ifndef _PS3
  8. #define MATSYS_INTERNAL
  9. #endif
  10. #include "cmaterialdict.h"
  11. #include "materialsystem_global.h"
  12. #include "filesystem.h"
  13. #include "imaterialinternal.h"
  14. // NOTE: This must be the last file included!!!
  15. #include "tier0/memdbgon.h"
  16. //-----------------------------------------------------------------------------
  17. // sort function
  18. //-----------------------------------------------------------------------------
  19. bool CMaterialDict::MaterialLessFunc( const MaterialLookup_t& src1,
  20. const MaterialLookup_t& src2 )
  21. {
  22. // Always sort manually-created materials to the front
  23. if ( src1.m_bManuallyCreated != src2.m_bManuallyCreated )
  24. return src1.m_bManuallyCreated;
  25. return src1.m_Name < src2.m_Name;
  26. }
  27. //-----------------------------------------------------------------------------
  28. // sort function for missing materials
  29. //-----------------------------------------------------------------------------
  30. bool CMaterialDict::MissingMaterialLessFunc( const MissingMaterial_t& src1,
  31. const MissingMaterial_t& src2 )
  32. {
  33. return src1.m_Name < src2.m_Name;
  34. }
  35. void CMaterialDict::Shutdown( )
  36. {
  37. // Clean up all materials..
  38. RemoveAllMaterials();
  39. // FIXME: Could dump list here...
  40. m_MissingList.RemoveAll();
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Adds/removes the material to the list of all materials
  44. //-----------------------------------------------------------------------------
  45. void CMaterialDict::AddMaterialToMaterialList( IMaterialInternal *pMaterial )
  46. {
  47. MaterialLookup_t lookup;
  48. lookup.m_pMaterial = pMaterial;
  49. lookup.m_Name = pMaterial->GetName();
  50. lookup.m_bManuallyCreated = pMaterial->IsManuallyCreated();
  51. m_MaterialDict.Insert( lookup );
  52. }
  53. void CMaterialDict::RemoveMaterialFromMaterialList( IMaterialInternal *pMaterial )
  54. {
  55. // Gotta iterate over this manually; name-based lookup is bogus if there are two
  56. // materials with the same name, which can happen for procedural materials
  57. // First remove all the subrect materials, because they'll point at their material pages.
  58. MaterialHandle_t i;
  59. MaterialHandle_t iNext = InvalidMaterial();
  60. for (i = FirstMaterial(); i != InvalidMaterial(); i = iNext )
  61. {
  62. iNext = NextMaterial(i);
  63. if ( m_MaterialDict[i].m_pMaterial == pMaterial )
  64. {
  65. m_MaterialDict.RemoveAt( i );
  66. break;
  67. }
  68. }
  69. Assert( i != InvalidMaterial() );
  70. #ifdef _DEBUG
  71. for ( i = iNext; i != InvalidMaterial(); i = NextMaterial(i) )
  72. {
  73. Assert( m_MaterialDict[i].m_pMaterial != pMaterial );
  74. }
  75. #endif
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Adds, removes materials
  79. //-----------------------------------------------------------------------------
  80. IMaterialInternal* CMaterialDict::AddMaterial( char const* pName, const char *pTextureGroupName )
  81. {
  82. IMaterialInternal *pMaterial = IMaterialInternal::CreateMaterial( pName, pTextureGroupName );
  83. Assert( pMaterial && pMaterial->IsRealTimeVersion() );
  84. AddMaterialToMaterialList( pMaterial );
  85. return pMaterial;
  86. }
  87. void CMaterialDict::RemoveMaterial( IMaterialInternal* pMaterial )
  88. {
  89. Assert( (pMaterial == NULL) || pMaterial->IsRealTimeVersion() );
  90. RemoveMaterialFromMaterialList( pMaterial );
  91. IMaterialInternal::DestroyMaterial( pMaterial );
  92. }
  93. IMaterialInternal *CMaterialDict::AddMaterialSubRect( const char *pName, const char *pTextureGroupName, KeyValues *pKeyValues, KeyValues *pPatchKeyValues )
  94. {
  95. IMaterialInternal *pMaterial = IMaterialInternal::CreateMaterialSubRect( pName, pTextureGroupName, pKeyValues, pPatchKeyValues, true );
  96. Assert( pMaterial );
  97. AddMaterialToMaterialList( pMaterial );
  98. return pMaterial;
  99. }
  100. void CMaterialDict::RemoveMaterialSubRect( IMaterialInternal *pMaterial )
  101. {
  102. RemoveMaterialFromMaterialList( pMaterial );
  103. IMaterialInternal::DestroyMaterialSubRect( pMaterial );
  104. }
  105. void CMaterialDict::RemoveMaterialFromMaterialList( MaterialHandle_t h )
  106. {
  107. m_MaterialDict.RemoveAt( h );
  108. }
  109. void CMaterialDict::RemoveAllMaterialsFromMaterialList()
  110. {
  111. m_MaterialDict.RemoveAll();
  112. }
  113. void CMaterialDict::RemoveAllMaterials()
  114. {
  115. // First remove all the subrect materials, because they'll point at their material pages.
  116. MaterialHandle_t i, iNext;
  117. for (i = FirstMaterial(); i != InvalidMaterial(); i = iNext )
  118. {
  119. iNext = NextMaterial(i);
  120. IMaterialInternal *pMaterial = GetMaterialInternal( i );
  121. if ( pMaterial->InMaterialPage() )
  122. {
  123. IMaterialInternal::DestroyMaterialSubRect( pMaterial );
  124. RemoveMaterialFromMaterialList( i );
  125. }
  126. }
  127. // Now get rid of the rest of the materials, including the pages.
  128. for (i = FirstMaterial(); i != InvalidMaterial(); i = NextMaterial(i) )
  129. {
  130. IMaterialInternal::DestroyMaterial( GetMaterialInternal( i ) );
  131. }
  132. RemoveAllMaterialsFromMaterialList();
  133. }