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.

259 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implementation of IEditorTexture interface for materials.
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef MATERIAL_H
  8. #define MATERIAL_H
  9. #pragma once
  10. #include "IEditorTexture.h"
  11. #include "materialsystem/imaterialvar.h"
  12. #include "materialsystem/imaterial.h"
  13. class IMaterial;
  14. class CMaterialCache;
  15. class IMaterialSystem;
  16. class IMaterialSystemHardwareConfig;
  17. struct MaterialSystem_Config_t;
  18. struct MaterialCacheEntry_t;
  19. #define INCLUDE_MODEL_MATERIALS 0x01
  20. #define INCLUDE_WORLD_MATERIALS 0x02
  21. #define INCLUDE_ALL_MATERIALS 0xFFFFFFFF
  22. //-----------------------------------------------------------------------------
  23. // Inherit from this to enumerate materials
  24. //-----------------------------------------------------------------------------
  25. class IMaterialEnumerator
  26. {
  27. public:
  28. virtual bool EnumMaterial( const char *pMaterialName, int nContext ) = 0;
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. //-----------------------------------------------------------------------------
  33. class CMaterial : public IEditorTexture
  34. {
  35. public:
  36. static bool Initialize( HWND hwnd );
  37. static void ShutDown(void);
  38. static void EnumerateMaterials( IMaterialEnumerator *pEnum, const char *szRoot, int nContext, int nFlags = INCLUDE_ALL_MATERIALS );
  39. static CMaterial *CreateMaterial( const char *pszMaterialName, bool bLoadImmediately, bool* pFound = 0 );
  40. virtual ~CMaterial(void);
  41. void Draw(CDC *pDC, RECT& rect, int iFontHeight, int iIconHeight, DrawTexData_t &DrawTexData); //DWORD dwFlags = (drawCaption|drawIcons));
  42. void FreeData(void);
  43. inline const char *GetName(void) const
  44. {
  45. return(m_szName);
  46. }
  47. int GetShortName(char *pszName) const;
  48. int GetKeywords(char *pszKeywords) const;
  49. void GetSize(SIZE &size) const;
  50. int GetImageDataRGB(void *pImageRGB);
  51. int GetImageDataRGBA(void *pImageRGBA);
  52. // Image dimensions
  53. int GetImageWidth(void) const;
  54. int GetImageHeight(void) const;
  55. int GetWidth(void) const;
  56. int GetHeight(void) const;
  57. float GetDecalScale(void) const;
  58. const char *GetFileName(void) const;
  59. inline CPalette *GetPalette(void) const
  60. {
  61. return(NULL);
  62. }
  63. inline int GetSurfaceAttributes(void) const
  64. {
  65. return(0);
  66. }
  67. inline int GetSurfaceContents(void) const
  68. {
  69. return(0);
  70. }
  71. inline int GetSurfaceValue(void) const
  72. {
  73. return(0);
  74. }
  75. inline TEXTUREFORMAT GetTextureFormat(void) const
  76. {
  77. return(tfVMT);
  78. }
  79. inline int GetTextureID(void) const
  80. {
  81. return(m_nTextureID);
  82. }
  83. bool HasAlpha(void) const
  84. {
  85. return(false);
  86. }
  87. inline bool HasData(void) const
  88. {
  89. return((m_nWidth != 0) && (m_nHeight != 0));
  90. }
  91. inline bool HasPalette(void) const
  92. {
  93. return(false);
  94. }
  95. inline bool IsDummy(void) const
  96. {
  97. return(false);
  98. }
  99. bool Load(void);
  100. void Reload( bool bFullReload );
  101. inline bool IsLoaded() const
  102. {
  103. return m_bLoaded;
  104. }
  105. inline void SetTextureID(int nTextureID)
  106. {
  107. m_nTextureID = nTextureID;
  108. }
  109. bool IsWater( void ) const;
  110. virtual IMaterial* GetMaterial( bool bForceLoad=true );
  111. protected:
  112. // Used to draw the bitmap for the texture browser
  113. void DrawBitmap( CDC *pDC, RECT& srcRect, RECT& dstRect );
  114. void DrawBrowserIcons( CDC *pDC, RECT& dstRect, bool detectErrors );
  115. void DrawIcon( CDC *pDC, CMaterial* pIcon, RECT& dstRect );
  116. static bool ShouldSkipMaterial(const char *pszName, int nFlags);
  117. // Finds all .VMT files in a particular directory
  118. static bool LoadMaterialsInDirectory( char const* pDirectoryName, int nDirectoryNameLen,
  119. IMaterialEnumerator *pEnum, int nContext, int nFlags );
  120. // Discovers all .VMT files lying under a particular directory recursively
  121. static bool InitDirectoryRecursive( char const* pDirectoryName,
  122. IMaterialEnumerator *pEnum, int nContext, int nFlags );
  123. CMaterial(void);
  124. bool LoadMaterialHeader(IMaterial *material);
  125. bool LoadMaterialImage();
  126. static bool IsIgnoredMaterial( const char *pName );
  127. // Will actually load the material bits
  128. // We don't want to load them all at once because it takes way too long
  129. bool LoadMaterial();
  130. char m_szName[MAX_PATH];
  131. char m_szFileName[MAX_PATH];
  132. char m_szKeywords[MAX_PATH];
  133. int m_nTextureID; // Uniquely identifies this texture in all 3D renderers.
  134. int m_nWidth; // Texture width in texels.
  135. int m_nHeight; // Texture height in texels.
  136. bool m_TranslucentBaseTexture;
  137. bool m_bLoaded; // We don't load these immediately; only when needed..
  138. void *m_pData; // Loaded texel data (NULL if not loaded).
  139. IMaterial *m_pMaterial;
  140. friend class CMaterialImageCache;
  141. };
  142. typedef CMaterial *CMaterialPtr;
  143. //-----------------------------------------------------------------------------
  144. // Purpose:
  145. //-----------------------------------------------------------------------------
  146. class CMaterialCache
  147. {
  148. public:
  149. CMaterialCache(void);
  150. ~CMaterialCache(void);
  151. inline bool CacheExists(void);
  152. bool Create(int nMaxEntries);
  153. CMaterial *CreateMaterial(const char *pszMaterialName);
  154. void AddRef(CMaterial *pMaterial);
  155. void Release(CMaterial *pMaterial);
  156. protected:
  157. CMaterial *FindMaterial(const char *pszMaterialName);
  158. void AddMaterial(CMaterial *pMaterial);
  159. MaterialCacheEntry_t *m_pCache;
  160. int m_nMaxEntries;
  161. int m_nEntries;
  162. };
  163. //-----------------------------------------------------------------------------
  164. // Purpose: Returns true if the cache has been allocated, false if not.
  165. //-----------------------------------------------------------------------------
  166. inline bool CMaterialCache::CacheExists(void)
  167. {
  168. return((m_pCache != NULL) && (m_nMaxEntries > 0));
  169. }
  170. //-----------------------------------------------------------------------------
  171. // returns the material system interface + config
  172. //-----------------------------------------------------------------------------
  173. inline IMaterialSystem *MaterialSystemInterface()
  174. {
  175. return materials;
  176. }
  177. inline MaterialSystem_Config_t& MaterialSystemConfig()
  178. {
  179. extern MaterialSystem_Config_t g_materialSystemConfig;
  180. return g_materialSystemConfig;
  181. }
  182. inline IMaterialSystemHardwareConfig* MaterialSystemHardwareConfig()
  183. {
  184. extern IMaterialSystemHardwareConfig* g_pMaterialSystemHardwareConfig;
  185. return g_pMaterialSystemHardwareConfig;
  186. }
  187. //--------------------------------------------------------------------------------
  188. // call AllocateLightingPreviewtextures to make sure necessary rts are allocated
  189. //--------------------------------------------------------------------------------
  190. void AllocateLightingPreviewtextures(void);
  191. #endif // MATERIAL_H