-
Notifications
You must be signed in to change notification settings - Fork 99
/
texbank.cpp
894 lines (691 loc) · 23 KB
/
texbank.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
/***************************************************************************\
Context.h
Miro "Jammer" Torrielli
10Oct03
- Begin Major Rewrite
\***************************************************************************/
#include "stdafx.h"
#include <io.h>
#include <fcntl.h>
#include "Utils/lzss.h"
#include "Loader.h"
#include "grinline.h"
#include "StateStack.h"
#include "ObjectLOD.h"
#include "PalBank.h"
#include "TexBank.h"
#include "Image.h"
#include "TerrTex.h"
#include "FalcLib/include/playerop.h"
#include "FalcLib/include/dispopts.h"
// Static data members (used to avoid requiring "this" to be passed to every access function)
int TextureBankClass::nTextures = 0;
TexBankEntry* TextureBankClass::TexturePool = NULL;
TempTexBankEntry* TextureBankClass::TempTexturePool = NULL;
BYTE* TextureBankClass::CompressedBuffer = NULL;
int TextureBankClass::deferredLoadState = 0;
char TextureBankClass::baseName[256];
FileMemMap TextureBankClass::TexFileMap;
#ifdef _DEBUG
int TextureBankClass::textureCount = 0;
#endif
TexFlagsType *TextureBankClass::TexFlags;
BYTE* TextureBankClass::TexBuffer;
DWORD TextureBankClass::TexBufferSize;
bool TextureBankClass::RatedLoad;
short *TextureBankClass::CacheLoad, *TextureBankClass::CacheRelease;
short TextureBankClass::LoadIn, TextureBankClass::LoadOut, TextureBankClass::ReleaseIn, TextureBankClass::ReleaseOut;
DWORD gDebugTextureID;
#ifdef USE_SH_POOLS
extern MEM_POOL gBSPLibMemPool;
#endif
extern bool g_bUseMappedFiles;
int nVer; // Header version
void TextureBankClass::Setup(int nEntries)
{
// Create our array of texture headers
nTextures = nEntries;
if (nEntries)
{
#ifdef USE_SH_POOLS
TexturePool = (TexBankEntry *)MemAllocPtr(gBSPLibMemPool, sizeof(TexBankEntry) * nEntries, 0);
TempTexturePool = (TempTexBankEntry *)MemAllocPtr(gBSPLibMemPool, sizeof(TempTexBankEntry) * nEntries, 0);
#else
TexturePool = new TexBankEntry[nEntries];
TempTexturePool = new TempTexBankEntry[nEntries];
#endif
TexFlags = (TexFlagsType*) malloc(nTextures * sizeof(TexFlagsType));
memset(TexFlags, 0, nTextures * sizeof(TexFlagsType));
// Allocte acche with a little safety margin
CacheLoad = (short*) malloc(sizeof(short) * (nTextures + CACHE_MARGIN));
CacheRelease = (short*) malloc(sizeof(short) * (nTextures + CACHE_MARGIN));
}
else
{
TexturePool = NULL;
TempTexturePool = NULL;
TexFlags = NULL;
TexBuffer = NULL;
CacheLoad = NULL;
CacheRelease = NULL;
}
RatedLoad = true;
LoadIn = LoadOut = ReleaseIn = ReleaseOut = 0;
}
void TextureBankClass::Cleanup(void)
{
// wait for any update on textures
WaitUpdates();
// Clean up our array of texture headers
#ifdef USE_SH_POOLS
MemFreePtr(TexturePool);
MemFreePtr(TempTexturePool);
#else
delete[] TexturePool;
delete[] TempTexturePool;
#endif
TexturePool = NULL;
TempTexturePool = NULL;
nTextures = 0;
if (TexFlags) free(TexFlags), TexFlags = NULL;
// Clean up the decompression buffer
#ifdef USE_SH_POOLS
MemFreePtr(CompressedBuffer);
#else
delete[] CompressedBuffer;
#endif
CompressedBuffer = NULL;
RatedLoad = false;
if (CacheLoad) free(CacheLoad), CacheLoad = NULL;
if (CacheRelease) free(CacheRelease), CacheRelease = NULL;
LoadIn = LoadOut = ReleaseIn = ReleaseOut = 0;
// Close our texture resource file
if (TexFileMap.IsReady())
{
CloseTextureFile();
}
}
void TextureBankClass::ReadPool(int file, char *basename)
{
int result;
int maxCompressedSize;
ZeroMemory(baseName, sizeof(baseName));
sprintf(baseName, "%s", basename);
// Read the number of textures in the pool
result = read(file, &nTextures, sizeof(nTextures));
if (nTextures == 0) return;
// Read the size of the biggest compressed texture in the pool
result = read(file, &maxCompressedSize, sizeof(maxCompressedSize));
// HACK - KO.dxh version - Note maxCompressedSize is used for the old BMP
// textures, not DDS textures.
nVer = maxCompressedSize;
#ifdef USE_SH_POOLS
CompressedBuffer = (BYTE *)MemAllocPtr(gBSPLibMemPool, sizeof(BYTE) * maxCompressedSize, 0);
#else
CompressedBuffer = new BYTE[maxCompressedSize];
#endif
ShiAssert(CompressedBuffer);
if (CompressedBuffer)
{
ZeroMemory(CompressedBuffer, maxCompressedSize);
}
// Setup the pool
Setup(nTextures);
// sfr: Ok some ????? did this and now we cannot change Texture class a bit
// since this stop working
result = read(file, TempTexturePool, sizeof(*TempTexturePool) * nTextures);
if (result < 0)
{
char message[256];
sprintf(message, "Reading object texture bank: %s", strerror(errno));
ShiError(message);
}
for (int i = 0; i < nTextures; i++)
{
TexturePool[i].fileOffset = TempTexturePool[i].fileOffset;
TexturePool[i].fileSize = TempTexturePool[i].fileSize;
TexturePool[i].tex = TempTexturePool[i].tex;
TexturePool[i].tex.flags |= MPR_TI_INVALID;
TexturePool[i].texN = TempTexturePool[i].tex;
TexturePool[i].texN.flags |= MPR_TI_INVALID;
if (DisplayOptions.bMipmapping)
{
TexturePool[i].tex.flags |= MPR_TI_MIPMAP;
TexturePool[i].texN.flags |= MPR_TI_MIPMAP;
}
TexturePool[i].palID = 0;//TempTexturePool[i].palID;
TexturePool[i].refCount = 0;//TempTexturePool[i].refCount;
}
OpenTextureFile();
}
void TextureBankClass::FreeCompressedBuffer()
{
#ifdef USE_SH_POOLS
MemFreePtr(CompressedBuffer);
#else
delete[] CompressedBuffer;
#endif
CompressedBuffer = NULL;
}
void TextureBankClass::AllocCompressedBuffer(int maxCompressedSize)
{
#ifdef USE_SH_POOLS
CompressedBuffer = (BYTE *)MemAllocPtr(gBSPLibMemPool, sizeof(BYTE) * maxCompressedSize, 0);
#else
CompressedBuffer = new BYTE[maxCompressedSize];
#endif
ShiAssert(CompressedBuffer);
if (CompressedBuffer)
{
ZeroMemory(CompressedBuffer, maxCompressedSize);
}
}
void TextureBankClass::OpenTextureFile()
{
char filename[_MAX_PATH];
ShiAssert(!TexFileMap.IsReady());
strcpy(filename, baseName);
strcat(filename, ".TEX");
if (!TexFileMap.Open(filename, FALSE, !g_bUseMappedFiles))
{
char message[256];
sprintf(message, "Failed to open object texture file %s\n", filename);
ShiError(message);
}
}
void TextureBankClass::CloseTextureFile(void)
{
TexFileMap.Close();
}
void TextureBankClass::Reference(int id)
{
int isLoaded;
gDebugTextureID = id;
ShiAssert(IsValidIndex(id));
// Get our reference to this texture recorded to ensure it doesn't disappear out from under us
//EnterCriticalSection(&ObjectLOD::cs_ObjectLOD);
isLoaded = TexturePool[id].refCount;
ShiAssert(isLoaded >= 0);
TexturePool[id].refCount++;
// If we already have the data, just verify that fact. Otherwise, load it.
if (isLoaded)
{
ShiAssert(TexturePool[id].tex.imageData || TexturePool[id].tex.TexHandle() || deferredLoadState);
//LeaveCriticalSection(&ObjectLOD::cs_ObjectLOD);
}
else
{
ShiAssert(TexFileMap.IsReady());
ShiAssert(CompressedBuffer);
ShiAssert(TexturePool[id].tex.imageData == NULL);
ShiAssert(TexturePool[id].tex.TexHandle() == NULL);
// Get the palette pointer
// would be great if we could set a flag saying this palette comes from bank...
// but since we cannot add anything to texture structure (because Jammer read them
// directly from file instead of from a method) I make the check when releasing
// the palette.
TexturePool[id].tex.SetPalette(&ThePaletteBank.PalettePool[TexturePool[id].palID]);
ShiAssert(TexturePool[id].tex.GetPalette());
TexturePool[id].tex.GetPalette()->Reference();
// Mark for the request if not already marked
if (!TexFlags[id].OnOrder)
{
TexFlags[id].OnOrder = true;
// put into load cache
CacheLoad[LoadIn++] = id;
// Ring the pointer
if (LoadIn >= (nTextures + CACHE_MARGIN)) LoadIn = 0;
// Kick the Loader
TheLoader.WakeUp();
}
}
gDebugTextureID = -1;
}
// Calls to this func are enclosed in the critical section cs_ObjectLOD by ObjectLOD::Unload()
void TextureBankClass::Release(int id)
{
ShiAssert(IsValidIndex(id));
ShiAssert(TexturePool[id].refCount > 0);
// RED - no reference, no party... !!!!!
if (!TexturePool[id].refCount) return;
TexturePool[id].refCount--;
if (TexturePool[id].refCount == 0)
{
if (!TexFlags[id].OnRelease)
{
TexFlags[id].OnRelease = true;
// put into load cache
CacheRelease[ReleaseIn++] = id;
// Ring the pointer
if (ReleaseIn >= (nTextures + CACHE_MARGIN)) ReleaseIn = 0;
// Kick the Loader
TheLoader.WakeUp();
}
}
}
void TextureBankClass::ReadImageData(int id, bool forceNoDDS)
{
int retval;
int size;
BYTE *cdata;
//sfr: added for more control
int cdataSize;
ShiAssert(TexturePool[id].refCount);
if (!forceNoDDS && DisplayOptions.m_texMode == DisplayOptionsClass::TEX_MODE_DDS)
{
ReadImageDDS(id);
ReadImageDDSN(id);
return;
}
if (g_bUseMappedFiles)
{
cdata = TexFileMap.GetData(TexturePool[id].fileOffset, TexturePool[id].fileSize);
cdataSize = TexturePool[id].fileSize - TexturePool[id].fileOffset;
ShiAssert(cdata);
}
else
{
if (!TexFileMap.ReadDataAt(TexturePool[id].fileOffset, CompressedBuffer, TexturePool[id].fileSize))
{
char message[120];
sprintf(message, "%s: Bad object texture seek (%0d)", strerror(errno), TexturePool[id].fileOffset);
ShiError(message);
}
cdata = CompressedBuffer;
//sfr: in this case, im not sure size is this
// FRB - fileOffset - fileOffset ????
//cdataSize = TexturePool[id].fileOffset - TexturePool[id].fileOffset;
cdataSize = TexturePool[id].fileSize - TexturePool[id].fileOffset;
}
// Allocate memory for the new texture
size = TexturePool[id].tex.dimensions;
size = size * size;
TexturePool[id].tex.imageData = glAllocateMemory(size, FALSE);
ShiAssert(TexturePool[id].tex.imageData);
// Uncompress the data into the texture structure
//sfr: using new cdataSize for control
retval = LZSS_Expand(cdata, cdataSize, (BYTE*)TexturePool[id].tex.imageData, size);
ShiAssert(retval == TexturePool[id].fileSize);
#ifdef _DEBUG
textureCount++;
#endif
}
void TextureBankClass::SetDeferredLoad(BOOL state)
{
LoaderQ *request;
// Allocate space for the async request
request = new LoaderQ;
if (!request)
ShiError("Failed to allocate memory for a object texture load state change request");
// Build the data transfer request to get the required object data
request->filename = NULL;
request->fileoffset = 0;
request->callback = LoaderCallBack;
request->parameter = (void*)state;
// Submit the request to the asynchronous loader
TheLoader.EnqueueRequest(request);
}
void TextureBankClass::LoaderCallBack(LoaderQ* request)
{
BOOL state = (int)request->parameter;
//EnterCriticalSection(&ObjectLOD::cs_ObjectLOD);
// If we're turning deferred loads off, go back and do all the loads we held up
if (deferredLoadState && !state)
{
DWORD Count = 5;
// Check each texture
for (int id = 0; id < nTextures; id++)
{
// See if it is in use
if (TexturePool[id].refCount)
// This one is in use. Is it already loaded?
if (/*!TexturePool[id].tex.imageData &&*/ !TexturePool[id].tex.TexHandle())
{
// Nope, go get it.
if (!TexturePool[id].tex.imageData) ReadImageData(id);
TexturePool[id].tex.CreateTexture();
Count--;
//TexturePool[id].tex.FreeImage();
}
if (!Count) break;
}
}
// Now store the new state
deferredLoadState = state;
//LeaveCriticalSection(&ObjectLOD::cs_ObjectLOD);
// Free the request queue entry
delete request;
}
void TextureBankClass::FlushHandles(void)
{
int id;
for (id = 0; id < nTextures; id++)
{
ShiAssert(TexturePool[id].refCount == 0);
while (TexturePool[id].refCount > 0)
{
Release(id);
}
}
WaitUpdates();
}
void TextureBankClass::Select(int id)
{
}
void TextureBankClass::SelectHandle(DWORD TexHandle)
{
TheStateStack.context->SelectTexture1(TexHandle);
}
BOOL TextureBankClass::IsValidIndex(int id)
{
return((id >= 0) && (id < nTextures));
}
void TextureBankClass::RestoreAll()
{
}
void TextureBankClass::SyncDDSTextures(bool bForce)
{
char szFile[256];
FILE *fp;
ShiAssert(TexturePool);
CreateDirectory(baseName, NULL);
for (DWORD id = 0; id < (DWORD)nTextures; id++)
{
sprintf(szFile, "%s\\%d.dds", baseName, id);
fp = fopen(szFile, "rb");
if (!fp || bForce)
{
if (fp)
fclose(fp);
UnpackPalettizedTexture(id);
}
else
fclose(fp);
TexturePool[id].tex.flags |= MPR_TI_DDS;
TexturePool[id].tex.flags &= ~MPR_TI_PALETTE;
TexturePool[id].texN.flags |= MPR_TI_DDS;
TexturePool[id].texN.flags &= ~MPR_TI_PALETTE;
}
}
void TextureBankClass::UnpackPalettizedTexture(DWORD id)
{
char szFile[256];
CreateDirectory(baseName, NULL);
if (TexturePool[id].tex.dimensions > 0)
{
//sfr: (see my comment regarding palette origin above)
TexturePool[id].tex.SetPalette(&ThePaletteBank.PalettePool[TexturePool[id].palID]);
ShiAssert(TexturePool[id].tex.GetPalette());
TexturePool[id].tex.GetPalette()->Reference();
ReadImageData(id, true);
sprintf(szFile, "%s\\%d", baseName, id);
TexturePool[id].tex.DumpImageToFile(szFile, TexturePool[id].palID);
Release(id);
}
else
{
sprintf(szFile, "%s\\%d.dds", baseName, id);
FILE* fp = fopen(szFile, "wb");
fclose(fp);
}
}
void TextureBankClass::ReadImageDDS(DWORD id)
{
DDSURFACEDESC2 ddsd;
DWORD dwSize, dwMagic;
char szFile[256];
FILE *fp;
TexturePool[id].tex.flags |= MPR_TI_DDS;
TexturePool[id].tex.flags &= ~MPR_TI_PALETTE;
sprintf(szFile, "%s\\%d.dds", baseName, id);
fp = fopen(szFile, "rb");
// RV - RED - Avoid CTD if a missing texture
if (!fp) return;
fread(&dwMagic, 1, sizeof(DWORD), fp);
ShiAssert(dwMagic == MAKEFOURCC('D', 'D', 'S', ' '));
// Read first compressed mipmap
fread(&ddsd, 1, sizeof(DDSURFACEDESC2), fp);
// MLR 1/25/2004 - Little kludge so F4 can read DDS files made by dxtex
if (ddsd.dwLinearSize == 0)
{
if (ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '3') ||
ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '5'))
{
ddsd.dwLinearSize = ddsd.dwWidth * ddsd.dwWidth;
ddsd.dwFlags |= DDSD_LINEARSIZE;
}
if (ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '1'))
{
ddsd.dwLinearSize = ddsd.dwWidth * ddsd.dwWidth / 2;
ddsd.dwFlags |= DDSD_LINEARSIZE;
}
}
ShiAssert(ddsd.dwFlags & DDSD_LINEARSIZE)
switch (ddsd.ddpfPixelFormat.dwFourCC)
{
case MAKEFOURCC('D', 'X', 'T', '1'):
TexturePool[id].tex.flags |= MPR_TI_DXT1;
break;
case MAKEFOURCC('D', 'X', 'T', '3'):
TexturePool[id].tex.flags |= MPR_TI_DXT3;
break;
case MAKEFOURCC('D', 'X', 'T', '5'):
TexturePool[id].tex.flags |= MPR_TI_DXT5;
break;
default:
ShiAssert(false);
}
switch (ddsd.dwWidth)
{
case 16:
TexturePool[id].tex.flags |= MPR_TI_16;
break;
case 32:
TexturePool[id].tex.flags |= MPR_TI_32;
break;
case 64:
TexturePool[id].tex.flags |= MPR_TI_64;
break;
case 128:
TexturePool[id].tex.flags |= MPR_TI_128;
break;
case 256:
TexturePool[id].tex.flags |= MPR_TI_256;
break;
case 512:
TexturePool[id].tex.flags |= MPR_TI_512;
break;
case 1024:
TexturePool[id].tex.flags |= MPR_TI_1024;
break;
case 2048:
TexturePool[id].tex.flags |= MPR_TI_2048;
break;
default:
ShiAssert(false);
}
dwSize = ddsd.dwLinearSize;
TexturePool[id].tex.imageData = (BYTE *)glAllocateMemory(dwSize, FALSE);
fread(TexturePool[id].tex.imageData, 1, dwSize, fp);
TexturePool[id].tex.dimensions = dwSize;
fclose(fp);
#ifdef _DEBUG
textureCount++;
#endif
}
void TextureBankClass::ReadImageDDSN(DWORD id)
{
DDSURFACEDESC2 ddsd;
DWORD dwSize, dwMagic;
char szFile[256];
FILE *fp;
sprintf(szFile, "%s\\%dN.dds", baseName, id);
fp = fopen(szFile, "rb");
if (!fp)
{
return;
}
TexturePool[id].texN.flags |= MPR_TI_DDS;
TexturePool[id].texN.flags &= ~MPR_TI_PALETTE;
TexturePool[id].texN.flags &= ~MPR_TI_INVALID;
fread(&dwMagic, 1, sizeof(DWORD), fp);
ShiAssert(dwMagic == MAKEFOURCC('D', 'D', 'S', ' '));
// Read first compressed mipmap
fread(&ddsd, 1, sizeof(DDSURFACEDESC2), fp);
// MLR 1/25/2004 - Little kludge so F4 can read DDS files made by dxtex
if (ddsd.dwLinearSize == 0)
{
if (ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '3') ||
ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '5'))
{
ddsd.dwLinearSize = ddsd.dwWidth * ddsd.dwWidth;
ddsd.dwFlags |= DDSD_LINEARSIZE;
}
if (ddsd.ddpfPixelFormat.dwFourCC == MAKEFOURCC('D', 'X', 'T', '1'))
{
ddsd.dwLinearSize = ddsd.dwWidth * ddsd.dwWidth / 2;
ddsd.dwFlags |= DDSD_LINEARSIZE;
}
}
ShiAssert(ddsd.dwFlags & DDSD_LINEARSIZE)
switch (ddsd.ddpfPixelFormat.dwFourCC)
{
case MAKEFOURCC('D', 'X', 'T', '1'):
TexturePool[id].texN.flags |= MPR_TI_DXT1;
break;
case MAKEFOURCC('D', 'X', 'T', '3'):
TexturePool[id].tex.flags |= MPR_TI_DXT3;
break;
case MAKEFOURCC('D', 'X', 'T', '5'):
TexturePool[id].texN.flags |= MPR_TI_DXT5;
break;
default:
ShiAssert(false);
}
switch (ddsd.dwWidth)
{
case 16:
TexturePool[id].texN.flags |= MPR_TI_16;
break;
case 32:
TexturePool[id].texN.flags |= MPR_TI_32;
break;
case 64:
TexturePool[id].texN.flags |= MPR_TI_64;
break;
case 128:
TexturePool[id].texN.flags |= MPR_TI_128;
break;
case 256:
TexturePool[id].texN.flags |= MPR_TI_256;
break;
case 512:
TexturePool[id].texN.flags |= MPR_TI_512;
break;
case 1024:
TexturePool[id].texN.flags |= MPR_TI_1024;
break;
case 2048:
TexturePool[id].texN.flags |= MPR_TI_2048;
break;
default:
ShiAssert(false);
}
dwSize = ddsd.dwLinearSize;
TexturePool[id].texN.imageData = (BYTE *)glAllocateMemory(dwSize, FALSE);
fread(TexturePool[id].texN.imageData, 1, dwSize, fp);
TexturePool[id].texN.dimensions = dwSize;
fclose(fp);
#ifdef _DEBUG
textureCount++;
#endif
}
void TextureBankClass::RestoreTexturePool()
{
for (int i = 0; i < nTextures; i++)
{
TexturePool[i].fileOffset = TempTexturePool[i].fileOffset;
TexturePool[i].fileSize = TempTexturePool[i].fileSize;
TexturePool[i].tex = TempTexturePool[i].tex;
TexturePool[i].texN = TempTexturePool[i].tex;
TexturePool[i].palID = TempTexturePool[i].palID;
TexturePool[i].refCount = TempTexturePool[i].refCount;
}
}
DWORD TextureBankClass::GetHandle(DWORD id)
{
// if already on release, avoid using or requesting it
if (TexFlags[id].OnRelease) return NULL;
// if the Handle is prsent, return it
if (IsValidIndex(id) && TexturePool[id].tex.TexHandle()) return TexturePool[id].tex.TexHandle();
// return a null pointer that means BLANK SURFACE
return NULL;
}
// RED - This function manages to load and create requested textures
bool TextureBankClass::UpdateBank(void)
{
DWORD id;
// till when data to update into caches
while (LoadIn != LoadOut || ReleaseIn != ReleaseOut)
{
// check for textures to be released
if (ReleaseIn != ReleaseOut)
{
// get the 1st texture Id from cache
id = CacheRelease[ReleaseOut++];
// if not an order again, and no Referenced, release it
if (!TexFlags[id].OnOrder && !TexturePool[id].refCount && TexFlags[id].OnRelease) TexturePool[id].tex.FreeAll();
// clear flag, in any case
TexFlags[id].OnRelease = false;
// ring the pointer
if (ReleaseOut >= (nTextures + CACHE_MARGIN)) ReleaseOut = 0;
// if any action, terminate here
if (RatedLoad) return true;
}
// check for textures to be released
if (LoadIn != LoadOut)
{
// get the 1st texture Id from cache
id = CacheLoad[LoadOut++];
// if Texture not yet loaded, load it
if (!TexturePool[id].tex.imageData) ReadImageData(id);
// if Texture not yet crated, crate it
if (!TexturePool[id].tex.TexHandle()) TexturePool[id].tex.CreateTexture();
// clear flag, in any case
TexFlags[id].OnOrder = false;
// ring the pointer
if (LoadOut >= (nTextures + CACHE_MARGIN)) LoadOut = 0;
// if any action, terminate here
if (RatedLoad) return true;
}
}
// if here, nothing done, back is up to date
return false;
}
void TextureBankClass::WaitUpdates(void)
{
// if no data to wait, exit here
if (LoadIn == LoadOut && ReleaseIn == ReleaseOut) return;
// Pause the Loader...
TheLoader.SetPause(true);
while (!TheLoader.Paused());
// Not slow loading
RatedLoad = false;
// Parse all objects till any opration to do
while (UpdateBank());
// Restore rated loading
RatedLoad = true;
// Run the Loader again
TheLoader.SetPause(false);
}
void TextureBankClass::CreateCallBack(LoaderQ* request)
{
}
void TextureBankClass::ReferenceTexSet(DWORD *TexList, DWORD Nr)
{
while (Nr--) Reference(*TexList), TexList++;
}
void TextureBankClass::ReleaseTexSet(DWORD *TexList, DWORD Nr)
{
while (Nr--) Release(*TexList), TexList++;
}