summaryrefslogtreecommitdiff
path: root/hammer/texturesystem.cpp
blob: 4c849e1b305c4447a311387061c9de4bc3b2e321 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Texture management functions. Exposes a list of available textures,
//			texture groups, and Most Recently Used textures.
//
//			There is one texture context per game configuration in GameCfg.ini.
//			
//=============================================================================//

#include "stdafx.h"
#include <process.h>
#include <io.h>
#include <sys\stat.h>
#include <fcntl.h>
#include "DummyTexture.h"		// Specific IEditorTexture implementation
#include "GlobalFunctions.h"
#include "MainFrm.h"
#include "MapDoc.h"
#include "Material.h"			// Specific IEditorTexture implementation
#include "Options.h"
#include "TextureSystem.h"
#include "WADTexture.h"			// Specific IEditorTexture implementation
#include "WADTypes.h"
#include "hammer.h"
#include "filesystem.h"
#include "materialsystem/itexture.h"
#include "tier1/utldict.h"
#include "FaceEditSheet.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)
#define IsSortChr(ch) ((ch == '-') || (ch == '+'))


//-----------------------------------------------------------------------------
// Stuff for loading WAD3 files.
//-----------------------------------------------------------------------------
typedef struct
{
	int			filepos;
	int			disksize;
	int			size;					// uncompressed
	char		type;
	char		compression;
	char		pad1, pad2;
	char		name[16];				// must be null terminated
} WAD3lumpinfo_t;


	
//-----------------------------------------------------------------------------
// List of global graphics
//-----------------------------------------------------------------------------
CTextureSystem g_Textures;



//-----------------------------------------------------------------------------
// CMaterialFileChangeWatcher implementation.
//-----------------------------------------------------------------------------
void CMaterialFileChangeWatcher::Init( CTextureSystem *pSystem, int context )
{
	m_pTextureSystem = pSystem;
	m_Context = context;

	m_Watcher.Init( this );
	
	char searchPaths[1024 * 16];
	if ( g_pFullFileSystem->GetSearchPath( "GAME", false, searchPaths, sizeof( searchPaths ) ) > 0 )
	{
		CUtlVector<char*> searchPathList;
		V_SplitString( searchPaths, ";", searchPathList );

		for ( int i=0; i < searchPathList.Count(); i++ )
		{
			m_Watcher.AddDirectory( searchPathList[i], "materials", true );
		}
		
		searchPathList.PurgeAndDeleteElements();
	}
	else
	{
		Warning( "Error in GetSearchPath. Dynamic material list updating will not be available." );
	}
}

void CMaterialFileChangeWatcher::OnFileChange( const char *pRelativeFilename, const char *pFullFilename )
{
	//Msg( "OnNewFile: %s\n", pRelativeFilename );

	CTextureSystem::EFileType eFileType;
	if ( CTextureSystem::GetFileTypeFromFilename( pRelativeFilename, &eFileType ) )
		m_pTextureSystem->OnFileChange( pRelativeFilename, m_Context, eFileType );
}

void CMaterialFileChangeWatcher::Update()
{
	m_Watcher.Update();
}


//-----------------------------------------------------------------------------
// Purpose: Constructor. Creates the "All" group and sets it as the active group.
//-----------------------------------------------------------------------------
CTextureSystem::CTextureSystem(void)
{
	m_pLastTex = NULL;
	m_nLastIndex = 0;
	m_pActiveContext = NULL;
	m_pActiveGroup = NULL;
	m_pCubemapTexture = NULL;
	m_pNoDrawTexture = NULL;
}


