-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathGraphicsManager.cpp
1231 lines (1058 loc) · 27.5 KB
/
GraphicsManager.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
#include "GraphicsManager.hpp"
#include "COF.hpp"
#include "DC6.hpp"
#include "DT1.hpp"
#include "Logging.hpp"
#include "FileSystem.hpp"
#include "Palette.hpp"
#include "TBL_Font.hpp"
#include <atomic>
GraphicsManager* graphicsManager;
/**
* DCCGraphicsHandle is the IGraphicsHandle implementation for DCC files.
*/
DCCReference::DCCReference(const char* fileName, GraphicsUsagePolicy usagePolicy)
: IGraphicsReference(usagePolicy)
{
D2Lib::strncpyz(dccHandleName, fileName, sizeof(dccHandleName));
bLoaded = false;
}
size_t DCCReference::GetTotalSizeInBytes(int32_t frame)
{
return 0;
}
size_t DCCReference::GetNumberOfFrames()
{
return 0;
}
void DCCReference::GetGraphicsData(void** pixels, int32_t frame,
uint32_t* width, uint32_t* height, int32_t* offsetX, int32_t* offsetY, int direction)
{
if (direction < 0)
{
direction = 0;
}
if (width)
{
*width = dccFile.directions[direction].frames[frame].dwWidth;
}
if (height)
{
*height = dccFile.directions[direction].frames[frame].dwHeight;
}
// The destination rectangle is oriented from the upper left corner, but whenever we do a draw call,
// we are orienting from the "base point" of the token's DCC files. So we need to correct that.
if (offsetX)
{
*offsetX = -(dccFile.directions[direction].frames[frame].nMinX - dccFile.directions[direction].nMinX);
*offsetX += dccFile.directions[direction].frames[frame].nXOffset;
}
if (offsetY)
{
*offsetY = -(dccFile.directions[direction].frames[frame].nMinY - dccFile.directions[direction].nMinY);
*offsetY += dccFile.directions[direction].frames[frame].nYOffset;
*offsetY -= dccFile.nDirectionH[direction] - 1;
}
}
void DCCReference::GetGraphicsInfo(bool bAtlassing, int32_t start, int32_t end, uint32_t* width, uint32_t* height)
{
}
void DCCReference::IterateFrames(bool bAtlassing, int32_t start, int32_t end, AtlassingCallback callback)
{
}
void DCCReference::GetAtlasInfo(int32_t frame, uint32_t* x, uint32_t* y, uint32_t* totalWidth, uint32_t* totalHeight, int direction)
{
if (direction < 0)
{
direction = 0;
}
if (x)
{
*x = xOffsetForFrame[direction][frame];
}
if (y)
{
*y = yOffsetForFrame[direction][frame];
}
if (totalWidth)
{
*totalWidth = dccFile.directions[direction].nWidth * dccFile.header.dwFramesPerDirection;
}
if (totalHeight)
{
*totalHeight = dccFile.directions[direction].nHeight;
}
}
void DCCReference::Deallocate()
{
switch (usagePolicy)
{
case UsagePolicy_Permanent:
graphicsManager->RemovePermanentDCCGraphic(dccHandleName);
break;
case UsagePolicy_Temporary:
// not yet implemented
break;
}
}
void* DCCReference::LoadSingleDirection(unsigned int direction, AnimTextureAllocCallback allocCallback, AnimTextureDecodeCallback decodeCallback)
{
static AnimTextureDecodeCallback g_decoder;
static AnimTextureAllocCallback g_allocator;
static void* g_newData;
static int g_frameStart;
static unsigned int* g_frameXOffsets;
static unsigned int* g_frameYOffsets;
static unsigned int* g_directionWidth;
static unsigned int* g_directionHeight;
uint32_t tempDirectionWidth, tempDirectionHeight;
if (!bLoaded)
{
LoadDCCFile();
}
if (directionCount <= 0 || direction == -1)
{
if (bAreGraphicsLoaded)
{
return loadedGraphicsData;
}
direction = 0;
}
else if (bAreGraphicsLoadedForDirection[direction])
{
return loadedGraphicsForDirection[direction];
}
#ifdef PAUL_SIRAMY_DCC
g_allocator = allocCallback;
g_decoder = decodeCallback;
g_frameStart = 0;
g_frameXOffsets = xOffsetForFrame[direction];
g_frameYOffsets = yOffsetForFrame[direction];
g_directionWidth = &tempDirectionWidth;
g_directionHeight = &tempDirectionHeight;
DCC::DecodeDirection(&dccFile, 0, [](unsigned int width, unsigned int height) {
g_newData = g_allocator(width, height);
}, [](BYTE* bitmap, uint32_t frameNum, int32_t frameX, int32_t frameY, uint32_t frameW, uint32_t frameH) {
g_frameXOffsets[frameNum] = frameX;
g_frameYOffsets[frameNum] = frameY;
g_decoder((void*)bitmap, g_newData, frameNum, frameX, frameY, frameW, frameH);
g_frameStart += frameW;
*g_directionWidth = g_frameStart;
if (frameY + frameH > *g_directionHeight)
{
*g_directionHeight = frameY + frameH;
}
});
#else
// Do alloc callback?
DCC::GetDirectionSize(&dccFile, direction, &this->directionWidth[direction], &this->directionHeight[direction]);
g_newData = allocCallback(this->directionWidth[direction], this->directionHeight[direction]);
g_decoder = decodeCallback;
tempDirectionWidth = this->directionWidth[direction];
tempDirectionHeight = this->directionHeight[direction];
// Do decode
g_frameStart = 0;
g_frameXOffsets = xOffsetForFrame[direction];
g_frameYOffsets = yOffsetForFrame[direction];
g_directionWidth = &tempDirectionWidth;
g_directionHeight = &tempDirectionHeight;
DCC::DecodeDirection(&dccFile, 0, [](BYTE* bitmap, uint32_t frameNum, int32_t frameX, int32_t frameY,
uint32_t frameW, uint32_t frameH) {
g_frameXOffsets[frameNum] = g_frameStart/* + frameX*/;
g_frameYOffsets[frameNum] = frameY;
g_decoder((void*)bitmap, g_newData, frameNum, g_frameStart, frameY, frameW, frameH);
g_frameStart += frameW;
*g_directionWidth = g_frameStart;
if (frameY + frameH > *g_directionHeight)
{
*g_directionHeight = frameY + frameH;
}
});
#endif
bAreGraphicsLoadedForDirection[direction] = true;
loadedGraphicsForDirection[direction] = g_newData;
return g_newData;
}
void DCCReference::LoadDCCFile()
{
DCC::LoadAnimation(dccHandleName, &dccFile);
bLoaded = true;
}
/**
* DC6GraphicsHandle is the IGraphicsHandle implementation for DC6 files.
*/
DC6Reference::DC6Reference(const char* fileName, GraphicsUsagePolicy usagePolicy)
: IGraphicsReference(usagePolicy)
{
D2Lib::strncpyz(filePath, fileName, sizeof(filePath));
bAreGraphicsLoaded = false;
loadedGraphicsData = nullptr;
}
DC6Reference::~DC6Reference()
{
DC6::UnloadImage(&image);
}
size_t DC6Reference::GetTotalSizeInBytes(int32_t frame)
{
if (!bLoaded)
{
DC6::LoadImage(filePath, &image);
bLoaded = true;
}
if (frame < 0 || frame > image.header.dwFrames)
{
return image.dwTotalWidth * image.dwTotalHeight;
}
return image.pFrames[frame].fh.dwWidth * image.pFrames[frame].fh.dwHeight;
}
size_t DC6Reference::GetNumberOfFrames()
{
if (!bLoaded)
{
DC6::LoadImage(filePath, &image);
bLoaded = true;
}
return image.header.dwFrames;
}
void DC6Reference::GetGraphicsData(void** pixels, int32_t frame,
uint32_t* width, uint32_t* height, int32_t* offsetX, int32_t* offsetY, int direction)
{
if (!bLoaded)
{
DC6::LoadImage(filePath, &image);
bLoaded = true;
}
if (frame < 0 || frame > image.header.dwFrames)
{
return;
}
if (width)
{
*width = image.pFrames[frame].fh.dwWidth;
}
if (height)
{
*height = image.pFrames[frame].fh.dwHeight;
}
if (offsetX)
{
*offsetX = image.pFrames[frame].fh.dwOffsetX;
}
if (offsetY)
{
*offsetY = image.pFrames[frame].fh.dwOffsetY;
}
if (pixels)
{
*pixels = DC6::GetPixelsAtFrame(&image, 0, frame, nullptr);
}
}
void DC6Reference::GetGraphicsInfo(bool bAtlassing, int32_t start, int32_t end, uint32_t* width, uint32_t* height)
{
if (!bLoaded)
{
DC6::LoadImage(filePath, &image);
bLoaded = true;
}
if (end < 0)
{
end = image.header.dwFrames - 1;
}
DWORD dwTotalWidth, dwTotalHeight;
if (bAtlassing)
{
DC6::AtlasStats(&image, start, end, &dwTotalWidth, &dwTotalHeight);
}
else
{
DWORD dwFrameWidth, dwFrameHeight;
DC6::StitchStats(&image, start, end, &dwFrameWidth, &dwFrameHeight, &dwTotalWidth, &dwTotalHeight);
}
if (width)
{
*width = dwTotalWidth;
}
if (height)
{
*height = dwTotalHeight;
}
}
void DC6Reference::IterateFrames(bool bAtlassing, int32_t start, int32_t end, AtlassingCallback callback)
{
if (!bLoaded)
{
DC6::LoadImage(filePath, &image);
bLoaded = true;
}
if (end < 0)
{
end = image.header.dwFrames - 1;
}
if (bAtlassing)
{
int currentX = 0;
int currentY = 0;
for (int i = 0; i <= end - start; i++)
{
DC6Frame* pFrame = &image.pFrames[start + i];
if (callback)
{
callback(DC6::GetPixelsAtFrame(&image, 0, start + i, nullptr), start + i,
currentX, currentY, pFrame->fh.dwWidth, pFrame->fh.dwHeight);
}
currentX += image.dwMaxFrameWidth;
if (currentX + image.dwMaxFrameWidth > 4096) // arbitrary cutoff
{
currentX = 0;
currentY += image.dwMaxFrameHeight;
}
}
}
else
{
DWORD dwTotalWidth, dwTotalHeight, dwFrameWidth, dwFrameHeight;
DC6::StitchStats(&image, start, end, &dwFrameWidth, &dwFrameHeight, &dwTotalWidth, &dwTotalHeight);
for (int i = 0; i <= end - start; i++)
{
DC6Frame* pFrame = &image.pFrames[start + i];
int dwBlitToX = (i % dwFrameWidth) * 256;
int dwBlitToY = (int)floor(i / (float)dwFrameWidth) * 255;
if (callback)
{
callback(DC6::GetPixelsAtFrame(&image, 0, start + i, nullptr), start + i,
dwBlitToX, dwBlitToY, pFrame->fh.dwWidth, pFrame->fh.dwHeight);
}
}
}
}
void DC6Reference::GetAtlasInfo(int32_t frame, uint32_t* x, uint32_t* y, uint32_t* totalWidth, uint32_t* totalHeight, int direction)
{
*totalWidth = 0;
*totalHeight = 0;
int currentX = 0;
int maxX = 0;
int currentY = 0;
for (int i = 0; i < image.header.dwFrames; i++)
{
if (i == frame)
{
*x = currentX;
*y = currentY;
}
currentX += image.dwMaxFrameWidth;
if (currentX + image.dwMaxFrameWidth > 4096)
{
maxX = currentX - image.dwMaxFrameWidth;
currentX = 0;
currentY += image.dwMaxFrameHeight;
}
}
if (maxX == 0)
{
maxX = currentX;
}
*totalWidth = maxX + image.dwMaxFrameWidth;
*totalHeight = currentY + image.dwMaxFrameHeight;
}
void DC6Reference::Deallocate()
{
switch (usagePolicy)
{
case UsagePolicy_Permanent:
graphicsManager->RemovePermanentDC6Graphic(filePath);
break;
case UsagePolicy_Temporary:
// not implemented yet
break;
}
}
/**
* DT1GraphicsHandle is the IGraphicsHandle implementation for DT1 files.
*/
DT1Reference::DT1Reference(const char* fileName, GraphicsUsagePolicy usagePolicy)
: IGraphicsReference(usagePolicy)
{
dt1Handle = DT1::LoadDT1(fileName);
D2Lib::strncpyz(filePath, fileName, MAX_D2PATH);
}
size_t DT1Reference::GetTotalSizeInBytes(int32_t frame)
{
return 0; // FIXME?
}
size_t DT1Reference::GetNumberOfFrames()
{
return DT1::GetNumBlocks(dt1Handle);
}
void DT1Reference::GetGraphicsData(void** pixels, int32_t frame,
uint32_t* width, uint32_t* height, int32_t* offsetX, int32_t* offsetY, int direction)
{
// not needed
}
void DT1Reference::GetGraphicsInfo(bool bAtlassing, int32_t start, int32_t end, uint32_t* width, uint32_t* height)
{
size_t numBlocks = GetNumberOfFrames();
uint32_t widthTemp = 0;
uint32_t heightTemp = 0;
if (end < 0 || end >= numBlocks)
{ // bounds-check
end = numBlocks - 1;
}
if (start < 0 || start > numBlocks)
{ // bound-check
start = 0;
}
if (start > end)
{ // flip em
int32_t temp = start;
start = end;
end = temp;
}
for (int32_t i = start; i <= end; i++)
{
const DT1File::DT1Block* block = DT1::GetBlock(dt1Handle, i);
widthTemp += block->sizeX;
if (block->sizeY > heightTemp)
{
heightTemp = block->sizeY;
}
}
if (width)
{
*width = widthTemp;
}
if (height)
{
*height = heightTemp;
}
}
void DT1Reference::IterateFrames(bool bAtlassing, int32_t start, int32_t end, AtlassingCallback callback)
{
size_t numBlocks = GetNumberOfFrames();
// do bounds checking and all that
if (end < 0 || end >= numBlocks)
{
end = numBlocks - 1;
}
if (start < 0 || start > numBlocks)
{
start = 0;
}
if (start > end)
{
int32_t temp = start;
start = end;
end = temp;
}
// pretty easy, get each block and go
for (int32_t i = start; i < end; i++)
{
uint32_t width, height;
int32_t x, y;
void* pix = DT1::DecodeDT1Block(dt1Handle, i, width, height, x, y);
callback(pix, i, x, y, width, height);
}
}
void DT1Reference::GetAtlasInfo(int32_t frame, uint32_t* x, uint32_t* y, uint32_t* totalWidth, uint32_t* totalHeight, int direction)
{
// not needed
}
void DT1Reference::Deallocate()
{
switch (usagePolicy)
{
case UsagePolicy_Permanent:
graphicsManager->RemovePermanentDT1Graphic(filePath);
break;
case UsagePolicy_Temporary:
// not implemented
break;
}
}
/**
* Font handles are used to load fonts.
*/
FontReference::FontReference(const char* graphicsFile, const char* tbl, GraphicsUsagePolicy usagePolicy)
: IGraphicsReference(usagePolicy)
{
tblHandle = TBLFont::RegisterFont(tbl);
DC6::LoadImage(graphicsFile, &image);
D2Lib::strncpyz(handleName, tbl, sizeof(handleName));
}
FontReference::~FontReference()
{
DC6::UnloadImage(&image);
}
size_t FontReference::GetTotalSizeInBytes(int32_t frame)
{
return 0;
}
size_t FontReference::GetNumberOfFrames()
{
return 0;
}
void FontReference::GetGraphicsData(void** pixels, int32_t frame, uint32_t* width,
uint32_t* height, int32_t* offsetX, int32_t* offsetY, int direction)
{
TBLFontFile* fontFile = TBLFont::GetPointerFromHandle(tblHandle);
if(!fontFile || frame >= 256)
{
return;
}
if(width)
{
*width = fontFile->glyphs[frame].nWidth;
}
if(height)
{
//*height = fontFile->glyphs[frame].nHeight;
*height = image.pFrames[frame].fh.dwHeight;
}
if(offsetX)
{
*offsetX = fontFile->glyphs[frame].dwUnknown4;
}
if(offsetY)
{
*offsetY = fontFile->glyphs[frame].wUnknown3;
}
}
void FontReference::GetGraphicsInfo(bool bAtlassing, int32_t start, int32_t end,
uint32_t* width, uint32_t* height)
{
uint32_t maxWidth = 0;
uint32_t maxHeight = 0;
uint32_t totalHeight = 0;
TBLFontFile* fontFile = TBLFont::GetPointerFromHandle(tblHandle);
uint32_t rowWidth = 0;
for (int i = 0; i < 256; i++)
{
if (image.pFrames[i].fh.dwHeight > maxHeight)
{
maxHeight = image.pFrames[i].fh.dwHeight;
}
fontFile->glyphs[i].dwUnknown4 = rowWidth;
fontFile->glyphs[i].wUnknown3 = totalHeight;
rowWidth += image.pFrames[i].fh.dwWidth;
if ((i + 1) % 16 == 0)
{
if (rowWidth > maxWidth)
{
maxWidth = rowWidth;
}
totalHeight += maxHeight;
maxHeight = 0;
rowWidth = 0;
}
}
*width = maxWidth;
*height = totalHeight;
}
void FontReference::IterateFrames(bool bAtlassing, int32_t start, int32_t end,
AtlassingCallback callback)
{
TBLFontFile* fontFile = TBLFont::GetPointerFromHandle(tblHandle);
for (int i = 0; i < image.header.dwFrames; i++)
{
if (callback)
{
callback(DC6::GetPixelsAtFrame(&image, 0, i, nullptr),
i, fontFile->glyphs[i].dwUnknown4,
fontFile->glyphs[i].wUnknown3,
image.pFrames[i].fh.dwWidth,
image.pFrames[i].fh.dwHeight);
}
}
}
void FontReference::GetAtlasInfo(int32_t frame, uint32_t* x, uint32_t* y,
uint32_t* totalWidth, uint32_t* totalHeight, int direction)
{
}
float FontReference::GetCapHeight()
{
TBLFontFile* fontFile = TBLFont::GetPointerFromHandle(tblHandle);
return fontFile->nHeight;
}
void FontReference::Deallocate()
{
graphicsManager->RemoveFont(handleName);
}
/**
* Base class for all token types.
*/
void ITokenReference::SetTokenName(const char* _tokenName)
{
int i;
for (i = 0; i < 4 && _tokenName[i]; i++)
{
tokenName[i] = _tokenName[i];
}
if (i < 4)
{
tokenName[i] = '\0';
}
memset(cofFiles, INVALID_HANDLE, sizeof(cofFiles));
}
void ITokenReference::LoadCOF(unsigned int mode, unsigned int hitclass)
{
cofFiles[mode][hitclass] = COF::Register(GetTokenDataFolder(), GetTokenName(), GetModeName(mode), GetHitclassName(hitclass));
Log_ErrorAssertVoidReturn(cofFiles[mode][hitclass] != INVALID_HANDLE);
}
IGraphicsReference* ITokenReference::GetTokenGraphic(unsigned int component, unsigned int hitclass,
unsigned int mode, const char* armorClass)
{
IGraphicsReference* reference;
char ref[8];
unsigned short packed;
bool hasFallback = false;
// See explanation in ITokenReference for how this is used.
packed = ((unsigned short)mode << 10) | ((unsigned short)hitclass << 5) | (unsigned short)component;
ref[0] = (char)((packed & 0xFF00) >> 8) + 1; // this is to make sure we don't hit any null terminators
ref[1] = (char)(packed & 0x00FF) + 1; // see above
strncpy(&ref[2], armorClass, sizeof(ref) - 2);
handle graphicHandle;
if (m_cachedGraphicsReferences.Contains(ref, &graphicHandle))
{
return m_cachedGraphicsReferences[graphicHandle];
}
char path[MAX_D2PATH];
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%s%s.dcc",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode), GetHitclassName(hitclass));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent); // for now
if (reference == nullptr && hitclass == WC_HT2)
{ // freak case; RH always uses HT1. don't ask.
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%sHT1.dcc",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent); // for now
}
if (reference == nullptr)
{
// File not found - try using "HTH" as a fallback for the hitclass
if (hitclass != WC_HTH)
{
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%sHTH.dcc",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent); // for now
}
if (reference == nullptr && hitclass != WC_1HS)
{
// Try 1hs as a fallback?
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%s1HS.dcc",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent); // for now
}
if (reference == nullptr)
{
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%s%s.dc6",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode), GetHitclassName(hitclass));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent);
if (reference == nullptr && hitclass != WC_HTH)
{ // try using "HTH" as a fallback for the hitclass
snprintf(path, MAX_D2PATH, "data\\global\\%s\\%s\\%s\\%s%s%s%sHTH.dc6",
GetTokenDataFolder(), GetTokenName(), GetComponentName(component),
GetTokenName(), GetComponentName(component), armorClass, GetModeName(mode));
reference = graphicsManager->CreateReference(path, UsagePolicy_Permanent);
}
}
}
// Revert to a fallback.
if (reference == nullptr)
{
GetFallbackForComponentMode(hasFallback, mode);
if (hasFallback)
{
return GetTokenGraphic(component, hitclass, mode, armorClass);
}
}
if (reference == nullptr)
{
return nullptr;
}
//Log_ErrorAssertReturn(reference != nullptr, reference);
m_cachedGraphicsReferences[ref] = reference;
return reference;
}
bool ITokenReference::HasComponentForMode(unsigned int component, unsigned int hitclass, unsigned int mode)
{
// Don't render shield in certain modes
if (component == COMP_SHIELD)
{
switch (hitclass)
{
case WC_1JS:
case WC_1JT:
case WC_1SS:
case WC_1ST:
case WC_BOW:
case WC_2HS:
case WC_XBW:
case WC_HT2:
return false;
}
}
// Load COF if it isn't already loaded
if (cofFiles[mode][hitclass] == INVALID_HANDLE)
{
LoadCOF(mode, hitclass);
}
return COF::LayerPresent(cofFiles[mode][hitclass], component);
}
const char* ITokenReference::GetHitclassName(unsigned int hitclass)
{
static const char* HitclassNames[WC_MAX] = {
"HTH", "HTH", "BOW", "1HS", "1HT", "STF", "2HS", "2HT", "XBW",
"1JS", "1JT", "1SS", "1ST", "HT1", "HT2"
};
return HitclassNames[hitclass];
}
const char* ITokenReference::GetModeName(unsigned int mode)
{
static const char* MonsterModes[MONMODE_MAX] = {
"DT", "NU", "WL", "GH", "A1", "A2", "BL", "SC",
"S1", "S2", "S3", "S4", "DD", "KB", "XX", "RN"
};
static const char* PlayerModes[PLRMODE_MAX] = {
"DT", "NU", "WL", "RN", "GH", "TN", "TW", "A1",
"A2", "BL", "SC", "TH", "KK", "S1", "S2", "S3",
"S4", "DD", "SQ", "KB"
};
static const char* ObjectModes[OBJMODE_MAX] = {
"NU", "OP", "ON", "S1", "S2", "S3", "S4", "S5"
};
switch (GetTokenType())
{
default:
case TOKEN_MONSTER:
return MonsterModes[mode];
case TOKEN_CHAR:
return PlayerModes[mode];
case TOKEN_OBJECT:
return ObjectModes[mode];
}
}
const char* ITokenReference::GetComponentName(unsigned int component)
{
static const char* ComponentNames[COMP_MAX] = {
"HD", "TR", "LG", "RA", "LA", "RH", "LH", "SH",
"S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8"
};
return ComponentNames[component];
}
const char* ITokenReference::GetTokenDataFolder()
{
switch (GetTokenType())
{
default:
case TOKEN_MONSTER:
return "monsters";
case TOKEN_CHAR:
return "chars";
case TOKEN_OBJECT:
return "objects";
}
}
inline const BYTE ITokenReference::GetNumberOfFrames(int mode, int weaponClass)
{
// Load COF if it isn't already loaded
if (cofFiles[mode][weaponClass] == INVALID_HANDLE)
{
LoadCOF(mode, weaponClass);
}
return COF::GetFileData(cofFiles[mode][weaponClass])->header.nFrames;
}
inline const BYTE ITokenReference::GetNumberOfDirections(int mode, int weaponClass)
{
// Load COF if it isn't already loaded
if (cofFiles[mode][weaponClass] == INVALID_HANDLE)
{
LoadCOF(mode, weaponClass);
}
return COF::GetFileData(cofFiles[mode][weaponClass])->header.nDirs;
}
IGraphicsReference* GetTokenGraphic(unsigned int component, unsigned int hitclass,
unsigned int mode, const char* armorClass)
{
// FIXME
return nullptr;
}
/**
* Monster token type
*/
MonsterTokenReference::MonsterTokenReference(const char* tokenName)
{
SetTokenName(tokenName);
}
void MonsterTokenReference::GetFallbackForComponentMode(bool& hasFallback, unsigned int& mode)
{
switch (mode)
{
case MONMODE_KB:
case MONMODE_BL:
hasFallback = true;
mode = MONMODE_GH;
return;
case MONMODE_A2:
hasFallback = true;
mode = MONMODE_A1;
return;
case MONMODE_RN:
hasFallback = true;
mode = MONMODE_WL;
return;
}
}
/**
* Object token type
*/
ObjectTokenReference::ObjectTokenReference(const char* tokenName)
{
SetTokenName(tokenName);
}
void ObjectTokenReference::GetFallbackForComponentMode(bool& hasFallback, unsigned int& mode)
{
// object tokens don't have any component-mode fallbacks
}
/**
* Player token type
*/
PlayerTokenReference::PlayerTokenReference(const char* tokenName)
{
SetTokenName(tokenName);
}
void PlayerTokenReference::GetFallbackForComponentMode(bool& hasFallback, unsigned int& mode)
{
switch (mode)
{
case PLRMODE_KB:
case PLRMODE_BL:
hasFallback = true;
mode = PLRMODE_GH;
return;
case PLRMODE_A2:
hasFallback = true;
mode = PLRMODE_A1;
return;
case PLRMODE_TN:
hasFallback = true;
mode = PLRMODE_NU;
return;
case PLRMODE_TW:
case PLRMODE_RN:
hasFallback = true;
mode = PLRMODE_WL;
return;
}
}
/**
* The graphics manager.
*/
GraphicsManager::GraphicsManager()
{
}
GraphicsManager::~GraphicsManager()
{
// TODO: delete all permanent entries, this will leak memory !!