-
Notifications
You must be signed in to change notification settings - Fork 98
/
terrtex.cpp
1722 lines (1389 loc) · 45.7 KB
/
terrtex.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
/***************************************************************************\
TerrTex.cpp
Miro "Jammer" Torrielli
15Oct03
- Begin Major Rewrite
\***************************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include "TimeMgr.h"
#include "TOD.h"
#include "Image.h"
#include "TerrTex.h"
#include "dxtlib.h"
#include "Falclib/Include/IsBad.h"
#include "FalcLib/include/dispopts.h"
#include "FalcLib/include/f4thread.h"
extern bool g_bEnableStaticTerrainTextures;
extern int fileout;
extern void ConvertToNormalMap(int kerneltype, int colorcnv, int alpha, float scale, int minz, bool wrap, bool bInvertX, bool bInvertY, int w, int h, int bits, void * data);
extern void ReadDTXnFile(unsigned long count, void * buffer);
extern void WriteDTXnFile(unsigned long count, void *buffer);
#include "FalcLib/include/PlayerOp.h"
#ifdef USE_SH_POOLS
MEM_POOL gTexDBMemPool = NULL;
#endif
#define ARGB_TEXEL_SIZE 4
#define ARGB_TEXEL_BITS 32
#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
//#define MAXIMUM(a,b,c) ((a>b)?MAX(a,c):MAX(b,c))
//#define MINIMUM(a,b,c) ((a<b)?MIN(a,c):MIN(b,c))
TextureDB TheTerrTextures;
// Kludge for 094
static bool bIs092;
// sfr: constructor and destructor here
TextureDB::TextureDB() : cs_textureList(F4CreateCriticalSection("texturedb mutex")), TextureSets(NULL)
{
}
TextureDB::~TextureDB()
{
F4DestroyCriticalSection(cs_textureList);
}
// Setup the texture database
BOOL TextureDB::Setup(DXContext *hrc, const char* path)
{
char filename[MAX_PATH];
HANDLE listFile;
BOOL result;
DWORD bytesRead;
int dataSize;
int i, j;
ShiAssert(hrc);
ShiAssert(path);
// sfr: lock texture for setup
F4ScopeLock sl(cs_textureList);
#ifdef USE_SH_POOLS
if (gTexDBMemPool == NULL)
gTexDBMemPool = MemPoolInit(0);
#endif
// Store the texture path for future reference
if (strlen(path) + 1 >= sizeof(texturePath))
ShiError("Texture path name overflow");
strcpy(texturePath, path);
if (texturePath[strlen(texturePath) - 1] not_eq '\\')
strcat(texturePath, "\\");
sprintf(texturePathD, "%stexture\\", texturePath);
// Store the rendering context to be used just for managing our textures
private_rc = hrc;
ShiAssert(private_rc not_eq NULL);
// Initialize data members to default values
overrideHandle = NULL;
numSets = 0;
#ifdef _DEBUG
LoadedSetCount = 0;
LoadedTextureCount = 0;
ActiveTextureCount = 0;
#endif
// Initialize the lighting conditions and register for future time of day updates
TimeUpdateCallback(this);
TheTimeManager.RegisterTimeUpdateCB(TimeUpdateCallback, this);
// Create the synchronization objects we'll need
//InitializeCriticalSection(&cs_textureList);
// Open the texture database description file
strcpy(filename, texturePath);
strcat(filename, "Texture.BIN");
listFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Read the number of texture sets and tiles in the database.
result = ReadFile(listFile, &numSets, sizeof(numSets), &bytesRead, NULL);
if (result)
result = ReadFile(listFile, &totalTiles, sizeof(totalTiles), &bytesRead, NULL);
if ( not result)
{
char string[80];
char message[120];
PutErrorString(string);
sprintf(message, "%s: Couldn'd read texture list.", string);
MessageBox(NULL, message, "Proceeding with Empty List", MB_OK);
numSets = 0;
}
// Allocate memory for the texture set records.
#ifdef USE_SH_POOLS
TextureSets = (SetEntry *)MemAllocPtr(gTexDBMemPool, sizeof(SetEntry) * numSets, 0) ;
#else
TextureSets = new SetEntry[numSets];
#endif
if ( not TextureSets)
ShiError("Failed to allocate memory for the texture database.");
// Read the descriptions for the sets.
for (i = 0; i < numSets; i++)
{
// Read the description
result = ReadFile(listFile, &TextureSets[i].numTiles, sizeof(TextureSets[i].numTiles), &bytesRead, NULL);
if (result)
result = ReadFile(listFile, &TextureSets[i].terrainType, sizeof(TextureSets[i].terrainType), &bytesRead, NULL);
if ( not result)
{
char string[80];
char message[120];
PutErrorString(string);
sprintf(message, "%s: Couldn't read set description - disk error?", string);
ShiError(message);
}
// Mark the set as unused
TextureSets[i].refCount = 0;
TextureSets[i].palette = NULL;
TextureSets[i].palHandle = NULL;
// Allocate memory for the tile headers in this set
if (TextureSets[i].numTiles > 0)
{
#ifdef USE_SH_POOLS
TextureSets[i].tiles = (TileEntry *)MemAllocPtr(gTexDBMemPool, sizeof(TileEntry) * TextureSets[i].numTiles, 0);
#else
TextureSets[i].tiles = new TileEntry[TextureSets[i].numTiles];
#endif
ShiAssert(TextureSets[i].tiles);
}
else
TextureSets[i].tiles = NULL;
// Read the descriptions for the tiles within the sets.
for (j = 0; j < TextureSets[i].numTiles; j++)
{
// Read the tile name and area and path counts
result = ReadFile(listFile, &TextureSets[i].tiles[j].filename, sizeof(TextureSets[i].tiles[j].filename), &bytesRead, NULL);
if (result)
result = ReadFile(listFile, &TextureSets[i].tiles[j].nAreas, sizeof(TextureSets[i].tiles[j].nAreas), &bytesRead, NULL);
if (result)
result = ReadFile(listFile, &TextureSets[i].tiles[j].nPaths, sizeof(TextureSets[i].tiles[j].nPaths), &bytesRead, NULL);
if ( not result)
{
char string[80];
char message[120];
PutErrorString(string);
sprintf(message, "%s: Couldn't read tile header - disk error?", string);
ShiError(message);
}
// Start with this tile unused
for (int k = 0; k < TEX_LEVELS; k++)
{
TextureSets[i].tiles[j].refCount[k] = 0;
TextureSets[i].tiles[j].bits[k] = NULL;
TextureSets[i].tiles[j].bitsN[k] = NULL;
TextureSets[i].tiles[j].handle[k] = NULL;
TextureSets[i].tiles[j].handleN[k] = NULL;
}
// Now read all the areas
if (TextureSets[i].tiles[j].nAreas == 0)
TextureSets[i].tiles[j].Areas = NULL;
else
{
dataSize = TextureSets[i].tiles[j].nAreas * sizeof(TexArea);
#ifdef USE_SH_POOLS
TextureSets[i].tiles[j].Areas = (TexArea *)MemAllocPtr(gTexDBMemPool, sizeof(char) * dataSize, 0);
#else
TextureSets[i].tiles[j].Areas = (TexArea *)new char[dataSize];
#endif
ShiAssert(TextureSets[i].tiles[j].Areas);
result = ReadFile(listFile, TextureSets[i].tiles[j].Areas, dataSize, &bytesRead, NULL);
if ( not result)
{
char string[80];
char message[120];
PutErrorString(string);
sprintf(message, "%s: Couldn't read tile areas - disk error?", string);
ShiError(message);
}
}
// Now read all the paths
if (TextureSets[i].tiles[j].nPaths == 0)
TextureSets[i].tiles[j].Paths = NULL;
else
{
dataSize = TextureSets[i].tiles[j].nPaths * sizeof(TexPath);
#ifdef USE_SH_POOLS
TextureSets[i].tiles[j].Paths = (TexPath *)MemAllocPtr(gTexDBMemPool, sizeof(char) * dataSize, 0);
#else
TextureSets[i].tiles[j].Paths = (TexPath *)new char[dataSize];
#endif
ShiAssert(TextureSets[i].tiles[j].Paths);
result = ReadFile(listFile, TextureSets[i].tiles[j].Paths, dataSize, &bytesRead, NULL);
if ( not result)
{
char string[80];
char message[120];
PutErrorString(string);
sprintf(message, "%s: Couldn't read tile paths - disk error?", string);
ShiError(message);
}
}
}
}
CloseHandle(listFile);
return TRUE;
}
void TextureDB::Cleanup(void)
{
int i, j;
ShiAssert(IsReady());
ShiAssert(TextureSets);
// sfr: lock texturedb for cleanup
F4ScopeLock sl(cs_textureList);
if ( not TextureSets) return;
// Stop receiving time updates
TheTimeManager.ReleaseTimeUpdateCB(TimeUpdateCallback, this);
// Free the entire texture list (palettes will be freed as textures go away)
for (i = 0; i < numSets; i++)
{
for (j = 0; j < TextureSets[i].numTiles; j++)
{
// Free the area descriptions
#ifdef USE_SH_POOLS
if (TextureSets[i].tiles[j].Areas) MemFreePtr(TextureSets[i].tiles[j].Areas);
TextureSets[i].tiles[j].Areas = NULL;
if (TextureSets[i].tiles[j].Paths) MemFreePtr(TextureSets[i].tiles[j].Paths);
TextureSets[i].tiles[j].Paths = NULL;
#else
delete[] TextureSets[i].tiles[j].Areas;
TextureSets[i].tiles[j].Areas = NULL;
delete[] TextureSets[i].tiles[j].Paths;
TextureSets[i].tiles[j].Paths = NULL;
#endif
// Free the texture data
for (int r = TEX_LEVELS - 1; r >= 0; r--)
{
if (TextureSets[i].tiles[j].handle[r])
Deactivate(&TextureSets[i], &TextureSets[i].tiles[j], r);
if (TextureSets[i].tiles[j].bits[r])
Free(&TextureSets[i], &TextureSets[i].tiles[j], r);
}
}
#ifdef USE_SH_POOLS
if (TextureSets[i].tiles) MemFreePtr(TextureSets[i].tiles);
#else
delete[] TextureSets[i].tiles;
#endif
TextureSets[i].tiles = NULL;
}
#ifdef USE_SH_POOLS
if (TextureSets) MemFreePtr(TextureSets);
#else
delete[] TextureSets;
#endif
TextureSets = NULL;
// We no longer need our texture managment RC
private_rc = NULL;
ShiAssert(ActiveTextureCount == 0);
ShiAssert(LoadedTextureCount == 0);
ShiAssert(LoadedSetCount == 0);
#ifdef USE_SH_POOLS
if (gTexDBMemPool not_eq NULL)
{
MemPoolFree(gTexDBMemPool);
gTexDBMemPool = NULL;
}
#endif
// Release the sychronization objects we've been using
// DeleteCriticalSection(&cs_textureList);
}
// Set the light level applied to the terrain textures.
void TextureDB::TimeUpdateCallback(void *self)
{
((TextureDB*)self)->SetLightLevel();
}
void TextureDB::SetLightLevel(void)
{
// Store the new light level
// Decide what color to use for lighting
if (TheTimeOfDay.GetNVGmode())
{
lightLevel = NVG_LIGHT_LEVEL;
lightColor.r = 0.0f;
lightColor.g = NVG_LIGHT_LEVEL;
lightColor.b = 0.0f;
}
else
{
lightLevel = TheTimeOfDay.GetLightLevel();
TheTimeOfDay.GetTextureLightingColor(&lightColor);
}
// Update all the currently loaded textures
if (DisplayOptions.m_texMode not_eq DisplayOptionsClass::TEX_MODE_DDS)
{
for (int i = 0; i < numSets; i++)
if (TextureSets[i].palHandle)
StoreMPRPalette(&TextureSets[i]);
}
}
// Lite and store a texture palette in MPR.
void TextureDB::StoreMPRPalette(SetEntry *pSet)
{
DWORD palette[256];
BYTE *to, *from, *stop;
FLOAT tmpR, tmpG, tmpB, h, s, v;
ShiAssert(pSet->palette);
ShiAssert(pSet->palHandle);
// Apply the current lighting
from = (BYTE *)pSet->palette;
// JB 010408 CTD
if (F4IsBadReadPtr(from, sizeof(BYTE))) return;
to = (BYTE *)palette;
stop = to + 256 * 4;
while (to < stop)
{
// *to = static_cast<BYTE>(FloatToInt32(*from * lightColor.r)); to++, from++; // Red
// *to = static_cast<BYTE>(FloatToInt32(*from * lightColor.g)); to++, from++; // Green
// *to = static_cast<BYTE>(FloatToInt32(*from * lightColor.b)); to++, from++; // Blue
// to++, from++; // Alpha
// tmpR = static_cast<BYTE>(FloatToInt32(*from * lightColor.r)); from++; // Red
// tmpG = static_cast<BYTE>(FloatToInt32(*from * lightColor.g)); from++; // Green
// tmpB = static_cast<BYTE>(FloatToInt32(*from * lightColor.b)); from++; // Blue
// from++; // Alpha
tmpR = *from * 1.0f;
from++; // Red
tmpG = *from * 1.0f;
from++; // Green
tmpB = *from * 1.0f;
from++; // Blue
from++; // Alpha
// 0:Summer, 1:Fall, 2:Winter, 3:Spring
if (PlayerOptions.Season == 1) //Autumn
{
if ( not ((tmpR == tmpG and tmpG == tmpB) or tmpG < 60 or (tmpR + tmpG + tmpB) / 3 > 225)) //Not Greyscale / green / not very bright
{
RGBtoHSV(tmpR, tmpG, tmpB, &h, &s, &v);
if (h >= 30 and h <= 165) //Green
{
//h *= 0.6f; // min27 (yellow/orange/terracota/brown)
h = h * 0.33f + 15; //Shift to brown
s *= 1.2f; //more saturated (intenser brown, just mudy green otherwise
v *= 0.9f; //darker
}
else if ( not (v > 0.9 and s > 0.9)) //Not a strong green, but neither very bright
{
s *= 0.9f; //less saturated
v *= 0.85f; //darken a bit
}
if (s > 255) s = 255;
if (h > 255) h = 255;
HSVtoRGB(&tmpR, &tmpG, &tmpB, h, s, v);
}
}
else if (PlayerOptions.Season == 2) //Winter
{
if ( not (tmpR == tmpG and tmpR == tmpB) or tmpG < 60) //((tmpR+tmpG+tmpB)/3)>225) //or (tmpR == 255 and tmpG == 255))) //Greyscale //or pure color
{
RGBtoHSV(tmpR, tmpG, tmpB, &h, &s, &v);
if ( not (s <= 0.2 or h == -1)) //If Not Greyscale
{
if (h >= 45 and h <= 150) //If Green
{
s = 0;
v = 255; //Make white
}
}
else
{
v *= 1.3f; //Make brighter
}
//else if (v<=200) v *= 0.9f; //Greyscale, but not white: darken a bit (to increase contrast)
//else if (v>=200) v *= 1.2f; //bright...make even brighter
//if (s==0 and v < 240) v *= 0.85f; //Greyscale, but not white: darken a bit (to increase contrast)
//if (s>230) s = 255; //bright...make even brighter
if (v > 255) v = 255;
HSVtoRGB(&tmpR, &tmpG, &tmpB, h, s, v);
}
}
else if (PlayerOptions.Season == 3) //Spring
{
RGBtoHSV(tmpR, tmpG, tmpB, &h, &s, &v);
if ( not (s <= 0.1 or h == -1)) //Not Greyscale
{
if (h >= 45 and h <= 160) //Green
{
s *= 0.8f;
v *= 1.2f;
}
if (s > 255) s = 255;
if (v > 255) v = 255;
HSVtoRGB(&tmpR, &tmpG, &tmpB, h, s, v);
}
}
*to = static_cast<BYTE>(FloatToInt32(tmpR * lightColor.r));
to++;
*to = static_cast<BYTE>(FloatToInt32(tmpG * lightColor.g));
to++;
*to = static_cast<BYTE>(FloatToInt32(tmpB * lightColor.b));
to++;
to++; // Alpha
}
// Turn on the lights if it is dark enough
if (lightLevel < 0.5f)
{
to = (BYTE *) bitand (palette[252]);
if (TheTimeOfDay.GetNVGmode())
{
*to = 0;
to++; // Red
*to = 255;
to++; // Green
*to = 0;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 0;
to++; // Red
*to = 255;
to++; // Green
*to = 0;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 0;
to++; // Red
*to = 255;
to++; // Green
*to = 0;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 0;
to++; // Red
*to = 255;
to++; // Green
*to = 0;
to++; // Blue
*to = 255;
to++; // Alpha
}
else
{
*to = 115;
to++; // Red
*to = 171;
to++; // Green
*to = 155;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 183;
to++; // Red
*to = 127;
to++; // Green
*to = 83;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 171;
to++; // Red
*to = 179;
to++; // Green
*to = 139;
to++; // Blue
*to = 255;
to++; // Alpha
*to = 171;
to++; // Red
*to = 171;
to++; // Green
*to = 171;
to++; // Blue
*to = 255;
to++; // Alpha
}
}
((PaletteHandle *)pSet->palHandle)->Load(
MPR_TI_PALETTE, // Palette info
32, // Bits per entry
0, // Start index
256, // Number of entries
(BYTE *)&palette);
}
// This function is called by anyone wishing the use of a particular texture.
void TextureDB::Request(TextureID texID)
{
int set = ExtractSet(texID);
int tile = ExtractTile(texID);
int res = ExtractRes(texID);
BOOL needToLoad[TEX_LEVELS];
ShiAssert(IsReady());
ShiAssert(set >= 0);
ShiAssert(set < numSets);
ShiAssert(tile < TextureSets[set].numTiles);
ShiAssert(res < TEX_LEVELS);
{
F4ScopeLock sl(cs_textureList);
//EnterCriticalSection(&cs_textureList);
// Decide if anyone needs to be loaded and increment our reference counts.
// sfr: @todo it seems we are reading one texture more than we should
for (int r = res; r >= 0; r--)
{
needToLoad[r] = (TextureSets[set].tiles[tile].refCount[r] == 0);
TextureSets[set].tiles[tile].refCount[r]++;
}
//LeaveCriticalSection( &cs_textureList );
}
// Get the data for this bitmap and any lower res bitmaps required
for (int r = res; r >= 0; r--)
{
if (needToLoad[r])
{
ShiAssert(TextureSets[set].tiles[tile].bits[r] == NULL);
ShiAssert(TextureSets[set].tiles[tile].handle[r] == NULL);
if (overrideHandle == NULL)
Load(&TextureSets[set], &TextureSets[set].tiles[tile], r);
else
{
// When using an override texture, we just assume its handle.
TextureSets[set].tiles[tile].handle[res] = overrideHandle;
TextureSets[set].tiles[tile].bits[res] = NULL;
}
}
}
}
// This function must eventually be called by anyone who calls the Request function above.
void TextureDB::Release(TextureID texID)
{
int set = ExtractSet(texID);
int tile = ExtractTile(texID);
int res = ExtractRes(texID);
ShiAssert(IsReady());
ShiAssert(set >= 0);
ShiAssert(set < numSets);
ShiAssert(tile < TextureSets[set].numTiles);
ShiAssert(res < TEX_LEVELS);
//EnterCriticalSection(&cs_textureList);
F4ScopeLock sl(cs_textureList);
// Release our hold on this texture and all mips
while (res >= 0)
{
TextureSets[set].tiles[tile].refCount[res]--;
if (TextureSets[set].tiles[tile].refCount[res] == 0)
{
if (overrideHandle == NULL)
{
Deactivate(&TextureSets[set], &TextureSets[set].tiles[tile], res);
Free(&TextureSets[set], &TextureSets[set].tiles[tile], res);
}
else
{
// When using an override texture, we don't clean up just drop our handle.
TextureSets[set].tiles[tile].handle[res] = NULL;
TextureSets[set].tiles[tile].bits[res] = NULL;
}
}
res--;
}
//LeaveCriticalSection(&cs_textureList);
}
// Return a pointer to the Nth path of type TYPE, where N is from the offset parameter. TYPE 0 = any type.
TexPath* TextureDB::GetPath(TextureID texID, int type, int offset)
{
TexPath *a;
TexPath *stop;
int set = ExtractSet(texID);
int tile = ExtractTile(texID);
ShiAssert(set >= 0);
ShiAssert(set < numSets);
a = TextureSets[set].tiles[tile].Paths;
stop = a + TextureSets[set].tiles[tile].nPaths;
// Find the first entry of the required type
if (type)
while ((a < stop) and (a->type not_eq type))
a++;
// Step to the requested offset
a += offset;
// We didn't find enough (or any) matching types
if ((a >= stop) or ((type) and (a->type not_eq type)))
return NULL;
// We found a match
return a;
}
// Return a pointer to the Nth area of type TYPE, where N is from the offset parameter. TYPE 0 = any type.
TexArea* TextureDB::GetArea(TextureID texID, int type, int offset)
{
TexArea *a;
TexArea *stop;
int set = ExtractSet(texID);
int tile = ExtractTile(texID);
ShiAssert(set >= 0);
ShiAssert(set < numSets);
a = TextureSets[set].tiles[tile].Areas;
stop = a + TextureSets[set].tiles[tile].nAreas;
// Find the first entry of the required type
if (type)
while ((a < stop) and (a->type not_eq type))
a++;
// Step to the requested offset
a += offset;
// We didn't find enough (or any) matching types
if ((a >= stop) or ((type) and (a->type not_eq type)))
return NULL;
// We found a match
return a;
}
BYTE TextureDB::GetTerrainType(TextureID texID)
{
int set = ExtractSet(texID);
if ((set < 0) or (set >= numSets))
return 0;
ShiAssert(set >= 0);
ShiAssert(set < numSets);
return TextureSets[set].terrainType;
}
// This function reads texel data from disk. Only the requested resolution level.
void TextureDB::Load(SetEntry* pSet, TileEntry* pTile, int res, bool forceNoDDS)
{
char filename[MAX_PATH];
int result;
CImageFileMemory texFile;
ShiAssert(IsReady());
ShiAssert(pSet);
ShiAssert(pTile);
ShiAssert( not pTile->handle[res]);
ShiAssert( not pTile->handle[res]);
if ( not forceNoDDS and DisplayOptions.m_texMode == DisplayOptionsClass::TEX_MODE_DDS)
{
ReadImageDDS(pTile, res);
pSet->palette = NULL;
}
else
{
// Construct the full texture file name including path
strcpy(filename, texturePath);
strcat(filename, pTile->filename);
if (res == 1)
filename[strlen(texturePath)] = 'M';
else if (res == 0)
filename[strlen(texturePath)] = 'L';
// Make sure we recognize this file type
texFile.imageType = CheckImageType(filename);
ShiAssert(texFile.imageType not_eq IMAGE_TYPE_UNKNOWN);
// Open the input file
result = texFile.glOpenFileMem(filename);
if (result not_eq 1)
{
char message[256];
sprintf(message, "Failed to open %s", filename);
ShiError(message);
}
// Read the image data (note that ReadTextureImage will close texFile for us)
texFile.glReadFileMem();
result = ReadTextureImage(&texFile);
if (result not_eq GOOD_READ)
ShiError("Failed to read terrain texture. CD Error?");
// Store pointer to the image data
pTile->bits[res] = (BYTE*)texFile.image.image;
ShiAssert(pTile->bits[res]);
// Store the width and height of the texture for use when loading the texture
ShiAssert(texFile.image.width == texFile.image.height);
ShiAssert(texFile.image.width < 2048);
pTile->width[res] = texFile.image.width;
pTile->height[res] = texFile.image.height;
// Make sure this texture is palettized
ShiAssert(texFile.image.palette);
// If we already have this palette, we don't need it again
if (pSet->palette)
glReleaseMemory(texFile.image.palette);
else
{
// This must be the first reference to this tile since we don't have a palette
ShiAssert(pSet->refCount == 0);
ShiAssert(pSet->palHandle == 0);
// Save the palette from this image for future use
pSet->palette = (DWORD*)texFile.image.palette;
}
ShiAssert(pSet->palette);
}
#ifdef _DEBUG
LoadedSetCount++;
#endif
// Note that this tile is referencing it's parent set
pSet->refCount++;
#ifdef _DEBUG
LoadedTextureCount++;
#endif
}
// sfr: function to return DDS width
namespace
{
int getDDSWidth(int flags)
{
if (flags bitand MPR_TI_16)
{
return 16;
}
else if (flags bitand MPR_TI_32)
{
return 32;
}
else if (flags bitand MPR_TI_64)
{
return 64;
}
else if (flags bitand MPR_TI_128)
{
return 128;
}
else if (flags bitand MPR_TI_256)
{
return 256;
}
else if (flags bitand MPR_TI_512)
{
return 512;
}
else if (flags bitand MPR_TI_1024)
{
return 1024;
}
else if (flags bitand MPR_TI_2048)
{
return 2048;
}
else
{
// BUG
return 4096;
}
}
}
// This function sends texture data to MPR
void TextureDB::Activate(SetEntry* pSet, TileEntry* pTile, int res)
{
ShiAssert(IsReady());
ShiAssert(private_rc);
ShiAssert(pSet);
ShiAssert(pTile);
ShiAssert(res < TEX_LEVELS);
ShiAssert( not pTile->handle[res]);
ShiAssert(pTile->bits[res]);
if (DisplayOptions.m_texMode not_eq DisplayOptionsClass::TEX_MODE_DDS)
{
// Pass the palette to MPR if it isn't already there
if (pSet->palHandle == 0)
{
pSet->palHandle = (UInt)new PaletteHandle(private_rc->m_pDD, 32, 256);
ShiAssert(pSet->palHandle);
StoreMPRPalette(pSet);
}
pTile->handle[res] = (UInt)new TextureHandle;
ShiAssert(pTile->handle[res]);
// Attach the palette
((PaletteHandle *)pSet->palHandle)->AttachToTexture((TextureHandle *)pTile->handle[res]);
DWORD dwFlags = NULL;
WORD info = MPR_TI_PALETTE;
if (g_bEnableStaticTerrainTextures)
dwFlags or_eq TextureHandle::FLAG_HINT_STATIC;
((TextureHandle *)pTile->handle[res])->Create("TextureDB", info, 8, static_cast<UInt16>(pTile->width[res]), static_cast<UInt16>(pTile->height[res]), dwFlags);
((TextureHandle *)pTile->handle[res])->Load(0, 0, (BYTE*)pTile->bits[res]);
// Now that we don't need the local copy of the image, drop it
glReleaseMemory((char*)pTile->bits[res]);
pTile->bits[res] = NULL;
#ifdef _DEBUG
LoadedTextureCount--;
#endif
}
else
{
// sfr: guess, hackers placed the flags in height member variable... sigh
int width = getDDSWidth(pTile->height[res]);
int widthN = getDDSWidth(pTile->heightN[res]);
#if 0
if (pTile->height[res]&MPR_TI_16)
width = 16;
else if (pTile->height[res]&MPR_TI_32)
width = 32;
else if (pTile->height[res]&MPR_TI_64)
width = 64;
else if (pTile->height[res]&MPR_TI_128)
width = 128;
else if (pTile->height[res]&MPR_TI_256)
width = 256;
else if (pTile->height[res]&MPR_TI_512)
width = 512;
else if (pTile->height[res]&MPR_TI_1024)
width = 1024;
else if (pTile->height[res]&MPR_TI_2048)
width = 2048;
#endif
// Day texture
pTile->handle[res] = (UInt)new TextureHandle;
ShiAssert(pTile->handle[res]);
((TextureHandle *)pTile->handle[res])->Create(
"TextureDB", (DWORD)pTile->height[res], 32, static_cast<UInt16>(width), static_cast<UInt16>(width)
);
((TextureHandle *)pTile->handle[res])->Load(
0, 0, (BYTE*)pTile->bits[res], false, false, pTile->width[res]
);
// Night texture
pTile->handleN[res] = (UInt)new TextureHandle;
ShiAssert(pTile->handleN[res]);