forked from FrenkelS/Doom8088
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_draw.c
4253 lines (3505 loc) · 153 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 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.
*
*-----------------------------------------------------------------------------*/
//This is to keep the codesize under control.
//This whole file needs to fit within IWRAM.
#pragma GCC optimize ("Os")
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doomstat.h"
#include "d_net.h"
#include "w_wad.h"
#include "r_main.h"
#include "r_things.h"
#include "m_bbox.h"
#include "m_fixed.h"
#include "v_video.h"
#include "st_stuff.h"
#include "i_system.h"
#include "g_game.h"
#include "m_random.h"
#include "globdata.h"
#if 0
static const int8_t viewangletoxTable[4096];
static const angle_t _huge tantoangleTable[2049];
static const fixed_t _huge finetangentTable[4096];
static const angle_t xtoviewangleTable[121] =
{
537395200,531628032,525336576,519569408,513802240,507510784,501219328,494927872,488636416,
481820672,475004928,468189184,461373440,454557696,447217664,439877632,432537600,425197568,
417857536,409993216,402128896,394264576,386400256,378011648,369623040,361234432,352845824,
343932928,335020032,326107136,317194240,307757056,298844160,289406976,279969792,270008320,
260046848,250609664,240648192,230162432,220200960,209715200,199229440,188743680,178257920,
167772160,156762112,145752064,135266304,124256256,113246208,101711872,90701824,79691776,
68157440,56623104,45613056,34078720,22544384,11534336,0,4283432960,4272422912,4260888576,
4249354240,4238344192,4226809856,4215275520,4204265472,4193255424,4181721088,4170711040,
4159700992,4149215232,4138205184,4127195136,4116709376,4106223616,4095737856,4085252096,
4074766336,4064804864,4054319104,4044357632,4034920448,4024958976,4014997504,4005560320,
3996123136,3987210240,3977773056,3968860160,3959947264,3951034368,3942121472,3933732864,
3925344256,3916955648,3908567040,3900702720,3892838400,3884974080,3877109760,3869769728,
3862429696,3855089664,3847749632,3840409600,3833593856,3826778112,3819962368,3813146624,
3806330880,3800039424,3793747968,3787456512,3781165056,3775397888,3769630720,3763339264,3221225472,
};
static const fixed_t yslopeTable[SCREENHEIGHT] =
{
132104,134218,136400,138655,140985,143395,145889,148471,151146,153919,156796,159783,162886,166111,
169467,172961,176602,180400,184365,188508,192842,197379,202135,207126,212370,217886,223696,229825,
236299,243148,250406,258111,266305,275036,284360,294337,305040,316551,328965,342392,356962,372827,
390168,409200,430185,453438,479349,508400,541201,578525,621378,671089,729444,798915,883011,986895,
1118481,1290555,1525201,1864135,2396745,3355443,5592405,16777216,16777216,5592405,3355443,2396745,
1864135,1525201,1290555,1118481,986895,883011,798915,729444,671089,621378,578525,541201,508400,479349,
453438,430185,409200,390168,372827,356962,342392,328965,316551,305040,294337,284360,275036,266305,
258111,250406,243148,236299,229825,223696,217886,212370,207126,202135,197379,192842,188508,184365,
180400,176602,172961,169467,166111,162886,159783,156796,153919,151146,148471,145889,143395,140985,
138655,136400,134218,132104,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
static const fixed_t distscaleTable[SCREENWIDTH] =
{
92789,
92014,91192,90456,89740,88976,88235,87513,86809,86068,85347,84648,83968,83306,82614,81944,81294,
80662,80050,79415,78799,78204,77628,77034,76459,75905,75371,74822,74295,73787,73300,72803,72353,
71895,71457,71015,70593,70212,69828,69445,69099,68754,68430,68124,67837,67568,67304,67060,66845,
66639,66450,66272,66121,65987,65866,65763,65684,65619,65573,65546,65537,65545,65571,65617,65681,
65759,65861,65981,66114,66265,66442,66629,66836,67049,67292,67554,67823,68109,68414,68738,69082,
69425,69808,70191,70572,70992,71433,71871,72327,72776,73271,73758,74264,74791,75338,75872,76424,
76996,77590,78165,78759,79373,80007,80618,81248,81897,82566,83256,83915,84594,85293,86011,86751,
87452,88174,88913,89674,90389,91124,91945,
};
#endif
#if 0
static int8_t viewangletox(int16_t viewangle)
{
return viewangletoxTable[viewangle];
}
static angle_t tantoangle(int16_t tan)
{
return tantoangleTable[tan];
}
static fixed_t finetangent(int16_t x)
{
return finetangentTable[x];
}
static angle_t xtoviewangle(int8_t x)
{
return xtoviewangleTable[x];
}
static fixed_t yslope(uint8_t y)
{
return yslopeTable[y];
}
static fixed_t distscale(uint8_t x)
{
return distscaleTable[x];
}
#else
static int8_t viewangletox(int16_t viewangle)
{
int8_t x;
fseek(_g->fileViewAngleToX, viewangle * sizeof(int8_t), SEEK_SET);
fread(&x, sizeof(int8_t), 1, _g->fileViewAngleToX);
return x;
}
static angle_t tantoangle(int16_t tan)
{
angle_t angle;
fseek(_g->fileTanToAngle, tan * sizeof(angle_t), SEEK_SET);
fread(&angle, sizeof(angle_t), 1, _g->fileTanToAngle);
return angle;
}
static fixed_t finetangent(int16_t x)
{
fixed_t f;
fseek(_g->fileFineTan, x * sizeof(fixed_t), SEEK_SET);
fread(&f, sizeof(fixed_t), 1, _g->fileFineTan);
return f;
}
static angle_t xtoviewangle(int8_t x)
{
angle_t viewangle;
fseek(_g->fileXToViewAngle, x * sizeof(angle_t), SEEK_SET);
fread(&viewangle, sizeof(angle_t), 1, _g->fileXToViewAngle);
return viewangle;
}
static fixed_t yslope(uint8_t y)
{
fixed_t s;
fseek(_g->fileYSlope, y * sizeof(fixed_t), SEEK_SET);
fread(&s, sizeof(fixed_t), 1, _g->fileYSlope);
return s;
}
static fixed_t distscale(uint8_t x)
{
fixed_t d;
fseek(_g->fileDistScale, x * sizeof(fixed_t), SEEK_SET);
fread(&d, sizeof(fixed_t), 1, _g->fileDistScale);
return d;
}
#endif
static uint32_t columnCacheEntries[128];
static int16_t floorclip[SCREENWIDTH];
static int16_t ceilingclip[SCREENWIDTH];
static vissprite_t* vissprite_ptrs[96];
static int16_t screenheightarray[SCREENWIDTH] =
{
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
};
static int16_t negonearray[SCREENWIDTH] =
{
-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, -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
};
//*****************************************
//Column cache stuff.
//GBA has 16kb of Video Memory for columns
//*****************************************
static byte columnCache[128*128];
//*****************************************
//Globals.
//*****************************************
int32_t numnodes;
const mapnode_t *nodes;
fixed_t viewx, viewy, viewz;
angle_t viewangle;
static byte solidcol[SCREENWIDTH];
static byte spanstart[SCREENHEIGHT]; // killough 2/8/98
static const seg_t *curline;
static side_t *sidedef;
static const line_t *linedef;
static sector_t *frontsector;
static sector_t *backsector;
static drawseg_t *ds_p;
static visplane_t *floorplane, *ceilingplane;
static int32_t rw_angle1;
static angle_t rw_normalangle; // angle to line origin
static fixed_t rw_distance;
static int32_t rw_stopx;
static fixed_t rw_scale;
static fixed_t rw_scalestep;
static int32_t worldtop;
static int32_t worldbottom;
static int32_t didsolidcol; /* True if at least one column was marked solid */
// True if any of the segs textures might be visible.
static boolean segtextured;
static boolean markfloor; // False if the back side is the same plane.
static boolean markceiling;
static boolean maskedtexture;
static int32_t toptexture;
static int32_t bottomtexture;
static int32_t midtexture;
static fixed_t rw_midtexturemid;
static fixed_t rw_toptexturemid;
static fixed_t rw_bottomtexturemid;
const lighttable_t *fullcolormap;
const lighttable_t *colormaps;
const lighttable_t* fixedcolormap;
int32_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 angle_t rw_centerangle;
static fixed_t rw_offset;
static int32_t rw_lightlevel;
static int16_t *maskedtexturecol; // dropoff overflow
fixed_t *textureheight; //needed for texture pegging (and TFE fix - killough)
int16_t *flattranslation; // for global animation
int16_t *texturetranslation;
static fixed_t basexscale, baseyscale;
fixed_t viewcos, viewsin;
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;
static lighttable_t current_colormap[256];
static const lighttable_t* current_colormap_ptr;
static fixed_t planeheight;
static size_t num_vissprite;
boolean highDetail = false;
//*****************************************
// Constants
//*****************************************
static const int32_t viewheight = SCREENHEIGHT-ST_SCALED_HEIGHT;
static const int32_t centery = (SCREENHEIGHT-ST_SCALED_HEIGHT)/2;
static const int32_t centerxfrac = ((int32_t)(SCREENWIDTH/2)) << FRACBITS;
static const int32_t centeryfrac = ((int32_t)((SCREENHEIGHT-ST_SCALED_HEIGHT)/2)) << FRACBITS;
static const fixed_t projection = ((int32_t)(SCREENWIDTH/2)) << FRACBITS;
static const fixed_t projectiony = ((SCREENHEIGHT * (SCREENWIDTH/2L) * 320) / 200) / SCREENWIDTH * FRACUNIT;
static const fixed_t pspritescale = FRACUNIT*SCREENWIDTH/320;
static const fixed_t pspriteiscale = FRACUNIT*320/SCREENWIDTH;
static const fixed_t pspriteyscale = (((int32_t)SCREENHEIGHT) << FRACBITS) / 200;
static const fixed_t pspriteyiscale = ((UINT32_MAX) / ((((int32_t)SCREENHEIGHT) << FRACBITS) / 200));
static const angle_t clipangle = 537395200; //xtoviewangle(0);
static const int32_t skytexturemid = 100*FRACUNIT;
static const fixed_t skyiscale = (FRACUNIT*200)/((SCREENHEIGHT-ST_HEIGHT)+16);
//********************************************
// On the GBA we exploit that an 8 bit write
// will mirror to the upper 8 bits too.
// it saves an OR and Shift per pixel.
//********************************************
typedef uint16_t pixel;
//********************************************
// This goes here as we want the Thumb code
// to BX to ARM as Thumb long mul is very slow.
//********************************************
#if defined __WATCOMC__
//
#else
inline
#endif
fixed_t CONSTFUNC FixedMul(fixed_t a, fixed_t b)
{
return (fixed_t)((int64_t) a*b >> FRACBITS);
}
static uint32_t reciprocal(uint32_t val)
{
#if 1
if (val == 0)
return 0;
return 4294967296 / val;
#else
return reciprocalTable[val];
#endif
}
//Approx Reciprocal of v
inline static CONSTFUNC fixed_t FixedReciprocal(fixed_t v)
{
uint32_t val = v < 0 ? -v : v;
uint32_t shift = 0;
while(val > (((int32_t)1) << FRACBITS))
{
val = (val >> 1u);
shift++;
}
fixed_t result = (reciprocal(val) >> shift);
return v < 0 ? -result : result;
}
//Approx fixed point divide of a/b using reciprocal. -> a * (1/b).
inline static CONSTFUNC fixed_t FixedApproxDiv(fixed_t a, fixed_t b)
{
return FixedMul(a, FixedReciprocal(b));
}
// killough 5/3/98: reformatted
#define SLOPERANGE 2048
static CONSTFUNC int32_t SlopeDiv(uint32_t num, uint32_t den)
{
den = den >> 8;
if (den == 0)
return SLOPERANGE;
const uint32_t ans = FixedApproxDiv(num << 3, den) >> FRACBITS;
return (ans <= SLOPERANGE) ? ans : SLOPERANGE;
}
//
// R_PointOnSide
// Traverse BSP (sub) tree,
// check point against partition plane.
// Returns side 0 (front) or 1 (back).
//
// killough 5/2/98: reformatted
//
static PUREFUNC int32_t R_PointOnSide(fixed_t x, fixed_t y, const mapnode_t *node)
{
fixed_t dx = (fixed_t)node->dx << FRACBITS;
fixed_t dy = (fixed_t)node->dy << FRACBITS;
fixed_t nx = (fixed_t)node->x << FRACBITS;
fixed_t ny = (fixed_t)node->y << FRACBITS;
if (!dx)
return x <= nx ? node->dy > 0 : node->dy < 0;
if (!dy)
return y <= ny ? node->dx < 0 : node->dx > 0;
x -= nx;
y -= ny;
// Try to quickly decide by looking at sign bits.
if ((dy ^ dx ^ x ^ y) < 0)
return (dy ^ x) < 0; // (left is negative)
return FixedMul(y, node->dx) >= FixedMul(node->dy, x);
}
//
// R_PointInSubsector
//
// killough 5/2/98: reformatted, cleaned up
subsector_t *R_PointInSubsector(fixed_t x, fixed_t y)
{
int32_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[nodenum & ~NF_SUBSECTOR];
}
//
// 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_PointToAngle2(fixed_t vx, fixed_t vy, fixed_t x, fixed_t y)
{
x -= vx;
y -= vy;
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));
}
}
}
}
static CONSTFUNC angle_t R_PointToAngle(fixed_t x, fixed_t y)
{
return R_PointToAngle2(viewx, viewy, x, y);
}
// killough 5/2/98: move from r_main.c, made static, simplified
#define SLOPEBITS 11
#define DBITS (FRACBITS-SLOPEBITS)
static CONSTFUNC fixed_t R_PointToDist(fixed_t x, fixed_t y)
{
fixed_t dx = D_abs(x - viewx);
fixed_t dy = D_abs(y - viewy);
if (dy > dx)
{
fixed_t t = dx;
dx = dy;
dy = t;
}
return FixedApproxDiv(dx, finesine((tantoangle(FixedApproxDiv(dy,dx) >> DBITS) + ANG90) >> ANGLETOFINESHIFT));
}
// 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
static const lighttable_t* R_ColourMap(int32_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;
int32_t cm = ((256-lightlevel)>>2) - 24;
if(cm >= NUMCOLORMAPS)
cm = NUMCOLORMAPS-1;
else if(cm < 0)
cm = 0;
return fullcolormap + cm*256;
}
}
//Load a colormap into IWRAM.
static const lighttable_t* R_LoadColorMap(int32_t lightlevel)
{
const lighttable_t* lm = R_ColourMap(lightlevel);
if(current_colormap_ptr != lm)
{
memcpy(current_colormap, lm, 256);
current_colormap_ptr = lm;
}
return current_colormap;
}
//
// A column is a vertical slice/span from a wall texture that,
// given the DOOM style restrictions on the view orientation,
// will always have constant z depth.
// Thus a special case loop for very fast rendering can
// be used. It has also been used with Wolfenstein 3D.
//
#pragma GCC push_options
#pragma GCC optimize ("Ofast")
#define COLEXTRABITS 9
#define COLBITS (FRACBITS + COLEXTRABITS)
inline static void R_DrawColumnPixel(uint16_t* dest, const byte* source, const byte* colormap, uint32_t frac)
{
pixel* d = (pixel*)dest;
uint32_t color = colormap[source[frac>>COLBITS]];
*d = (color | (color << 8));
}
// Packaged into a struct - POPE
typedef struct {
int32_t x;
int32_t yl;
int32_t yh;
fixed_t iscale;
fixed_t texturemid;
const byte *source; // first pixel in a column
const lighttable_t *colormap;
const byte *translation;
boolean odd_pixel;
} draw_column_vars_t;
static void R_DrawColumn (const draw_column_vars_t *dcvars)
{
int32_t count = (dcvars->yh - dcvars->yl) + 1;
// Zero length, column does not exceed a pixel.
if (count <= 0)
return;
const byte *source = dcvars->source;
const byte *colormap = dcvars->colormap;
uint16_t* dest = _g->screen + ScreenYToOffset(dcvars->yl) + dcvars->x;
const uint32_t fracstep = (dcvars->iscale << COLEXTRABITS);
uint32_t frac = (dcvars->texturemid + (dcvars->yl - centery)*dcvars->iscale) << COLEXTRABITS;
// Inner loop that does the actual texture mapping,
// e.g. a DDA-lile scaling.
// This is as fast as it gets.
uint32_t l = (count >> 4);
while(l--)
{
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
}
uint32_t r = (count & 15);
switch(r)
{
case 15: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 14: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 13: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 12: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 11: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 10: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 9: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 8: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 7: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 6: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 5: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 4: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 3: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 2: R_DrawColumnPixel(dest, source, colormap, frac); dest+=SCREENWIDTH; frac+=fracstep;
case 1: R_DrawColumnPixel(dest, source, colormap, frac);
}
}
static void R_DrawColumnHiRes(const draw_column_vars_t *dcvars)
{
int32_t count = (dcvars->yh - dcvars->yl) + 1;
// Zero length, column does not exceed a pixel.
if (count <= 0)
return;
const byte *source = dcvars->source;
const byte *colormap = dcvars->colormap;
volatile uint16_t* dest = _g->screen + ScreenYToOffset(dcvars->yl) + dcvars->x;
const uint32_t fracstep = (dcvars->iscale << COLEXTRABITS);
uint32_t frac = (dcvars->texturemid + (dcvars->yl - centery)*dcvars->iscale) << COLEXTRABITS;
// Inner loop that does the actual texture mapping,
// e.g. a DDA-lile scaling.
// This is as fast as it gets.
uint16_t mask;
uint8_t shift;
if(!dcvars->odd_pixel)
{
mask = 0xff00;
shift = 0;
}
else
{
mask = 0xff;
shift = 8;
}
while(count--)
{
uint32_t old = *dest;
uint32_t color = colormap[source[frac>>COLBITS]];
*dest = ((old & mask) | (color << shift));
dest += SCREENWIDTH;
frac += fracstep;
}
}
#define FUZZOFF (SCREENWIDTH)
#define FUZZTABLE 50
static const int32_t fuzzoffset[FUZZTABLE] =
{
FUZZOFF,-FUZZOFF,FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,
FUZZOFF,FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,
FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,-FUZZOFF,-FUZZOFF,-FUZZOFF,
FUZZOFF,-FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,
FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,-FUZZOFF,FUZZOFF,
FUZZOFF,-FUZZOFF,-FUZZOFF,-FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,
FUZZOFF,FUZZOFF,-FUZZOFF,FUZZOFF,FUZZOFF,-FUZZOFF,FUZZOFF
};
//
// Framebuffer postprocessing.
// Creates a fuzzy image by copying pixels
// from adjacent ones to left and right.
// Used with an all black colormap, this
// could create the SHADOW effect,
// i.e. spectres and invisible players.
//
static void R_DrawFuzzColumn (const draw_column_vars_t *dcvars)
{
int32_t dc_yl = dcvars->yl;
int32_t dc_yh = dcvars->yh;
// Adjust borders. Low...
if (dc_yl <= 0)
dc_yl = 1;
// .. and high.
if (dc_yh >= viewheight-1)
dc_yh = viewheight - 2;
int32_t count = (dc_yh - dc_yl) + 1;
// Zero length, column does not exceed a pixel.
if (count <= 0)
return;
const byte* colormap = &fullcolormap[6*256];
uint16_t* dest = _g->screen + ScreenYToOffset(dc_yl) + dcvars->x;
uint32_t fuzzpos = _g->fuzzpos;
do
{
R_DrawColumnPixel(dest, (const byte*)&dest[fuzzoffset[fuzzpos]], colormap, 0); dest += SCREENWIDTH; fuzzpos++;
if(fuzzpos >= 50)
fuzzpos = 0;
} while(--count);
_g->fuzzpos = fuzzpos;
}
#pragma GCC pop_options
//
// 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 *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;
int32_t yh = (bottomscreen-1)>>FRACBITS;
int32_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 (yh < viewheight && yl <= yh)
{
dcvars->source = (const byte*)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 *)((const byte *)column + column->length + 4);
}
dcvars->texturemid = basetexturemid;
}
static void R_SetDefaultDrawColumnVars(draw_column_vars_t *dcvars)
{
dcvars->x = 0;
dcvars->yl = 0;
dcvars->yh = 0;
dcvars->iscale = 0;
dcvars->texturemid = 0;
dcvars->source = NULL;
dcvars->colormap = colormaps;
dcvars->translation = NULL;
}
//
// 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;
boolean hires = false;
R_SetDefaultDrawColumnVars(&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
else
{
hires = highDetail;
if(hires)
colfunc = R_DrawColumnHiRes;
}
// proff 11/06/98: Changed for high-res
dcvars.iscale = vis->iscale;
dcvars.texturemid = vis->texturemid;
frac = vis->startfrac;
spryscale = vis->scale;
sprtopscreen = centeryfrac - FixedMul(dcvars.texturemid, spryscale);
const patch_t *patch = W_GetLumpByNum(vis->patch_num);