Team Fortress 2 Source Code as on 22/4/2020
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.

179 lines
5.2 KiB

  1. //========= Copyright 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. Assert( ThreadInMainThread() );
  31. }
  32. void Shutdown();
  33. int GetNumMaterials( void ) const;
  34. IMaterial* GetMaterial( MaterialHandle_t h ) const;
  35. IMaterialInternal * GetMaterialInternal( MaterialHandle_t idx ) const;
  36. MaterialHandle_t FirstMaterial() const;
  37. MaterialHandle_t NextMaterial( MaterialHandle_t h ) const;
  38. MaterialHandle_t InvalidMaterial() const;
  39. IMaterialInternal * FindMaterial( const char *pszName, bool bManuallyCreated ) const;
  40. void AddMaterialToMaterialList( IMaterialInternal *pMaterial );
  41. void RemoveMaterialFromMaterialList( IMaterialInternal *pMaterial );
  42. void RemoveMaterial( IMaterialInternal *pMaterial );
  43. void RemoveMaterialSubRect( IMaterialInternal *pMaterial );
  44. IMaterialInternal* AddMaterial( const char* pName, const char *pTextureGroupName );
  45. // pKeyValues and pPatchKeyValues should come from LoadVMTFile()
  46. IMaterialInternal* AddMaterialSubRect( const char* pName, const char *pTextureGroupName, KeyValues *pKeyValues, KeyValues *pPatchKeyValues );
  47. bool NoteMissing( 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. Assert( ThreadInMainThread() );
  79. return m_MaterialDict.FirstInorder();
  80. }
  81. inline MaterialHandle_t CMaterialDict::NextMaterial( MaterialHandle_t h ) const
  82. {
  83. Assert( ThreadInMainThread() );
  84. return m_MaterialDict.NextInorder(h);
  85. }
  86. inline int CMaterialDict::GetNumMaterials( ) const
  87. {
  88. Assert( ThreadInMainThread() );
  89. return m_MaterialDict.Count();
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Invalid index handle....
  93. //-----------------------------------------------------------------------------
  94. inline MaterialHandle_t CMaterialDict::InvalidMaterial() const
  95. {
  96. Assert( ThreadInMainThread() );
  97. return m_MaterialDict.InvalidIndex();
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Handle to material
  101. //-----------------------------------------------------------------------------
  102. inline IMaterial* CMaterialDict::GetMaterial( MaterialHandle_t idx ) const
  103. {
  104. Assert( ThreadInMainThread() );
  105. return m_MaterialDict[idx].m_pMaterial;
  106. }
  107. inline IMaterialInternal* CMaterialDict::GetMaterialInternal( MaterialHandle_t idx ) const
  108. {
  109. Assert( ThreadInMainThread() );
  110. Assert( (m_MaterialDict[idx].m_pMaterial == NULL) || m_MaterialDict[idx].m_pMaterial->IsRealTimeVersion() );
  111. return m_MaterialDict[idx].m_pMaterial;
  112. }
  113. inline IMaterialInternal* CMaterialDict::FindMaterial( const char *pszName, bool bManuallyCreated ) const
  114. {
  115. Assert( ThreadInMainThread() );
  116. MaterialLookup_t lookup;
  117. lookup.m_Name = pszName;
  118. lookup.m_bManuallyCreated = bManuallyCreated; // This causes the search to find only file-created materials
  119. MaterialHandle_t h = m_MaterialDict.Find( lookup );
  120. if ( h != m_MaterialDict.InvalidIndex() )
  121. {
  122. return m_MaterialDict[h].m_pMaterial;
  123. }
  124. return NULL;
  125. }
  126. inline bool CMaterialDict::NoteMissing( const char *pszName )
  127. {
  128. Assert( ThreadInMainThread() );
  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