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.

183 lines
4.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // Functionality common to wad and decal code in gl_draw.c and draw.c
  6. //
  7. //===========================================================================//
  8. #include "render_pch.h"
  9. #include "decal.h"
  10. #include "decal_private.h"
  11. #include "zone.h"
  12. #include "sys.h"
  13. #include "gl_matsysiface.h"
  14. #include "filesystem.h"
  15. #include "filesystem_engine.h"
  16. #include "materialsystem/imaterial.h"
  17. #include "utldict.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. struct DecalEntry
  21. {
  22. #ifdef _DEBUG
  23. char *m_pDebugName; // only used in debug builds
  24. #endif
  25. IMaterial *material;
  26. int index;
  27. };
  28. // This stores the list of all decals
  29. CUtlMap< FileNameHandle_t, DecalEntry > g_DecalDictionary( 0, 0, DefLessFunc( FileNameHandle_t ) );
  30. // This is a list of indices into the dictionary.
  31. // This list is indexed by network id, so it maps network ids to decal dictionary entries
  32. CUtlVector< int > g_DecalLookup;
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. // Output : int
  36. //-----------------------------------------------------------------------------
  37. int Draw_DecalMax( void )
  38. {
  39. return g_nMaxDecals;
  40. }
  41. static bool BIsPlayerLogoDecal( int index )
  42. {
  43. return ( ( ( index >> 24 ) & 0x7F ) != 0 );
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose:
  47. // Sets the name of the bitmap from decals.wad to be used in a specific slot #
  48. // called from cl_parse.cpp twice
  49. // This sets the name of a decal prototype texture
  50. // Input : decal -
  51. // *name -
  52. //-----------------------------------------------------------------------------
  53. // called from gl_rsurf.cpp
  54. IMaterial *Draw_DecalMaterial( int index )
  55. {
  56. if ( BIsPlayerLogoDecal( index ) )
  57. return materials->FindMaterial( "decals/playerlogo01", TEXTURE_GROUP_DECAL, true );
  58. if ( index < 0 || index >= g_DecalLookup.Count() )
  59. return NULL;
  60. int slot = g_DecalLookup[index];
  61. if ( slot < 0 || slot >= (int)g_DecalDictionary.MaxElement() )
  62. return NULL;
  63. DecalEntry * entry = &g_DecalDictionary[slot];
  64. if ( entry )
  65. {
  66. return entry->material;
  67. }
  68. else
  69. {
  70. return NULL;
  71. }
  72. }
  73. #ifndef DEDICATED
  74. void Draw_DecalSetName( int decal, char *name )
  75. {
  76. while ( decal >= g_DecalLookup.Count() )
  77. {
  78. MEM_ALLOC_CREDIT();
  79. int idx = g_DecalLookup.AddToTail();
  80. g_DecalLookup[idx] = g_DecalDictionary.InvalidIndex();
  81. }
  82. FileNameHandle_t fnHandle = g_pFileSystem->FindOrAddFileName( name );
  83. int lookup = g_DecalDictionary.Find( fnHandle );
  84. if ( lookup == g_DecalDictionary.InvalidIndex() )
  85. {
  86. DecalEntry entry;
  87. #ifdef _DEBUG
  88. int len = strlen(name) + 1;
  89. entry.m_pDebugName = new char[len];
  90. memcpy( entry.m_pDebugName, name, len );
  91. #endif
  92. // fully precache the decal
  93. entry.material = GL_LoadMaterial( name, TEXTURE_GROUP_DECAL, true );
  94. entry.index = decal;
  95. lookup = g_DecalDictionary.Insert( fnHandle, entry );
  96. }
  97. else
  98. {
  99. g_DecalDictionary[lookup].index = decal;
  100. }
  101. g_DecalLookup[decal] = lookup;
  102. }
  103. // called from cl_parse.cpp
  104. // find the server side decal id given it's name.
  105. // used for save/restore
  106. int Draw_DecalIndexFromName( char *name, bool *found )
  107. {
  108. Assert( found );
  109. FileNameHandle_t fnHandle = g_pFileSystem->FindOrAddFileName( name );
  110. int lookup = g_DecalDictionary.Find( fnHandle );
  111. if ( lookup == g_DecalDictionary.InvalidIndex() )
  112. {
  113. if ( found )
  114. {
  115. *found = false;
  116. }
  117. return 0;
  118. }
  119. if ( found )
  120. {
  121. *found = true;
  122. }
  123. return g_DecalDictionary[lookup].index;
  124. }
  125. #endif
  126. const char *Draw_DecalNameFromIndex( int index )
  127. {
  128. #if !defined(DEDICATED)
  129. return g_DecalDictionary[index].material ? g_DecalDictionary[index].material->GetName() : "";
  130. #else
  131. return "";
  132. #endif
  133. }
  134. // This is called to reset all loaded decals
  135. // called from cl_parse.cpp and host.cpp
  136. void Decal_Init( void )
  137. {
  138. Decal_Shutdown();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void Decal_Shutdown( void )
  144. {
  145. for ( int index = g_DecalDictionary.FirstInorder(); index != g_DecalDictionary.InvalidIndex(); index = g_DecalDictionary.NextInorder(index) )
  146. {
  147. IMaterial *mat = g_DecalDictionary[index].material;
  148. if ( mat )
  149. {
  150. GL_UnloadMaterial( mat );
  151. }
  152. #ifdef _DEBUG
  153. delete[] g_DecalDictionary[index].m_pDebugName;
  154. #endif
  155. }
  156. g_DecalLookup.Purge();
  157. g_DecalDictionary.RemoveAll();
  158. }