-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathC3_DRAW.C
1592 lines (1298 loc) · 32.9 KB
/
C3_DRAW.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
/* Catacomb 3-D Source Code
* Copyright (C) 1993-2014 Flat Rock Software
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// C3_DRAW.C
#include "C3_DEF.H"
#pragma hdrstop
//#define DRAWEACH // draw walls one at a time for debugging
unsigned highest;
unsigned mostwalls,numwalls;
/*
=============================================================================
LOCAL CONSTANTS
=============================================================================
*/
#define PI 3.141592657
#define ANGLEQUAD (ANGLES/4)
unsigned oldend;
#define FINEANGLES 3600
#define MINRATIO 16
const unsigned MAXSCALEHEIGHT = (VIEWWIDTH/2);
const unsigned MAXVISHEIGHT = (VIEWHEIGHT/2);
const unsigned BASESCALE = 32;
/*
=============================================================================
GLOBAL VARIABLES
=============================================================================
*/
//
// calculate location of screens in video memory so they have the
// maximum possible distance seperating them (for scaling overflow)
//
unsigned screenloc[3]= {0x900,0x2000,0x3700};
unsigned freelatch = 0x4e00;
boolean fizzlein;
long scaleshapecalll;
long scaletablecall;
/*
=============================================================================
LOCAL VARIABLES
=============================================================================
*/
long bytecount,endcount; // for profiling
int animframe;
int pixelangle[VIEWWIDTH];
int far finetangent[FINEANGLES+1];
int fineviewangle;
unsigned viewxpix,viewypix;
/*
============================================================================
3 - D DEFINITIONS
============================================================================
*/
fixed tileglobal = TILEGLOBAL;
fixed focallength = FOCALLENGTH;
fixed mindist = MINDIST;
int viewheight = VIEWHEIGHT;
fixed scale;
tilept tile,lasttile, // tile of wall being followed
focal, // focal point in tiles
left,mid,right; // rightmost tile in view
globpt edge,view;
int segstart[VIEWHEIGHT], // addline tracks line segment and draws
segend[VIEWHEIGHT],
segcolor[VIEWHEIGHT]; // only when the color changes
walltype walls[MAXWALLS],*leftwall,*rightwall;
//==========================================================================
//
// refresh stuff
//
int screenpage;
long lasttimecount;
//
// rendering stuff
//
int firstangle,lastangle;
fixed prestep;
fixed sintable[ANGLES+ANGLES/4],*costable = sintable+(ANGLES/4);
fixed viewx,viewy; // the focal point
int viewangle;
fixed viewsin,viewcos;
int zbuffer[VIEWXH+1]; // holds the height of the wall at that point
//==========================================================================
void DrawLine (int xl, int xh, int y,int color);
void DrawWall (walltype *wallptr);
void TraceRay (unsigned angle);
fixed FixedByFrac (fixed a, fixed b);
fixed FixedAdd (void);
fixed TransformX (fixed gx, fixed gy);
int FollowTrace (fixed tracex, fixed tracey, long deltax, long deltay, int max);
int BackTrace (int finish);
void ForwardTrace (void);
int TurnClockwise (void);
int TurnCounterClockwise (void);
void FollowWall (void);
void NewScene (void);
void BuildTables (void);
//==========================================================================
/*
==================
=
= DrawLine
=
= Must be in write mode 2 with all planes enabled
= The bit mask is left set to the end value, so clear it after all lines are
= drawn
=
= draws a black dot at the left edge of the line
=
==================
*/
unsigned static char dotmask[8] = {0x80,0x40,0x20,0x10,8,4,2,1};
unsigned static char leftmask[8] = {0xff,0x7f,0x3f,0x1f,0xf,7,3,1};
unsigned static char rightmask[8] = {0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
void DrawLine (int xl, int xh, int y,int color)
{
unsigned dest,xlp,xlb,xhb,maskleft,maskright,maskdot,mid;
xlb=xl/8;
xhb=xh/8;
if (xh<xl)
Quit("DrawLine: xh<xl");
if (y<VIEWY)
Quit("DrawLine: y<VIEWY");
if (y>VIEWYH)
Quit("DrawLine: y>VIEWYH");
xlp = xl&7;
maskleft = leftmask[xlp];
maskright = rightmask[xh&7];
mid = xhb-xlb-1;
dest = bufferofs+ylookup[y]+xlb;
//
// set the GC index register to point to the bit mask register
//
asm mov al,GC_BITMASK
asm mov dx,GC_INDEX
asm out dx,al
if (xlb==xhb)
{
//
// entire line is in one byte
//
maskleft&=maskright;
asm mov es,[screenseg]
asm mov di,[dest]
asm mov dx,GC_INDEX+1
asm mov al,[BYTE PTR maskleft]
asm out dx,al // mask off pixels
asm mov al,[BYTE PTR color]
asm xchg al,[es:di] // load latches and write pixels
return;
}
asm mov es,[screenseg]
asm mov di,[dest]
asm mov dx,GC_INDEX+1
asm mov bh,[BYTE PTR color]
//
// draw left side
//
asm mov al,[BYTE PTR maskleft]
asm out dx,al // mask off pixels
asm mov al,bh
asm xchg al,[es:di] // load latches and write pixels
asm inc di
//
// draw middle
//
asm mov al,255
asm out dx,al // no masking
asm mov al,bh
asm mov cx,[mid]
asm rep stosb
//
// draw right side
//
asm mov al,[BYTE PTR maskright]
asm out dx,al // mask off pixels
asm xchg bh,[es:di] // load latches and write pixels
}
//==========================================================================
void DrawLineDot (int xl, int xh, int y,int color)
{
unsigned dest,xlp,xlb,xhb,maskleft,maskright,maskdot,mid;
xlb=xl/8;
xhb=xh/8;
if (xh<xl)
Quit("DrawLine: xh<xl");
if (y<VIEWY)
Quit("DrawLine: y<VIEWY");
if (y>VIEWYH)
Quit("DrawLine: y>VIEWYH");
xlp = xl&7;
maskdot = dotmask[xlp];
maskleft = leftmask[xlp];
maskright = rightmask[xh&7];
mid = xhb-xlb-1;
dest = bufferofs+ylookup[y]+xlb;
//
// set the GC index register to point to the bit mask register
//
asm mov al,GC_BITMASK
asm mov dx,GC_INDEX
asm out dx,al
if (xlb==xhb)
{
//
// entire line is in one byte
//
maskleft&=maskright;
asm mov es,[screenseg]
asm mov di,[dest]
asm mov dx,GC_INDEX+1
asm mov al,[BYTE PTR maskleft]
asm out dx,al // mask off pixels
asm mov al,[BYTE PTR color]
asm xchg al,[es:di] // load latches and write pixels
//
// write the black dot at the start
//
asm mov al,[BYTE PTR maskdot]
asm out dx,al // mask off pixels
asm xor al,al
asm xchg al,[es:di] // load latches and write pixels
return;
}
asm mov es,[screenseg]
asm mov di,[dest]
asm mov dx,GC_INDEX+1
asm mov bh,[BYTE PTR color]
//
// draw left side
//
asm mov al,[BYTE PTR maskleft]
asm out dx,al // mask off pixels
asm mov al,bh
asm xchg al,[es:di] // load latches and write pixels
//
// write the black dot at the start
//
asm mov al,[BYTE PTR maskdot]
asm out dx,al // mask off pixels
asm xor al,al
asm xchg al,[es:di] // load latches and write pixels
asm inc di
//
// draw middle
//
asm mov al,255
asm out dx,al // no masking
asm mov al,bh
asm mov cx,[mid]
asm rep stosb
//
// draw right side
//
asm mov al,[BYTE PTR maskright]
asm out dx,al // mask off pixels
asm xchg bh,[es:di] // load latches and write pixels
}
//==========================================================================
long wallscalesource;
#ifdef DRAWEACH
/*
====================
=
= ScaleOneWall
=
====================
*/
void near ScaleOneWall (int xl, int xh)
{
int x,pixwidth,height;
*(((unsigned *)&wallscalesource)+1) = wallseg[xl];
for (x=xl;x<=xh;x+=pixwidth)
{
height = wallheight[x];
pixwidth = wallwidth[x];
(unsigned)wallscalesource = wallofs[x];
*(((unsigned *)&scaletablecall)+1) = (unsigned)scaledirectory[height];
(unsigned)scaletablecall = scaledirectory[height]->codeofs[0];
//
// scale a byte wide strip of wall
//
asm mov bx,[x]
asm mov di,bx
asm shr di,1
asm shr di,1
asm shr di,1 // X in bytes
asm add di,[bufferofs]
asm and bx,7
asm shl bx,1
asm shl bx,1
asm shl bx,1
asm add bx,[pixwidth] // bx = pixel*8+pixwidth-1
asm dec bx
asm mov al,BYTE PTR [bitmasks1+bx]
asm mov dx,GC_INDEX+1
asm out dx,al // set bit mask register
asm mov es,[screenseg]
asm lds si,[wallscalesource]
asm call [DWORD PTR ss:scaletablecall] // scale the line of pixels
asm mov al,BYTE PTR [ss:bitmasks2+bx]
asm or al,al
asm jz nosecond
//
// draw a second byte for vertical strips that cross two bytes
//
asm inc di
asm out dx,al // set bit mask register
asm call [DWORD PTR ss:scaletablecall] // scale the line of pixels
nosecond:
asm mov ax,ss
asm mov ds,ax
}
}
#endif
int walllight1[NUMFLOORS] = {0,
WALL1LPIC,WALL2LPIC,WALL3LPIC,WALL4LPIC,WALL5LPIC,WALL6LPIC,WALL7LPIC,
WALL1LPIC,WALL2LPIC,WALL3LPIC,WALL4LPIC,WALL5LPIC,WALL6LPIC,WALL7LPIC,
EXPWALL1PIC,EXPWALL2PIC,EXPWALL3PIC,
RDOOR1PIC,RDOOR2PIC,RDOOR1PIC,RDOOR2PIC,
YDOOR1PIC,YDOOR2PIC,YDOOR1PIC,YDOOR2PIC,
GDOOR1PIC,GDOOR2PIC,GDOOR1PIC,GDOOR2PIC,
BDOOR1PIC,BDOOR2PIC,BDOOR1PIC,BDOOR2PIC};
int walldark1[NUMFLOORS] = {0,
WALL1DPIC,WALL2DPIC,WALL3DPIC,WALL4DPIC,WALL5DPIC,WALL6DPIC,WALL7DPIC,
WALL1DPIC,WALL2DPIC,WALL3DPIC,WALL4DPIC,WALL5DPIC,WALL6DPIC,WALL7DPIC,
EXPWALL1PIC,EXPWALL2PIC,EXPWALL3PIC,
RDOOR1PIC,RDOOR2PIC,RDOOR1PIC,RDOOR2PIC,
YDOOR1PIC,YDOOR2PIC,YDOOR1PIC,YDOOR2PIC,
GDOOR1PIC,GDOOR2PIC,GDOOR1PIC,GDOOR2PIC,
BDOOR1PIC,BDOOR2PIC,BDOOR1PIC,BDOOR2PIC};
int walllight2[NUMFLOORS] = {0,
WALL1LPIC,WALL2LPIC,WALL3LPIC,WALL4LPIC,WALL5LPIC,WALL6LPIC,WALL7LPIC,
WALL1LPIC,WALL2LPIC,WALL3LPIC,WALL4LPIC,WALL5LPIC,WALL6LPIC,WALL7LPIC,
EXPWALL1PIC,EXPWALL2PIC,EXPWALL3PIC,
RDOOR2PIC,RDOOR1PIC,RDOOR2PIC,RDOOR1PIC,
YDOOR2PIC,YDOOR1PIC,YDOOR2PIC,YDOOR1PIC,
GDOOR2PIC,GDOOR1PIC,GDOOR2PIC,GDOOR1PIC,
BDOOR2PIC,BDOOR1PIC,BDOOR2PIC,BDOOR1PIC};
int walldark2[NUMFLOORS] = {0,
WALL1DPIC,WALL2DPIC,WALL3DPIC,WALL4DPIC,WALL5DPIC,WALL6DPIC,WALL7DPIC,
WALL1DPIC,WALL2DPIC,WALL3DPIC,WALL4DPIC,WALL5DPIC,WALL6DPIC,WALL7DPIC,
EXPWALL1PIC,EXPWALL2PIC,EXPWALL3PIC,
RDOOR2PIC,RDOOR1PIC,RDOOR2PIC,RDOOR1PIC,
YDOOR2PIC,YDOOR1PIC,YDOOR2PIC,YDOOR1PIC,
GDOOR2PIC,GDOOR1PIC,GDOOR2PIC,GDOOR1PIC,
BDOOR2PIC,BDOOR1PIC,BDOOR2PIC,BDOOR1PIC};
/*
=====================
=
= DrawVWall
=
= Draws a wall by vertical segments, for texture mapping!
=
= wallptr->side is true for east/west walls (constant x)
=
= fracheight and fracstep are 16.16 bit fractions
=
=====================
*/
void DrawVWall (walltype *wallptr)
{
int x,i;
unsigned source;
unsigned width,sourceint;
unsigned wallpic,wallpicseg;
unsigned skip;
long fracheight,fracstep,longheightchange;
unsigned height;
int heightchange;
unsigned slope,distance;
int traceangle,angle;
int mapadd;
unsigned lastpix,lastsource,lastwidth;
if (wallptr->rightclip < wallptr->leftclip)
Quit ("DrawVWall: Right < Left");
//
// setup for height calculation
//
wallptr->height1 >>= 1;
wallptr->height2 >>= 1;
wallptr->planecoord>>=10; // remove non significant bits
width = wallptr->x2 - wallptr->x1;
if (width)
{
heightchange = wallptr->height2 - wallptr->height1;
asm mov ax,[heightchange]
asm mov WORD PTR [longheightchange+2],ax
asm mov WORD PTR [longheightchange],0 // avoid long shift by 16
fracstep = longheightchange/width;
}
fracheight = ((long)wallptr->height1<<16)+0x8000;
skip = wallptr->leftclip - wallptr->x1;
if (skip)
fracheight += fracstep*skip;
//
// setup for texture mapping
//
// mapadd is 64*64 (to keep source positive) + the origin wall intercept
// distance has 6 unit bits, and 6 frac bits
// traceangle is the center view angle in FINEANGLES, moved to be in
// the +-90 degree range (to thew right of origin)
//
traceangle = fineviewangle;
//
// find wall picture to map from
//
if (wallptr->side)
{ // east or west wall
if (animframe)
wallpic = walllight2[wallptr->color];
else
wallpic = walllight1[wallptr->color];
if (wallptr->planecoord < viewxpix)
{
distance = viewxpix-wallptr->planecoord;
traceangle -= FINEANGLES/2;
mapadd = (64-viewypix&63); // the pixel spot of the origin
}
else
{
distance = wallptr->planecoord-viewxpix;
// traceangle is correct
mapadd = viewypix&63; // the pixel spot of the origin
}
}
else
{ // north or south wall
if (animframe)
wallpic = walldark2[wallptr->color];
else
wallpic = walldark1[wallptr->color];
if (wallptr->planecoord < viewypix)
{
distance = viewypix-wallptr->planecoord;
traceangle -= FINEANGLES/4;
mapadd = viewxpix&63; // the pixel spot of the origin
}
else
{
distance = wallptr->planecoord-viewypix;
traceangle -= FINEANGLES*3/4;
mapadd = (64-viewxpix&63); // the pixel spot of the origin
}
}
mapadd = 64*64-mapadd; // make sure it stays positive
wallpicseg = (unsigned)walldirectory[wallpic-FIRSTWALLPIC];
if (traceangle > FINEANGLES/2)
traceangle -= FINEANGLES;
//
// calculate everything
//
// IMPORTANT! This loop is executed around 5000 times / second!
//
lastpix = lastsource = (unsigned)-1;
for (x = wallptr->leftclip ; x <= wallptr->rightclip ; x++)
{
//
// height
//
asm mov ax,WORD PTR [fracheight]
asm mov dx,WORD PTR [fracheight+2]
asm mov cx,dx
asm add ax,WORD PTR [fracstep]
asm adc dx,WORD PTR [fracstep+2]
asm mov WORD PTR [fracheight],ax
asm mov WORD PTR [fracheight+2],dx
asm mov bx,[x]
asm shl bx,1
asm cmp cx,MAXSCALEHEIGHT
asm jbe storeheight
asm mov cx,MAXSCALEHEIGHT
storeheight:
asm mov WORD PTR [wallheight+bx],cx
asm mov WORD PTR [zbuffer+bx],cx
// height = fracheight>>16;
// fracheight += fracstep;
// if (height > MAXSCALEHEIGHT)
// height = MAXSCALEHEIGHT;
// wallheight[x] = zbuffer[x] = height;
//
// texture map
//
angle = pixelangle[x]+traceangle;
if (angle<0)
angle+=FINEANGLES;
slope = finetangent[angle];
//
// distance is an unsigned 6.6 bit number (12 pixel bits)
// slope is a signed 5.10 bit number
// result is a signed 11.16 bit number
//
#if 0
source = distance*slope;
source >>=20;
source += mapadd;
source &= 63; // mask off the unused units
source = 63-source;
source <<= 6; // multiply by 64 for offset into pic
#endif
asm mov ax,[distance]
asm imul [slope] // ax is the source pixel
asm mov al,ah
asm shr al,1
asm shr al,1 // low 6 bits is now pixel number
asm add ax,[mapadd]
asm and ax,63
asm mov dx,63
asm sub dx,ax // otherwise it is backwards
asm shl dx,1
asm shl dx,1
asm shl dx,1
asm shl dx,1
asm shl dx,1
asm shl dx,1 // *64 to index into shape
asm mov [source],dx
if (source != lastsource)
{
if (lastpix != (unsigned)-1)
{
wallofs[lastpix] = lastsource;
wallseg[lastpix] = wallpicseg;
wallwidth[lastpix] = lastwidth;
}
lastpix = x;
lastsource = source;
lastwidth = 1;
}
else
lastwidth++; // optimized draw, same map as last one
}
wallofs[lastpix] = lastsource;
wallseg[lastpix] = wallpicseg;
wallwidth[lastpix] = lastwidth;
}
//==========================================================================
/*
=================
=
= TraceRay
=
= Used to find the left and rightmost tile in the view area to be traced from
= Follows a ray of the given angle from viewx,viewy in the global map until
= it hits a solid tile
= sets:
= tile.x,tile.y : tile coordinates of contacted tile
= tilecolor : solid tile's color
=
==================
*/
int tilecolor;
void TraceRay (unsigned angle)
{
long tracex,tracey,tracexstep,traceystep,searchx,searchy;
fixed fixtemp;
int otx,oty,searchsteps;
tracexstep = costable[angle];
traceystep = sintable[angle];
//
// advance point so it is even with the view plane before we start checking
//
fixtemp = FixedByFrac(prestep,tracexstep);
tracex = viewx+fixtemp;
fixtemp = FixedByFrac(prestep,traceystep);
tracey = viewy-fixtemp;
tile.x = tracex>>TILESHIFT; // starting point in tiles
tile.y = tracey>>TILESHIFT;
if (tracexstep<0) // use 2's complement, not signed magnitude
tracexstep = -(tracexstep&0x7fffffff);
if (traceystep<0) // use 2's complement, not signed magnitude
traceystep = -(traceystep&0x7fffffff);
//
// we assume viewx,viewy is not inside a solid tile, so go ahead one step
//
do // until a solid tile is hit
{
otx = tile.x;
oty = tile.y;
spotvis[otx][oty] = true;
tracex += tracexstep;
tracey -= traceystep;
tile.x = tracex>>TILESHIFT;
tile.y = tracey>>TILESHIFT;
if (tile.x!=otx && tile.y!=oty && (tilemap[otx][tile.y] || tilemap[tile.x][oty]) )
{
//
// trace crossed two solid tiles, so do a binary search along the line
// to find a spot where only one tile edge is crossed
//
searchsteps = 0;
searchx = tracexstep;
searchy = traceystep;
do
{
searchx/=2;
searchy/=2;
if (tile.x!=otx && tile.y!=oty)
{
// still too far
tracex -= searchx;
tracey += searchy;
}
else
{
// not far enough, no tiles crossed
tracex += searchx;
tracey -= searchy;
}
//
// if it is REAL close, go for the most clockwise intersection
//
if (++searchsteps == 16)
{
tracex = (long)otx<<TILESHIFT;
tracey = (long)oty<<TILESHIFT;
if (tracexstep>0)
{
if (traceystep<0)
{
tracex += TILEGLOBAL-1;
tracey += TILEGLOBAL;
}
else
{
tracex += TILEGLOBAL;
}
}
else
{
if (traceystep<0)
{
tracex --;
tracey += TILEGLOBAL-1;
}
else
{
tracey --;
}
}
}
tile.x = tracex>>TILESHIFT;
tile.y = tracey>>TILESHIFT;
} while (( tile.x!=otx && tile.y!=oty) || (tile.x==otx && tile.y==oty) );
}
} while (!(tilecolor = tilemap[tile.x][tile.y]) );
}
//==========================================================================
/*
========================
=
= FixedByFrac
=
= multiply a 16/16 bit, 2's complement fixed point number by a 16 bit
= fraction, passed as a signed magnitude 32 bit number
=
========================
*/
#pragma warn -rvl // I stick the return value in with ASMs
fixed FixedByFrac (fixed a, fixed b)
{
fixed value;
//
// setup
//
asm mov si,[WORD PTR b+2] // sign of result = sign of fraction
asm mov ax,[WORD PTR a]
asm mov cx,[WORD PTR a+2]
asm or cx,cx
asm jns aok: // negative?
asm not ax
asm not cx
asm add ax,1
asm adc cx,0
asm xor si,0x8000 // toggle sign of result
aok:
//
// multiply cx:ax by bx
//
asm mov bx,[WORD PTR b]
asm mul bx // fraction*fraction
asm mov di,dx // di is low word of result
asm mov ax,cx //
asm mul bx // units*fraction
asm add ax,di
asm adc dx,0
//
// put result dx:ax in 2's complement
//
asm test si,0x8000 // is the result negative?
asm jz ansok:
asm not ax
asm not dx
asm add ax,1
asm adc dx,0
ansok:;
}
#pragma warn +rvl
//==========================================================================
/*
========================
=
= TransformPoint
=
= Takes paramaters:
= gx,gy : globalx/globaly of point
=
= globals:
= viewx,viewy : point of view
= viewcos,viewsin : sin/cos of viewangle
=
=
= defines:
= CENTERX : pixel location of center of view window
= TILEGLOBAL : size of one
= FOCALLENGTH : distance behind viewx/y for center of projection
= scale : conversion from global value to screen value
=
= returns:
= screenx,screenheight: projected edge location and size
=
========================
*/
void TransformPoint (fixed gx, fixed gy, int *screenx, unsigned *screenheight)
{
int ratio;
fixed gxt,gyt,nx,ny;
//
// translate point to view centered coordinates
//
gx = gx-viewx;
gy = gy-viewy;
//
// calculate newx
//
gxt = FixedByFrac(gx,viewcos);
gyt = FixedByFrac(gy,viewsin);
nx = gxt-gyt;
//
// calculate newy
//
gxt = FixedByFrac(gx,viewsin);
gyt = FixedByFrac(gy,viewcos);
ny = gyt+gxt;
//
// calculate perspective ratio
//
if (nx<0)
nx = 0;
ratio = nx*scale/FOCALLENGTH;
if (ratio<=MINRATIO)
ratio = MINRATIO;
*screenx = CENTERX + ny/ratio;
*screenheight = TILEGLOBAL/ratio;
}
//
// transform actor
//
void TransformActor (objtype *ob)
{
int ratio;
fixed gx,gy,gxt,gyt,nx,ny;
//
// translate point to view centered coordinates
//
gx = ob->x-viewx;
gy = ob->y-viewy;
//
// calculate newx
//
gxt = FixedByFrac(gx,viewcos);
gyt = FixedByFrac(gy,viewsin);
nx = gxt-gyt-ob->size;
//
// calculate newy
//
gxt = FixedByFrac(gx,viewsin);
gyt = FixedByFrac(gy,viewcos);
ny = gyt+gxt;
//
// calculate perspective ratio
//
if (nx<0)
nx = 0;
ratio = nx*scale/FOCALLENGTH;
if (ratio<=MINRATIO)
ratio = MINRATIO;
ob->viewx = CENTERX + ny/ratio;
ob->viewheight = TILEGLOBAL/ratio;
}
//==========================================================================
fixed TransformX (fixed gx, fixed gy)
{
int ratio;
fixed gxt,gyt,nx,ny;
//
// translate point to view centered coordinates