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.

375 lines
9.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Texture management functions. Exposes a list of available textures,
  4. // texture groups, and Most Recently Used textures.
  5. //
  6. //=============================================================================//
  7. #ifndef TEXTURESYSTEM_H
  8. #define TEXTURESYSTEM_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "IEditorTexture.h"
  13. #include "Material.h"
  14. #include "utlvector.h"
  15. #include "utldict.h"
  16. #include "FileChangeWatcher.h"
  17. class CGameConfig;
  18. class CTextureSystem;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Defines the interface to a set of textures of a given texture format.
  21. // The textures are stored as an index into the global array of textures.
  22. //-----------------------------------------------------------------------------
  23. class CTextureGroup
  24. {
  25. public:
  26. CTextureGroup(const char *pszName);
  27. inline const char *GetName()
  28. {
  29. return(m_szName);
  30. }
  31. inline int GetCount(void)
  32. {
  33. return m_Textures.Count();
  34. }
  35. inline TEXTUREFORMAT GetTextureFormat(void)
  36. {
  37. return(m_eTextureFormat);
  38. }
  39. inline void SetTextureFormat(TEXTUREFORMAT eTextureFormat)
  40. {
  41. m_eTextureFormat = eTextureFormat;
  42. }
  43. void AddTexture(IEditorTexture *pTexture);
  44. void Sort(void);
  45. IEditorTexture *GetTexture(int nIndex);
  46. IEditorTexture* GetTexture( char const* pName );
  47. // Fast find texture..
  48. IEditorTexture* FindTextureByName( const char *pName, int *piIndex, TEXTUREFORMAT eDesiredFormat );
  49. // Used to lazily load in all the textures
  50. void LazyLoadTextures();
  51. protected:
  52. char m_szName[MAX_PATH];
  53. TEXTUREFORMAT m_eTextureFormat;
  54. CUtlVector<IEditorTexture *> m_Textures;
  55. CUtlDict<int,int> m_TextureNameMap; // Maps the texture name to an index into m_Textures (the key is IEditorTexture::GetName).
  56. // Used to lazily load the textures in the group
  57. int m_nTextureToLoad;
  58. };
  59. typedef CUtlVector<CTextureGroup *> TextureGroupList_t;
  60. typedef struct tagGF
  61. {
  62. char filename[MAX_PATH];
  63. DWORD id;
  64. int fd;
  65. TEXTUREFORMAT format;
  66. BOOL bLoaded;
  67. } GRAPHICSFILESTRUCT;
  68. //
  69. // When the user switches game configs, all the textures and materials are switched.
  70. // This structure holds all the context necessary to accomplish this.
  71. //
  72. struct TextureContext_t
  73. {
  74. CGameConfig *pConfig; // The game config that this texture context corresponds to.
  75. CTextureGroup *pAllGroup;
  76. TextureGroupList_t Groups;
  77. EditorTextureList_t MRU; // List of Most Recently Used textures, first is the most recent.
  78. EditorTextureList_t Dummies; // List of Dummy textures - textures that were created to hold the place of missing textures.
  79. };
  80. class CMaterialFileChangeWatcher : private CFileChangeWatcher::ICallbacks
  81. {
  82. public:
  83. void Init( CTextureSystem *pSystem, int context );
  84. void Update(); // Call this periodically to update.
  85. private:
  86. // CFileChangeWatcher::ICallbacks..
  87. virtual void OnFileChange( const char *pRelativeFilename, const char *pFullFilename );
  88. private:
  89. CFileChangeWatcher m_Watcher;
  90. CTextureSystem *m_pTextureSystem;
  91. int m_Context;
  92. };
  93. class CTextureSystem : public IMaterialEnumerator
  94. {
  95. public:
  96. friend class CMaterialFileChangeWatcher;
  97. CTextureSystem(void);
  98. virtual ~CTextureSystem(void);
  99. bool Initialize(HWND hwnd);
  100. void ShutDown(void);
  101. void SetActiveConfig(CGameConfig *pConfig);
  102. //
  103. // Exposes a list of all texture (WAD) files.
  104. //
  105. inline int FilesGetCount(void) const;
  106. inline void FilesGetInfo(GRAPHICSFILESTRUCT *pFileInfo, int nIndex) const;
  107. bool FindGraphicsFile(GRAPHICSFILESTRUCT *pFileInfo, DWORD id, int *piIndex = NULL);
  108. //
  109. // Exposes a list of texture groups (sets of textures of a given format).
  110. //
  111. void SetActiveGroup(const char *pcszName);
  112. inline int GroupsGetCount() const;
  113. inline CTextureGroup *GroupsGet(int nIndex) const;
  114. //
  115. // Exposes a list of active textures based on the currently active texture group.
  116. //
  117. inline int GetActiveTextureCount(void) const;
  118. inline IEditorTexture *GetActiveTexture(int nIndex) const;
  119. IEditorTexture *EnumActiveTextures(int *piIndex, TEXTUREFORMAT eDesiredFormat) const;
  120. IEditorTexture *FindActiveTexture(LPCSTR pszName, int *piIndex = NULL, BOOL bDummy = TRUE);
  121. bool HasTexturesForConfig(CGameConfig *pConfig);
  122. //
  123. // Exposes a list of Most Recently Used textures.
  124. //
  125. void AddMRU(IEditorTexture *pTex);
  126. inline int MRUGetCount() const;
  127. inline IEditorTexture *MRUGet(int nIndex) const;
  128. //
  129. // Exposes a list of all unique keywords found in the master texture list.
  130. //
  131. int GetNumKeywords();
  132. const char *GetKeyword(int index);
  133. //
  134. // Holds a list of placeholder textures used when a map refers to missing textures.
  135. //
  136. IEditorTexture *AddDummy(LPCTSTR pszName, TEXTUREFORMAT eFormat);
  137. //
  138. // Load graphics files from options list.
  139. //
  140. void LoadAllGraphicsFiles(void);
  141. void InformPaletteChanged(void);
  142. // IMaterialEnumerator interface, Used to add all the world materials into the material list.
  143. bool EnumMaterial( const char *pMaterialName, int nContext );
  144. // Used to lazily load in all the textures during app idle.
  145. void LazyLoadTextures();
  146. // Registers the keywords as existing in a particular material.
  147. void RegisterTextureKeywords( IEditorTexture *pTexture );
  148. // Opens the source file associated with a material.
  149. void OpenSource( const char *pMaterialName );
  150. // Reload individual textures.
  151. void ReloadTextures( const char *pFilterName );
  152. // bind local cubemap again
  153. void RebindDefaultCubeMap();
  154. void UpdateFileChangeWatchers();
  155. // Gets tools/toolsnodraw
  156. IEditorTexture* GetNoDrawTexture() { return m_pNoDrawTexture; }
  157. int AddTexture( IEditorTexture *pTexture );
  158. protected:
  159. // CMaterialFileChangeWatcher stuff - watches for changes to VMTs or VTFs and handles them.
  160. enum EFileType
  161. {
  162. k_eFileTypeVMT,
  163. k_eFileTypeVTF
  164. };
  165. void OnFileChange( const char *pFilename, int context, EFileType eFileType );
  166. void ReloadMaterialsUsingTexture( ITexture *pTestTexture );
  167. static bool GetFileTypeFromFilename( const char *pFilename, CTextureSystem::EFileType *pFileType );
  168. CUtlVector<CMaterialFileChangeWatcher*> m_ChangeWatchers;
  169. // Internal stuff.
  170. void FreeAllTextures();
  171. TextureContext_t *AddTextureContext();
  172. TextureContext_t *FindTextureContextForConfig(CGameConfig *pConfig);
  173. void LoadMaterials(CGameConfig *pConfig);
  174. void LoadWADFiles(CGameConfig *pConfig);
  175. DWORD LoadGraphicsFile(const char *pFilename);
  176. void LoadGraphicsFileWAD3(GRAPHICSFILESTRUCT *pFile, int fd, CTextureGroup *pGroup);
  177. //
  178. // Array of open graphics files.
  179. //
  180. CUtlVector<GRAPHICSFILESTRUCT> m_GraphicsFiles;
  181. //
  182. // Master array of textures.
  183. //
  184. CUtlVector<IEditorTexture *> m_Textures;
  185. IEditorTexture *m_pLastTex;
  186. int m_nLastIndex;
  187. //
  188. // List of groups (sets of textures of a given texture format). Only one
  189. // group can be active at a time, based on the game configuration.
  190. //
  191. CUtlVector<TextureContext_t> m_TextureContexts; // One per game config.
  192. TextureContext_t *m_pActiveContext; // Points to the active entry in m_TextureContexts.
  193. CTextureGroup *m_pActiveGroup; // Points to the active entry in m_TextureContexts.
  194. //
  195. // List of keywords found in all textures.
  196. //
  197. CUtlVector<const char *> m_Keywords;
  198. // default cubemap
  199. ITexture *m_pCubemapTexture;
  200. // tools/toolsnodraw
  201. IEditorTexture* m_pNoDrawTexture;
  202. };
  203. //-----------------------------------------------------------------------------
  204. // Purpose:
  205. //-----------------------------------------------------------------------------
  206. int CTextureSystem::FilesGetCount(void) const
  207. {
  208. return(m_GraphicsFiles.Count());
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose:
  212. // Input : pFileInfo -
  213. // nIndex -
  214. //-----------------------------------------------------------------------------
  215. void CTextureSystem::FilesGetInfo(GRAPHICSFILESTRUCT *pFileInfo, int nIndex) const
  216. {
  217. if (pFileInfo != NULL)
  218. {
  219. *pFileInfo = m_GraphicsFiles[nIndex];
  220. }
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose: Returns the number of textures in the active group.
  224. //-----------------------------------------------------------------------------
  225. int CTextureSystem::GetActiveTextureCount(void) const
  226. {
  227. if (m_pActiveGroup != NULL)
  228. {
  229. return m_pActiveGroup->GetCount();
  230. }
  231. return(0);
  232. }
  233. IEditorTexture *CTextureSystem::GetActiveTexture(int nIndex) const
  234. {
  235. if (m_pActiveGroup != NULL)
  236. {
  237. return m_pActiveGroup->GetTexture(nIndex);
  238. }
  239. return NULL;
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Purpose:
  243. //-----------------------------------------------------------------------------
  244. int CTextureSystem::GroupsGetCount() const
  245. {
  246. if (!m_pActiveContext)
  247. return 0;
  248. return m_pActiveContext->Groups.Count();
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Purpose:
  252. //-----------------------------------------------------------------------------
  253. CTextureGroup *CTextureSystem::GroupsGet(int nIndex) const
  254. {
  255. if (!m_pActiveContext)
  256. return NULL;
  257. return m_pActiveContext->Groups.Element(nIndex);
  258. }
  259. //-----------------------------------------------------------------------------
  260. // Purpose: Initiates an iteration of the MRU list.
  261. //-----------------------------------------------------------------------------
  262. int CTextureSystem::MRUGetCount() const
  263. {
  264. if (!m_pActiveContext)
  265. return NULL;
  266. return m_pActiveContext->MRU.Count();
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Purpose: Returns the next texture in the MRU of the given format.
  270. // Input : pos - Iterator.
  271. // eDesiredFormat - Texture format to return.
  272. // Output : Pointer to the texture.
  273. //-----------------------------------------------------------------------------
  274. IEditorTexture *CTextureSystem::MRUGet(int nIndex) const
  275. {
  276. if (!m_pActiveContext)
  277. return NULL;
  278. return m_pActiveContext->MRU.Element(nIndex);
  279. }
  280. extern CTextureSystem g_Textures;
  281. #endif // TEXTURESYSTEM_H