-
Notifications
You must be signed in to change notification settings - Fork 2
/
wl_draw.c
1467 lines (1217 loc) · 30.1 KB
/
wl_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
#include "wl_def.h"
/* C AsmRefresh() and related code
originally from David Haslam -- [email protected] */
/* the door is the last picture before the sprites */
#define DOORWALL (PMSpriteStart-8)
#define ACTORSIZE 0x4000
#ifndef DRAWCEIL
/* #define DRAWCEIL */
#endif
#ifndef ENABLE_COLOR
#define gfxbuf gfxbuf4
#endif
static umyshort wallheight[MAXVIEWWIDTH];
/* refresh variables */
extern fixed viewx, viewy; /* the focal point */
static myint viewangle;
static unsigned tilehit;
static myint xtile, ytile;
static myint xtilestep, ytilestep;
static fixed xintercept, yintercept;
static unsigned postx;
#ifdef EMBEDDED
#define focallength FOCALLENGTH
#define scale ((viewwidth/2)*(focallength+MINDIST)/(VIEWGLOBAL/2))
#define heightnumerator ((TILEGLOBAL*scale)>>6)
#else
static fixed focallength;
static fixed scale;
static long heightnumerator;
#endif
static void AsmRefresh();
void ScaleShape(myint xcenter, myint shapenum, unsigned height);
void SimpleScaleShape(myint xcenter, myint shapenum, unsigned height);
/* ======================================================================== */
#ifndef EMBEDDED
/*
====================
=
= CalcProjection
=
====================
*/
static const double radtoint = (double)FINEANGLES/2.0/PI;
void CalcProjection(long focal)
{
myint i;
long intang;
double angle, tang, facedist;
myint halfview;
focallength = focal;
facedist = focal+MINDIST;
halfview = viewwidth/2; /* half view in pixels */
/*
calculate scale value for vertical height calculations
and sprite x calculations
*/
scale = halfview*facedist/(VIEWGLOBAL/2);
/*
divide heightnumerator by a posts distance to get the posts height for
the heightbuffer. The pixel height is height>>2
*/
heightnumerator = (TILEGLOBAL*scale)>>6;
/* calculate the angle offset from view angle of each pixel's ray */
for (i = 0; i < halfview; i++) {
tang = ((double)i)*VIEWGLOBAL/viewwidth/facedist;
angle = atan(tang);
intang = angle*radtoint;
pixelangle[halfview-1-i] = intang;
pixelangle[halfview+i] = -intang;
}
}
#endif
/*
========================
=
= TransformActor
=
= Takes paramaters:
= gx,gy : globalx/globaly of point
=
= globals:
= viewx,viewy : point of view
= viewcos,viewsin : sin/cos of viewangle
= scale : conversion from global value to screen value
=
= sets:
= screenx,transx,transy,screenheight: projected edge location and size
=
========================
*/
static void TransformActor(objtype *ob)
{
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-ACTORSIZE; // fudge the shape forward a bit, because
// the midpoint could put parts of the shape
// into an adjacent wall
//
// calculate newy
//
gxt = FixedByFrac(gx,viewsin);
gyt = FixedByFrac(gy,viewcos);
ny = gyt+gxt;
//
// calculate perspective ratio
//
ob->transx = nx;
ob->transy = ny;
if (nx < MINDIST) /* too close, don't overflow the divide */
{
ob->viewheight = 0;
return;
}
ob->viewx = centerx + ny*scale/nx;
ob->viewheight = heightnumerator/(nx>>8);
}
/*
========================
=
= TransformTile
=
= Takes paramaters:
= tx,ty : tile the object is centered in
=
= globals:
= viewx,viewy : point of view
= viewcos,viewsin : sin/cos of viewangle
= scale : conversion from global value to screen value
=
= sets:
= screenx,transx,transy,screenheight: projected edge location and size
=
= Returns true if the tile is withing getting distance
=
========================
*/
static boolean TransformTile(myint tx, myint ty, myshort *dispx, myshort *dispheight)
{
fixed gx,gy,gxt,gyt,nx,ny;
//
// translate point to view centered coordinates
//
gx = ((long)tx<<TILESHIFT)+0x8000-viewx;
gy = ((long)ty<<TILESHIFT)+0x8000-viewy;
//
// calculate newx
//
gxt = FixedByFrac(gx,viewcos);
gyt = FixedByFrac(gy,viewsin);
nx = gxt-gyt-0x2000; // 0x2000 is size of object
//
// calculate newy
//
gxt = FixedByFrac(gx,viewsin);
gyt = FixedByFrac(gy,viewcos);
ny = gyt+gxt;
//
// calculate perspective ratio
//
if (nx<MINDIST) /* too close, don't overflow the divide */
{
*dispheight = 0;
return false;
}
*dispx = centerx + ny*scale/nx;
*dispheight = heightnumerator/(nx>>8);
//
// see if it should be grabbed
//
if ( (nx<TILEGLOBAL) && (ny>-TILEGLOBAL/2) && (ny<TILEGLOBAL/2) )
return true;
else
return false;
}
/* ======================================================================== */
/*
=====================
=
= CalcRotate
=
=====================
*/
static const myint dirangle[9] = {0,ANGLES/8,2*ANGLES/8,3*ANGLES/8,4*ANGLES/8, 5*ANGLES/8,6*ANGLES/8,7*ANGLES/8,ANGLES};
static myint CalcRotate(objtype *ob)
{
myint angle,viewangle;
/* this isn't exactly correct, as it should vary by a trig value, */
/* but it is close enough with only eight rotations */
viewangle = player->angle + (centerx - ob->viewx)/8;
if (ob->obclass == rocketobj || ob->obclass == hrocketobj)
angle = (viewangle-180)- ob->angle;
else
angle = (viewangle-180)- dirangle[ob->dir];
angle+=ANGLES/16;
while (angle>=ANGLES)
angle-=ANGLES;
while (angle<0)
angle+=ANGLES;
if (gamestates[ob->state].rotate == 2) // 2 rotation pain frame
return 4*(angle/(ANGLES/2)); // seperated by 3
return angle/(ANGLES/8);
}
/*
=====================
=
= DrawScaleds
=
= Draws all objects that are visible
=
=====================
*/
#define MAXVISABLE 64
typedef struct {
myshort viewx;
myshort viewheight;
myshort shapenum;
} visobj_t;
static visobj_t vislist[MAXVISABLE], *visptr, *visstep, *farthest;
static void DrawScaleds()
{
myint i,least,numvisable,height;
byte *tilespot;
unsigned spotloc;
statobj_t *statptr;
objtype *obj;
visptr = &vislist[0];
/* place static objects */
for (statptr = &statobjlist[0]; statptr != laststatobj; statptr++)
{
if ((visptr->shapenum = statptr->shapenum) == -1)
continue; /* object has been deleted */
if (!getspotvis(statptr->tilex, statptr->tiley))
continue; /* not visable */
if (TransformTile(statptr->tilex, statptr->tiley
,&visptr->viewx,&visptr->viewheight) && statptr->is_bonus)
{
GetBonus(statptr);
continue;
}
if (!visptr->viewheight)
continue; /* too close to the object */
if (visptr < &vislist[MAXVISABLE-1]) /* don't let it overflow */
visptr++;
}
//
// place active objects
//
for (obj = obj_next(player); obj; obj = obj_next(obj))
{
uint64_t spotmask;
uint64_t spota;
uint64_t spotb;
uint64_t spotc;
if (!(visptr->shapenum = gamestates[obj->state].shapenum))
continue; // no shape
spotloc = (obj->tilex << 6) + obj->tiley;
tilespot = &tilemap[0][0] + spotloc;
spota = spotvis[obj->tilex-1];
spotb = spotvis[obj->tilex];
spotc = spotvis[obj->tilex+1];
spotmask = 1ull << obj->tiley;
//
// could be in any of the nine surrounding tiles
//
if ((spotb & spotmask)
|| ((spotb & (spotmask >> 1)) && !*(tilespot-1))
|| ((spotb & (spotmask << 1)) && !*(tilespot+1))
|| ((spota & (spotmask >> 1)) && !*(tilespot-65))
|| ((spota & spotmask) && !*(tilespot-64))
|| ((spota & (spotmask << 1)) && !*(tilespot-63))
|| ((spotc & (spotmask >> 1)) && !*(tilespot+63))
|| ((spotc & spotmask) && !*(tilespot+64))
|| ((spotc & (spotmask << 1)) && !*(tilespot+65)))
{
obj->active = ac_yes;
TransformActor(obj);
if (!obj->viewheight)
continue; // too close or far away
visptr->viewx = obj->viewx;
visptr->viewheight = obj->viewheight;
if (visptr->shapenum == -1)
visptr->shapenum = obj->temp1; // special shape
if (gamestates[obj->state].rotate)
visptr->shapenum += CalcRotate(obj);
if (visptr < &vislist[MAXVISABLE-1]) /* don't let it overflow */
visptr++;
obj->flags |= FL_VISABLE;
} else
obj->flags &= ~FL_VISABLE;
}
//
// draw from back to front
//
numvisable = visptr-&vislist[0];
if (!numvisable)
return; // no visable objects
for (i = 0; i < numvisable; i++)
{
least = 32000;
for (visstep = &vislist[0]; visstep < visptr; visstep++)
{
height = visstep->viewheight;
if (height < least)
{
least = height;
farthest = visstep;
}
}
//
// draw farthest
//
ScaleShape(farthest->viewx, farthest->shapenum, farthest->viewheight);
farthest->viewheight = 32000;
}
}
/* ======================================================================== */
/*
==============
=
= DrawPlayerWeapon
=
= Draw the player's hands
=
==============
*/
static const short weaponscale[NUMWEAPONS] = {SPR_KNIFEREADY,SPR_PISTOLREADY
,SPR_MACHINEGUNREADY,SPR_CHAINREADY};
static void DrawPlayerWeapon()
{
myint shapenum;
#ifndef SPEAR
if (gamestate.victoryflag)
{
if ((player->state == s_deathcam) && (get_TimeCount() & 32))
SimpleScaleShape(viewwidth/2,SPR_DEATHCAM,sviewheight+1);
return;
}
#endif
if (gamestate.weapon != wp_none) {
shapenum = weaponscale[gamestate.weapon]+gamestate.weaponframe;
SimpleScaleShape(viewwidth/2,shapenum,sviewheight+1);
}
#ifdef ENABLE_DEMO
if (demorecord || demoplayback)
SimpleScaleShape(viewwidth/2,SPR_DEMO,sviewheight+1);
#endif
}
/*
====================
=
= WallRefresh
=
====================
*/
static void WallRefresh()
{
viewangle = player->angle;
viewsin = sinfix(viewangle);
viewcos = cosfix(viewangle);
viewx = player->x - FixedByFrac(focallength, viewcos);
viewy = player->y + FixedByFrac(focallength, viewsin);
AsmRefresh();
}
#ifdef DRAWCEIL
/* ======================================================================== */
#define MAXVIEWHEIGHT (MAXVIEWWIDTH/2)
static myint spanstart[MAXVIEWHEIGHT/2];
static fixed basedist[MAXVIEWHEIGHT/2];
static byte planepics[8192]; /* 4k of ceiling, 4k of floor */
static myint halfheight = 0;
static byte *planeylookup[MAXVIEWHEIGHT/2];
static unsigned mirrorofs[MAXVIEWHEIGHT/2];
static myint mr_rowofs;
static myint mr_count;
static unsigned short mr_xstep;
static unsigned short mr_ystep;
static unsigned short mr_xfrac;
static unsigned short mr_yfrac;
static byte *mr_dest;
static void MapRow()
{
unsigned myint ebx, edx, esi;
edx = (mr_ystep << 16) | (mr_xstep);
esi = (mr_yfrac << 16) | (mr_xfrac);
while (mr_count--) {
ebx = ((esi & 0xFC000000) >> 25) | ((esi & 0xFC00) >> 3);
esi += edx;
//ebx = ((mr_yfrac & 0xFC00) >> (25-16)) | ((mr_xfrac & 0xFC00) >> 3);
//mr_yfrac += mr_ystep;
//mr_xfrac += mr_xstep;
mr_dest[0] = planepics[ebx+0];
mr_dest[mr_rowofs] = planepics[ebx+1];
mr_dest++;
}
}
/*
==============
=
= DrawSpans
=
= Height ranges from 0 (infinity) to viewheight/2 (nearest)
==============
*/
static void DrawSpans(myint x1, myint x2, myint height)
{
fixed length;
myint prestep;
fixed startxfrac, startyfrac;
byte *toprow;
toprow = planeylookup[height]+(vwidth*yoffset+xoffset);
mr_rowofs = mirrorofs[height];
mr_xstep = (viewsin / height) >> 1;
mr_ystep = (viewcos / height) >> 1;
length = basedist[height];
startxfrac = (viewx + FixedByFrac(length, viewcos));
startyfrac = (viewy - FixedByFrac(length, viewsin));
// draw two spans simultaniously
prestep = viewwidth/2 - x1;
mr_xfrac = startxfrac - mr_xstep*prestep;
mr_yfrac = startyfrac - mr_ystep*prestep;
mr_dest = toprow + x1;
mr_count = x2 - x1 + 1;
if (mr_count)
MapRow();
}
/*
===================
=
= SetPlaneViewSize
=
===================
*/
static void SetPlaneViewSize()
{
myint x, y;
byte *dest, *src;
halfheight = sviewheight >> 1;
for (y = 0; y < halfheight; y++) {
planeylookup[y] = gfxbuf + (halfheight-1-y)*vwidth;
mirrorofs[y] = (y*2+1)*vwidth;
if (y > 0)
basedist[y] = GLOBAL1/2*scale/y;
}
src = PM_GetPage(0);
dest = planepics;
for (x = 0; x < 4096; x++) {
*dest = *src++;
dest += 2;
}
src = PM_GetPage(1);
dest = planepics+1;
for (x = 0; x < 4096; x++) {
*dest = *src++;
dest += 2;
}
}
/*
===================
=
= DrawPlanes
=
===================
*/
void DrawPlanes()
{
myint height, lastheight;
myint x;
if ((sviewheight>>1) != halfheight)
SetPlaneViewSize(); /* screen size has changed */
/* loop over all columns */
lastheight = halfheight;
for (x = 0; x < viewwidth; x++)
{
height = wallheight[x]>>3;
if (height < lastheight) { // more starts
do {
spanstart[--lastheight] = x;
} while (lastheight > height);
} else if (height > lastheight) { // draw spans
if (height > halfheight)
height = halfheight;
for (; lastheight < height; lastheight++)
DrawSpans(spanstart[lastheight], x-1, lastheight);
}
}
height = halfheight;
for (; lastheight < height; lastheight++)
DrawSpans(spanstart[lastheight], x-1, lastheight);
}
#endif
/* ======================================================================== */
static const byte Ceiling[]=
{
#ifndef SPEAR
0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0xbf,
0x4e,0x4e,0x4e,0x1d,0x8d,0x4e,0x1d,0x2d,0x1d,0x8d,
0x1d,0x1d,0x1d,0x1d,0x1d,0x2d,0xdd,0x1d,0x1d,0x98,
0x1d,0x9d,0x2d,0xdd,0xdd,0x9d,0x2d,0x4d,0x1d,0xdd,
0x7d,0x1d,0x2d,0x2d,0xdd,0xd7,0x1d,0x1d,0x1d,0x2d,
0x1d,0x1d,0x1d,0x1d,0xdd,0xdd,0x7d,0xdd,0xdd,0xdd
#else
0x6f,0x4f,0x1d,0xde,0xdf,0x2e,0x7f,0x9e,0xae,0x7f,
0x1d,0xde,0xdf,0xde,0xdf,0xde,0xe1,0xdc,0x2e,0x1d,0xdc
#endif
};
/*
=====================
=
= ClearScreen
=
=====================
*/
static void ClearScreen()
{
unsigned myint ceiling = Ceiling[gamestate_episode*10+mapon];
unsigned myint floor = 0x19;
VL_Bar(xoffset, yoffset, viewwidth, sviewheight / 2, ceiling);
VL_Bar(xoffset, yoffset + sviewheight / 2, viewwidth, sviewheight / 2, floor);
}
/* ======================================================================== */
/*
========================
=
= ThreeDRefresh
=
========================
*/
void ThreeDRefresh()
{
/* clear out the traced array */
memset(spotvis, 0, sizeof(spotvis));
#ifndef DRAWCEIL
ClearScreen();
#endif
WallRefresh();
#ifdef DRAWCEIL
DrawPlanes(); /* silly floor/ceiling drawing */
#endif
/* draw all the scaled images */
DrawScaleds(); /* draw scaled stuff */
DrawPlayerWeapon(); /* draw player's hands */
/* show screen and time last cycle */
VW_UpdateScreen();
frameon++;
}
/* ======================================================================== */
static void ScaledDraw(byte *gfx, myint count, byte *vid, int x,
unsigned myint frac, unsigned myint delta)
{
#ifdef EMBEDDED
vid += x >> 1;
if (x & 1) {
while (count--) {
*vid = (*vid & 0xf0) | pal4bit[gfx[frac >> 16]];
vid += vpitch;
frac += delta;
}
} else {
while (count--) {
*vid = (*vid & 0x0f) | (pal4bit[gfx[frac >> 16]] << 4);
vid += vpitch;
frac += delta;
}
}
#else
vid += x;
while (count--) {
*vid = gfx[frac >> 16];
vid += vpitch;
frac += delta;
}
#endif
}
static void ScaleLine(unsigned myint height, byte *source, myint x)
{
unsigned myint y, frac, delta;
if (height) {
frac = (64 << 16) / height;
delta = (64 << 16) - frac*height;
if (height < sviewheight) {
y = yoffset + (sviewheight - height) / 2;
ScaledDraw(source, height, gfxbuf + (y * vpitch) + xoffset,
x, delta, frac);
return;
}
y = (height - sviewheight) / 2;
y *= frac;
ScaledDraw(source, sviewheight, gfxbuf + (yoffset * vpitch) + xoffset,
x, y+delta, frac);
}
}
#ifdef ENABLE_COLOR
#define ScaledDraw4 ScaledDraw
#else
static void ScaledDraw4(const byte *gfx, myint count, byte *vid, int x,
unsigned myint frac, unsigned myint delta)
{
byte c;
vid += x >> 1;
if (x & 1) {
while (count--) {
c = gfx[frac >> 17];
if (frac & (1 << 16))
c &= 0xf;
else
c >>= 4;
*vid = (*vid & 0xf0) | c;
vid += vpitch;
frac += delta;
}
} else {
while (count--) {
c = gfx[frac >> 17];
if (frac & (1 << 16))
c <<= 4;
else
c &= 0xf0;
*vid = (*vid & 0x0f) | c;
vid += vpitch;
frac += delta;
}
}
}
#endif
/* Render a single vertical stripe of a sprite. */
static void RenderLine(unsigned myint height, byte *row, int x,
const byte *sprite, int cmd
#ifndef ENABLE_COLOR
, int cmdend
#endif
)
{
fixed delta;
fixed yfrac;
fixed yclip;
int ytop;
int y0;
int y1;
int pstart;
int pend;
const byte *post;
int texoff;
// FIXME: Cache delta and yclip across lines.
delta = (64 << 16) / height;
if (height < sviewheight) {
#ifdef ENABLE_COLOR
while (sprite[cmd+0]) {
y1 = sprite[cmd+0] / 2;
/* y0 = (sprite[cmd+4] | (sprite[cmd+5] << 8)) / 2; */
y0 = sprite[cmd+4] / 2;
texoff = (int16_t)(sprite[cmd+2] | (sprite[cmd+3] << 8));
post = sprite + y0 + texoff;
cmd += 6;
#else
while (cmd < cmdend) {
y1 = sprite[cmd];
y0 = sprite[cmd + 1];
texoff = sprite[cmd+2];
post = sprite + cmd + texoff + 3;
cmd += 3;
if (texoff == 0)
cmd += (y1 + 1) >> 1;
y1 += y0;
#endif
pstart = (y0 * height) >> 6;
pend = (y1 * height) >> 6;
ScaledDraw4(post, pend - pstart, row + (pstart * vpitch),
x, 0/*frac*/, delta);
}
} else {
yclip = (height - sviewheight) / 2;
yclip *= delta;
ytop = yclip >> 16;
#ifdef ENABLE_COLOR
while (sprite[cmd+0]) {
y1 = sprite[cmd+0] / 2;
/* y0 = (sprite[cmd+4] | (sprite[cmd+5] << 8)) / 2; */
y0 = sprite[cmd+4] / 2;
texoff = (int16_t)(sprite[cmd+2] | (sprite[cmd+3] << 8));
post = sprite + y0 + texoff;
cmd += 6;
#else
while (cmd < cmdend) {
y1 = sprite[cmd];
y0 = sprite[cmd + 1];
texoff = sprite[cmd+2];
post = sprite + cmd + texoff + 3;
cmd += 3;
if (texoff == 0)
cmd += (y1 + 1) >> 1;
y1 += y0;
#endif
if (y0 < ytop) {
yfrac = yclip - (y0 << 16);
pstart = 0;
} else {
pstart = ((y0 - ytop) * height) >> 6;
yfrac = 0;
}
if (y1 <= ytop) {
pend = 0;
} else {
pend = ((y1 - ytop) * height) >> 6;
}
if (pend > sviewheight)
pend = sviewheight;
if (pstart < sviewheight)
ScaledDraw4(post, pend - pstart, row + (pstart * vpitch),
x, yfrac, delta);
}
}
}
static void RenderShape(myint xcenter, myint shapenum, unsigned height,
unsigned z)
{
unsigned myint scaler;
unsigned myint x;
myint p;
byte *row;
const byte *sprite;
fixed left;
fixed right;
int cmd;
#ifndef ENABLE_COLOR
int cmdend;
#endif
sprite = PM_GetSpritePage(shapenum);
#ifdef ENABLE_COLOR
left = sprite[0];
right = sprite[2];
#else
left = sprite[0];
right = sprite[1];
#endif
left <<= 16;
right = (right + 1) << 16;
scaler = (64 << 16) / height;
p = xcenter - (height >> 1);
if (p < 0) {
x = (-p)*scaler;
p = 0;
} else {
x = 0;
}
/* Shift to the real start of the texture. */
if (x < left) {
int n = ((left - x) * height) >> 22;
p += n;
x = 0;
} else {
x -= left;
}
right -= left;
if (height < sviewheight)
row = gfxbuf + (yoffset + (sviewheight - height) / 2) * vpitch;
else
row = gfxbuf + yoffset * vpitch;
row += xoffset;
for (; x < right; x += scaler, p++) {
int n;
if (p >= viewwidth)
break;
if (wallheight[p] >= z)
continue;
#ifdef ENABLE_COLOR
n = ((x >> 16) << 1) + 4;
cmd = sprite[n] | (sprite[n + 1] << 8);
RenderLine(height, row, p, sprite, cmd);
#else
n = (((x >> 16) * 3) / 2) + 2;
cmd = sprite[n] | (sprite[n + 1] << 8);
if (x & (1 << 16)) {
cmd >>= 4;
cmdend = sprite[n + 2] | ((sprite[n + 3] & 0xf) << 8);
} else {
cmdend = (cmd >> 12) | (sprite[n + 2] << 4);
cmd &= 0xfff;
}
RenderLine(height, row, p, sprite, cmd, cmdend);
#endif
}
}
void ScaleShape(myint xcenter, myint shapenum, unsigned height)
{
RenderShape(xcenter, shapenum, height >> 2, height);
}
void SimpleScaleShape(myint xcenter, myint shapenum, unsigned height)
{
RenderShape(xcenter, shapenum, height, 32000);
}
/* ======================================================================== */
/*
====================
=
= CalcHeight
=
= Calculates the height of xintercept,yintercept from viewx,viewy
=
====================
*/
static myint CalcHeight()
{
fixed gxt,gyt,nx,gx,gy;
gx = xintercept - viewx;
gxt = FixedByFrac(gx, viewcos);
gy = yintercept - viewy;
gyt = FixedByFrac(gy, viewsin);
nx = gxt-gyt;
//
// calculate perspective ratio (heightnumerator/(nx>>8))
//
if (nx < MINDIST)
nx = MINDIST; /* don't let divide overflow */
return heightnumerator/(nx>>8);
}
static void ScalePost(byte *wall, myint texture)
{
myint height;
byte *source;
height = (wallheight[postx] & 0xFFF8) >> 2;