forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmaterialdict.h
179 lines (139 loc) · 5.25 KB
/
cmaterialdict.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#ifndef CMATERIALDICT_H
#define CMATERIALDICT_H
#include "tier1/utlsymbol.h"
#include "tier1/utlrbtree.h"
#ifndef MATSYS_INTERNAL
#error "This file is private to the implementation of IMaterialSystem/IMaterialSystemInternal"
#endif
#if defined( _WIN32 )
#pragma once
#endif
//-----------------------------------------------------------------------------
class IMaterial;
class IMaterialInternal;
typedef unsigned short MaterialHandle_t;
//-----------------------------------------------------------------------------
// Dictionary of all known materials
//-----------------------------------------------------------------------------
class CMaterialDict
{
public:
CMaterialDict() :
m_MaterialDict( 0, 256, MaterialLessFunc ),
m_MissingList( 0, 32, MissingMaterialLessFunc )
{
Assert( ThreadInMainThread() );
}
void Shutdown();
int GetNumMaterials( void ) const;
IMaterial* GetMaterial( MaterialHandle_t h ) const;
IMaterialInternal * GetMaterialInternal( MaterialHandle_t idx ) const;
MaterialHandle_t FirstMaterial() const;
MaterialHandle_t NextMaterial( MaterialHandle_t h ) const;
MaterialHandle_t InvalidMaterial() const;
IMaterialInternal * FindMaterial( const char *pszName, bool bManuallyCreated ) const;
void AddMaterialToMaterialList( IMaterialInternal *pMaterial );
void RemoveMaterialFromMaterialList( IMaterialInternal *pMaterial );
void RemoveMaterial( IMaterialInternal *pMaterial );
void RemoveMaterialSubRect( IMaterialInternal *pMaterial );
IMaterialInternal* AddMaterial( const char* pName, const char *pTextureGroupName );
// pKeyValues and pPatchKeyValues should come from LoadVMTFile()
IMaterialInternal* AddMaterialSubRect( const char* pName, const char *pTextureGroupName, KeyValues *pKeyValues, KeyValues *pPatchKeyValues );
bool NoteMissing( const char *pszName );
protected: /*private:*/
void RemoveAllMaterials();
void RemoveAllMaterialsFromMaterialList();
void RemoveMaterialFromMaterialList( MaterialHandle_t h );
// Stores a dictionary of materials, searched by name
struct MaterialLookup_t
{
IMaterialInternal* m_pMaterial;
CUtlSymbol m_Name;
bool m_bManuallyCreated;
};
// Stores a dictionary of missing materials to cut down on redundant warning messages
// TODO: 1) Could add a counter
// 2) Could dump to file/console at exit for exact list of missing materials
struct MissingMaterial_t
{
CUtlSymbol m_Name;
};
static bool MaterialLessFunc( const MaterialLookup_t& src1,
const MaterialLookup_t& src2 );
static bool MissingMaterialLessFunc( const MissingMaterial_t& src1,
const MissingMaterial_t& src2 );
CUtlRBTree< MaterialLookup_t, MaterialHandle_t > m_MaterialDict;
CUtlRBTree< MissingMaterial_t, int > m_MissingList;
};
//-----------------------------------------------------------------------------
// Material iteration methods
//-----------------------------------------------------------------------------
inline MaterialHandle_t CMaterialDict::FirstMaterial() const
{
Assert( ThreadInMainThread() );
return m_MaterialDict.FirstInorder();
}
inline MaterialHandle_t CMaterialDict::NextMaterial( MaterialHandle_t h ) const
{
Assert( ThreadInMainThread() );
return m_MaterialDict.NextInorder(h);
}
inline int CMaterialDict::GetNumMaterials( ) const
{
Assert( ThreadInMainThread() );
return m_MaterialDict.Count();
}
//-----------------------------------------------------------------------------
// Invalid index handle....
//-----------------------------------------------------------------------------
inline MaterialHandle_t CMaterialDict::InvalidMaterial() const
{
Assert( ThreadInMainThread() );
return m_MaterialDict.InvalidIndex();
}
//-----------------------------------------------------------------------------
// Handle to material
//-----------------------------------------------------------------------------
inline IMaterial* CMaterialDict::GetMaterial( MaterialHandle_t idx ) const
{
Assert( ThreadInMainThread() );
return m_MaterialDict[idx].m_pMaterial;
}
inline IMaterialInternal* CMaterialDict::GetMaterialInternal( MaterialHandle_t idx ) const
{
Assert( ThreadInMainThread() );
Assert( (m_MaterialDict[idx].m_pMaterial == NULL) || m_MaterialDict[idx].m_pMaterial->IsRealTimeVersion() );
return m_MaterialDict[idx].m_pMaterial;
}
inline IMaterialInternal* CMaterialDict::FindMaterial( const char *pszName, bool bManuallyCreated ) const
{
Assert( ThreadInMainThread() );
MaterialLookup_t lookup;
lookup.m_Name = pszName;
lookup.m_bManuallyCreated = bManuallyCreated; // This causes the search to find only file-created materials
MaterialHandle_t h = m_MaterialDict.Find( lookup );
if ( h != m_MaterialDict.InvalidIndex() )
{
return m_MaterialDict[h].m_pMaterial;
}
return NULL;
}
inline bool CMaterialDict::NoteMissing( const char *pszName )
{
Assert( ThreadInMainThread() );
MissingMaterial_t missing;
missing.m_Name = pszName;
if ( m_MissingList.Find( missing ) != m_MissingList.InvalidIndex() )
{
return false;
}
m_MissingList.Insert( missing );
return true;
}
//-----------------------------------------------------------------------------
#endif // CMATERIALDICT_H