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.

378 lines
9.9 KiB

  1. //========= Copyright � 1996-2005, 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. // Opens explorer dialog and selects the source file
  151. void ExploreToSource( const char *pMaterialName );
  152. // Reload individual textures.
  153. void ReloadTextures( const char *pFilterName );
  154. // bind local cubemap again
  155. void RebindDefaultCubeMap();
  156. void UpdateFileChangeWatchers();
  157. // Gets tools/toolsnodraw
  158. IEditorTexture* GetNoDrawTexture() { return m_pNoDrawTexture; }
  159. int AddTexture( IEditorTexture *pTexture );
  160. protected:
  161. // CMaterialFileChangeWatcher stuff - watches for changes to VMTs or VTFs and handles them.
  162. enum EFileType
  163. {
  164. k_eFileTypeVMT,
  165. k_eFileTypeVTF
  166. };
  167. void OnFileChange( const char *pFilename, int context, EFileType eFileType );
  168. void ReloadMaterialsUsingTexture( ITexture *pTestTexture );
  169. static bool GetFileTypeFromFilename( const char *pFilename, CTextureSystem::EFileType *pFileType );
  170. CUtlVector<CMaterialFileChangeWatcher*> m_ChangeWatchers;
  171. // Internal stuff.
  172. void FreeAllTextures();
  173. TextureContext_t *AddTextureContext();
  174. TextureContext_t *FindTextureContextForConfig(CGameConfig *pConfig);
  175. void LoadMaterials(CGameConfig *pConfig);
  176. void LoadWADFiles(CGameConfig *pConfig);
  177. DWORD LoadGraphicsFile(const char *pFilename);
  178. void LoadGraphicsFileWAD3(GRAPHICSFILESTRUCT *pFile, int fd, CTextureGroup *pGroup);
  179. //
  180. // Array of open graphics files.
  181. //
  182. CUtlVector<GRAPHICSFILESTRUCT> m_GraphicsFiles;
  183. //
  184. // Master array of textures.
  185. //
  186. CUtlVector<IEditorTexture *> m_Textures;
  187. IEditorTexture *m_pLastTex;
  188. int m_nLastIndex;
  189. //
  190. // List of groups (sets of textures of a given texture format). Only one
  191. // group can be active at a time, based on the game configuration.
  192. //
  193. CUtlVector<TextureContext_t> m_TextureContexts; // One per game config.
  194. TextureContext_t *m_pActiveContext; // Points to the active entry in m_TextureContexts.
  195. CTextureGroup *m_pActiveGroup; // Points to the active entry in m_TextureContexts.
  196. //
  197. // List of keywords found in all textures.
  198. //
  199. CUtlVector<const char *> m_Keywords;
  200. // default cubemap
  201. ITexture *m_pCubemapTexture;
  202. // tools/toolsnodraw
  203. IEditorTexture* m_pNoDrawTexture;
  204. };
  205. //-----------------------------------------------------------------------------
  206. // Purpose:
  207. //-----------------------------------------------------------------------------
  208. int CTextureSystem::FilesGetCount(void) const
  209. {
  210. return(m_GraphicsFiles.Count());
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose:
  214. // Input : pFileInfo -
  215. // nIndex -
  216. //-----------------------------------------------------------------------------
  217. void CTextureSystem::FilesGetInfo(GRAPHICSFILESTRUCT *pFileInfo, int nIndex) const
  218. {
  219. if (pFileInfo != NULL)
  220. {
  221. *pFileInfo = m_GraphicsFiles[nIndex];
  222. }
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Purpose: Returns the number of textures in the active group.
  226. //-----------------------------------------------------------------------------
  227. int CTextureSystem::GetActiveTextureCount(void) const
  228. {
  229. if (m_pActiveGroup != NULL)
  230. {
  231. return m_pActiveGroup->GetCount();
  232. }
  233. return(0);
  234. }
  235. IEditorTexture *CTextureSystem::GetActiveTexture(int nIndex) const
  236. {
  237. if (m_pActiveGroup != NULL)
  238. {
  239. return m_pActiveGroup->GetTexture(nIndex);
  240. }
  241. return NULL;
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Purpose:
  245. //-----------------------------------------------------------------------------
  246. int CTextureSystem::GroupsGetCount() const
  247. {
  248. if (!m_pActiveContext)
  249. return 0;
  250. return m_pActiveContext->Groups.Count();
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose:
  254. //-----------------------------------------------------------------------------
  255. CTextureGroup *CTextureSystem::GroupsGet(int nIndex) const
  256. {
  257. if (!m_pActiveContext)
  258. return NULL;
  259. return m_pActiveContext->Groups.Element(nIndex);
  260. }
  261. //-----------------------------------------------------------------------------
  262. // Purpose: Initiates an iteration of the MRU list.
  263. //-----------------------------------------------------------------------------
  264. int CTextureSystem::MRUGetCount() const
  265. {
  266. if (!m_pActiveContext)
  267. return NULL;
  268. return m_pActiveContext->MRU.Count();
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Purpose: Returns the next texture in the MRU of the given format.
  272. // Input : pos - Iterator.
  273. // eDesiredFormat - Texture format to return.
  274. // Output : Pointer to the texture.
  275. //-----------------------------------------------------------------------------
  276. IEditorTexture *CTextureSystem::MRUGet(int nIndex) const
  277. {
  278. if (!m_pActiveContext)
  279. return NULL;
  280. return m_pActiveContext->MRU.Element(nIndex);
  281. }
  282. extern CTextureSystem g_Textures;
  283. #endif // TEXTURESYSTEM_H