//-----------------------------------------------------------------------------
// Purpose: Destructor. Frees the list of groups and dummy textures.
//-----------------------------------------------------------------------------
CTextureSystem::~CTextureSystem(void)
{
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTextureSystem::FreeAllTextures()
{
	if ( m_pCubemapTexture )
	{
	 	m_pCubemapTexture->DecrementReferenceCount();
		m_pCubemapTexture = NULL;
	}

	int nContextCount = m_TextureContexts.Count();
	for (int nContext = 0; nContext < nContextCount; nContext++)
	{
		TextureContext_t *pContext = &m_TextureContexts.Element(nContext);

		//
		// Delete all the texture groups for this context.
		//
		int nGroupCount = pContext->Groups.Count();
		for (int nGroup = 0; nGroup < nGroupCount; nGroup++)
		{
			delete pContext->Groups.Element(nGroup);
		}

		//
		// Delete dummy textures.
		//
		int nDummyCount = pContext->Dummies.Count();
		for (int nDummy = 0; nDummy < nDummyCount; nDummy++)
		{
			IEditorTexture *pTex = pContext->Dummies.Element(nDummy);
			delete pTex;
		}
	}

	//
	// Delete all the textures from the master list.
	//
	for (int i = 0; i < m_Textures.Count(); i++)
	{
		IEditorTexture *pTex = m_Textures[i];
		delete pTex;
	}
	m_Textures.RemoveAll();

	m_pLastTex = NULL;
	m_nLastIndex = -1;


	// Delete the keywords.
	m_Keywords.PurgeAndDeleteElements();
	m_ChangeWatchers.PurgeAndDeleteElements();
}


//-----------------------------------------------------------------------------
// Purpose: Adds a texture to the master list of textures.
// Input  : pTexture - Pointer to texture to add.
// Output : Returns the index of the texture in the master texture list.
//-----------------------------------------------------------------------------
int CTextureSystem::AddTexture(IEditorTexture *pTexture)
{
	return m_Textures.AddToTail(pTexture);
}


//-----------------------------------------------------------------------------
// Purpose: Begins iterating the list of texture/material keywords.
//-----------------------------------------------------------------------------
int CTextureSystem::GetNumKeywords(void)
{
	return(m_Keywords.Count());
}


//-----------------------------------------------------------------------------
// Purpose: Continues iterating the list of texture/material keywords.
//-----------------------------------------------------------------------------
const char *CTextureSystem::GetKeyword(int pos)
{
	return(m_Keywords.Element(pos));
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *piIndex - 
//			bUseMRU - 
// Output : 
//-----------------------------------------------------------------------------
IEditorTexture *CTextureSystem::EnumActiveTextures(int *piIndex, TEXTUREFORMAT eDesiredFormat) const
{
	Assert(piIndex != NULL);
	
	if (piIndex != NULL)
	{
		if (m_pActiveGroup != NULL)
		{
			IEditorTexture *pTex = NULL;

			do
			{
				pTex = m_pActiveGroup->GetTexture(*piIndex);
				if (pTex != NULL)
				{
					(*piIndex)++;

					if ((eDesiredFormat == tfNone) || (pTex->GetTextureFormat() == eDesiredFormat))
					{
						return(pTex);
					}
				}
			} while (pTex != NULL);
		}
	}

	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Initializes the texture system.
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CTextureSystem::Initialize(HWND hwnd)
{
	bool bWAD = CWADTexture::Initialize();
	bool bMaterial = CMaterial::Initialize(hwnd);

	return(bWAD && bMaterial);
}


//-----------------------------------------------------------------------------
// Purpose: Shuts down the texture system.
//-----------------------------------------------------------------------------
void CTextureSystem::ShutDown(void)
{
	CWADTexture::ShutDown();
	CMaterial::ShutDown();
	FreeAllTextures();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pszName - 
//			piIndex - 
//			bDummy - 
// Output : 
//-----------------------------------------------------------------------------
IEditorTexture *CTextureSystem::FindActiveTexture(LPCSTR pszInputName, int *piIndex, BOOL bDummy)
{

	// The .vmf file format gets confused if there are backslashes in material names,
	// so make sure they're all using forward slashes here.
	char szName[MAX_PATH];
	Q_StrSubst( pszInputName, "\\", "/", szName, sizeof( szName ) );
	const char *pszName = szName;
	IEditorTexture *pTex = NULL;
	//
	// Check the cache first.
	//
	if (m_pLastTex && !stricmp(pszName, m_pLastTex->GetName()))
	{
		if (piIndex)
		{
			*piIndex = m_nLastIndex;
		}

		return m_pLastTex;
	}

	int iIndex = 0;

	// We're finding by name, so we don't care what the format is as long as the name matches.
	if ( m_pActiveGroup )
	{
		pTex = m_pActiveGroup->FindTextureByName( pszName, &iIndex, tfNone );
		if ( pTex )
		{
			if ( piIndex )
				*piIndex = iIndex;
			
			m_pLastTex = pTex;
			m_nLastIndex = iIndex;
			
			return pTex;
		}
	}

	//
	// Let's try again, this time with \textures\ decoration
	// TODO: remove this?
	//
	{
		iIndex = 0;
		char szBuf[512];

		sprintf(szBuf, "textures\\%s", pszName);

		for (int i = strlen(szBuf) -1; i >= 0; i--)
		{
			if (szBuf[i] == '/')
				szBuf[i] = '\\';
		}

		strlwr(szBuf);

		if ( m_pActiveGroup )
		{
			pTex = m_pActiveGroup->FindTextureByName( szBuf, &iIndex, tfNone );
			if ( pTex )
			{
				if ( piIndex )
					*piIndex = iIndex;
				
				m_pLastTex = pTex;
				m_nLastIndex = iIndex;
				
				return pTex;
			}
		}
	}
	//
	// Caller doesn't want dummies.
	//
	if (!bDummy)
	{
		return(NULL);
	}

	Assert(!piIndex);

	//
	// Check the list of dummies for a texture with the same name and texture format.
	//
	if (m_pActiveContext)
	{
		int nDummyCount = m_pActiveContext->Dummies.Count();
		for (int nDummy = 0; nDummy < nDummyCount; nDummy++)
		{
			IEditorTexture *pTexDummy = m_pActiveContext->Dummies.Element(nDummy);
			if (!strcmpi(pszName, pTexDummy->GetName()))
			{
				m_pLastTex = pTexDummy;
				m_nLastIndex = -1;
				return(pTexDummy);
			}
		}

		//
		// Not found; add a dummy as a placeholder for the missing texture.
		//
		pTex = AddDummy(pszName, g_pGameConfig->GetTextureFormat());
	}

	if (pTex != NULL)
	{
		m_pLastTex = pTex;
		m_nLastIndex = -1;
	}

	return(pTex);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pTex - 
//-----------------------------------------------------------------------------
void CTextureSystem::AddMRU(IEditorTexture *pTex)
{
	if (!m_pActiveContext)
		return;

	int nIndex = m_pActiveContext->MRU.Find(pTex);
	if (nIndex != -1)
	{
		m_pActiveContext->MRU.Remove(nIndex);
	}
	else if (m_pActiveContext->MRU.Count() == 8)
	{
		m_pActiveContext->MRU.Remove(7);
	}

	m_pActiveContext->MRU.AddToHead(pTex);
}


//-----------------------------------------------------------------------------
// Purpose: Change palette on all textures.
// Input  : 
// dvs: need to handle a palette change for Quake support
//-----------------------------------------------------------------------------
void CTextureSystem::InformPaletteChanged()
{
//	int nGraphics = GetCount();
//
//	for (int i = 0; i < nGraphics; i++)
//	{
//		IEditorTexture *pTex = &GetAt(i);
//	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns the texture context that corresponds to the given game config.
//-----------------------------------------------------------------------------
TextureContext_t *CTextureSystem::FindTextureContextForConfig(CGameConfig *pConfig)
{
	for (int i = 0; i < m_TextureContexts.Count(); i++)
	{
		if (m_TextureContexts.Element(i).pConfig == pConfig)
		{
			return &m_TextureContexts.Element(i);
		}
	}

	return NULL;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTextureSystem::SetActiveConfig(CGameConfig *pConfig)
{
	TextureContext_t *pContext = FindTextureContextForConfig(pConfig);
	if (pContext)
	{
		m_pActiveContext = pContext;
		m_pActiveGroup = m_pActiveContext->pAllGroup;
	}
	else
	{
		m_pActiveContext = NULL;
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : char *pcszName - 
//-----------------------------------------------------------------------------
void CTextureSystem::SetActiveGroup(const char *pcszName)
{
	if (!m_pActiveContext)
		return;

	char szBuf[MAX_PATH];
	sprintf(szBuf, "textures\\%s", pcszName);

	int iCount = m_pActiveContext->Groups.Count();
	for (int i = 0; i < iCount; i++)
	{
		CTextureGroup *pGroup = m_pActiveContext->Groups.Element(i);
		if (!strcmpi(pGroup->GetName(), pcszName))
		{
			m_pActiveGroup = pGroup;
			return;
		}

		if (strstr(pGroup->GetName(), pcszName))
		{
			m_pActiveGroup = pGroup;
			return;
		}

	}

	TRACE0("No Group Found!");
}


void HammerFileSystem_ReportSearchPath( const char *szPathID )
{
	char szSearchPath[ 4096 ];
	g_pFullFileSystem->GetSearchPath( szPathID, true, szSearchPath, sizeof( szSearchPath ) );

	Msg( mwStatus, "------------------------------------------------------------------" );

	char *pszOnePath = strtok( szSearchPath, ";" );
	while ( pszOnePath )
	{
		Msg( mwStatus, "Search Path (%s): %s", szPathID, pszOnePath );
		pszOnePath = strtok( NULL, ";" );
	}
}


//-----------------------------------------------------------------------------
// FIXME: Make this work correctly, using the version in filesystem_tools.cpp
// (it doesn't work currently owing to filesystem setup issues)
//-----------------------------------------------------------------------------
void HammerFileSystem_SetGame( const char *pExeDir, const char *pModDir )
{
	static bool s_bOnce = false;
	Assert( !s_bOnce );
	s_bOnce = true;

	char buf[MAX_PATH];

	Q_snprintf( buf, MAX_PATH, "%s\\hl2", pExeDir );
	g_pFullFileSystem->AddSearchPath( buf, "GAME", PATH_ADD_TO_HEAD );

	if ( pModDir && *pModDir != '\0' )
	{
		g_pFullFileSystem->AddSearchPath( pModDir, "GAME", PATH_ADD_TO_HEAD );
	}

	HammerFileSystem_ReportSearchPath( "GAME" );
}


//-----------------------------------------------------------------------------
// Purpose: Loads textures from all texture files.
//-----------------------------------------------------------------------------
void CTextureSystem::LoadAllGraphicsFiles(void)
{
	FreeAllTextures();

	// For each game config...
	// dvs: Disabled for single-config running.
	//for (int nConfig = 0; nConfig < Options.configs.GetGameConfigCount(); nConfig++)
	{
		//CGameConfig *pConfig = Options.configs.GetGameConfig(nConfig);
		CGameConfig *pConfig = g_pGameConfig;

		// Create a new texture context with the WADs and materials for that config.
		TextureContext_t *pContext = AddTextureContext();

		// Bind it to this config.
		pContext->pConfig = pConfig;

		// Create a group to hold all the textures for this context.
		pContext->pAllGroup = new CTextureGroup("All Textures");
		pContext->Groups.AddToTail(pContext->pAllGroup);

		HammerFileSystem_SetGame(pConfig->m_szGameExeDir, pConfig->m_szModDir);

		// Set the new context as the active context.
		m_pActiveContext = pContext;

		// Load the textures for all WAD files set in this config.
		// Only do this for configs that use WAD textures.
		if (pConfig->GetTextureFormat() == tfWAD3)
		{
			LoadWADFiles(pConfig);
		}

		// Load the materials for this config.
		// Do this unconditionally so that we get necessary editor materials.
		LoadMaterials(pConfig);

		m_pActiveContext->pAllGroup->Sort();
	}
}


//-----------------------------------------------------------------------------
// Purpose: Loads all WAD files for the given game config.
//-----------------------------------------------------------------------------
void CTextureSystem::LoadWADFiles(CGameConfig *pConfig)
{
	// dvs: FIXME: WADs are not currently per-config
	for (int i = 0; i < Options.textures.nTextureFiles; i++)
	{
		LoadGraphicsFile(Options.textures.TextureFiles[i]);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Loads all the materials for the given game config.
//-----------------------------------------------------------------------------
void CTextureSystem::LoadMaterials(CGameConfig *pConfig)
{
	CTextureGroup *pGroup = new CTextureGroup("Materials");
	pGroup->SetTextureFormat(tfVMT);
	m_pActiveContext->Groups.AddToTail(pGroup);

	// Add all the materials to the group.
	CMaterial::EnumerateMaterials( this, "materials", (int)pGroup, INCLUDE_WORLD_MATERIALS );
	
	// Watch the materials directory recursively...
	CMaterialFileChangeWatcher *pWatcher = new CMaterialFileChangeWatcher;
	pWatcher->Init( this, (int)pGroup );
	m_ChangeWatchers.AddToTail( pWatcher );

	Assert( m_pCubemapTexture == NULL );

	m_pCubemapTexture = MaterialSystemInterface()->FindTexture( "editor/cubemap", NULL, true );

	if ( m_pCubemapTexture )
	{
		m_pCubemapTexture->IncrementReferenceCount();
		CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
		pRenderContext->BindLocalCubemap( m_pCubemapTexture );
	}
	
	// Get the nodraw texture.
	m_pNoDrawTexture = NULL;
	for ( int i=0; i < m_Textures.Count(); i++ )
	{
		if ( V_stricmp( m_Textures[i]->GetName(), "tools/toolsnodraw" ) == 0 || V_stricmp( m_Textures[i]->GetName(), "tools/toolsnodraw" ) == 0 )
		{
			m_pNoDrawTexture = m_Textures[i];
			break;
		}
	}
	if ( !m_pNoDrawTexture )				
		m_pNoDrawTexture = CMaterial::CreateMaterial( "tools/toolsnodraw", true );
}

void CTextureSystem::RebindDefaultCubeMap()
{
	// rebind with the default cubemap
	
	if (  m_pCubemapTexture )
	{
		CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
		pRenderContext->BindLocalCubemap( m_pCubemapTexture );
	}
}


void CTextureSystem::UpdateFileChangeWatchers()
{
	for ( int i=0; i < m_ChangeWatchers.Count(); i++ )
		m_ChangeWatchers[i]->Update();
}


void CTextureSystem::OnFileChange( const char *pFilename, int context, CTextureSystem::EFileType eFileType )
{
	// It requires the forward slashes later...
	char fixedSlashes[MAX_PATH];
	V_StrSubst( pFilename, "\\", "/", fixedSlashes, sizeof( fixedSlashes ) );	

	// Get rid of the extension.
	if ( V_strlen( fixedSlashes ) < 5 )
	{
		Assert( false );
		return;
	}
	fixedSlashes[ V_strlen( fixedSlashes ) - 4 ] = 0;


	// Handle it based on what type of file we've got.
	if ( eFileType == k_eFileTypeVMT )
	{
		IEditorTexture *pTex = FindActiveTexture( fixedSlashes, NULL, FALSE );
		if ( pTex )
		{
			pTex->Reload( true );
		}
		else
		{
			EnumMaterial( fixedSlashes, context );
			IEditorTexture *pTexFixed = FindActiveTexture( fixedSlashes, NULL, FALSE );
			if ( pTexFixed )
			{
				GetMainWnd()->m_TextureBar.NotifyNewMaterial( pTexFixed );
				GetMainWnd()->GetFaceEditSheet()->NotifyNewMaterial( pTexFixed );
			}
		}
	}
	else if ( eFileType == k_eFileTypeVTF )
	{
		// Whether a VTF was added, removed, or modified, we do the same thing.. refresh it and any materials that reference it.
		ITexture *pTexture = materials->FindTexture( fixedSlashes, TEXTURE_GROUP_UNACCOUNTED, false );
		if ( pTexture )
		{
			pTexture->Download( NULL );
			ReloadMaterialsUsingTexture( pTexture );
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Load any materials that reference this texture. Used so we can refresh a 
// material's preview image if a relevant .vtf changes.
//-----------------------------------------------------------------------------
void CTextureSystem::ReloadMaterialsUsingTexture( ITexture *pTestTexture )
{
	for ( int i=0; i < m_Textures.Count(); i++ )
	{
		IEditorTexture *pEditorTex = m_Textures[i];
		IMaterial *pMat = pEditorTex->GetMaterial( false );
		if ( !pMat )
			continue;
		
		IMaterialVar **pParams = pMat->GetShaderParams();
		int nParams = pMat->ShaderParamCount();
		for ( int iParam=0; iParam < nParams; iParam++ )
		{
			if ( pParams[iParam]->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
				continue;
			
			ITexture *pTex = pParams[iParam]->GetTextureValue();
			if ( !pTex )
				continue;
			
			if ( pTex == pTestTexture )
			{
				pEditorTex->Reload( true );
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Figure out the file type from its extension. Returns false if we don't have an enum for that extension.
//-----------------------------------------------------------------------------
bool CTextureSystem::GetFileTypeFromFilename( const char *pFilename, CTextureSystem::EFileType *pFileType )
{
	char strRight[16];
	V_StrRight( pFilename, 4, strRight, sizeof( strRight ) );
	if ( V_stricmp( strRight, ".vmt" ) == 0 )
	{
		*pFileType = CTextureSystem::k_eFileTypeVMT;
		return true;
	}
	else if ( V_stricmp( strRight, ".vtf" ) == 0 )
	{
		*pFileType = CTextureSystem::k_eFileTypeVTF;
		return true;
	}
	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Loads textures from all texture files.
//-----------------------------------------------------------------------------
void CTextureSystem::ReloadTextures( const char *pFilterName )
{
	MaterialSystemInterface()->ReloadMaterials( pFilterName );

	for ( int i = 0; i < m_Textures.Count(); i++ )
	{
		if ( !Q_stristr( pFilterName, m_Textures[i]->GetName() ) )
			continue;

		m_Textures[i]->Reload( false );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Adds a placeholder texture for a texture that exists in the map, but
//			was not found on disk.
// Input  : pszName - Name of missing texture.
// Output : Returns a pointer to the new dummy texture.
//-----------------------------------------------------------------------------
IEditorTexture *CTextureSystem::AddDummy(LPCTSTR pszName, TEXTUREFORMAT eFormat)
{
	if (!m_pActiveContext)
		return NULL;

	IEditorTexture *pTex = new CDummyTexture(pszName, eFormat);
	m_pActiveContext->Dummies.AddToTail(pTex);

	return(pTex);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : elem1 - 
//			elem2 - 
// Output : static int __cdecl
//-----------------------------------------------------------------------------
static int __cdecl SortTexturesProc(IEditorTexture * const *elem1, IEditorTexture * const *elem2)
{
	IEditorTexture *pElem1 = *((IEditorTexture **)elem1);
	IEditorTexture *pElem2 = *((IEditorTexture **)elem2);

	Assert((pElem1 != NULL) && (pElem2 != NULL));
	if ((pElem1 == NULL) || (pElem2 == NULL))
	{
		return(0);
	}

	const char *pszName1 = pElem1->GetName();
	const char *pszName2 = pElem2->GetName();

	char ch1 = pszName1[0];
	char ch2 = pszName2[0];

	if (IsSortChr(ch1) && !IsSortChr(ch2))
	{
		int iFamilyLen = strlen(pszName1+2);
		int iFamily = strnicmp(pszName1+2, pszName2, iFamilyLen);
		if (!iFamily)
		{
			return(-1);	// same family - put elem1 before elem2
		}
		return(iFamily);	// sort normally
	}
	else if (!IsSortChr(ch1) && IsSortChr(ch2))
	{
		int iFamilyLen = strlen(pszName2+2);
		int iFamily = strnicmp(pszName1, pszName2+2, iFamilyLen);
		if (!iFamily)
		{
			return(1);	// same family - put elem2 before elem1
		}
		return(iFamily);	// sort normally
	}
	else if (IsSortChr(ch1) && IsSortChr(ch2))
	{
		// do family name sorting
		int iFamily = strcmpi(pszName1+2, pszName2+2);

		if (!iFamily)
		{
			// same family - sort by number
			return pszName1[1] - pszName2[1];
		}

		// different family
		return(iFamily);
	}

	return(strcmpi(pszName1, pszName2));
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : sizeSrc - 
//			sizeDest - 
//			*src - 
//			*dest - 
//-----------------------------------------------------------------------------
void ScaleBitmap(CSize sizeSrc, CSize sizeDest, char *src, char *dest)
{
    int i;
    int e_y = (sizeSrc.cy << 1) - sizeDest.cy;
    int sizeDest2_y = (sizeDest.cy << 1);
	int sizeSrc2_y = sizeSrc.cy << 1;
	int srcline = 0, destline = 0;
	char *srclinep, *destlinep;
	int e_x = (sizeSrc.cx << 1) - sizeDest.cx;
	int sizeDest2_x = (sizeDest.cx << 1);
	int sizeSrc2_x = sizeSrc.cx << 1;

    for( i = 0; i < sizeDest.cy; i++ )
    {
		// scale by X
		{
			srclinep = src + (srcline * sizeSrc.cx);
			destlinep = dest + (destline * sizeDest.cx);

			for( int j = 0; j < sizeDest.cx; j++ )
			{
				*destlinep = *srclinep;

				while( e_x >= 0 )
				{
					++srclinep;
					e_x -= sizeDest2_x;
				}

				++destlinep;
				e_x += sizeSrc2_x;
			}
		}

        while( e_y >= 0 )
        {
            ++srcline;
            e_y -= sizeDest2_y;
        }

        ++destline;
        e_y += sizeSrc2_y;
    }
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : id - 
//			*piIndex - 
// Output : GRAPHICSFILESTRUCT *
//-----------------------------------------------------------------------------
bool CTextureSystem::FindGraphicsFile(GRAPHICSFILESTRUCT *pFileInfo, DWORD id, int *piIndex)
{
	for (int i = 0; i < m_GraphicsFiles.Count(); i++)
	{
		if (m_GraphicsFiles[i].id == id)
		{
			if (piIndex)
			{
				piIndex[0] = i;
			}

			if (pFileInfo != NULL)
			{
				*pFileInfo = m_GraphicsFiles[i];
			}

			return(true);
		}
	}

	return(false);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pFile - 
//			fd - 
//			pGroup - 
//-----------------------------------------------------------------------------
void CTextureSystem::LoadGraphicsFileWAD3(GRAPHICSFILESTRUCT *pFile, int fd, CTextureGroup *pGroup)
{
	// read wad header
	wadinfo_t hdr;
	_lseek(fd, 0, SEEK_SET);
	_read(fd, (char*)&hdr, sizeof hdr);

	_lseek(fd, hdr.infotableofs, SEEK_SET);

	// allocate directory memory.
	WAD3lumpinfo_t *dir = new WAD3lumpinfo_t[hdr.numlumps];
		
	// read entries.
	_read(fd, dir, sizeof(WAD3lumpinfo_t) * hdr.numlumps);

	// load graphics!
	for (int i = 0; i < hdr.numlumps; i++)
	{
		if (dir[i].type == TYP_MIPTEX)
		{
			_lseek(fd, dir[i].filepos, SEEK_SET);

			CWADTexture *pNew = new CWADTexture;
			if (pNew != NULL)
			{
				if (pNew->Init(fd, pFile->id, FALSE, dir[i].name))
				{
					pNew->SetTextureFormat(pFile->format);

					//
					// Add the texture to master list of textures.
					//
					AddTexture(pNew);

					//
					// Add the texture's index to the given group and to the "All" group.
					//
					pGroup->AddTexture(pNew);
					if (pGroup != m_pActiveContext->pAllGroup)
					{
						m_pActiveContext->pAllGroup->AddTexture(pNew);
					}
				}
				else
				{
					delete pNew;
				}
			}
		}
	}

	// free memory
	delete[] dir;
}


//-----------------------------------------------------------------------------
// Purpose: Loads all textures in a given graphics file and returns an ID for
//			the file.
// Input  : filename - Full path of graphics file to load.
// Output : Returns the file ID.
//-----------------------------------------------------------------------------
DWORD CTextureSystem::LoadGraphicsFile(const char *pFilename)
{
	static DWORD __GraphFileID = 1;	// must start at 1.

	//
	// Make sure it's not already there.
	//
	int i = m_GraphicsFiles.Count() - 1;
	while (i > -1)
	{
		if (!strcmp(m_GraphicsFiles[i].filename, pFilename))
		{
			return(m_GraphicsFiles[i].id);
		}

		i--;
	}

	//
	// Is this a WAD file?
	//
	DWORD dwAttrib = GetFileAttributes(pFilename);
	if (dwAttrib == 0xFFFFFFFF)
	{
		return(0);
	}

	GRAPHICSFILESTRUCT gf;

	if (!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
	{
		// open the file, and add it to the GraphicFileList array
		gf.fd = _open(pFilename, _O_BINARY | _O_RDONLY);
		if (gf.fd == -1)
		{
			// todo: if errno is "out of handles", close some other
			// graphics files.

			// StatusMsg(IDS_ERROPENGRAPHFILE, errno);
			return 0;	// could not open
		}

		char buf[4];
		_read(gf.fd, buf, 4);

		//
		// Make sure the file is in a format that we can read.
		//
		if (!memcmp(buf, "WAD3", 4))
		{
			gf.format = tfWAD3;
		}
		else
		{
			char str[MAX_PATH*2];
			Q_snprintf( str, sizeof(str), "The file \"%s\" is not a valid WAD3 file and will not be used.", pFilename);
			AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
			_close(gf.fd);
			return(0);
		}
	}

	// got it -- setup the rest of the gf structure
	gf.id = __GraphFileID++;
	Q_strncpy( gf.filename, pFilename, sizeof(gf.filename) );
	gf.bLoaded = FALSE;

	//
	// Add file to list of texture files.
	//
	m_GraphicsFiles.AddToTail(gf);

	//
	// Create a new texture group for the file.
	//
	CTextureGroup *pGroup = new CTextureGroup(pFilename);
	pGroup->SetTextureFormat(gf.format);
	m_pActiveContext->Groups.AddToTail(pGroup);
	
	//
	// Load the textures from the file and place them in the texture group.
	//
	LoadGraphicsFileWAD3(&gf, gf.fd, pGroup);
	gf.bLoaded = TRUE;

	//
	// Sort this group's list
	//
	pGroup->Sort();

	return(gf.id);
}


//-----------------------------------------------------------------------------
// Purpose: Determines whether or not there is at least one available texture
//			group for a given texture format.
// Input  : format - Texture format to look for.
// Output : Returns TRUE if textures of a given format are available, FALSE if not.
//-----------------------------------------------------------------------------
bool CTextureSystem::HasTexturesForConfig(CGameConfig *pConfig)
{
	if (!pConfig)
		return false;

	TextureContext_t *pContext = FindTextureContextForConfig(pConfig);
	if (!pContext)
		return false;

	int nCount = pContext->Groups.Count();
	for (int i = 0; i < nCount; i++)
	{
		CTextureGroup *pGroup = pContext->Groups.Element(i);
		if (pGroup->GetTextureFormat() == pConfig->GetTextureFormat())
		{
			return true;
		}
	}

	return false;
}


//-----------------------------------------------------------------------------
// Used to add all the world materials into the material list
//-----------------------------------------------------------------------------
bool CTextureSystem::EnumMaterial( const char *pMaterialName, int nContext )
{
	CTextureGroup *pGroup = (CTextureGroup *)nContext;
	CMaterial *pMaterial = CMaterial::CreateMaterial(pMaterialName, false);
	if (pMaterial != NULL)
	{
		// Add it to the master list of textures.
		AddTexture(pMaterial);

		// Add the texture's index to the given group and to the "All" group.
		pGroup->AddTexture(pMaterial);
		if (pGroup != m_pActiveContext->pAllGroup)
		{
			m_pActiveContext->pAllGroup->AddTexture(pMaterial);
		}
	}
	return true;
}


//-----------------------------------------------------------------------------
// Registers the keywords as existing in a particular material
//-----------------------------------------------------------------------------
void CTextureSystem::RegisterTextureKeywords( IEditorTexture *pTexture )
{
	//
	// Add any new keywords from this material to the list of keywords.
	//
	char szKeywords[MAX_PATH];
	pTexture->GetKeywords(szKeywords);
	if (szKeywords[0] != '\0')
	{
		char *pch = strtok(szKeywords, " ,;");
		while (pch != NULL)
		{
			// dvs: hide in a Find function
			bool bFound = false;
			
			for( int pos=0; pos < m_Keywords.Count(); pos++ )
			{
				const char *pszTest = m_Keywords.Element(pos);
				if (!stricmp(pszTest, pch))
				{
					bFound = true;
					break;
				}
			}

			if (!bFound)
			{
				char *pszKeyword = new char[strlen(pch) + 1];
				strcpy(pszKeyword, pch);
				m_Keywords.AddToTail(pszKeyword);
			}

			pch = strtok(NULL, " ,;");
		}
	}
}


//-----------------------------------------------------------------------------
// Used to lazily load in all the textures
//-----------------------------------------------------------------------------
void CTextureSystem::LazyLoadTextures()
{
	if ( m_pActiveContext && m_pActiveContext->pAllGroup && !IsRunningInEngine() )
	{
		m_pActiveContext->pAllGroup->LazyLoadTextures();
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : TextureContext_t
//-----------------------------------------------------------------------------
TextureContext_t *CTextureSystem::AddTextureContext()
{
	// Allocate a new texture context.
	int nIndex = m_TextureContexts.AddToTail();

	// Add the group to this config's list of texture groups.
	TextureContext_t *pContext = &m_TextureContexts.Element(nIndex);
	return pContext;
}


//-----------------------------------------------------------------------------
// Opens the source file associated with a material
//-----------------------------------------------------------------------------
void CTextureSystem::OpenSource( const char *pMaterialName )
{
	if ( !pMaterialName )
		return;

	char pRelativePath[MAX_PATH];
	Q_snprintf( pRelativePath, MAX_PATH, "materials/%s.vmt", pMaterialName );

	char pFullPath[MAX_PATH];
	if ( g_pFullFileSystem->GetLocalPath( pRelativePath, pFullPath, MAX_PATH ) )
	{
		ShellExecute( NULL, "open", pFullPath, NULL, NULL, SW_SHOWNORMAL );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
// Input  : pszName - Name of group, ie "Materials" or "u:\hl\tfc\tfc.wad".
//-----------------------------------------------------------------------------
CTextureGroup::CTextureGroup(const char *pszName)
{
	strcpy(m_szName, pszName);
	m_eTextureFormat = tfNone;
	m_nTextureToLoad = 0;
}


//-----------------------------------------------------------------------------
// Purpose: Adds a texture to this group.
// Input  : pTexture - Texture to add.
//-----------------------------------------------------------------------------
void CTextureGroup::AddTexture(IEditorTexture *pTexture)
{
	int index = m_Textures.AddToTail(pTexture);
	m_TextureNameMap.Insert( pTexture->GetName(), index );
}


//-----------------------------------------------------------------------------
// Purpose: Sorts the group.
//-----------------------------------------------------------------------------
void CTextureGroup::Sort(void)
{
	m_Textures.Sort(SortTexturesProc);

	// Redo the name map.
	m_TextureNameMap.RemoveAll();
	for ( int i=0; i < m_Textures.Count(); i++ )
	{
		IEditorTexture *pTex = m_Textures[i];
		m_TextureNameMap.Insert( pTex->GetName(), i );
	}

	// Changing the order means we don't know where we should be loading from
	m_nTextureToLoad = 0;
}


//-----------------------------------------------------------------------------
// Purpose: Retrieves a texture by index.
// Input  : nIndex - Index of the texture in this group.
//-----------------------------------------------------------------------------
IEditorTexture *CTextureGroup::GetTexture(int nIndex)
{
	if ((nIndex >= m_Textures.Count()) || (nIndex < 0))
	{
		return(NULL);
	}

	return(m_Textures[nIndex]);
}


//-----------------------------------------------------------------------------
// finds a texture by name
//-----------------------------------------------------------------------------
IEditorTexture *CTextureGroup::GetTexture( char const* pName )
{
	for (int i = 0; i < m_Textures.Count(); i++)
	{
		if (!strcmp(pName, m_Textures[i]->GetName()))
			return m_Textures[i];
	}

	return NULL;
}


//-----------------------------------------------------------------------------
// Quickly find a texture by name.
//-----------------------------------------------------------------------------
IEditorTexture* CTextureGroup::FindTextureByName( const char *pName, int *piIndex, TEXTUREFORMAT eDesiredFormat )
{
	int iMapEntry = m_TextureNameMap.Find( pName );
	if ( iMapEntry == m_TextureNameMap.InvalidIndex() )
	{
		return NULL;
	}
	else
	{
		IEditorTexture *pTex = m_Textures[ m_TextureNameMap[iMapEntry] ];
		if ((eDesiredFormat == tfNone) || (pTex->GetTextureFormat() == eDesiredFormat))
			return pTex;
		else
			return NULL;
	}		
}


//-----------------------------------------------------------------------------
// Used to lazily load in all the textures
//-----------------------------------------------------------------------------
void CTextureGroup::LazyLoadTextures()
{
	// Load at most once per call
	while (m_nTextureToLoad < m_Textures.Count())
	{
		if (!m_Textures[m_nTextureToLoad]->IsLoaded())
		{
			m_Textures[m_nTextureToLoad]->Load();
			++m_nTextureToLoad;
			return;
		}

		// This one was already loaded; skip it
		++m_nTextureToLoad;
	}
}