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.

175 lines
4.2 KiB

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