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.

181 lines
5.2 KiB

  1. //========== Copyright � 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef CMATERIALDICT_H
  7. #define CMATERIALDICT_H
  8. #include "tier1/utlsymbol.h"
  9. #include "tier1/utlrbtree.h"
  10. #ifndef MATSYS_INTERNAL
  11. #error "This file is private to the implementation of IMaterialSystem/IMaterialSystemInternal"
  12. #endif
  13. #if defined( _WIN32 )
  14. #pragma once
  15. #endif
  16. //-----------------------------------------------------------------------------
  17. class IMaterial;
  18. class IMaterialInternal;
  19. typedef unsigned short MaterialHandle_t;
  20. //-----------------------------------------------------------------------------
  21. // Dictionary of all known materials
  22. //-----------------------------------------------------------------------------
  23. class CMaterialDict
  24. {
  25. public:
  26. CMaterialDict() :
  27. m_MaterialDict( 0, 256, MaterialLessFunc ),
  28. m_MissingList( 0, 32, MissingMaterialLessFunc )
  29. {
  30. }
  31. void Shutdown();
  32. int GetNumMaterials( void ) const;
  33. IMaterial* GetMaterial( MaterialHandle_t h ) const;
  34. IMaterialInternal * GetMaterialInternal( MaterialHandle_t idx ) const;
  35. MaterialHandle_t FirstMaterial() const;
  36. MaterialHandle_t NextMaterial( MaterialHandle_t h ) const;
  37. MaterialHandle_t InvalidMaterial() const;
  38. IMaterialInternal * FindMaterial( const char *pszName, bool bManuallyCreated ) const;
  39. void AddMaterialToMaterialList( IMaterialInternal *pMaterial );
  40. void RemoveMaterialFromMaterialList( IMaterialInternal *pMaterial );
  41. void RemoveMaterial( IMaterialInternal *pMaterial );
  42. void RemoveMaterialSubRect( IMaterialInternal *pMaterial );
  43. IMaterialInternal* AddMaterial( const char* pName, const char *pTextureGroupName );
  44. // pKeyValues and pPatchKeyValues should come from LoadVMTFile()
  45. IMaterialInternal* AddMaterialSubRect( const char* pName, const char *pTextureGroupName, KeyValues *pKeyValues, KeyValues *pPatchKeyValues );
  46. bool NoteMissing( const char *pszName );
  47. bool IsMissing( const char *pszName );
  48. protected: /*private:*/
  49. void RemoveAllMaterials();
  50. void RemoveAllMaterialsFromMaterialList();
  51. void RemoveMaterialFromMaterialList( MaterialHandle_t h );
  52. // Stores a dictionary of materials, searched by name
  53. struct MaterialLookup_t
  54. {
  55. IMaterialInternal* m_pMaterial;
  56. CUtlSymbol m_Name;
  57. bool m_bManuallyCreated;
  58. };
  59. // Stores a dictionary of missing materials to cut down on redundant warning messages
  60. // TODO: 1) Could add a counter
  61. // 2) Could dump to file/console at exit for exact list of missing materials
  62. struct MissingMaterial_t
  63. {
  64. CUtlSymbol m_Name;
  65. };
  66. static bool MaterialLessFunc( const MaterialLookup_t& src1,
  67. const MaterialLookup_t& src2 );
  68. static bool MissingMaterialLessFunc( const MissingMaterial_t& src1,
  69. const MissingMaterial_t& src2 );
  70. CUtlRBTree< MaterialLookup_t, MaterialHandle_t > m_MaterialDict;
  71. CUtlRBTree< MissingMaterial_t, int > m_MissingList;
  72. };
  73. //-----------------------------------------------------------------------------
  74. // Material iteration methods
  75. //-----------------------------------------------------------------------------
  76. inline MaterialHandle_t CMaterialDict::FirstMaterial() const
  77. {
  78. return m_MaterialDict.FirstInorder();
  79. }
  80. inline MaterialHandle_t CMaterialDict::NextMaterial( MaterialHandle_t h ) const
  81. {
  82. return m_MaterialDict.NextInorder(h);
  83. }
  84. inline int CMaterialDict::GetNumMaterials( ) const
  85. {
  86. return m_MaterialDict.Count();
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Invalid index handle....
  90. //-----------------------------------------------------------------------------
  91. inline MaterialHandle_t CMaterialDict::InvalidMaterial() const
  92. {
  93. return m_MaterialDict.InvalidIndex();
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Handle to material
  97. //-----------------------------------------------------------------------------
  98. inline IMaterial* CMaterialDict::GetMaterial( MaterialHandle_t idx ) const
  99. {
  100. return m_MaterialDict[idx].m_pMaterial;
  101. }
  102. inline IMaterialInternal* CMaterialDict::GetMaterialInternal( MaterialHandle_t idx ) const
  103. {
  104. Assert( (m_MaterialDict[idx].m_pMaterial == NULL) || m_MaterialDict[idx].m_pMaterial->IsRealTimeVersion() );
  105. return m_MaterialDict[idx].m_pMaterial;
  106. }
  107. inline IMaterialInternal* CMaterialDict::FindMaterial( const char *pszName, bool bManuallyCreated ) const
  108. {
  109. MaterialLookup_t lookup;
  110. lookup.m_Name = pszName;
  111. lookup.m_bManuallyCreated = bManuallyCreated; // This causes the search to find only file-created materials
  112. MaterialHandle_t h = m_MaterialDict.Find( lookup );
  113. if ( h != m_MaterialDict.InvalidIndex() )
  114. {
  115. return m_MaterialDict[h].m_pMaterial;
  116. }
  117. return NULL;
  118. }
  119. inline bool CMaterialDict::IsMissing( const char *pszName )
  120. {
  121. MissingMaterial_t missing;
  122. missing.m_Name = pszName;
  123. if ( m_MissingList.Find( missing ) != m_MissingList.InvalidIndex() )
  124. return true;
  125. return false;
  126. }
  127. inline bool CMaterialDict::NoteMissing( const char *pszName )
  128. {
  129. MissingMaterial_t missing;
  130. missing.m_Name = pszName;
  131. if ( m_MissingList.Find( missing ) != m_MissingList.InvalidIndex() )
  132. {
  133. return false;
  134. }
  135. m_MissingList.Insert( missing );
  136. return true;
  137. }
  138. //-----------------------------------------------------------------------------
  139. #endif // CMATERIALDICT_H