forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.cpp
1429 lines (1187 loc) · 38.5 KB
/
material.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Implementation of IEditorTexture interface for materials.
//
// Materials are kept in a directory tree containing pairs of VMT
// and VTF files. Each pair of files represents a material.
//
//=============================================================================//
#include "stdafx.h"
#include <process.h>
#include <afxtempl.h>
#include <io.h>
#include <sys\stat.h>
#include <fcntl.h>
#include "hammer.h"
#include "MapDoc.h"
#include "Material.h"
#include "Options.h"
#include "MainFrm.h"
#include "GlobalFunctions.h"
#include "WADTypes.h"
#include "BSPFile.h"
#include "materialsystem/IMaterialSystem.h"
#include "materialsystem/IMaterialSystemHardwareConfig.h"
#include "materialsystem/MaterialSystem_Config.h"
#include "materialsystem/MaterialSystemUtil.h"
#include "materialsystem/ITexture.h"
#include "materialsystem/IMaterial.h"
#include "bitmap/imageformat.h" // hack : don't want to include this just for ImageFormat
#include "FileSystem.h"
#include "StudioModel.h"
#include "tier1/strtools.h"
#include "tier0/dbg.h"
#include "TextureSystem.h"
#include "materialproxyfactory_wc.h"
#include "vstdlib/cvar.h"
#include "interface.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#pragma warning(disable:4244)
#define _GraphicCacheAllocate(n) malloc(n)
MaterialSystem_Config_t g_materialSystemConfig;
static MaterialHandle_t g_CurrMaterial;
extern void ScaleBitmap(CSize sizeSrc, CSize sizeDest, char *src, char *dest);
struct MaterialCacheEntry_t
{
char szName[MAX_PATH]; //
CMaterial *pMaterial; //
int nRefCount; //
};
//-----------------------------------------------------------------------------
// Purpose:
// This class speeds up the call to IMaterial::GetPreviewImageProperties because
// we call it thousands of times per level load when there are detail props.
//-----------------------------------------------------------------------------
class CPreviewImagePropertiesCache
{
public:
//-----------------------------------------------------------------------------
// Purpose: Anyone can call this instead of IMaterial::GetPreviewImageProperties
// and it'll be a lot faster if there are redundant calls to it.
//-----------------------------------------------------------------------------
static PreviewImageRetVal_t GetPreviewImageProperties( IMaterial *pMaterial, int *width, int *height, ImageFormat *imageFormat, bool* isTranslucent )
{
int i = s_PreviewImagePropertiesCache.Find( pMaterial );
if ( i == s_PreviewImagePropertiesCache.InvalidIndex() )
{
// Add an entry to the cache.
CPreviewImagePropertiesCache::CEntry entry;
entry.m_RetVal = pMaterial->GetPreviewImageProperties( &entry.m_Width, &entry.m_Height, &entry.m_ImageFormat, &entry.m_bIsTranslucent );
i = s_PreviewImagePropertiesCache.Insert( pMaterial, entry );
}
CPreviewImagePropertiesCache::CEntry &entry = s_PreviewImagePropertiesCache[i];
*width = entry.m_Width;
*height = entry.m_Height;
*imageFormat = entry.m_ImageFormat;
*isTranslucent = entry.m_bIsTranslucent;
return entry.m_RetVal;
}
private:
class CEntry
{
public:
int m_Width;
int m_Height;
ImageFormat m_ImageFormat;
bool m_bIsTranslucent;
PreviewImageRetVal_t m_RetVal;
};
static bool PreviewImageLessFunc( IMaterial* const &a, IMaterial* const &b )
{
return a < b;
}
static CUtlMap<IMaterial*, CPreviewImagePropertiesCache::CEntry> s_PreviewImagePropertiesCache;
};
CUtlMap<IMaterial*, CPreviewImagePropertiesCache::CEntry> CPreviewImagePropertiesCache::s_PreviewImagePropertiesCache( 64, 64, &CPreviewImagePropertiesCache::PreviewImageLessFunc );
//-----------------------------------------------------------------------------
// Purpose: stuff for caching textures in memory.
//-----------------------------------------------------------------------------
class CMaterialImageCache
{
public:
CMaterialImageCache(int maxNumGraphicsLoaded);
~CMaterialImageCache(void);
void EnCache( CMaterial *pMaterial );
protected:
CMaterial **pool;
int cacheSize;
int currentID; // next one to get killed.
};
//-----------------------------------------------------------------------------
// Purpose: Constructor. Allocates a pool of material pointers.
// Input : maxNumGraphicsLoaded -
//-----------------------------------------------------------------------------
CMaterialImageCache::CMaterialImageCache(int maxNumGraphicsLoaded)
{
cacheSize = maxNumGraphicsLoaded;
pool = new CMaterialPtr[cacheSize];
if (pool != NULL)
{
memset(pool, 0, sizeof(CMaterialPtr) * cacheSize);
}
currentID = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Frees the pool memory.
//-----------------------------------------------------------------------------
CMaterialImageCache::~CMaterialImageCache(void)
{
if (pool != NULL)
{
delete [] pool;
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pMaterial -
//-----------------------------------------------------------------------------
void CMaterialImageCache::EnCache( CMaterial *pMaterial )
{
if (pMaterial->m_pData != NULL)
{
// Already cached.
return;
}
// kill currentID
if ((pool[currentID]) && (pool[currentID]->HasData()))
{
pool[currentID]->FreeData();
}
pool[currentID] = pMaterial;
pMaterial->LoadMaterialImage();
currentID = ( currentID + 1 ) % cacheSize;
#if 0
OutputDebugString( "CMaterialCache::Encache: " );
OutputDebugString( pMaterial->m_szName );
OutputDebugString( "\n" );
#endif
}
static CMaterialImageCache *g_pMaterialImageCache = NULL;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMaterialCache::CMaterialCache(void)
{
m_pCache = NULL;
m_nMaxEntries = 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMaterialCache::~CMaterialCache(void)
{
if (m_pCache != NULL)
{
delete m_pCache;
}
}
//-----------------------------------------------------------------------------
// Purpose: Allocates cache memory for a given number of materials.
// Input : nMaxEntries - Maximum number of materials in the cache.
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CMaterialCache::Create(int nMaxEntries)
{
Assert((m_pCache == NULL) && (m_nMaxEntries == 0));
if (m_pCache != NULL)
{
delete m_pCache;
m_pCache = NULL;
m_nMaxEntries = 0;
}
if (nMaxEntries <= 0)
{
nMaxEntries = 500;
}
m_pCache = new MaterialCacheEntry_t[nMaxEntries];
if (m_pCache != NULL)
{
memset(m_pCache, 0, sizeof(m_pCache[0]) * nMaxEntries);
m_nMaxEntries = nMaxEntries;
}
return(m_pCache != NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Factory. Creates a material by name, first looking in the cache.
// Input : pszMaterialName - Name of material, ie "brick/brickfloor01".
// Output : Returns a pointer to the new material object, NULL if the given
// material did not exist.
//-----------------------------------------------------------------------------
CMaterial *CMaterialCache::CreateMaterial(const char *pszMaterialName)
{
CMaterial *pMaterial = NULL;
if (pszMaterialName != NULL)
{
//
// Find this material in the cache. If it is here, return it.
//
pMaterial = FindMaterial(pszMaterialName);
if (pMaterial == NULL)
{
//
// Not found in the cache, try to create it.
//
pMaterial = CMaterial::CreateMaterial(pszMaterialName, true);
if (pMaterial != NULL)
{
//
// Success. Add the newly created material to the cache.
//
AddMaterial(pMaterial);
return(pMaterial);
}
}
else
{
//
// Found in the cache, bump the reference count.
//
AddRef(pMaterial);
}
}
return(pMaterial);
}
//-----------------------------------------------------------------------------
// Purpose: Finds a material in the cache.
// Input : char *pszMaterialName -
// Output : CMaterial
//-----------------------------------------------------------------------------
void CMaterialCache::AddMaterial(CMaterial *pMaterial)
{
if (pMaterial != NULL)
{
Assert(m_nEntries < m_nMaxEntries);
if (m_nEntries < m_nMaxEntries)
{
m_pCache[m_nEntries].pMaterial = pMaterial;
m_pCache[m_nEntries].nRefCount = 1;
m_nEntries++;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Increments the reference count on a material in the cache. Called by
// client code when a pointer to the model is copied, making that
// reference independent.
// Input : pModel - Model for which to increment the reference count.
//-----------------------------------------------------------------------------
void CMaterialCache::AddRef(CMaterial *pMaterial)
{
for (int i = 0; i < m_nEntries; i++)
{
if (m_pCache[i].pMaterial == pMaterial)
{
m_pCache[i].nRefCount++;
return;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Finds a material in the cache by name.
// Input : char *pszMaterialName -
// Output : CMaterial
//-----------------------------------------------------------------------------
CMaterial *CMaterialCache::FindMaterial(const char *pszMaterialName)
{
if (pszMaterialName != NULL)
{
for (int i = 0; i < m_nEntries; i++)
{
if (!stricmp(m_pCache[i].pMaterial->GetName(), pszMaterialName))
{
return(m_pCache[i].pMaterial);
}
}
}
return(NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Decrements the reference count of a material, deleting it and
// removing it from the cache if its reference count becomes zero.
// Input : pMaterial - Material to release.
//-----------------------------------------------------------------------------
void CMaterialCache::Release(CMaterial *pMaterial)
{
if (pMaterial != NULL)
{
for (int i = 0; i < m_nEntries; i++)
{
if (m_pCache[i].pMaterial == pMaterial)
{
m_pCache[i].nRefCount--;
if (m_pCache[i].nRefCount == 0)
{
delete m_pCache[i].pMaterial;
m_nEntries--;
m_pCache[i] = m_pCache[m_nEntries];
memset(&m_pCache[m_nEntries], 0, sizeof(m_pCache[0]));
}
break;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Constructor. Initializes data members.
//-----------------------------------------------------------------------------
CMaterial::CMaterial(void)
{
memset(m_szName, 0, sizeof(m_szName));
memset(m_szFileName, 0, sizeof(m_szFileName));
memset(m_szKeywords, 0, sizeof(m_szKeywords));
m_nPreviewImageWidth = 0;
m_nPreviewImageHeight = 0;
m_nTextureID = 0;
m_pData = NULL;
m_bLoaded = false;
m_pMaterial = NULL;
m_TranslucentBaseTexture = false;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Frees texture image data and palette.
//-----------------------------------------------------------------------------
CMaterial::~CMaterial(void)
{
//
// Free image data.
//
if (m_pData != NULL)
{
free(m_pData);
m_pData = NULL;
}
/* FIXME: Texture manager shuts down after the material system
if (m_pMaterial)
{
m_pMaterial->DecrementReferenceCount();
m_pMaterial = NULL;
}
*/
}
#define MATERIAL_PREFIX_LEN 10
//-----------------------------------------------------------------------------
// Finds all .VMT files in a particular directory
//-----------------------------------------------------------------------------
bool CMaterial::LoadMaterialsInDirectory( char const* pDirectoryName, int nDirectoryNameLen,
IMaterialEnumerator *pEnum, int nContext, int nFlags )
{
//Assert( Q_strnicmp( pDirectoryName, "materials", 9 ) == 0 );
char *pWildCard;
pWildCard = ( char * )stackalloc( nDirectoryNameLen + 7 );
Q_snprintf( pWildCard, nDirectoryNameLen + 7, "%s/*.vmt", pDirectoryName );
if ( !g_pFileSystem )
{
return false;
}
FileFindHandle_t findHandle;
const char *pFileName = g_pFullFileSystem->FindFirstEx( pWildCard, "GAME", &findHandle );
while( pFileName )
{
if (IsIgnoredMaterial(pFileName))
{
pFileName = g_pFullFileSystem->FindNext( findHandle );
continue;
}
if( !g_pFullFileSystem->FindIsDirectory( findHandle ) )
{
// Strip off the 'materials/' part of the material name.
char *pFileNameWithPath;
int nAllocSize = nDirectoryNameLen + Q_strlen(pFileName) + 2;
pFileNameWithPath = (char *)stackalloc( nAllocSize );
Q_snprintf( pFileNameWithPath, nAllocSize, "%s/%s", &pDirectoryName[MATERIAL_PREFIX_LEN], pFileName );
Q_strnlwr( pFileNameWithPath, nAllocSize );
// Strip off the extension...
char *pExt = Q_strrchr( pFileNameWithPath, '.');
if (pExt)
*pExt = 0;
if (!pEnum->EnumMaterial( pFileNameWithPath, nContext ))
{
return false;
}
}
pFileName = g_pFullFileSystem->FindNext( findHandle );
}
g_pFullFileSystem->FindClose( findHandle );
return true;
}
//-----------------------------------------------------------------------------
// Discovers all .VMT files lying under a particular directory
// It only finds their names so we can generate shell materials for them
// that we can load up at a later time
//-----------------------------------------------------------------------------
bool CMaterial::InitDirectoryRecursive( char const* pDirectoryName,
IMaterialEnumerator *pEnum, int nContext, int nFlags )
{
// Make sure this is an ok directory, otherwise don't bother
if (ShouldSkipMaterial( pDirectoryName + MATERIAL_PREFIX_LEN, nFlags ))
return true;
// Compute directory name length
int nDirectoryNameLen = Q_strlen( pDirectoryName );
if (!LoadMaterialsInDirectory( pDirectoryName, nDirectoryNameLen, pEnum, nContext, nFlags ))
return false;
char *pWildCard = ( char * )stackalloc( nDirectoryNameLen + 5 );
strcpy(pWildCard, pDirectoryName);
strcat(pWildCard, "/*.*");
int nPathStrLen = nDirectoryNameLen + 1;
FileFindHandle_t findHandle;
const char *pFileName = g_pFullFileSystem->FindFirstEx( pWildCard, "GAME", &findHandle );
while( pFileName )
{
if (!IsIgnoredMaterial(pFileName))
{
if ((pFileName[0] != '.') || (pFileName[1] != '.' && pFileName[1] != 0))
{
if( g_pFullFileSystem->FindIsDirectory( findHandle ) )
{
int fileNameStrLen = Q_strlen( pFileName );
char *pFileNameWithPath = ( char * )stackalloc( nPathStrLen + fileNameStrLen + 1 );
memcpy( pFileNameWithPath, pWildCard, nPathStrLen );
pFileNameWithPath[nPathStrLen] = '\0';
Q_strncat( pFileNameWithPath, pFileName, nPathStrLen + fileNameStrLen + 1 );
if (!InitDirectoryRecursive( pFileNameWithPath, pEnum, nContext, nFlags ))
return false;
}
}
}
pFileName = g_pFullFileSystem->FindNext( findHandle );
}
return true;
}
//-----------------------------------------------------------------------------
// Discovers all .VMT files lying under a particular directory
// It only finds their names so we can generate shell materials for them
// that we can load up at a later time
//-----------------------------------------------------------------------------
void CMaterial::EnumerateMaterials( IMaterialEnumerator *pEnum, const char *szRoot, int nContext, int nFlags )
{
InitDirectoryRecursive( szRoot, pEnum, nContext, nFlags );
}
//-----------------------------------------------------------------------------
// Purpose: Called from GetFirst/GetNextMaterialName to skip unwanted materials.
// Input : pszName - Name of material to evaluate.
// nFlags - One or more of the following:
// INCLUDE_ALL_MATERIALS
// INCLUDE_WORLD_MATERIALS
// INCLUDE_MODEL_MATERIALS
// Output : Returns true to skip, false to not skip this material.
//-----------------------------------------------------------------------------
bool CMaterial::ShouldSkipMaterial(const char *pszName, int nFlags)
{
static char szStrippedName[MAX_PATH];
// if NULL skip it
if( !pszName )
return true;
//
// check against the list of user-defined exclusion directories
//
for( int i = 0; i < g_pGameConfig->m_MaterialExcludeCount; i++ )
{
// This will guarantee the match is at the start of the string
const char *pMatchFound = Q_stristr( pszName, g_pGameConfig->m_MaterialExclusions[i].szDirectory );
if( pMatchFound == pszName )
return true;
}
// also check against any FGD-defined exclusions
if (pGD != NULL)
{
for( int i = 0; i < pGD->m_FGDMaterialExclusions.Count(); i++ )
{
const char *pMatchFound = Q_stristr( pszName, pGD->m_FGDMaterialExclusions[i].szDirectory );
if( pMatchFound == pszName )
return true;
}
}
return false;
#if 0
bool bSkip = false;
if (pszName != NULL)
{
if (!(nFlags & INCLUDE_MODEL_MATERIALS))
{
if (_strnicmp(pszName, "models/", 7) == 0)
{
bSkip = true;
}
}
if (!(nFlags & INCLUDE_WORLD_MATERIALS))
{
if (_strnicmp(pszName, "models/", 7) != 0)
{
bSkip = true;
}
}
}
else
{
bSkip = true;
}
return(bSkip);
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Factory. Creates a material by name.
// Input : pszMaterialName - Name of material, ie "brick/brickfloor01".
// Output : Returns a pointer to the new material object, NULL if the given
// material did not exist.
//-----------------------------------------------------------------------------
CMaterial *CMaterial::CreateMaterial(const char *pszMaterialName, bool bLoadImmediately, bool* pFound)
{
Assert (pszMaterialName);
CMaterial *pMaterial = new CMaterial;
Assert( pMaterial );
// Store off the material name so we can load it later if we need to
Q_snprintf( pMaterial->m_szFileName, MAX_PATH, pszMaterialName );
Q_snprintf( pMaterial->m_szName, MAX_PATH, pszMaterialName );
//
// Find the material by name and load it.
//
if (bLoadImmediately)
{
bool bFound = pMaterial->LoadMaterial();
// Returns if the material was found or not
if (pFound)
*pFound = bFound;
}
return pMaterial;
}
bool CMaterial::IsIgnoredMaterial( const char *pName )
{
//TODO: make this a customizable user option?
if ( !Q_strnicmp(pName, ".svn", 4) || strstr (pName, ".svn") ||
!Q_strnicmp(pName, "models", 6) || strstr (pName, "models") )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Will actually load the material bits
// We don't want to load them all at once because it takes way too long
//-----------------------------------------------------------------------------
bool CMaterial::LoadMaterial()
{
bool bFound = true;
if (!m_bLoaded)
{
if (IsIgnoredMaterial(m_szFileName))
{
return false;
}
m_bLoaded = true;
IMaterial *pMat = materials->FindMaterial(m_szFileName, TEXTURE_GROUP_OTHER);
if ( IsErrorMaterial( pMat ) )
bFound = false;
Assert( pMat );
if (!pMat)
{
return false;
}
if (!LoadMaterialHeader(pMat))
{
// dvs: yeaaaaaaaaah, we're gonna disable this until the spew can be reduced
//Msg( mwError,"Load material header failed: %s", m_szFileName );
bFound = false;
pMat = materials->FindMaterial("debug/debugempty", TEXTURE_GROUP_OTHER);
if (pMat)
{
LoadMaterialHeader(pMat);
}
}
}
return bFound;
}
//-----------------------------------------------------------------------------
// Reloads owing to a material change
//-----------------------------------------------------------------------------
void CMaterial::Reload( bool bFullReload )
{
// Don't bother if we're not loaded yet
if (!m_bLoaded)
return;
FreeData();
if ( m_pMaterial )
{
m_pMaterial->DecrementReferenceCount();
}
m_pMaterial = materials->FindMaterial(m_szFileName, TEXTURE_GROUP_OTHER);
Assert( m_pMaterial );
if ( bFullReload )
m_pMaterial->Refresh();
PreviewImageRetVal_t retVal;
bool translucentBaseTexture;
ImageFormat eImageFormat;
int width, height;
retVal = m_pMaterial->GetPreviewImageProperties(&width, &height, &eImageFormat, &translucentBaseTexture);
if (retVal == MATERIAL_PREVIEW_IMAGE_BAD)
return;
m_nPreviewImageWidth = width;
m_nPreviewImageHeight = height;
m_TranslucentBaseTexture = translucentBaseTexture;
// Find the keywords for this material from the vmt file.
bool bFound;
IMaterialVar *pVar = m_pMaterial->FindVar("%keywords", &bFound, false);
if (bFound)
{
strcpy(m_szKeywords, pVar->GetStringValue());
// Register the keywords
g_Textures.RegisterTextureKeywords( this );
}
}
//-----------------------------------------------------------------------------
// Returns the material
//-----------------------------------------------------------------------------
IMaterial* CMaterial::GetMaterial( bool bForceLoad )
{
if ( bForceLoad )
LoadMaterial();
return m_pMaterial;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMaterial::DrawIcon( CDC *pDC, CMaterial* pIcon, RECT& dstRect )
{
if (!pIcon)
return;
g_pMaterialImageCache->EnCache(pIcon);
RECT rect, dst;
rect.left = 0; rect.right = pIcon->GetWidth();
// FIXME: Workaround the fact that materials must be power of 2, I want 12 bite
rect.top = 2; rect.bottom = pIcon->GetHeight() - 2;
dst = dstRect;
float dstHeight = dstRect.bottom - dstRect.top;
float srcAspect = (float)(rect.right - rect.left) / (float)(rect.bottom - rect.top);
dst.right = dst.left + (dstHeight * srcAspect);
pIcon->DrawBitmap( pDC, rect, dst );
dstRect.left += dst.right - dst.left;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pDC -
// dstRect -
// detectErrors -
//-----------------------------------------------------------------------------
void CMaterial::DrawBrowserIcons( CDC *pDC, RECT& dstRect, bool detectErrors )
{
static CMaterial* pTranslucentIcon = 0;
static CMaterial* pOpaqueIcon = 0;
static CMaterial* pSelfIllumIcon = 0;
static CMaterial* pBaseAlphaEnvMapMaskIcon = 0;
static CMaterial* pErrorIcon = 0;
if (!pTranslucentIcon)
{
pTranslucentIcon = CreateMaterial("editor/translucenticon", true);
pOpaqueIcon = CreateMaterial("editor/opaqueicon", true);
pSelfIllumIcon = CreateMaterial("editor/selfillumicon", true);
pBaseAlphaEnvMapMaskIcon = CreateMaterial("editor/basealphaenvmapmaskicon", true);
pErrorIcon = CreateMaterial("editor/erroricon", true);
Assert( pTranslucentIcon && pOpaqueIcon && pSelfIllumIcon && pBaseAlphaEnvMapMaskIcon && pErrorIcon );
}
bool error = false;
IMaterial* pMaterial = GetMaterial();
if ( pMaterial->GetMaterialVarFlag( MATERIAL_VAR_TRANSLUCENT ) )
{
DrawIcon( pDC, pTranslucentIcon, dstRect );
if (detectErrors)
{
error = error || !m_TranslucentBaseTexture;
}
}
else
{
DrawIcon( pDC, pOpaqueIcon, dstRect );
}
if ( pMaterial->GetMaterialVarFlag( MATERIAL_VAR_SELFILLUM ))
{
DrawIcon( pDC, pSelfIllumIcon, dstRect );
if (detectErrors)
{
error = error || !m_TranslucentBaseTexture;
}
}
if ( pMaterial->GetMaterialVarFlag( MATERIAL_VAR_BASEALPHAENVMAPMASK ))
{
DrawIcon( pDC, pBaseAlphaEnvMapMaskIcon, dstRect );
if (detectErrors)
{
error = error || !m_TranslucentBaseTexture;
}
}
if (error)
{
DrawIcon( pDC, pErrorIcon, dstRect );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pDC -
// srcRect -
// dstRect -
//-----------------------------------------------------------------------------
void CMaterial::DrawBitmap( CDC *pDC, RECT& srcRect, RECT& dstRect )
{
static struct
{
BITMAPINFOHEADER bmih;
unsigned short colorindex[256];
} bmi;
int srcWidth = srcRect.right - srcRect.left;
int srcHeight = srcRect.bottom - srcRect.top;
BITMAPINFOHEADER &bmih = bmi.bmih;
memset(&bmih, 0, sizeof(bmih));
bmih.biSize = sizeof(bmih);
bmih.biWidth = srcWidth;
bmih.biHeight = -srcHeight;
bmih.biCompression = BI_RGB;
bmih.biBitCount = 24;
bmih.biPlanes = 1;
static BOOL bInit = false;
if (!bInit)
{
bInit = true;
for (int i = 0; i < 256; i++)
{
bmi.colorindex[i] = i;
}
}
int dest_width = dstRect.right - dstRect.left;
int dest_height = dstRect.bottom - dstRect.top;
// ** bits **
SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
if (StretchDIBits(pDC->m_hDC, dstRect.left, dstRect.top, dest_width, dest_height,
srcRect.left, -srcRect.top, srcWidth, srcHeight, m_pData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS, SRCCOPY) == GDI_ERROR)
{
Msg(mwError, "CMaterial::Draw(): StretchDIBits failed.");
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pDC -
// rect -
// iFontHeight -
// dwFlags -
//-----------------------------------------------------------------------------
void CMaterial::Draw(CDC *pDC, RECT& rect, int iFontHeight, int iIconHeight, DrawTexData_t &DrawTexData)//, BrowserData_t *pBrowserData)
{
g_pMaterialImageCache->EnCache(this);
if (!this->HasData())
{
return;
}
if (m_nPreviewImageWidth <= 0)
{
NoData:
// draw "no data"
CFont *pOldFont = (CFont*) pDC->SelectStockObject(ANSI_VAR_FONT);
COLORREF cr = pDC->SetTextColor(RGB(0xff, 0xff, 0xff));
COLORREF cr2 = pDC->SetBkColor(RGB(0, 0, 0));
// draw black rect first
pDC->FillRect(&rect, CBrush::FromHandle(HBRUSH(GetStockObject(BLACK_BRUSH))));
// then text
pDC->TextOut(rect.left+2, rect.top+2, "No Image", 8);
pDC->SelectObject(pOldFont);
pDC->SetTextColor(cr);
pDC->SetBkColor(cr2);
return;
}
// no data -
if (!m_pData)
{
// try to load -
if (!Load())
{
// can't load -
goto NoData;
}
}
// Draw the material image
RECT srcRect, dstRect;
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = m_nPreviewImageWidth;
srcRect.bottom = m_nPreviewImageHeight;
dstRect = rect;
if (DrawTexData.nFlags & drawCaption)
{
dstRect.bottom -= iFontHeight + 4;
}
if (DrawTexData.nFlags & drawIcons)
{
dstRect.bottom -= iIconHeight;
}
if (!(DrawTexData.nFlags & drawResizeAlways))
{
if (m_nPreviewImageWidth < dstRect.right - dstRect.left )
{
dstRect.right = dstRect.left + m_nPreviewImageWidth;
}
if (m_nPreviewImageHeight < dstRect.bottom - dstRect.top )
{
dstRect.bottom = dstRect.top + m_nPreviewImageHeight;
}
}
DrawBitmap( pDC, srcRect, dstRect );
// Draw the icons
if (DrawTexData.nFlags & drawIcons)
{
dstRect = rect;
if (DrawTexData.nFlags & drawCaption)
{
dstRect.bottom -= iFontHeight + 5;
}
dstRect.top = dstRect.bottom - iIconHeight;
DrawBrowserIcons(pDC, dstRect, (DrawTexData.nFlags & drawErrors) != 0 );
}
// ** caption **
if (DrawTexData.nFlags & drawCaption)
{
// draw background for name
CBrush brCaption(RGB(0, 0, 255));