forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
3815 lines (3538 loc) · 73 KB
/
engine.cpp
File metadata and controls
3815 lines (3538 loc) · 73 KB
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 "diablo.h"
#include "../3rdParty/Storm/Source/storm.h"
#ifdef USE_ASM
#pragma warning(disable : 4731) // frame pointer register 'ebp' modified by inline assembly code
#endif
char gbPixelCol; // automap pixel color 8-bit (palette entry)
BOOL gbRotateMap; // flip - if y < x
int orgseed;
int sgnWidth;
int sglGameSeed;
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
int SeedCount;
BOOL gbNotInView; // valid - if x/y are in bounds
const int RndInc = 1;
const int RndMult = 0x015A4E35;
__FINLINE BYTE *CelGetFrame(BYTE *pCelBuff, int nCel, int *nDataSize)
{
DWORD *pFrameTable;
DWORD nCellStart;
pFrameTable = (DWORD *)pCelBuff;
nCellStart = SwapLE32(pFrameTable[nCel]);
*nDataSize = SwapLE32(pFrameTable[nCel + 1]) - nCellStart;
return pCelBuff + nCellStart;
}
__FINLINE int CelGetFrameSize(BYTE *pCelBuff, int nCel)
{
DWORD *pFrameTable;
pFrameTable = (DWORD *)pCelBuff;
return SwapLE32(pFrameTable[nCel + 1]) - SwapLE32(pFrameTable[nCel]);
}
void CelBlit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth)
{
int w;
/// ASSERT: assert(pDecodeTo != NULL);
if (!pDecodeTo)
return;
/// ASSERT: assert(pRLEBytes != NULL);
if (!pRLEBytes)
return;
#ifdef USE_ASM
__asm {
mov esi, pRLEBytes
mov edi, pDecodeTo
mov eax, BUFFER_WIDTH
add eax, nWidth
mov w, eax
mov ebx, nDataSize
add ebx, esi
label1:
mov edx, nWidth
label2:
xor eax, eax
lodsb
or al, al
js label6
sub edx, eax
mov ecx, eax
shr ecx, 1
jnb label3
movsb
jecxz label5
label3:
shr ecx, 1
jnb label4
movsw
jecxz label5
label4:
rep movsd
label5:
or edx, edx
jz label7
jmp label2
label6:
neg al
add edi, eax
sub edx, eax
jnz label2
label7:
sub edi, w
cmp ebx, esi
jnz label1
}
#else
int i;
BYTE width;
BYTE *src, *dst;
src = pRLEBytes;
dst = pDecodeTo;
w = nWidth;
for (; src != &pRLEBytes[nDataSize]; dst -= BUFFER_WIDTH + w) {
for (i = w; i;) {
width = *src++;
if (!(width & 0x80)) {
i -= width;
if (width & 1) {
dst[0] = src[0];
src++;
dst++;
}
width >>= 1;
if (width & 1) {
dst[0] = src[0];
dst[1] = src[1];
src += 2;
dst += 2;
}
width >>= 1;
for (; width; width--) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
src += 4;
dst += 4;
}
} else {
width = -(char)width;
dst += width;
i -= width;
}
}
}
#endif
}
void CelDraw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth)
{
int nDataSize;
BYTE *pRLEBytes;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize);
CelBlit(&gpBuffer[sx + PitchTbl[sy]], pRLEBytes, nDataSize, nWidth);
}
void CelBlitFrame(BYTE *pBuff, BYTE *pCelBuff, int nCel, int nWidth)
{
int nDataSize;
BYTE *pRLEBytes;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
/// ASSERT: assert(pBuff != NULL);
if (!pBuff)
return;
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize);
CelBlit(pBuff, pRLEBytes, nDataSize, nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedDraw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
BYTE *pRLEBytes;
DWORD *pFrameTable;
int nDataStart, nDataSize, nDataCap;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
CelBlit(
&gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]],
pRLEBytes + nDataStart,
nDataSize,
nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedBlit(BYTE *pBuff, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
BYTE *pRLEBytes;
DWORD *pFrameTable;
int nDataStart, nDataSize, nDataCap;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
/// ASSERT: assert(pBuff != NULL);
if (!pBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
CelBlit(pBuff, pRLEBytes + nDataStart, nDataSize, nWidth);
}
void CelBlitLight(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth)
{
int w;
BYTE *tbl;
/// ASSERT: assert(pDecodeTo != NULL);
if (!pDecodeTo)
return;
/// ASSERT: assert(pRLEBytes != NULL);
if (!pRLEBytes)
return;
#ifdef USE_ASM
__asm {
mov eax, light_table_index
shl eax, 8
add eax, pLightTbl
mov tbl, eax
mov esi, pRLEBytes
mov edi, pDecodeTo
mov eax, BUFFER_WIDTH
add eax, nWidth
mov w, eax
mov ebx, nDataSize
add ebx, esi
label1:
mov edx, nWidth
label2:
xor eax, eax
lodsb
or al, al
js label3
push ebx
mov ebx, tbl
sub edx, eax
mov ecx, eax
push edx
call CelBlitLightEntry
pop edx
pop ebx
or edx, edx
jnz label2
jmp label4
label3:
neg al
add edi, eax
sub edx, eax
jnz label2
label4:
sub edi, w
cmp ebx, esi
jnz label1
jmp labexit
}
/* Assembly Macro */
// clang-format off
__asm {
CelBlitLightEntry:
shr cl, 1
jnb label5
mov dl, [esi]
mov dl, [ebx+edx]
mov [edi], dl
add esi, 1
add edi, 1
label5:
shr cl, 1
jnb label6
mov dl, [esi]
mov ch, [ebx+edx]
mov [edi], ch
mov dl, [esi+1]
mov ch, [ebx+edx]
mov [edi+1], ch
add esi, 2
add edi, 2
label6:
test cl, cl
jz labret
label7:
mov eax, [esi]
add esi, 4
mov dl, al
mov ch, [ebx+edx]
mov dl, ah
ror eax, 10h
mov [edi], ch
mov ch, [ebx+edx]
mov dl, al
mov [edi+1], ch
mov ch, [ebx+edx]
mov dl, ah
mov [edi+2], ch
mov ch, [ebx+edx]
mov [edi+3], ch
add edi, 4
dec cl
jnz label7
labret:
retn
}
// clang-format on
__asm {
labexit:
}
#else
int i;
BYTE width;
BYTE *src, *dst;
src = pRLEBytes;
dst = pDecodeTo;
tbl = &pLightTbl[light_table_index * 256];
w = nWidth;
for (; src != &pRLEBytes[nDataSize]; dst -= BUFFER_WIDTH + w) {
for (i = w; i;) {
width = *src++;
if (!(width & 0x80)) {
i -= width;
if (width & 1) {
dst[0] = tbl[src[0]];
src++;
dst++;
}
width >>= 1;
if (width & 1) {
dst[0] = tbl[src[0]];
dst[1] = tbl[src[1]];
src += 2;
dst += 2;
}
width >>= 1;
for (; width; width--) {
dst[0] = tbl[src[0]];
dst[1] = tbl[src[1]];
dst[2] = tbl[src[2]];
dst[3] = tbl[src[3]];
src += 4;
dst += 4;
}
} else {
width = -(char)width;
dst += width;
i -= width;
}
}
}
#endif
}
void CelBlitLightTrans(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth)
{
int w;
BOOL shift;
BYTE *tbl;
/// ASSERT: assert(pDecodeTo != NULL);
if (!pDecodeTo)
return;
/// ASSERT: assert(pRLEBytes != NULL);
if (!pRLEBytes)
return;
#ifdef USE_ASM
__asm {
mov eax, light_table_index
shl eax, 8
add eax, pLightTbl
mov tbl, eax
mov esi, pRLEBytes
mov edi, pDecodeTo
mov eax, BUFFER_WIDTH
add eax, nWidth
mov w, eax
mov ebx, nDataSize
add ebx, esi
mov eax, edi
and eax, 1
mov shift, eax
label1:
mov edx, nWidth
label2:
xor eax, eax
lodsb
or al, al
js label9
push ebx
mov ebx, tbl
sub edx, eax
mov ecx, eax
mov eax, edi
and eax, 1
cmp eax, shift
jnz label5
shr ecx, 1
jnb label3
inc esi
inc edi
jecxz label8
jmp label6
label3:
shr ecx, 1
jnb label4
inc esi
inc edi
lodsb
xlat
stosb
jecxz label8
label4:
lodsd
inc edi
ror eax, 8
xlat
stosb
ror eax, 10h
inc edi
xlat
stosb
loop label4
jmp label8
label5:
shr ecx, 1
jnb label6
lodsb
xlat
stosb
jecxz label8
jmp label3
label6:
shr ecx, 1
jnb label7
lodsb
xlat
stosb
inc esi
inc edi
jecxz label8
label7:
lodsd
xlat
stosb
inc edi
ror eax, 10h
xlat
stosb
inc edi
loop label7
label8:
pop ebx
or edx, edx
jz label10
jmp label2
label9:
neg al
add edi, eax
sub edx, eax
jnz label2
label10:
sub edi, w
mov eax, shift
inc eax
and eax, 1
mov shift, eax
cmp ebx, esi
jnz label1
}
#else
int i;
BYTE width;
BYTE *src, *dst;
src = pRLEBytes;
dst = pDecodeTo;
tbl = &pLightTbl[light_table_index * 256];
w = nWidth;
shift = (BYTE)dst & 1;
for (; src != &pRLEBytes[nDataSize]; dst -= BUFFER_WIDTH + w, shift = (shift + 1) & 1) {
for (i = w; i;) {
width = *src++;
if (!(width & 0x80)) {
i -= width;
if (((BYTE)dst & 1) == shift) {
if (!(width & 1)) {
goto L_ODD;
} else {
src++;
dst++;
L_EVEN:
width >>= 1;
if (width & 1) {
dst[0] = tbl[src[0]];
src += 2;
dst += 2;
}
width >>= 1;
for (; width; width--) {
dst[0] = tbl[src[0]];
dst[2] = tbl[src[2]];
src += 4;
dst += 4;
}
}
} else {
if (!(width & 1)) {
goto L_EVEN;
} else {
dst[0] = tbl[src[0]];
src++;
dst++;
L_ODD:
width >>= 1;
if (width & 1) {
dst[1] = tbl[src[1]];
src += 2;
dst += 2;
}
width >>= 1;
for (; width; width--) {
dst[1] = tbl[src[1]];
dst[3] = tbl[src[3]];
src += 4;
dst += 4;
}
}
}
} else {
width = -(char)width;
dst += width;
i -= width;
}
}
}
#endif
}
void CelDrawLight(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth)
{
int nDataSize;
BYTE *pDecodeTo, *pRLEBytes;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pRLEBytes = CelGetFrame(pCelBuff, nCel, &nDataSize);
pDecodeTo = &gpBuffer[sx + PitchTbl[sy]];
if (light_table_index)
CelBlitLight(pDecodeTo, pRLEBytes, nDataSize, nWidth);
else
CelBlit(pDecodeTo, pRLEBytes, nDataSize, nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedDrawLight(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
int nDataStart, nDataSize, nDataCap;
BYTE *pRLEBytes, *pDecodeTo;
DWORD *pFrameTable;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
pRLEBytes += nDataStart;
pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]];
if (light_table_index)
CelBlitLight(pDecodeTo, pRLEBytes, nDataSize, nWidth);
else
CelBlit(pDecodeTo, pRLEBytes, nDataSize, nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedBlitLightTrans(BYTE *pBuff, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
int nDataStart, nDataSize, nDataCap;
BYTE *pRLEBytes;
DWORD *pFrameTable;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
/// ASSERT: assert(pBuff != NULL);
if (!pBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
pRLEBytes += nDataStart;
if (cel_transparency_active)
CelBlitLightTrans(pBuff, pRLEBytes, nDataSize, nWidth);
else if (light_table_index)
CelBlitLight(pBuff, pRLEBytes, nDataSize, nWidth);
else
CelBlit(pBuff, pRLEBytes, nDataSize, nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelDrawLightRed(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light)
{
int nDataStart, nDataSize, nDataCap, w, idx;
BYTE *pRLEBytes, *dst, *tbl;
DWORD *pFrameTable;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
pRLEBytes += nDataStart;
dst = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]];
idx = light4flag ? 1024 : 4096;
if (light == 2)
idx += 256;
if (light >= 4)
idx += (light - 1) << 8;
#ifdef USE_ASM
__asm {
mov eax, pLightTbl
add eax, idx
mov tbl, eax
mov esi, pRLEBytes
mov edi, dst
mov eax, BUFFER_WIDTH
add eax, nWidth
mov w, eax /* use C for w? w = BUFFER_WIDTH + nWidth */
mov ebx, nDataSize
add ebx, esi
label1:
mov edx, nWidth
label2:
xor eax, eax
mov al, [esi]
inc esi
test al, al
js label4
push ebx
mov ebx, tbl
sub edx, eax
mov ecx, eax
label3:
mov al, [esi]
inc esi
mov al, [ebx+eax]
mov [edi], al
dec ecx
lea edi, [edi+1]
jnz label3
pop ebx
test edx, edx
jz label5
jmp label2
label4:
neg al
add edi, eax
sub edx, eax
jnz label2
label5:
sub edi, w
cmp ebx, esi
jnz label1
}
#else
BYTE width;
BYTE *end;
tbl = &pLightTbl[idx];
end = &pRLEBytes[nDataSize];
for (; pRLEBytes != end; dst -= BUFFER_WIDTH + nWidth) {
for (w = nWidth; w;) {
width = *pRLEBytes++;
if (!(width & 0x80)) {
w -= width;
while (width) {
*dst = tbl[*pRLEBytes];
pRLEBytes++;
dst++;
width--;
}
} else {
width = -(char)width;
dst += width;
w -= width;
}
}
}
#endif
}
/**
* @brief Same as CelBlit but checks for drawing outside the buffer
*/
void CelBlitSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth)
{
int w;
/// ASSERT: assert(pDecodeTo != NULL);
if (!pDecodeTo)
return;
/// ASSERT: assert(pRLEBytes != NULL);
if (!pRLEBytes)
return;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
#ifdef USE_ASM
__asm {
mov esi, pRLEBytes
mov edi, pDecodeTo
mov eax, BUFFER_WIDTH
add eax, nWidth
mov w, eax
mov ebx, nDataSize
add ebx, esi
label1:
mov edx, nWidth
label2:
xor eax, eax
lodsb
or al, al
js label7
sub edx, eax
cmp edi, gpBufEnd
jb label3
add esi, eax
add edi, eax
jmp label6
label3:
mov ecx, eax
shr ecx, 1
jnb label4
movsb
jecxz label6
label4:
shr ecx, 1
jnb label5
movsw
jecxz label6
label5:
rep movsd
label6:
or edx, edx
jz label8
jmp label2
label7:
neg al
add edi, eax
sub edx, eax
jnz label2
label8:
sub edi, w
cmp ebx, esi
jnz label1
}
#else
int i;
BYTE width;
BYTE *src, *dst;
src = pRLEBytes;
dst = pDecodeTo;
w = nWidth;
for (; src != &pRLEBytes[nDataSize]; dst -= BUFFER_WIDTH + w) {
for (i = w; i;) {
width = *src++;
if (!(width & 0x80)) {
i -= width;
if (dst < gpBufEnd) {
if (width & 1) {
dst[0] = src[0];
src++;
dst++;
}
width >>= 1;
if (width & 1) {
dst[0] = src[0];
dst[1] = src[1];
src += 2;
dst += 2;
}
width >>= 1;
for (; width; width--) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
src += 4;
dst += 4;
}
} else {
src += width;
dst += width;
}
} else {
width = -(char)width;
dst += width;
i -= width;
}
}
}
#endif
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedDrawSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
BYTE *pRLEBytes;
DWORD *pFrameTable;
int nDataStart, nDataSize, nDataCap;
/// ASSERT: assert(gpBuffer);
if (!gpBuffer)
return;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
if (CelCap == 8)
nDataCap = 0;
else
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (nDataCap)
nDataSize = nDataCap - nDataStart;
else
nDataSize -= nDataStart;
CelBlitSafe(
&gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]],
pRLEBytes + nDataStart,
nDataSize,
nWidth);
}
/**
* @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8
* @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8
*/
void CelClippedBlitSafe(BYTE *pBuff, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap)
{
BYTE *pRLEBytes;
DWORD *pFrameTable;
int nDataStart, nDataSize, nDataCap;
/// ASSERT: assert(pCelBuff != NULL);
if (!pCelBuff)
return;
/// ASSERT: assert(pBuff != NULL);
if (!pBuff)
return;
pFrameTable = (DWORD *)pCelBuff;
pRLEBytes = &pCelBuff[pFrameTable[nCel]];
nDataStart = *(WORD *)&pRLEBytes[CelSkip];
if (!nDataStart)
return;
nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel];
nDataCap = *(WORD *)&pRLEBytes[CelCap];
if (CelCap == 8)