-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.c
8847 lines (7601 loc) · 250 KB
/
engine.c
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
/*
* "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
* Ken Silverman's official web site: "http://www.advsys.net/ken"
* See the included license file "BUILDLIC.TXT" for license info.
* This file has been modified from Ken Silverman's original release
*/
/* SUPERBUILD define is in engine.h ... */
#define ENGINE
/* set this to something non-zero to get loadtile() debugging info on stderr. */
#define BUILD_CACHEDEBUG 0
#include <string.h>
#if !PLATFORM_MACOSX && !PLATFORM_FREEBSD
#include <malloc.h>
#endif
#if PLATFORM_FREEBSD
#include <stdlib.h>
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#if (defined USE_OPENGL)
#include "buildgl.h"
#endif
#include "pragmas.h"
#include "platform.h"
#include "build.h"
#include "cache1d.h"
#include "engine.h"
long stereowidth = 23040, stereopixelwidth = 28, ostereopixelwidth = -1;
volatile long stereomode = 0, visualpage, activepage, whiteband, blackband;
volatile char oa1, o3c2, ortca, ortcb, overtbits, laststereoint;
#include "display.h"
#define MAXCLIPNUM 512
#define MAXPERMS 512
#define MAXTILEFILES 256
#define MAXYSAVES ((MAXXDIM*MAXSPRITES)>>7)
#define MAXNODESPERLINE 42 /* Warning: This depends on MAXYSAVES & MAXYDIM! */
#define MAXWALLSB 2048
#define MAXCLIPDIST 1024
#ifdef PLATFORM_DOS
/* MUST CALL MALLOC THIS WAY TO FORCE CALLS TO KMALLOC! */
void *kmalloc(size_t size) { return(malloc(size)); }
void *kkmalloc(size_t size);
#pragma aux kkmalloc =\
"call kmalloc",\
parm [eax]\
/* MUST CALL FREE THIS WAY TO FORCE CALLS TO KFREE! */
void kfree(void *buffer) { free(buffer); }
void kkfree(void *buffer);
#pragma aux kkfree =\
"call kfree",\
parm [eax]\
#endif
#ifdef SUPERBUILD
/* MUST CALL LOADVOXEL THIS WAY BECAUSE WATCOM STINKS! */
/* !!! wtf does this do?! --ryan. */
static void loadvoxel(long voxindex)
{
voxindex = 0; /* prevent compiler whining. */
}
#if ((defined __WATCOMC__) && (defined PLATFORM_DOS))
void kloadvoxel(long voxindex);
#pragma aux kloadvoxel =\
"call loadvoxel",\
parm [eax]\
#else
#define kloadvoxel(a) (loadvoxel(a))
#endif
/* These variables need to be copied into BUILD */
#define MAXXSIZ 128
#define MAXYSIZ 128
#define MAXZSIZ 200
#define MAXVOXELS 512
#define MAXVOXMIPS 5
long voxoff[MAXVOXELS][MAXVOXMIPS], voxlock[MAXVOXELS][MAXVOXMIPS];
static long ggxinc[MAXXSIZ+1], ggyinc[MAXXSIZ+1];
static long lowrecip[1024], nytooclose, nytoofar;
static unsigned long distrecip[16384];
#endif
/* used to be static. --ryan. */
char moustat = 0;
long transarea = 0, totalarea = 0, beforedrawrooms = 1;
/* used to be static. --ryan. */
long oxdimen = -1, oviewingrange = -1, oxyaspect = -1;
/* used to be static. --ryan. */
long curbrightness = 0;
/* Textured Map variables */
static char globalpolytype;
static short *dotp1[MAXYDIM], *dotp2[MAXYDIM];
static unsigned char tempbuf[MAXWALLS];
long ebpbak, espbak;
long slopalookup[16384];
/*
* !!! used to be static. If we ever put the original setgamemode() back, this
* !!! can be made static again. --ryan.
*/
unsigned char permanentlock = 255;
long artversion, mapversion;
char *pic = NULL;
char picsiz[MAXTILES], tilefilenum[MAXTILES];
long lastageclock;
long tilefileoffs[MAXTILES];
long artsize = 0, cachesize = 0;
static short radarang[1280], radarang2[MAXXDIM+1];
static unsigned short sqrtable[4096], shlookup[4096+256];
char pow2char[8] = {1,2,4,8,16,32,64,-128};
long pow2long[32] =
{
1L,2L,4L,8L,
16L,32L,64L,128L,
256L,512L,1024L,2048L,
4096L,8192L,16384L,32768L,
65536L,131072L,262144L,524288L,
1048576L,2097152L,4194304L,8388608L,
16777216L,33554432L,67108864L,134217728L,
268435456L,536870912L,1073741824L,2147483647L,
};
long reciptable[2048], fpuasm;
char kensmessage[128];
int has_altivec = 0; /* PowerPC-specific. */
/* rcg02132001 Cygwin support. */
#if (defined C_IDENTIFIERS_UNDERSCORED)
#define SYM_sqrtable "_sqrtable"
#define SYM_walock "_walock"
#define SYM_shlookup "_shlookup"
#define SYM_pow2char "_pow2char"
#define SYM_gotpic "_gotpic"
#define SYM_dmval "_dmval"
#else
#define SYM_sqrtable "sqrtable"
#define SYM_walock "walock"
#define SYM_shlookup "shlookup"
#define SYM_pow2char "pow2char"
#define SYM_gotpic "gotpic"
#define SYM_dmval "dmval"
#endif
char britable[16][64];
char textfont[1024], smalltextfont[1024];
static long xb1[MAXWALLSB], yb1[MAXWALLSB], xb2[MAXWALLSB], yb2[MAXWALLSB];
static long rx1[MAXWALLSB], ry1[MAXWALLSB], rx2[MAXWALLSB], ry2[MAXWALLSB];
static short p2[MAXWALLSB], thesector[MAXWALLSB], thewall[MAXWALLSB];
static short bunchfirst[MAXWALLSB], bunchlast[MAXWALLSB];
static short smost[MAXYSAVES], smostcnt;
static short smoststart[MAXWALLSB];
static char smostwalltype[MAXWALLSB];
static long smostwall[MAXWALLSB], smostwallcnt = -1L;
static short maskwall[MAXWALLSB], maskwallcnt;
static long spritesx[MAXSPRITESONSCREEN];
static long spritesy[MAXSPRITESONSCREEN+1];
static long spritesz[MAXSPRITESONSCREEN];
static spritetype *tspriteptr[MAXSPRITESONSCREEN];
short umost[MAXXDIM+1], dmost[MAXXDIM+1];
static short bakumost[MAXXDIM+1], bakdmost[MAXXDIM+1];
short uplc[MAXXDIM+1], dplc[MAXXDIM+1];
static short uwall[MAXXDIM+1], dwall[MAXXDIM+1];
static long swplc[MAXXDIM+1], lplc[MAXXDIM+1];
static long swall[MAXXDIM+1], lwall[MAXXDIM+4];
long xdimen = -1, xdimenrecip, halfxdimen, xdimenscale, xdimscale;
long wx1, wy1, wx2, wy2, ydimen;
long viewoffset, frameoffset;
static long rxi[8], ryi[8], rzi[8], rxi2[8], ryi2[8], rzi2[8];
static long xsi[8], ysi[8];
/* used to be static. --ryan. */
long *horizlookup, *horizlookup2, horizycent;
long globalposx, globalposy, globalposz, globalhoriz;
short globalang, globalcursectnum;
long globalpal, cosglobalang, singlobalang;
long cosviewingrangeglobalang, sinviewingrangeglobalang;
char *globalpalwritten;
long globaluclip, globaldclip, globvis;
long globalvisibility, globalhisibility, globalpisibility, globalcisibility;
char globparaceilclip, globparaflorclip;
long xyaspect, viewingrangerecip;
long asm1, asm2, asm3, asm4;
long vplce[4], vince[4], palookupoffse[4], bufplce[4];
char globalxshift, globalyshift;
long globalxpanning, globalypanning, globalshade;
short globalpicnum, globalshiftval;
long globalzd, globalbufplc, globalyscale, globalorientation;
long globalx1, globaly1, globalx2, globaly2, globalx3, globaly3, globalzx;
long globalx, globaly, globalz;
static short sectorborder[256], sectorbordercnt;
static char tablesloaded = 0;
long pageoffset, ydim16, qsetmode = 0;
long startposx, startposy, startposz;
short startang, startsectnum;
short pointhighlight, linehighlight, highlightcnt;
static long lastx[MAXYDIM];
char *transluc = NULL, paletteloaded = 0;
#define FASTPALGRIDSIZ 8
static long rdist[129], gdist[129], bdist[129];
static char colhere[((FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2))>>3];
static char colhead[(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)*(FASTPALGRIDSIZ+2)];
static long colnext[256];
static char coldist[8] = {0,1,2,3,4,3,2,1};
static long colscan[27];
static short clipnum, hitwalls[4];
long hitscangoalx = (1<<29)-1, hitscangoaly = (1<<29)-1;
typedef struct { long x1, y1, x2, y2; } linetype;
static linetype clipit[MAXCLIPNUM];
static short clipsectorlist[MAXCLIPNUM], clipsectnum;
static short clipobjectval[MAXCLIPNUM];
typedef struct
{
long sx, sy, z;
short a, picnum;
signed char dashade;
unsigned char dapalnum, dastat, pagesleft;
long cx1, cy1, cx2, cy2;
} permfifotype;
static permfifotype permfifo[MAXPERMS];
static long permhead = 0, permtail = 0;
short numscans, numhits, numbunches;
short editstatus = 0;
short searchit;
long searchx = -1, searchy; /* search input */
short searchsector, searchwall, searchstat; /* search output */
static char artfilename[20];
static long numtilefiles, artfil = -1, artfilnum, artfilplc;
static char inpreparemirror = 0;
static long mirrorsx1, mirrorsy1, mirrorsx2, mirrorsy2;
long totalclocklock;
/* !!! can we move this somewhere? --ryan. */
#if ((defined USE_I386_ASM) && (defined __WATCOMC__))
extern long mmxoverlay();
#pragma aux mmxoverlay modify [eax ebx ecx edx];
extern long sethlinesizes(long,long,long);
#pragma aux sethlinesizes parm [eax][ebx][ecx];
extern long setpalookupaddress(char *);
#pragma aux setpalookupaddress parm [eax];
extern long setuphlineasm4(long,long);
#pragma aux setuphlineasm4 parm [eax][ebx];
extern long hlineasm4(long,long,long,long,long,long);
#pragma aux hlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long setuprhlineasm4(long,long,long,long,long,long);
#pragma aux setuprhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long rhlineasm4(long,long,long,long,long,long);
#pragma aux rhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long setuprmhlineasm4(long,long,long,long,long,long);
#pragma aux setuprmhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long rmhlineasm4(long,long,long,long,long,long);
#pragma aux rmhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long setupqrhlineasm4(long,long,long,long,long,long);
#pragma aux setupqrhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long qrhlineasm4(long,long,long,long,long,long);
#pragma aux qrhlineasm4 parm [eax][ebx][ecx][edx][esi][edi];
extern long setvlinebpl(long);
#pragma aux setvlinebpl parm [eax];
extern long fixtransluscence(long);
#pragma aux fixtransluscence parm [eax];
extern long prevlineasm1(long,long,long,long,long,long);
#pragma aux prevlineasm1 parm [eax][ebx][ecx][edx][esi][edi];
extern long vlineasm1(long,long,long,long,long,long);
#pragma aux vlineasm1 parm [eax][ebx][ecx][edx][esi][edi];
extern long setuptvlineasm(long);
#pragma aux setuptvlineasm parm [eax];
extern long tvlineasm1(long,long,long,long,long,long);
#pragma aux tvlineasm1 parm [eax][ebx][ecx][edx][esi][edi];
extern long setuptvlineasm2(long,long,long);
#pragma aux setuptvlineasm2 parm [eax][ebx][ecx];
extern long tvlineasm2(long,long,long,long,long,long);
#pragma aux tvlineasm2 parm [eax][ebx][ecx][edx][esi][edi];
extern long mvlineasm1(long,long,long,long,long,long);
#pragma aux mvlineasm1 parm [eax][ebx][ecx][edx][esi][edi];
extern long setupvlineasm(long);
#pragma aux setupvlineasm parm [eax];
extern long vlineasm4(long,long);
#pragma aux vlineasm4 parm [ecx][edi] modify [eax ebx ecx edx esi edi];
extern long setupmvlineasm(long);
#pragma aux setupmvlineasm parm [eax];
extern long mvlineasm4(long,long);
#pragma aux mvlineasm4 parm [ecx][edi] modify [eax ebx ecx edx esi edi];
extern void setupspritevline(long,long,long,long,long,long);
#pragma aux setupspritevline parm [eax][ebx][ecx][edx][esi][edi];
extern void spritevline(long,long,long,long,long,long);
#pragma aux spritevline parm [eax][ebx][ecx][edx][esi][edi];
extern void msetupspritevline(long,long,long,long,long,long);
#pragma aux msetupspritevline parm [eax][ebx][ecx][edx][esi][edi];
extern void mspritevline(long,long,long,long,long,long);
#pragma aux mspritevline parm [eax][ebx][ecx][edx][esi][edi];
extern void tsetupspritevline(long,long,long,long,long,long);
#pragma aux tsetupspritevline parm [eax][ebx][ecx][edx][esi][edi];
extern void tspritevline(long,long,long,long,long,long);
#pragma aux tspritevline parm [eax][ebx][ecx][edx][esi][edi];
extern long mhline(long,long,long,long,long,long);
#pragma aux mhline parm [eax][ebx][ecx][edx][esi][edi];
extern long mhlineskipmodify(long,long,long,long,long,long);
#pragma aux mhlineskipmodify parm [eax][ebx][ecx][edx][esi][edi];
extern long msethlineshift(long,long);
#pragma aux msethlineshift parm [eax][ebx];
extern long thline(long,long,long,long,long,long);
#pragma aux thline parm [eax][ebx][ecx][edx][esi][edi];
extern long thlineskipmodify(long,long,long,long,long,long);
#pragma aux thlineskipmodify parm [eax][ebx][ecx][edx][esi][edi];
extern long tsethlineshift(long,long);
#pragma aux tsethlineshift parm [eax][ebx];
extern long setupslopevlin(long,long,long);
#pragma aux setupslopevlin parm [eax][ebx][ecx] modify [edx];
extern long slopevlin(long,long,long,long,long,long);
#pragma aux slopevlin parm [eax][ebx][ecx][edx][esi][edi];
extern long settransnormal();
#pragma aux settransnormal parm;
extern long settransreverse();
#pragma aux settransreverse parm;
extern long setupdrawslab(long,long);
#pragma aux setupdrawslab parm [eax][ebx];
extern long drawslab(long,long,long,long,long,long);
#pragma aux drawslab parm [eax][ebx][ecx][edx][esi][edi];
#else
#include "a.h"
#endif
#if (defined USE_I386_ASM)
#if (defined __WATCOMC__)
long nsqrtasm(int param);
#pragma aux nsqrtasm =\
"test eax, 0xff000000",\
"mov ebx, eax",\
"jnz short over24",\
"shr ebx, 12",\
"mov cx, word ptr shlookup[ebx*2]",\
"jmp short under24",\
"over24: shr ebx, 24",\
"mov cx, word ptr shlookup[ebx*2+8192]",\
"under24: shr eax, cl",\
"mov cl, ch",\
"mov ax, word ptr sqrtable[eax*2]",\
"shr eax, cl",\
parm nomemory [eax]\
modify exact [eax ebx ecx]\
#elif (defined __GNUC__) || (defined __ICC)
static __attribute__((noinline)) long nsqrtasm(int i1)
{
long retval;
__asm__ __volatile__ (
"\n\t"
"testl $0xff000000, %%eax\n\t"
"movl %%eax, %%ebx\n\t"
"jnz nsqrover24\n\t"
"shrl $12, %%ebx\n\t"
"movw " SYM_shlookup "(, %%ebx, 2), %%cx\n\t"
"jmp nsqrunder24\n\t"
"nsqrover24: shr $24, %%ebx\n\t"
"movw (" SYM_shlookup "+ 8192)(, %%ebx, 2), %%cx\n\t"
"nsqrunder24: shrl %%cl, %%eax\n\t"
"movb %%ch, %%cl\n\t"
"movw " SYM_sqrtable "(, %%eax, 2), %%ax\n\t"
"shrl %%cl, %%eax\n\t"
: "=a" (retval) : "a" (i1) : "ebx", "ecx", "cc");
return(retval);
} /* nsqrtasm */
#elif (defined _MSC_VER)
long nsqrtasm(int param)
{
__asm
{
mov eax, param
test eax, 0xff000000
mov ebx, eax
jnz short over24
shr ebx, 12
mov cx, word ptr shlookup[ebx*2]
jmp short under24
over24: shr ebx, 24
mov cx, word ptr shlookup[ebx*2+8192]
under24: shr eax, cl
mov cl, ch
mov ax, word ptr sqrtable[eax*2]
shr eax, cl
mov param, eax
} /* asm */
return(param);
}
#else
#error Please write Assembly code for your platform!
#endif
#else /* !defined USE_I386_ASM */
long nsqrtasm(unsigned long param)
{
unsigned short *shlookup_a = (unsigned short*)shlookup;
unsigned short *sqrtable_a = (unsigned short*)sqrtable;
unsigned short cx;
if (param & 0xff000000)
cx = shlookup_a[(param>>24)+4096];
else
cx = shlookup_a[param>>12];
param = param >> (cx&0xff);
param = ((param&0xffff0000)|sqrtable_a[param]);
param = param >> ((cx&0xff00)>>8);
return param;
}
#endif /* defined USE_I386_ASM */
#if (defined USE_I386_ASM)
#if (defined __WATCOMC__)
/* 0x007ff000 is (11<<13), 0x3f800000 is (127<<23) */
long krecipasm(long param);
#pragma aux krecipasm =\
"mov fpuasm, eax",\
"fild dword ptr fpuasm",\
"add eax, eax",\
"fstp dword ptr fpuasm",\
"sbb ebx, ebx",\
"mov eax, fpuasm",\
"mov ecx, eax",\
"and eax, 0x007ff000",\
"shr eax, 10",\
"sub ecx, 0x3f800000",\
"shr ecx, 23",\
"mov eax, dword ptr reciptable[eax]",\
"sar eax, cl",\
"xor eax, ebx",\
parm [eax]\
modify exact [eax ebx ecx]\
#elif (defined __GNUC__) || (defined __ICC)
static long krecipasm(long i1)
{
long retval;
__asm__ __volatile__ (
"\n\t"
"call _asm_krecipasm\n\t"
: "=a" (retval) : "a" (i1)
: "cc", "ebx", "ecx", "memory");
return(retval);
} /* krecipasm */
#elif (defined _MSC_VER)
/* 0x007ff000 is (11<<13), 0x3f800000 is (127<<23) */
long krecipasm(long param)
{
__asm
{
mov eax, param
mov fpuasm, eax
fild dword ptr fpuasm
add eax, eax
fstp dword ptr fpuasm
sbb ebx, ebx
mov eax, fpuasm
mov ecx, eax
and eax, 0x007ff000
shr eax, 10
sub ecx, 0x3f800000
shr ecx, 23
mov eax, dword ptr reciptable[eax]
sar eax, cl
xor eax, ebx
mov param,eax
} /* asm */
return(param);
} /* krecipasm */
#else
#error Please write Assembly for your platform!
#endif
#else /* USE_I386_ASM */
static long krecipasm(long param)
{
unsigned int xormask;
int mantissa;
int exponent;
long recip;
union {
float f;
unsigned int i;
} fi;
fi.f = (float) param;
fpuasm = fi.i;
xormask = (param < 0) ? 0xffffffff : 0;
mantissa = (fi.i & 0x007ff000) >> 12;
exponent = (fi.i - 0x3f800000) >> 23;
recip = (reciptable[mantissa] >> exponent) ^ xormask;
return recip;
} /* krecipasm */
/*
static long krecipasm(long x)
{
float xf;
int z;
int not;
if (x & 0x80000000)
not = 0x0FFFFFFFF;
else
not = 0;
xf = (float)x;
x = *((int*)(&xf));
z = x;
x = x & 0x007FF000;
x = x >> 10;
z = z - 0x03F800000;
z = z >> 23;
x = shift_algebraic_right(reciptable[(x>>2)], z) ^ not;
return x;
}
*/
#endif /* defined USE_I386_ASM */
#if (defined USE_I386_ASM)
#if (defined __WATCOMC__)
int setgotpic(long param);
#pragma aux setgotpic =\
"mov ebx, eax",\
"cmp byte ptr walock[eax], 200",\
"jae skipit",\
"mov byte ptr walock[eax], 199",\
"skipit: shr eax, 3",\
"and ebx, 7",\
"mov dl, byte ptr gotpic[eax]",\
"mov bl, byte ptr pow2char[ebx]",\
"or dl, bl",\
"mov byte ptr gotpic[eax], dl",\
parm [eax]\
modify exact [eax ebx ecx edx]\
#elif (defined __GNUC__) || (defined __ICC)
int __attribute__((noinline)) setgotpic(long i1)
{
int retval = 0;
__asm__ __volatile__ (
"\n\t"
"movl %%eax, %%ebx\n\t"
"cmpb $200, " SYM_walock "(%%eax)\n\t"
"jae setgotpic_skipit\n\t"
"movb $199, " SYM_walock "(%%eax)\n\t"
"setgotpic_skipit: shrl $3, %%eax\n\t"
"andl $7, %%ebx\n\t"
"movb " SYM_gotpic "(%%eax), %%dl\n\t"
"movb " SYM_pow2char "(%%ebx), %%bl\n\t"
"orb %%bl, %%dl\n\t"
"movb %%dl, (" SYM_gotpic ")(%%eax)\n\t"
: "=a" (retval) : "a" (i1) : "ebx", "ecx", "edx", "cc", "memory");
return(retval);
} /* setgotpic */
#elif (defined _MSC_VER)
int setgotpic(long param)
{
__asm
{
mov eax, param
mov ebx, eax
cmp byte ptr walock[eax], 200
jae skipit
mov byte ptr walock[eax], 199
skipit: shr eax, 3
and ebx, 7
mov dl, byte ptr gotpic[eax]
mov bl, byte ptr pow2char[ebx]
or dl, bl
mov byte ptr gotpic[eax], dl
mov param, eax
} /* asm */
return(param);
} /* setgotpic */
#else
#error Please write Assembly for your platform!
#endif
#else /* !defined USE_I386_ASM */
void setgotpic(unsigned long param)
{
unsigned char *gotpic_a = (unsigned char*)gotpic;
unsigned char *walock_a = (unsigned char*)walock;
unsigned char *pow2char_a = (unsigned char*)pow2char;
if (walock_a[param] < 200) walock_a[param] = 199;
gotpic_a[param>>3] |= pow2char_a[param&7];
}
#endif /* defined USE_I386_ASM */
#if (defined USE_I386_ASM)
#if (defined __WATCOMC__)
long getclipmask(int i1, int i2, int i3, int i4);
#pragma aux getclipmask =\
"sar eax, 31",\
"add ebx, ebx",\
"adc eax, eax",\
"add ecx, ecx",\
"adc eax, eax",\
"add edx, edx",\
"adc eax, eax",\
"mov ebx, eax",\
"shl ebx, 4",\
"or al, 0xf0",\
"xor eax, ebx",\
parm [eax][ebx][ecx][edx]\
modify exact [eax ebx ecx edx]\
#elif (defined __GNUC__) || (defined __ICC)
long getclipmask(int i1, int i2, int i3, int i4)
{
int retval;
__asm__ __volatile__ (
"\n\t"
"sarl $31, %%eax\n\t"
"addl %%ebx, %%ebx\n\t"
"adcl %%eax, %%eax\n\t"
"addl %%ecx, %%ecx\n\t"
"adcl %%eax, %%eax\n\t"
"addl %%edx, %%edx\n\t"
"adcl %%eax, %%eax\n\t"
"movl %%eax, %%ebx\n\t"
"shll $4, %%ebx\n\t"
"orb $0xf0, %%al\n\t"
"xorl %%ebx, %%eax\n\t"
: "=a" (retval) : "a" (i1), "b" (i2), "c" (i3), "d" (i4)
: "cc", "memory");
return(retval);
} /* getclipmask */
#elif (defined _MSC_VER)
long getclipmask(int i1, int i2, int i3, int i4)
{
__asm
{
mov eax, i1
mov ebx, i2
mov ecx, i3
mov edx, i4
sar eax, 31
add ebx, ebx
adc eax, eax
add ecx, ecx
adc eax, eax
add edx, edx
adc eax, eax
mov ebx, eax
shl ebx, 4
or al, 0xf0
xor eax, ebx
mov i1, eax
} /* asm */
return(i1);
} /* getclipmask */
#else
#error Please write Assembly for your platform!
#endif
#else /* !defined USE_I386_ASM */
long getclipmask(int i1, int i2, int i3, int i4)
{
unsigned long eax;
eax = i1 >> 31;
eax = eax << 1; i2 *= 2;
if (i2&0x80000000) { eax++; }
eax = eax << 1; i3 *= 2;
if (i3&0x80000000) { eax++; }
eax = eax << 1; i4 *= 2;
if (i4&0x80000000) { eax++; }
return ((eax<<4)^(eax|0xf0));
}
#endif /* defined USE_I386_ASM */
static void scansector (short sectnum)
{
walltype *wal, *wal2;
spritetype *spr;
long xs, ys, x1, y1, x2, y2, xp1, yp1, xp2=0, yp2=0, templong;
short z, zz, startwall, endwall, numscansbefore, scanfirst, bunchfrst;
short nextsectnum;
if (sectnum < 0) return;
if (automapping) show2dsector[sectnum>>3] |= pow2char[sectnum&7];
sectorborder[0] = sectnum, sectorbordercnt = 1;
do
{
sectnum = sectorborder[--sectorbordercnt];
for(z=headspritesect[sectnum];z>=0;z=nextspritesect[z])
{
spr = &sprite[z];
if ((((spr->cstat&0x8000) == 0) || (showinvisibility)) &&
(spr->xrepeat > 0) && (spr->yrepeat > 0) &&
(spritesortcnt < MAXSPRITESONSCREEN))
{
xs = spr->x-globalposx; ys = spr->y-globalposy;
if ((spr->cstat&48) || (xs*cosglobalang+ys*singlobalang > 0))
{
copybufbyte(spr,&tsprite[spritesortcnt],sizeof(spritetype));
tsprite[spritesortcnt++].owner = z;
}
}
}
gotsector[sectnum>>3] |= pow2char[sectnum&7];
bunchfrst = numbunches;
numscansbefore = numscans;
startwall = sector[sectnum].wallptr;
endwall = startwall + sector[sectnum].wallnum;
scanfirst = numscans;
for(z=startwall,wal=&wall[z];z<endwall;z++,wal++)
{
nextsectnum = wal->nextsector;
wal2 = &wall[wal->point2];
x1 = wal->x-globalposx; y1 = wal->y-globalposy;
x2 = wal2->x-globalposx; y2 = wal2->y-globalposy;
if ((nextsectnum >= 0) && ((wal->cstat&32) == 0))
if ((gotsector[nextsectnum>>3]&pow2char[nextsectnum&7]) == 0)
{
templong = x1*y2-x2*y1;
if (((unsigned)templong+262144) < 524288)
if (mulscale5(templong,templong) <= (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
sectorborder[sectorbordercnt++] = nextsectnum;
}
if ((z == startwall) || (wall[z-1].point2 != z))
{
xp1 = dmulscale6(y1,cosglobalang,-x1,singlobalang);
yp1 = dmulscale6(x1,cosviewingrangeglobalang,y1,sinviewingrangeglobalang);
}
else
{
xp1 = xp2;
yp1 = yp2;
}
xp2 = dmulscale6(y2,cosglobalang,-x2,singlobalang);
yp2 = dmulscale6(x2,cosviewingrangeglobalang,y2,sinviewingrangeglobalang);
if ((yp1 < 256) && (yp2 < 256)) goto skipitaddwall;
/* If wall's NOT facing you */
if (dmulscale32(xp1,yp2,-xp2,yp1) >= 0) goto skipitaddwall;
if (xp1 >= -yp1)
{
if ((xp1 > yp1) || (yp1 == 0)) goto skipitaddwall;
xb1[numscans] = halfxdimen + scale(xp1,halfxdimen,yp1);
if (xp1 >= 0) xb1[numscans]++; /* Fix for SIGNED divide */
if (xb1[numscans] >= xdimen) xb1[numscans] = xdimen-1;
yb1[numscans] = yp1;
}
else
{
if (xp2 < -yp2) goto skipitaddwall;
xb1[numscans] = 0;
templong = yp1-yp2+xp1-xp2;
if (templong == 0) goto skipitaddwall;
yb1[numscans] = yp1 + scale(yp2-yp1,xp1+yp1,templong);
}
if (yb1[numscans] < 256) goto skipitaddwall;
if (xp2 <= yp2)
{
if ((xp2 < -yp2) || (yp2 == 0)) goto skipitaddwall;
xb2[numscans] = halfxdimen + scale(xp2,halfxdimen,yp2) - 1;
if (xp2 >= 0) xb2[numscans]++; /* Fix for SIGNED divide */
if (xb2[numscans] >= xdimen) xb2[numscans] = xdimen-1;
yb2[numscans] = yp2;
}
else
{
if (xp1 > yp1) goto skipitaddwall;
xb2[numscans] = xdimen-1;
templong = xp2-xp1+yp1-yp2;
if (templong == 0) goto skipitaddwall;
yb2[numscans] = yp1 + scale(yp2-yp1,yp1-xp1,templong);
}
if ((yb2[numscans] < 256) || (xb1[numscans] > xb2[numscans])) goto skipitaddwall;
/* Made it all the way! */
thesector[numscans] = sectnum; thewall[numscans] = z;
rx1[numscans] = xp1; ry1[numscans] = yp1;
rx2[numscans] = xp2; ry2[numscans] = yp2;
p2[numscans] = numscans+1;
numscans++;
skipitaddwall:
if ((wall[z].point2 < z) && (scanfirst < numscans))
p2[numscans-1] = scanfirst, scanfirst = numscans;
}
for(z=numscansbefore;z<numscans;z++)
if ((wall[thewall[z]].point2 != thewall[p2[z]]) || (xb2[z] >= xb1[p2[z]]))
bunchfirst[numbunches++] = p2[z], p2[z] = -1;
for(z=bunchfrst;z<numbunches;z++)
{
for(zz=bunchfirst[z];p2[zz]>=0;zz=p2[zz]);
bunchlast[z] = zz;
}
} while (sectorbordercnt > 0);
}
static void prepwall(long z, walltype *wal)
{
long i, l=0, ol=0, splc, sinc, x, topinc, top, botinc, bot, walxrepeat;
walxrepeat = (wal->xrepeat<<3);
/* lwall calculation */
i = xb1[z]-halfxdimen;
topinc = -(ry1[z]>>2);
botinc = ((ry2[z]-ry1[z])>>8);
top = mulscale5(rx1[z],xdimen)+mulscale2(topinc,i);
bot = mulscale11(rx1[z]-rx2[z],xdimen)+mulscale2(botinc,i);
splc = mulscale19(ry1[z],xdimscale);
sinc = mulscale16(ry2[z]-ry1[z],xdimscale);
x = xb1[z];
if (bot != 0)
{
l = divscale12(top,bot);
swall[x] = mulscale21(l,sinc)+splc;
l *= walxrepeat;
lwall[x] = (l>>18);
}
while (x+4 <= xb2[z])
{
top += topinc; bot += botinc;
if (bot != 0)
{
ol = l; l = divscale12(top,bot);
swall[x+4] = mulscale21(l,sinc)+splc;
l *= walxrepeat;
lwall[x+4] = (l>>18);
}
i = ((ol+l)>>1);
lwall[x+2] = (i>>18);
lwall[x+1] = ((ol+i)>>19);
lwall[x+3] = ((l+i)>>19);
swall[x+2] = ((swall[x]+swall[x+4])>>1);
swall[x+1] = ((swall[x]+swall[x+2])>>1);
swall[x+3] = ((swall[x+4]+swall[x+2])>>1);
x += 4;
}
if (x+2 <= xb2[z])
{
top += (topinc>>1); bot += (botinc>>1);
if (bot != 0)
{
ol = l; l = divscale12(top,bot);
swall[x+2] = mulscale21(l,sinc)+splc;
l *= walxrepeat;
lwall[x+2] = (l>>18);
}
lwall[x+1] = ((l+ol)>>19);
swall[x+1] = ((swall[x]+swall[x+2])>>1);
x += 2;
}
if (x+1 <= xb2[z])
{
bot += (botinc>>2);
if (bot != 0)
{
l = divscale12(top+(topinc>>2),bot);
swall[x+1] = mulscale21(l,sinc)+splc;
lwall[x+1] = mulscale18(l,walxrepeat);
}
}
if (lwall[xb1[z]] < 0) lwall[xb1[z]] = 0;
if ((lwall[xb2[z]] >= walxrepeat) && (walxrepeat)) lwall[xb2[z]] = walxrepeat-1;
if (wal->cstat&8)
{
walxrepeat--;
for(x=xb1[z];x<=xb2[z];x++) lwall[x] = walxrepeat-lwall[x];
}
}
static int getpalookup(long davis, long dashade)
{
return(min(max(dashade+(davis>>8),0),numpalookups-1));
}
static void hline (long xr, long yp)
{
long xl, r, s;
xl = lastx[yp]; if (xl > xr) return;
r = horizlookup2[yp-globalhoriz+horizycent];
asm1 = globalx1*r;
asm2 = globaly2*r;
s = ((long)getpalookup((long)mulscale16(r,globvis),globalshade)<<8);
hlineasm4(xr-xl,0L,s,globalx2*r+globalypanning,globaly1*r+globalxpanning,
ylookup[yp]+xr+frameoffset);
}
static void slowhline (long xr, long yp)
{
long xl, r;
xl = lastx[yp]; if (xl > xr) return;
r = horizlookup2[yp-globalhoriz+horizycent];
asm1 = globalx1*r;
asm2 = globaly2*r;
asm3 = (long)globalpalwritten + ((long)getpalookup((long)mulscale16(r,globvis),globalshade)<<8);
if (!(globalorientation&256))
{
mhline(globalbufplc,globaly1*r+globalxpanning-asm1*(xr-xl),(xr-xl)<<16,0L,
globalx2*r+globalypanning-asm2*(xr-xl),ylookup[yp]+xl+frameoffset);
return;
}