-
Notifications
You must be signed in to change notification settings - Fork 5
/
r_draw.c
3508 lines (2921 loc) · 133 KB
/
r_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
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2000 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* 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., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION:
* Rendering main loop and setup functions,
* utility functions (BSP, geometry, trigonometry).
* See tables.c, too.
*
*-----------------------------------------------------------------------------*/
#include <stdint.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "compiler.h"
#include "d_player.h"
#include "w_wad.h"
#include "r_main.h"
#include "r_things.h"
#include "m_fixed.h"
#include "st_stuff.h"
#include "i_system.h"
#include "g_game.h"
#include "m_random.h"
#include "globdata.h"
// Silhouette, needed for clipping Segs (mainly)
// and sprites representing things.
#define SIL_NONE 0
#define SIL_BOTTOM 1
#define SIL_TOP 2
#define SIL_BOTH 3
typedef struct drawseg_s
{
const seg_t __far* curline;
int16_t x1, x2;
fixed_t scale1, scale2, scalestep;
int16_t silhouette; // 0=none, 1=bottom, 2=top, 3=both
fixed_t bsilheight; // do not clip sprites above this
fixed_t tsilheight; // do not clip sprites below this
// Pointers to lists for sprite clipping,
// all three adjusted so [x1] is first value.
int16_t *sprtopclip, *sprbottomclip;
int16_t *maskedtexturecol; // dropoff overflow
} drawseg_t;
#define MAXDRAWSEGS 192
static drawseg_t _s_drawsegs[MAXDRAWSEGS];
#define MAXOPENINGS (VIEWWINDOWWIDTH*16)
static int16_t openings[MAXOPENINGS];
static int16_t* lastopening;
#if VIEWWINDOWWIDTH == 80
#define VIEWANGLETOXTABLESIZE (4096-1023-1040)
#elif VIEWWINDOWWIDTH == 60
#define VIEWANGLETOXTABLESIZE (4096-1023-1046)
#elif VIEWWINDOWWIDTH == 40
#define VIEWANGLETOXTABLESIZE (4096-1023-1057)
#else
#error unsupported VIEWWINDOWWIDTH value
#endif
static const int8_t viewangletoxTable[VIEWANGLETOXTABLESIZE];
static int8_t viewangletox(int16_t va)
{
#ifdef RANGECHECK
if (va < 0)
I_Error("viewangletox: va < 0: %i", va);
else if (va >= 4096)
I_Error("viewangletox: va >= 4096: %i", va);
#endif
#if VIEWWINDOWWIDTH == 80
if (va < 1040) // 0 <= va < 1040
return VIEWWINDOWWIDTH;
else if (3073 <= va) // 3073 <= va < 4096
return 0;
else // 1040 <= va < 3073
return viewangletoxTable[va - 1040];
#elif VIEWWINDOWWIDTH == 60
if (va < 1046) // 0 <= va < 1046
return VIEWWINDOWWIDTH;
else if (3073 <= va) // 3073 <= va < 4096
return 0;
else // 1046 <= va < 3073
return viewangletoxTable[va - 1046];
#elif VIEWWINDOWWIDTH == 40
if (va < 1057) // 0 <= va < 1057
return VIEWWINDOWWIDTH;
else if (3073 <= va) // 3073 <= va < 4096
return 0;
else // 1057 <= va < 3073
return viewangletoxTable[va - 1057];
#else
#error unsupported VIEWWINDOWWIDTH value
#endif
}
static const angle_t tantoangleTable[2049];
static const angle16_t* tantoangle16Table = ((angle16_t*)&tantoangleTable[0]) + 1;
#define tantoangle(t) tantoangleTable[t]
#define tantoangle16(t) tantoangle16Table[(t)*2]
static const uint16_t finetangentTable_part_3[1024];
static const fixed_t finetangentTable_part_4[1024];
static int16_t floorclip[VIEWWINDOWWIDTH];
static int16_t ceilingclip[VIEWWINDOWWIDTH];
static int16_t screenheightarray[VIEWWINDOWWIDTH] =
{
#if VIEWWINDOWWIDTH == 80
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT
#elif VIEWWINDOWWIDTH == 60
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT
#elif VIEWWINDOWWIDTH == 40
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT,
VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT, VIEWWINDOWHEIGHT
#else
#error unsupported VIEWWINDOWWIDTH value
#endif
};
static int16_t negonearray[VIEWWINDOWWIDTH] =
{
#if VIEWWINDOWWIDTH == 80
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1
#elif VIEWWINDOWWIDTH == 60
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1
#elif VIEWWINDOWWIDTH == 40
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1,
-1, -1, -1, -1
#else
#error unsupported VIEWWINDOWWIDTH value
#endif
};
//*****************************************
//Globals.
//*****************************************
int16_t numnodes;
const mapnode_t __far* nodes;
#if defined FLAT_SPAN
static fixed_t viewx, viewy, viewz;
static fixed_t viewcos, viewsin;
#else
fixed_t viewx, viewy, viewz;
fixed_t viewcos, viewsin;
#endif
angle_t viewangle;
static angle16_t viewangle16;
static byte solidcol[VIEWWINDOWWIDTH];
static const seg_t __far* curline;
static side_t __far* sidedef;
static line_t __far* linedef;
static sector_t __far* frontsector;
static sector_t __far* backsector;
static drawseg_t *ds_p;
#if defined FLAT_SPAN
static int16_t floorplane_color;
static int16_t ceilingplane_color;
#else
static visplane_t __far* floorplane;
static visplane_t __far* ceilingplane;
#endif
static angle16_t rw_angle1;
static angle16_t rw_normalangle; // angle to line origin
static int16_t rw_distance;
static int16_t rw_stopx;
static fixed_t rw_scale;
static fixed_t rw_scalestep;
static int32_t worldtop;
static int32_t worldbottom;
static boolean didsolidcol; /* True if at least one column was marked solid */
static boolean maskedtexture;
static int16_t toptexture;
static int16_t bottomtexture;
static int16_t midtexture;
static const texture_t __far* textoptexture;
static const texture_t __far* texbottomtexture;
static const texture_t __far* texmidtexture;
static fixed_t rw_midtexturemid;
static fixed_t rw_toptexturemid;
static fixed_t rw_bottomtexturemid;
const uint8_t __far* fullcolormap;
const uint8_t __far* fixedcolormap;
static int16_t extralight; // bumped light from gun blasts
static int16_t *mfloorclip; // dropoff overflow
static int16_t *mceilingclip; // dropoff overflow
static fixed_t spryscale;
static fixed_t sprtopscreen;
static angle16_t rw_centerangle;
static int16_t rw_offset;
static int16_t rw_lightlevel;
static int16_t *maskedtexturecol; // dropoff overflow
int16_t __far* textureheight; //needed for texture pegging (and TFE fix - killough)
int16_t __far* texturetranslation;
static fixed_t topfrac;
static fixed_t topstep;
static fixed_t bottomfrac;
static fixed_t bottomstep;
static fixed_t pixhigh;
static fixed_t pixlow;
static fixed_t pixhighstep;
static fixed_t pixlowstep;
static int32_t worldhigh;
static int32_t worldlow;
uint16_t validcount = 1; // increment every time a check is made
//*****************************************
// Constants
//*****************************************
static const int16_t CENTERX = VIEWWINDOWWIDTH / 2;
const int16_t CENTERY = VIEWWINDOWHEIGHT / 2;
static const fixed_t PROJECTION = (VIEWWINDOWWIDTH / 2L) << FRACBITS;
static const int16_t PSPRITESCALE = FRACUNIT * VIEWWINDOWWIDTH / SCREENWIDTH_VGA;
static const fixed_t PSPRITEISCALE = FRACUNIT * SCREENWIDTH_VGA / VIEWWINDOWWIDTH; // = FixedReciprocal(PSPRITESCALE)
static const uint16_t PSPRITEYSCALE = FRACUNIT * (VIEWWINDOWHEIGHT * 5 / 4) / SCREENHEIGHT_VGA;
static const fixed_t PSPRITEYISCALE = FRACUNIT * SCREENHEIGHT_VGA / (VIEWWINDOWHEIGHT * 5 / 4); // = FixedReciprocal(PSPRITEYSCALE)
static const angle16_t clipangle = 0x2008; // = xtoviewangleTable[0]
#if defined __WATCOMC__
//
#else
inline
#endif
fixed_t CONSTFUNC FixedMul(fixed_t a, fixed_t b)
{
uint16_t alw = a;
int16_t ahw = a >> FRACBITS;
uint16_t blw = b;
int16_t bhw = b >> FRACBITS;
if (bhw == 0) {
uint32_t ll = (uint32_t) alw * blw;
int32_t hl = ( int32_t) ahw * blw;
return (ll >> FRACBITS) + hl;
} else if (alw == 0) {
//return ahw * b;
int32_t hl = ( int32_t) ahw * blw;
int32_t hh = ( int32_t) ahw * bhw;
return hl + (hh << FRACBITS);
} else {
uint32_t ll = (uint32_t) alw * blw;
int32_t hl = ( int32_t) ahw * blw;
return (a * bhw) + (ll >> FRACBITS) + hl;
}
}
inline static fixed_t CONSTFUNC FixedMul3232(fixed_t a, fixed_t b)
{
uint16_t alw = a;
int16_t ahw = a >> FRACBITS;
uint16_t blw = b;
int16_t bhw = b >> FRACBITS;
uint32_t ll = (uint32_t) alw * blw;
int32_t hl = ( int32_t) ahw * blw;
return (a * bhw) + (ll >> FRACBITS) + hl;
}
//
// FixedMulAngle
// b should be coming from finesine() or finecosine(), so its high word is either 0x0000 or 0xffff
//
#if defined __WATCOMC__
//
#else
inline
#endif
fixed_t CONSTFUNC FixedMulAngle(fixed_t a, fixed_t b)
{
uint16_t alw = a;
int16_t ahw = a >> FRACBITS;
uint16_t blw = b;
uint32_t ll = (uint32_t) alw * blw;
int32_t hl = ( int32_t) ahw * blw;
fixed_t r = (ll >> FRACBITS) + hl;
if (b < 0)
r -= a;
return r;
}
inline static fixed_t CONSTFUNC FixedMul3216(fixed_t a, uint16_t blw)
{
uint16_t alw = a;
int16_t ahw = a >> FRACBITS;
uint32_t ll = (uint32_t) alw * blw;
int32_t hl = ( int32_t) ahw * blw;
return (ll >> FRACBITS) + hl;
}
//Approx fixed point divide of a/b using reciprocal. -> a * (1/b).
#if defined __WATCOMC__
//
#else
inline
#endif
fixed_t CONSTFUNC FixedApproxDiv(fixed_t a, fixed_t b)
{
if (b <= 0xffffu)
return FixedMul3232(a, FixedReciprocalSmall(b));
else
return FixedMul3216(a, FixedReciprocalBig(b));
}
//
// R_PointOnSide
// Traverse BSP (sub) tree,
// check point against partition plane.
// Returns side 0 (front) or 1 (back).
//
static PUREFUNC int8_t R_PointOnSide(fixed_t x, fixed_t y, const mapnode_t __far* node)
{
int16_t ix = x >> FRACBITS;
if (!node->dx)
return ix <= node->x ? node->dy > 0 : node->dy < 0;
int16_t iy = y >> FRACBITS;
if (!node->dy)
return iy <= node->y ? node->dx < 0 : node->dx > 0;
x -= (fixed_t)node->x << FRACBITS;
y -= (fixed_t)node->y << FRACBITS;
ix = x >> FRACBITS;
iy = y >> FRACBITS;
// Try to quickly decide by looking at sign bits.
if ((node->dy ^ node->dx ^ ix ^ iy) < 0)
return (node->dy ^ ix) < 0; // (left is negative)
//return FixedMul(y, node->dx) >= FixedMul(x, node->dy);
return (y >> 8) * node->dx >= (x >> 8) * node->dy;
}
//
// R_PointInSubsector
//
subsector_t __far* R_PointInSubsector(fixed_t x, fixed_t y)
{
int16_t nodenum = numnodes-1;
// special case for trivial maps (single subsector, no nodes)
if (numnodes == 0)
return _g_subsectors;
while (!(nodenum & NF_SUBSECTOR))
nodenum = nodes[nodenum].children[R_PointOnSide(x, y, nodes+nodenum)];
return &_g_subsectors[(int16_t)(nodenum & ~NF_SUBSECTOR)];
}
#define SLOPERANGE 2048
static CONSTFUNC int16_t SlopeDiv(uint32_t num, uint32_t den)
{
den = den >> 8;
if (den == 0)
return SLOPERANGE;
const uint16_t ans = (num << 3) / den;//FixedApproxDiv(num << 3, den) >> FRACBITS;
return (ans <= SLOPERANGE) ? ans : SLOPERANGE;
}
static CONSTFUNC int16_t SlopeDiv16(uint16_t n, uint16_t d)
{
if (d == 0)
return SLOPERANGE;
const uint16_t ans = ((uint32_t)n * SLOPERANGE) / d;
return (ans <= SLOPERANGE) ? ans : SLOPERANGE;
}
//
// R_PointToAngle
// To get a global angle from cartesian coordinates,
// the coordinates are flipped until they are in
// the first octant of the coordinate system, then
// the y (<=x) is scaled and divided by x to get a
// tangent (slope) value which is looked up in the
// tantoangleTable[] table.
//
CONSTFUNC angle_t R_PointToAngle3(fixed_t x, fixed_t y)
{
if ( (!x) && (!y) )
return 0;
if (x>= 0)
{
// x >=0
if (y>= 0)
{
// y>= 0
if (x>y)
{
// octant 0
return tantoangle(SlopeDiv(y,x));
}
else
{
// octant 1
return ANG90-1-tantoangle(SlopeDiv(x,y));
}
}
else
{
// y<0
y = -y;
if (x>y)
{
// octant 8
return -tantoangle(SlopeDiv(y,x));
}
else
{
// octant 7
return ANG270+tantoangle(SlopeDiv(x,y));
}
}
}
else
{
// x<0
x = -x;
if (y>= 0)
{
// y>= 0
if (x>y)
{
// octant 3
return ANG180-1-tantoangle(SlopeDiv(y,x));
}
else
{
// octant 2
return ANG90+ tantoangle(SlopeDiv(x,y));
}
}
else
{
// y<0
y = -y;
if (x>y)
{
// octant 4
return ANG180+tantoangle(SlopeDiv(y,x));
}
else
{
// octant 5
return ANG270-1-tantoangle(SlopeDiv(x,y));
}
}
}
}
#define R_PointToAngle(x,y) R_PointToAngle16((x)>>FRACBITS,(y)>>FRACBITS)
static angle16_t R_PointToAngle16(int16_t x, int16_t y)
{
x = x - (viewx >> FRACBITS);
y = y - (viewy >> FRACBITS);
if (!x && !y)
return 0;
if (x >= 0)
{
// x >= 0
if (y >= 0)
{
// y >= 0
if (x > y)
{
// octant 0
return tantoangle16(SlopeDiv16(y, x));
}
else
{
// octant 1
return ANG90_16 - 1 - tantoangle16(SlopeDiv16(x, y));
}
}
else
{
// y < 0
y = -y;
if (x > y)
{
// octant 8
return -tantoangle16(SlopeDiv16(y, x));
}
else
{
// octant 7
return ANG270_16 + tantoangle16(SlopeDiv16(x, y));
}
}
}
else
{
// x < 0
x = -x;
if (y >= 0)
{
// y >= 0
if (x > y)
{
// octant 3
return ANG180_16 - 1 - tantoangle16(SlopeDiv16(y, x));
}
else
{
// octant 2
return ANG90_16 + tantoangle16(SlopeDiv16(x, y));
}
}
else
{
// y < 0
y = -y;
if (x > y)
{
// octant 4
return ANG180_16 + tantoangle16(SlopeDiv16(y, x));
}
else
{
// octant 5
return ANG270_16 - 1 - tantoangle16(SlopeDiv16(x, y));
}
}
}
}
#define SLOPEBITS 11
#define DBITS (FRACBITS-SLOPEBITS)
static CONSTFUNC int16_t R_PointToDist(int16_t x, int16_t y)
{
if (viewx == (fixed_t)x << FRACBITS && viewy == (fixed_t)y << FRACBITS)
return 0;
fixed_t dx = D_abs(((fixed_t)x << FRACBITS) - viewx);
fixed_t dy = D_abs(((fixed_t)y << FRACBITS) - viewy);
if (dy > dx)
{
fixed_t t = dx;
dx = dy;
dy = t;
}
return dx / finecosineapprox((FixedApproxDiv(dy,dx) >> DBITS) / 2);
}
// Lighting constants.
#define LIGHTSEGSHIFT 4
// Number of diminishing brightness levels.
// There a 0-31, i.e. 32 LUT in the COLORMAP lump.
#define NUMCOLORMAPS 32
const uint8_t __far* R_LoadColorMap(int16_t lightlevel)
{
if (fixedcolormap)
return fixedcolormap;
else
{
if (curline)
{
if (curline->v1.y == curline->v2.y)
lightlevel -= 1 << LIGHTSEGSHIFT;
else if (curline->v1.x == curline->v2.x)
lightlevel += 1 << LIGHTSEGSHIFT;
}
lightlevel += (extralight +_g_gamma) << LIGHTSEGSHIFT;
int16_t cm = ((256-lightlevel)>>2) - 24;
if(cm >= NUMCOLORMAPS)
cm = NUMCOLORMAPS-1;
else if(cm < 0)
cm = 0;
return fullcolormap + cm*256;
}
}
//
// R_DrawMaskedColumn
// Used for sprites and masked mid textures.
// Masked means: partly transparent, i.e. stored
// in posts/runs of opaque pixels.
//
typedef void (*R_DrawColumn_f)(const draw_column_vars_t *dcvars);
static void R_DrawMaskedColumn(R_DrawColumn_f colfunc, draw_column_vars_t *dcvars, const column_t __far* column)
{
const fixed_t basetexturemid = dcvars->texturemid;
const int16_t fclip_x = mfloorclip[dcvars->x];
const int16_t cclip_x = mceilingclip[dcvars->x];
while (column->topdelta != 0xff)
{
// calculate unclipped screen coordinates for post
const int32_t topscreen = sprtopscreen + spryscale*column->topdelta;
const int32_t bottomscreen = topscreen + spryscale*column->length;
int16_t yh = (bottomscreen-1)>>FRACBITS;
int16_t yl = (topscreen+FRACUNIT-1)>>FRACBITS;
if (yh >= fclip_x)
yh = fclip_x - 1;
if (yl <= cclip_x)
yl = cclip_x + 1;
// killough 3/2/98, 3/27/98: Failsafe against overflow/crash:
if (yl <= yh && yh < VIEWWINDOWHEIGHT)
{
dcvars->source = (const byte __far*)column + 3;
dcvars->texturemid = basetexturemid - (((int32_t)column->topdelta)<<FRACBITS);
dcvars->yh = yh;
dcvars->yl = yl;
// Drawn by either R_DrawColumn or (SHADOW) R_DrawFuzzColumn.
colfunc (dcvars);
}
column = (const column_t __far*)((const byte __far*)column + column->length + 4);
}
dcvars->texturemid = basetexturemid;
}
//
// R_InitColormaps
//
void R_InitColormaps(void)
{
fullcolormap = W_GetLumpByName("COLORMAP"); // Never freed
}
//
// A vissprite_t is a thing that will be drawn during a refresh.
// i.e. a sprite object that is partly visible.
//
typedef struct vissprite_s
{
int16_t x1, x2;
fixed_t gx, gy; // for line side calculation
fixed_t gz; // global bottom for silhouette clipping
fixed_t startfrac; // horizontal position of x1
fixed_t scale;
fixed_t xiscale; // negative if flipped
fixed_t texturemid;
fixed_t iscale;
int16_t lump_num;
int16_t patch_topoffset;
// for color translation and shadow draw, maxbright frames as well
const uint8_t __far* colormap;
} vissprite_t;
void R_DrawFuzzColumn (const draw_column_vars_t *dcvars);
//
// R_DrawVisSprite
// mfloorclip and mceilingclip should also be set.
//
// CPhipps - new wad lump handling, *'s to const*'s
static void R_DrawVisSprite(const vissprite_t *vis)
{
fixed_t frac;
R_DrawColumn_f colfunc = R_DrawColumn;
draw_column_vars_t dcvars;
dcvars.colormap = vis->colormap;
// killough 4/11/98: rearrange and handle translucent sprites
// mixed with translucent/non-translucenct 2s normals
if (!dcvars.colormap) // NULL colormap = shadow draw
colfunc = R_DrawFuzzColumn; // killough 3/14/98
// proff 11/06/98: Changed for high-res
dcvars.iscale = vis->iscale;
dcvars.texturemid = vis->texturemid;
frac = vis->startfrac;
spryscale = vis->scale;
sprtopscreen = CENTERY * FRACUNIT - FixedMul(dcvars.texturemid, spryscale);
const patch_t __far* patch = W_GetLumpByNum(vis->lump_num);
dcvars.x = vis->x1;
while (dcvars.x < VIEWWINDOWWIDTH)
{
const column_t __far* column = (const column_t __far*) ((const byte __far*)patch + (uint16_t)patch->columnofs[frac >> FRACBITS]);
R_DrawMaskedColumn(colfunc, &dcvars, column);
frac += vis->xiscale;
if(((frac >> FRACBITS) >= patch->width) || frac < 0)
break;
dcvars.x++;
}
Z_ChangeTagToCache(patch);
}
static void R_GetColumn(const texture_t __far* texture, int16_t texcolumn, int16_t* patch_num, int16_t* x_c)
{
const uint8_t patchcount = texture->patchcount;
const int16_t xc = texcolumn & texture->widthmask;
if (patchcount == 1)
{
//simple texture.
*patch_num = texture->patches[0].patch_num;
*x_c = xc;
}
else
{
uint8_t i = 0;
do
{
const texpatch_t __far* patch = &texture->patches[i];
int16_t x = xc - patch->originx;
if (0 <= x && x < patch->patch_width)
{
*patch_num = patch->patch_num;
*x_c = x;
break;
}
} while (++i < patchcount);
}
}
//
// R_RenderMaskedSegRange
//
static void R_RenderMaskedSegRange(const drawseg_t *ds, int16_t x1, int16_t x2)
{
draw_column_vars_t dcvars;
// Calculate light table.
// Use different light tables
// for horizontal / vertical / diagonal. Diagonal?
curline = ds->curline; // OPTIMIZE: get rid of LIGHTSEGSHIFT globally
frontsector = &_g_sectors[curline->frontsectornum];
backsector = &_g_sectors[curline->backsectornum];
int16_t texnum = texturetranslation[_g_sides[curline->sidenum].midtexture];
// killough 4/13/98: get correct lightlevel for 2s normal textures
rw_lightlevel = frontsector->lightlevel;
maskedtexturecol = ds->maskedtexturecol;
rw_scalestep = ds->scalestep;
spryscale = ds->scale1 + (x1 - ds->x1) * rw_scalestep;
mfloorclip = ds->sprbottomclip;
mceilingclip = ds->sprtopclip;
// find positioning
if (_g_lines[curline->linenum].flags & ML_DONTPEGBOTTOM)
{
dcvars.texturemid = frontsector->floorheight > backsector->floorheight ? frontsector->floorheight : backsector->floorheight;
dcvars.texturemid = dcvars.texturemid + ((int32_t)textureheight[texnum] << FRACBITS) - viewz;
}
else
{
dcvars.texturemid =frontsector->ceilingheight<backsector->ceilingheight ? frontsector->ceilingheight : backsector->ceilingheight;
dcvars.texturemid = dcvars.texturemid - viewz;
}