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.

150 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Deals with singleton
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #if !defined( CLIENTEFFECTPRECACHESYSTEM_H )
  9. #define CLIENTEFFECTPRECACHESYSTEM_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "igamesystem.h"
  14. #include "commonmacros.h"
  15. #include "utlvector.h"
  16. #include "materialsystem/imaterialsystem.h"
  17. #include "materialsystem/imaterial.h"
  18. //-----------------------------------------------------------------------------
  19. // Interface to automated system for precaching materials
  20. //-----------------------------------------------------------------------------
  21. class IClientEffect
  22. {
  23. public:
  24. virtual void Cache( bool precache = true ) = 0;
  25. };
  26. //-----------------------------------------------------------------------------
  27. // Responsible for managing precaching of particles
  28. //-----------------------------------------------------------------------------
  29. class CClientEffectPrecacheSystem : public IGameSystem
  30. {
  31. public:
  32. virtual char const *Name() { return "CCLientEffectPrecacheSystem"; }
  33. virtual bool IsPerFrame() { return false; }
  34. // constructor, destructor
  35. CClientEffectPrecacheSystem() {}
  36. virtual ~CClientEffectPrecacheSystem() {}
  37. // Init, shutdown
  38. virtual bool Init() { return true; }
  39. virtual void PostInit() {}
  40. virtual void Shutdown();
  41. // Level init, shutdown
  42. virtual void LevelInitPreEntity();
  43. virtual void LevelInitPostEntity() {}
  44. virtual void LevelShutdownPreEntity();
  45. virtual void LevelShutdownPostEntity();
  46. virtual void OnSave() {}
  47. virtual void OnRestore() {}
  48. virtual void SafeRemoveIfDesired() {}
  49. void Register( IClientEffect *effect );
  50. protected:
  51. CUtlVector< IClientEffect * > m_Effects;
  52. };
  53. //Singleton accessor
  54. extern CClientEffectPrecacheSystem *ClientEffectPrecacheSystem();
  55. //-----------------------------------------------------------------------------
  56. // Deals with automated registering and precaching of materials for effects
  57. //-----------------------------------------------------------------------------
  58. class CClientEffect : public IClientEffect
  59. {
  60. public:
  61. CClientEffect( void )
  62. {
  63. //Register with the main effect system
  64. ClientEffectPrecacheSystem()->Register( this );
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Precache a material by artificially incrementing its reference counter
  68. // Input : *materialName - name of the material
  69. // : increment - whether to increment or decrement the reference counter
  70. //-----------------------------------------------------------------------------
  71. inline void ReferenceMaterial( const char *materialName, bool increment = true )
  72. {
  73. IMaterial *material = materials->FindMaterial( materialName, TEXTURE_GROUP_CLIENT_EFFECTS );
  74. if ( !IsErrorMaterial( material ) )
  75. {
  76. if ( increment )
  77. {
  78. material->IncrementReferenceCount();
  79. }
  80. else
  81. {
  82. material->DecrementReferenceCount();
  83. }
  84. }
  85. }
  86. };
  87. //Automatic precache macros
  88. //Beginning
  89. #define CLIENTEFFECT_REGISTER_BEGIN( className ) \
  90. namespace className { \
  91. class ClientEffectRegister : public CClientEffect \
  92. { \
  93. private: \
  94. static const char *m_pszMaterials[]; \
  95. public: \
  96. void Cache( bool precache = true ); \
  97. }; \
  98. const char *ClientEffectRegister::m_pszMaterials[] = {
  99. //Material definitions
  100. #define CLIENTEFFECT_MATERIAL( materialName ) materialName,
  101. //End
  102. #define CLIENTEFFECT_REGISTER_END( ) }; \
  103. void ClientEffectRegister::Cache( bool precache ) \
  104. { \
  105. for ( int i = 0; i < ARRAYSIZE( m_pszMaterials ); i++ ) \
  106. { \
  107. ReferenceMaterial( m_pszMaterials[i], precache ); \
  108. } \
  109. } \
  110. ClientEffectRegister register_ClientEffectRegister; \
  111. }
  112. #define CLIENTEFFECT_REGISTER_END_CONDITIONAL(condition ) }; \
  113. void ClientEffectRegister::Cache( bool precache ) \
  114. { \
  115. if ( condition) \
  116. { \
  117. for ( int i = 0; i < ARRAYSIZE( m_pszMaterials ); i++ ) \
  118. { \
  119. ReferenceMaterial( m_pszMaterials[i], precache ); \
  120. } \
  121. } \
  122. } \
  123. ClientEffectRegister register_ClientEffectRegister; \
  124. }
  125. #endif //CLIENTEFFECTPRECACHESYSTEM_H