]> icculus.org git repositories - theoddone33/hheretic.git/blob - base/r_things.c
Optimized blit functions.
[theoddone33/hheretic.git] / base / r_things.c
1 // R_things.c
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "doomdef.h"
5 #include "r_local.h"
6 #ifdef RENDER3D
7 #include "ogl_def.h"
8 #include <math.h>
9 #endif
10
11 void R_DrawColumn (void);
12 void R_DrawFuzzColumn (void);
13
14 typedef struct
15 {
16         int             x1, x2;
17
18         int             column;
19         int             topclip;
20         int             bottomclip;
21 } maskdraw_t;
22
23 /*
24
25 Sprite rotation 0 is facing the viewer, rotation 1 is one angle turn CLOCKWISE around the axis.
26 This is not the same as the angle, which increases counter clockwise
27 (protractor).  There was a lot of stuff grabbed wrong, so I changed it...
28
29 */
30
31
32 fixed_t         pspritescale, pspriteiscale;
33
34 lighttable_t    **spritelights;
35
36 // constant arrays used for psprite clipping and initializing clipping
37 short   negonearray[SCREENWIDTH];
38 short   screenheightarray[SCREENWIDTH];
39
40 /*
41 ===============================================================================
42
43                                                 INITIALIZATION FUNCTIONS
44
45 ===============================================================================
46 */
47
48 // variables used to look up and range check thing_t sprites patches
49 spritedef_t             *sprites;
50 int                             numsprites;
51
52 spriteframe_t   sprtemp[26];
53 int                             maxframe;
54 char                    *spritename;
55
56
57
58 /*
59 =================
60 =
61 = R_InstallSpriteLump
62 =
63 = Local function for R_InitSprites
64 =================
65 */
66
67 void R_InstallSpriteLump (int lump, unsigned frame, unsigned rotation, boolean flipped)
68 {
69         int             r;
70
71         if (frame >= 26 || rotation > 8)
72                 I_Error ("R_InstallSpriteLump: Bad frame characters in lump %i", lump);
73
74         if ((int)frame > maxframe)
75                 maxframe = frame;
76
77         if (rotation == 0)
78         {
79 // the lump should be used for all rotations
80                 if (sprtemp[frame].rotate == false)
81                         I_Error ("R_InitSprites: Sprite %s frame %c has multip rot=0 lump"
82                         , spritename, 'A'+frame);
83                 if (sprtemp[frame].rotate == true)
84                         I_Error ("R_InitSprites: Sprite %s frame %c has rotations and a rot=0 lump"
85                         , spritename, 'A'+frame);
86
87                 sprtemp[frame].rotate = false;
88                 for (r=0 ; r<8 ; r++)
89                 {
90                         sprtemp[frame].lump[r] = lump - firstspritelump;
91                         sprtemp[frame].flip[r] = (byte)flipped;
92                 }
93                 return;
94         }
95
96 // the lump is only used for one rotation
97         if (sprtemp[frame].rotate == false)
98                 I_Error ("R_InitSprites: Sprite %s frame %c has rotations and a rot=0 lump"
99                 , spritename, 'A'+frame);
100
101         sprtemp[frame].rotate = true;
102
103         rotation--;             // make 0 based
104         if (sprtemp[frame].lump[rotation] != -1)
105                 I_Error ("R_InitSprites: Sprite %s : %c : %c has two lumps mapped to it"
106                 ,spritename, 'A'+frame, '1'+rotation);
107
108         sprtemp[frame].lump[rotation] = lump - firstspritelump;
109         sprtemp[frame].flip[rotation] = (byte)flipped;
110 }
111
112 /*
113 =================
114 =
115 = R_InitSpriteDefs
116 =
117 = Pass a null terminated list of sprite names (4 chars exactly) to be used
118 = Builds the sprite rotation matrixes to account for horizontally flipped
119 = sprites.  Will report an error if the lumps are inconsistant
120 =Only called at startup
121 =
122 = Sprite lump names are 4 characters for the actor, a letter for the frame,
123 = and a number for the rotation, A sprite that is flippable will have an
124 = additional letter/number appended.  The rotation character can be 0 to
125 = signify no rotations
126 =================
127 */
128
129 void R_InitSpriteDefs (char **namelist)
130 {
131         char            **check;
132         int             i, l, intname, frame, rotation;
133         int             start, end;
134
135 // count the number of sprite names
136         check = namelist;
137         while (*check != NULL)
138                 check++;
139         numsprites = check-namelist;
140
141         if (!numsprites)
142                 return;
143
144         sprites = Z_Malloc(numsprites *sizeof(*sprites), PU_STATIC, NULL);
145
146         start = firstspritelump-1;
147         end = lastspritelump+1;
148
149 // scan all the lump names for each of the names, noting the highest
150 // frame letter
151 // Just compare 4 characters as ints
152         for (i=0 ; i<numsprites ; i++)
153         {
154                 spritename = namelist[i];
155                 memset (sprtemp,-1, sizeof(sprtemp));
156
157                 maxframe = -1;
158                 intname = *(int *)namelist[i];
159
160                 //
161                 // scan the lumps, filling in the frames for whatever is found
162                 //
163                 for (l=start+1 ; l<end ; l++)
164                         if (*(int *)lumpinfo[l].name == intname)
165                         {
166                                 frame = lumpinfo[l].name[4] - 'A';
167                                 rotation = lumpinfo[l].name[5] - '0';
168                                 R_InstallSpriteLump (l, frame, rotation, false);
169                                 if (lumpinfo[l].name[6])
170                                 {
171                                         frame = lumpinfo[l].name[6] - 'A';
172                                         rotation = lumpinfo[l].name[7] - '0';
173                                         R_InstallSpriteLump (l, frame, rotation, true);
174                                 }
175                         }
176
177                 //
178                 // check the frames that were found for completeness
179                 //
180                 if (maxframe == -1)
181                 {
182                         //continue;
183                         sprites[i].numframes = 0;
184                         if (shareware)
185                                 continue;
186                         I_Error ("R_InitSprites: No lumps found for sprite %s"
187                         ,namelist[i]);
188                 }
189
190                 maxframe++;
191                 for (frame = 0 ; frame < maxframe ; frame++)
192                 {
193                         switch ((int)sprtemp[frame].rotate)
194                         {
195                         case -1:        // no rotations were found for that frame at all
196                                 I_Error ("R_InitSprites: No patches found for %s frame %c"
197                                 , namelist[i], frame+'A');
198                         case 0: // only the first rotation is needed
199                                 break;
200
201                         case 1: // must have all 8 frames
202                                 for (rotation=0 ; rotation<8 ; rotation++)
203                                         if (sprtemp[frame].lump[rotation] == -1)
204                                                 I_Error ("R_InitSprites: Sprite %s frame %c is missing rotations"
205                                                 , namelist[i], frame+'A');
206                         }
207                 }
208
209                 //
210                 // allocate space for the frames present and copy sprtemp to it
211                 //
212                 sprites[i].numframes = maxframe;
213                 sprites[i].spriteframes =
214                         Z_Malloc (maxframe * sizeof(spriteframe_t), PU_STATIC, NULL);
215                 memcpy (sprites[i].spriteframes, sprtemp, maxframe*sizeof(spriteframe_t));
216         }
217
218 }
219
220
221 /*
222 ===============================================================================
223
224                                                         GAME FUNCTIONS
225
226 ===============================================================================
227 */
228
229 vissprite_t     vissprites[MAXVISSPRITES], *vissprite_p;
230 int                     newvissprite;
231
232
233 /*
234 ===================
235 =
236 = R_InitSprites
237 =
238 = Called at program start
239 ===================
240 */
241
242 void R_InitSprites (char **namelist)
243 {
244         int             i;
245
246         for (i=0 ; i<SCREENWIDTH ; i++)
247         {
248                 negonearray[i] = -1;
249         }
250
251         R_InitSpriteDefs (namelist);
252 }
253
254
255 /*
256 ===================
257 =
258 = R_ClearSprites
259 =
260 = Called at frame start
261 ===================
262 */
263
264 void R_ClearSprites (void)
265 {
266         vissprite_p = vissprites;
267 }
268
269
270 /*
271 ===================
272 =
273 = R_NewVisSprite
274 =
275 ===================
276 */
277
278 vissprite_t             overflowsprite;
279
280 vissprite_t *R_NewVisSprite (void)
281 {
282         if (vissprite_p == &vissprites[MAXVISSPRITES])
283                 return &overflowsprite;
284         vissprite_p++;
285         return vissprite_p-1;
286 }
287
288 #ifndef RENDER3D
289 /*
290 ================
291 =
292 = R_DrawMaskedColumn
293 =
294 = Used for sprites and masked mid textures
295 ================
296 */
297
298 short           *mfloorclip;
299 short           *mceilingclip;
300 fixed_t         spryscale;
301 fixed_t         sprtopscreen;
302 fixed_t         sprbotscreen;
303
304 void R_DrawMaskedColumn (column_t *column, signed int baseclip)
305 {
306         int             topscreen, bottomscreen;
307         fixed_t basetexturemid;
308
309         basetexturemid = dc_texturemid;
310
311         for ( ; column->topdelta != 0xff ; )
312         {
313 // calculate unclipped screen coordinates for post
314                 topscreen = sprtopscreen + spryscale*column->topdelta;
315                 bottomscreen = topscreen + spryscale*column->length;
316                 dc_yl = (topscreen+FRACUNIT-1)>>FRACBITS;
317                 dc_yh = (bottomscreen-1)>>FRACBITS;
318
319                 if (dc_yh >= mfloorclip[dc_x])
320                         dc_yh = mfloorclip[dc_x]-1;
321                 if (dc_yl <= mceilingclip[dc_x])
322                         dc_yl = mceilingclip[dc_x]+1;
323
324                 if(dc_yh >= baseclip && baseclip != -1)
325                         dc_yh = baseclip;
326
327                 if (dc_yl <= dc_yh)
328                 {
329                         dc_source = (byte *)column + 3;
330                         dc_texturemid = basetexturemid - (column->topdelta<<FRACBITS);
331 //                      dc_source = (byte *)column + 3 - column->topdelta;
332                         colfunc ();             // either R_DrawColumn or R_DrawFuzzColumn
333                 }
334                 column = (column_t *)(  (byte *)column + column->length + 4);
335         }
336
337         dc_texturemid = basetexturemid;
338 }
339
340
341 /*
342 ================
343 =
344 = R_DrawVisSprite
345 =
346 = mfloorclip and mceilingclip should also be set
347 ================
348 */
349
350 void R_DrawVisSprite (vissprite_t *vis, int x1, int x2)
351 {
352         column_t        *column;
353         int                     texturecolumn;
354         fixed_t         frac;
355         patch_t         *patch;
356         fixed_t         baseclip;
357
358
359         patch = W_CacheLumpNum(vis->patch+firstspritelump, PU_CACHE);
360
361         dc_colormap = vis->colormap;
362
363 //      if(!dc_colormap)
364 //              colfunc = fuzzcolfunc;  // NULL colormap = shadow draw
365
366         if(vis->mobjflags&MF_SHADOW)
367         {
368                 if(vis->mobjflags&MF_TRANSLATION)
369                 {
370                         colfunc = R_DrawTranslatedFuzzColumn;
371                         dc_translation = translationtables - 256 +
372                                 ((vis->mobjflags & MF_TRANSLATION) >> (MF_TRANSSHIFT-8));
373                 }
374                 else
375                 { // Draw using shadow column function
376                         colfunc = fuzzcolfunc;
377                 }
378         }
379         else if(vis->mobjflags&MF_TRANSLATION)
380         {
381                 // Draw using translated column function
382                 colfunc = R_DrawTranslatedColumn;
383                 dc_translation = translationtables - 256 +
384                         ( (vis->mobjflags & MF_TRANSLATION) >> (MF_TRANSSHIFT-8) );
385         }
386
387         dc_iscale = abs(vis->xiscale)>>detailshift;
388         dc_texturemid = vis->texturemid;
389         frac = vis->startfrac;
390         spryscale = vis->scale;
391
392         sprtopscreen = centeryfrac - FixedMul(dc_texturemid,spryscale);
393
394 // check to see if weapon is a vissprite
395         if(vis->psprite)
396         {
397                 dc_texturemid += FixedMul(((centery-viewheight/2)<<FRACBITS),
398                         vis->xiscale);
399                 sprtopscreen += (viewheight/2-centery)<<FRACBITS;
400         }
401
402         if(vis->footclip && !vis->psprite)
403         {
404                 sprbotscreen = sprtopscreen+FixedMul(patch->height<<FRACBITS,
405                         spryscale);
406                 baseclip = (sprbotscreen-FixedMul(vis->footclip<<FRACBITS,
407                         spryscale))>>FRACBITS;
408         }
409         else
410         {
411                 baseclip = -1;
412         }
413
414         for (dc_x=vis->x1 ; dc_x<=vis->x2 ; dc_x++, frac += vis->xiscale)
415         {
416                 texturecolumn = frac>>FRACBITS;
417 #ifdef RANGECHECK
418                 if (texturecolumn < 0 || texturecolumn >= SHORT(patch->width))
419                         I_Error ("R_DrawSpriteRange: bad texturecolumn");
420 #endif
421                 column = (column_t *) ((byte *)patch +
422                  LONG(patch->columnofs[texturecolumn]));
423                  R_DrawMaskedColumn (column, baseclip);
424         }
425
426         colfunc = basecolfunc;
427 }
428 #endif  // !RENDER3D
429
430
431 /*
432 ===================
433 =
434 = R_ProjectSprite
435 =
436 = Generates a vissprite for a thing if it might be visible
437 =
438 ===================
439 */
440
441 void R_ProjectSprite (mobj_t *thing)
442 {
443         fixed_t         trx,try;
444         fixed_t         gxt,gyt;
445         fixed_t         tx,tz;
446         fixed_t         xscale;
447         int                     x1=0, x2=0;
448         spritedef_t     *sprdef;
449         spriteframe_t   *sprframe;
450         int                     lump;
451         unsigned        rot;
452         boolean         flip;
453 #ifndef RENDER3D
454         int                     index;
455 #endif
456         vissprite_t     *vis;
457         angle_t         ang;
458         fixed_t         iscale;
459
460 #ifdef RENDER3D
461     float v1[2], v2[2];
462     float sinrv, cosrv, thangle;  // rv = real value
463 #endif
464
465         if(thing->flags2&MF2_DONTDRAW)
466         { // Never make a vissprite when MF2_DONTDRAW is flagged.
467                 return;
468         }
469
470 //
471 // transform the origin point
472 //
473         trx = thing->x - viewx;
474         try = thing->y - viewy;
475
476         gxt = FixedMul(trx,viewcos);
477         gyt = -FixedMul(try,viewsin);
478         tz = gxt-gyt;
479
480 #ifdef RENDER3D
481     if( tz < 0 )
482         tz = -tz;       // Make it positive. The clipper will handle backside.
483     if( tz < FRACUNIT )
484         tz = FRACUNIT;
485 #else
486         if (tz < MINZ)
487                 return;         // thing is behind view plane
488 #endif
489         xscale = FixedDiv(projection, tz);
490
491         gxt = -FixedMul(trx,viewsin);
492         gyt = FixedMul(try,viewcos);
493         tx = -(gyt+gxt);
494         
495 #ifndef RENDER3D
496         if (abs(tx)>(tz<<2))
497                 return;         // too far off the side
498 #endif
499
500 //
501 // decide which patch to use for sprite reletive to player
502 //
503 #ifdef RANGECHECK
504         if ((unsigned)thing->sprite >= numsprites)
505                 I_Error ("R_ProjectSprite: invalid sprite number %i ",thing->sprite);
506 #endif
507         sprdef = &sprites[thing->sprite];
508 #ifdef RANGECHECK
509         if ( (thing->frame&FF_FRAMEMASK) >= sprdef->numframes )
510                 I_Error ("R_ProjectSprite: invalid sprite frame %i : %i "
511                 ,thing->sprite, thing->frame);
512 #endif
513         sprframe = &sprdef->spriteframes[ thing->frame & FF_FRAMEMASK];
514
515         if (sprframe->rotate)
516         {       // choose a different rotation based on player view
517                 ang = R_PointToAngle (thing->x, thing->y);
518                 rot = (ang-thing->angle+(unsigned)(ANG45/2)*9)>>29;
519                 lump = sprframe->lump[rot];
520                 flip = (boolean)sprframe->flip[rot];
521         }
522         else
523         {       // use single rotation for all views
524                 lump = sprframe->lump[0];
525                 flip = (boolean)sprframe->flip[0];
526         }
527
528 //
529 // calculate edges of the shape
530 //
531 #ifdef RENDER3D
532     v1[VX] = FIX2FLT(thing->x);
533     v1[VY] = FIX2FLT(thing->y);
534     //thangle = BANG2RAD(bamsAtan2((v1[VY]-FIX2FLT(viewy))*10,
535     //(v1[VX]-FIX2FLT(viewx))*10)) - PI/2;
536     thangle = BANG2RAD(bamsAtan2(FIX2FLT(try)*10, FIX2FLT(trx)*10)) - PI/2;
537     sinrv = sin(thangle);
538     cosrv = cos(thangle);
539     v1[VX] -= cosrv*(spriteoffset[lump]>>FRACBITS);
540     v1[VY] -= sinrv*(spriteoffset[lump]>>FRACBITS);
541     v2[VX] = v1[VX] + cosrv*(spritewidth[lump]>>FRACBITS);
542     v2[VY] = v1[VY] + sinrv*(spritewidth[lump]>>FRACBITS);
543
544     // Check for visibility.
545     if(!C_CheckViewRelSeg(v1[VX], v1[VY], v2[VX], v2[VY]))
546     //if(!C_IsAngleVisible(RAD2BANG(thangle+PI/2)))
547         return; // Isn't visible.
548 #else
549         tx -= spriteoffset[lump];
550         x1 = (centerxfrac + FixedMul (tx,xscale) ) >>FRACBITS;
551         if (x1 > viewwidth)
552                 return;         // off the right side
553         tx +=  spritewidth[lump];
554         x2 = ((centerxfrac + FixedMul (tx,xscale) ) >>FRACBITS) - 1;
555         if (x2 < 0)
556                 return;         // off the left side
557 #endif
558
559 //
560 // store information in a vissprite
561 //
562         vis = R_NewVisSprite ();
563         vis->mobjflags = thing->flags;
564         vis->psprite = false;
565         vis->scale = xscale<<detailshift;
566         vis->gx = thing->x;
567         vis->gy = thing->y;
568         vis->gz = thing->z;
569         vis->gzt = thing->z + spritetopoffset[lump];
570
571 #ifdef RENDER3D
572     vis->secfloor = FIX2FLT(thing->subsector->sector->floorheight);
573     vis->secceil  = FIX2FLT(thing->subsector->sector->ceilingheight);
574 #endif
575
576         // foot clipping
577         if(thing->flags2&MF2_FEETARECLIPPED
578                 && thing->z <= thing->subsector->sector->floorheight)
579         {
580                 vis->footclip = 10;
581         }
582         else vis->footclip = 0;
583         vis->texturemid = vis->gzt - viewz - (vis->footclip<<FRACBITS);
584
585 #ifdef RENDER3D
586     // The start and end vertices.
587     vis->v1[VX] = v1[VX];
588     vis->v1[VY] = v1[VY];
589     vis->v2[VX] = v2[VX];
590     vis->v2[VY] = v2[VY];
591 #endif
592
593         vis->x1 = x1 < 0 ? 0 : x1;
594         vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
595         iscale = FixedDiv (FRACUNIT, xscale);
596         if (flip)
597         {
598                 vis->startfrac = spritewidth[lump]-1;
599                 vis->xiscale = -iscale;
600         }
601         else
602         {
603                 vis->startfrac = 0;
604                 vis->xiscale = iscale;
605         }
606         if (vis->x1 > x1)
607                 vis->startfrac += vis->xiscale*(vis->x1-x1);
608         vis->patch = lump;
609 //
610 // get light level
611 //
612
613 //      if (thing->flags & MF_SHADOW)
614 //              vis->colormap = NULL;                   // shadow draw
615 //      else ...
616
617 #ifdef RENDER3D
618     if( thing->frame & FF_FULLBRIGHT )
619     {                                   // full bright
620         vis->lightlevel = -1;
621     }
622     else
623     {                                   // diminished light
624         vis->lightlevel = thing->subsector->sector->lightlevel;
625     }
626 #else
627         if (fixedcolormap)
628                 vis->colormap = fixedcolormap;  // fixed map
629         else if (thing->frame & FF_FULLBRIGHT)
630                 vis->colormap = colormaps;              // full bright
631         else
632         {                                                                       // diminished light
633                 index = xscale>>(LIGHTSCALESHIFT-detailshift);
634                 if (index >= MAXLIGHTSCALE)
635                         index = MAXLIGHTSCALE-1;
636                 vis->colormap = spritelights[index];
637         }
638 #endif
639 }
640
641
642
643
644 /*
645 ========================
646 =
647 = R_AddSprites
648 =
649 ========================
650 */
651
652 void R_AddSprites (sector_t *sec)
653 {
654         mobj_t          *thing;
655         int                     lightnum;
656
657         if (sec->validcount == validcount)
658                 return;         // already added
659
660         sec->validcount = validcount;
661
662         lightnum = (sec->lightlevel >> LIGHTSEGSHIFT)+extralight;
663         if (lightnum < 0)
664                 spritelights = scalelight[0];
665         else if (lightnum >= LIGHTLEVELS)
666                 spritelights = scalelight[LIGHTLEVELS-1];
667         else
668                 spritelights = scalelight[lightnum];
669
670
671         for (thing = sec->thinglist ; thing ; thing = thing->snext)
672                 R_ProjectSprite (thing);
673 }
674
675
676 /*
677 ========================
678 =
679 = R_DrawPSprite
680 =
681 ========================
682 */
683
684 int PSpriteSY[NUMWEAPONS] =
685 {
686         0,                              // staff
687         5*FRACUNIT,             // goldwand
688         15*FRACUNIT,    // crossbow
689         15*FRACUNIT,    // blaster
690         15*FRACUNIT,    // skullrod
691         15*FRACUNIT,    // phoenix rod
692         15*FRACUNIT,    // mace
693         15*FRACUNIT,    // gauntlets
694         15*FRACUNIT             // beak
695 };
696
697 void R_DrawPSprite (pspdef_t *psp)
698 {
699         fixed_t         tx;
700         int                     x1;
701 #ifndef RENDER3D
702         int             x2;
703 #endif
704         spritedef_t     *sprdef;
705         spriteframe_t   *sprframe;
706         int                     lump;
707         boolean         flip;
708 #ifdef RENDER3D
709     float       light, alpha;
710     int         y;
711 #else
712         vissprite_t     *vis, avis;
713 #endif
714
715         int tempangle;
716
717 //
718 // decide which patch to use
719 //
720 #ifdef RANGECHECK
721         if ( (unsigned)psp->state->sprite >= numsprites)
722                 I_Error ("R_ProjectSprite: invalid sprite number %i "
723                 , psp->state->sprite);
724 #endif
725         sprdef = &sprites[psp->state->sprite];
726 #ifdef RANGECHECK
727         if ( (psp->state->frame & FF_FRAMEMASK)  >= sprdef->numframes)
728                 I_Error ("R_ProjectSprite: invalid sprite frame %i : %i "
729                 , psp->state->sprite, psp->state->frame);
730 #endif
731         sprframe = &sprdef->spriteframes[ psp->state->frame & FF_FRAMEMASK ];
732
733         lump = sprframe->lump[0];
734         flip = (boolean)sprframe->flip[0];
735
736 //
737 // calculate edges of the shape
738 //
739         tx = psp->sx-160*FRACUNIT;
740
741         tx -= spriteoffset[lump];
742         if(viewangleoffset)
743         {
744                 tempangle = ((centerxfrac/1024)*(viewangleoffset>>ANGLETOFINESHIFT));
745         }
746         else
747         {
748                 tempangle = 0;
749         }
750         x1 = (centerxfrac + FixedMul (tx,pspritescale)+tempangle ) >>FRACBITS;
751 #ifdef RENDER3D
752     // Set the OpenGL color & alpha.
753     light = 1;
754     alpha = 1;
755         //vis->colormap = spritelights[MAXLIGHTSCALE-1];
756         if(viewplayer->powers[pw_invulnerability] > 4*32)
757         {
758             if(viewplayer->mo->flags2&MF2_DONTDRAW)
759             { // don't draw the psprite
760                 //vis->mobjflags |= MF_SHADOW;
761                 alpha = .333f;
762             }
763             else if(viewplayer->mo->flags&MF_SHADOW)
764             {
765                 //vis->mobjflags |= MF_ALTSHADOW;
766                 alpha = .666f;
767             }
768         }
769         else if(viewplayer->powers[pw_invulnerability]&8)
770         {
771             //vis->mobjflags |= MF_SHADOW;
772             alpha = .333f;
773         }
774     if(fixedcolormap)
775     {
776         // Fixed color
777         //vis->colormap = fixedcolormap;
778         light = 1;
779     }
780     else if(psp->state->frame & FF_FULLBRIGHT)
781     {
782         // Full bright
783         //vis->colormap = colormaps;
784         light = 1;
785     }
786     else
787     {
788         // local light
789         //vis->colormap = spritelights[MAXLIGHTSCALE-1];
790         light = viewplayer->mo->subsector->sector->lightlevel / 255.0;
791     }
792 //
793 // do some OpenGL rendering, oh yeah
794 //
795     y = -(spritetopoffset[lump]>>FRACBITS)+(psp->sy>>FRACBITS);
796     if(viewheight == SCREENHEIGHT)
797     {
798         y += PSpriteSY[players[consoleplayer].readyweapon]
799              >> FRACBITS;
800     }
801     else
802         y -= 39/2;
803
804     light += .1f;   // Add some extra light.
805     OGL_SetColorAndAlpha(light, light, light, alpha);
806     OGL_DrawPSprite(x1, y, 1, flip, lump);
807
808 #else
809
810         if (x1 > viewwidth)
811                 return;         // off the right side
812         tx +=  spritewidth[lump];
813         x2 = ((centerxfrac + FixedMul (tx, pspritescale)+tempangle ) >>FRACBITS) - 1;
814         if (x2 < 0)
815                 return;         // off the left side
816
817 //
818 // store information in a vissprite
819 //
820         vis = &avis;
821         vis->mobjflags = 0;
822         vis->psprite = true;
823         vis->texturemid = (BASEYCENTER<<FRACBITS)+FRACUNIT/2-(psp->sy-spritetopoffset[lump]);
824         if(viewheight == SCREENHEIGHT)
825         {
826                 vis->texturemid -= PSpriteSY[players[consoleplayer].readyweapon];
827         }
828         vis->x1 = x1 < 0 ? 0 : x1;
829         vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
830         vis->scale = pspritescale<<detailshift;
831         if (flip)
832         {
833                 vis->xiscale = -pspriteiscale;
834                 vis->startfrac = spritewidth[lump]-1;
835         }
836         else
837         {
838                 vis->xiscale = pspriteiscale;
839                 vis->startfrac = 0;
840         }
841         if (vis->x1 > x1)
842                 vis->startfrac += vis->xiscale*(vis->x1-x1);
843         vis->patch = lump;
844
845         if(viewplayer->powers[pw_invisibility] > 4*32 ||
846         viewplayer->powers[pw_invisibility] & 8)
847         {
848                 // Invisibility
849                 vis->colormap = spritelights[MAXLIGHTSCALE-1];
850                 vis->mobjflags |= MF_SHADOW;
851         }
852         else if(fixedcolormap)
853         {
854                 // Fixed color
855                 vis->colormap = fixedcolormap;
856         }
857         else if(psp->state->frame & FF_FULLBRIGHT)
858         {
859                 // Full bright
860                 vis->colormap = colormaps;
861         }
862         else
863         {
864                 // local light
865                 vis->colormap = spritelights[MAXLIGHTSCALE-1];
866         }
867         R_DrawVisSprite(vis, vis->x1, vis->x2);
868 #endif
869 }
870
871 /*
872 ========================
873 =
874 = R_DrawPlayerSprites
875 =
876 ========================
877 */
878
879 void R_DrawPlayerSprites (void)
880 {
881         int                     i, lightnum;
882         pspdef_t        *psp;
883
884 //
885 // get light level
886 //
887         lightnum = (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT)
888                 +extralight;
889         if (lightnum < 0)
890                 spritelights = scalelight[0];
891         else if (lightnum >= LIGHTLEVELS)
892                 spritelights = scalelight[LIGHTLEVELS-1];
893         else
894                 spritelights = scalelight[lightnum];
895 //
896 // clip to screen bounds
897 //
898 #ifndef RENDER3D
899         mfloorclip = screenheightarray;
900         mceilingclip = negonearray;
901 #endif
902
903 //
904 // add all active psprites
905 //
906         for (i=0, psp=viewplayer->psprites ; i<NUMPSPRITES ; i++,psp++)
907                 if (psp->state)
908                         R_DrawPSprite (psp);
909
910 }
911
912
913 /*
914 ========================
915 =
916 = R_SortVisSprites
917 =
918 ========================
919 */
920
921 vissprite_t     vsprsortedhead;
922
923 void R_SortVisSprites (void)
924 {
925         int                     i, count;
926         vissprite_t     *ds, *best;
927         vissprite_t     unsorted;
928         fixed_t         bestscale;
929
930         count = vissprite_p - vissprites;
931
932         unsorted.next = unsorted.prev = &unsorted;
933         if (!count)
934                 return;
935
936         for (ds=vissprites ; ds<vissprite_p ; ds++)
937         {
938                 ds->next = ds+1;
939                 ds->prev = ds-1;
940         }
941         vissprites[0].prev = &unsorted;
942         unsorted.next = &vissprites[0];
943         (vissprite_p-1)->next = &unsorted;
944         unsorted.prev = vissprite_p-1;
945
946 //
947 // pull the vissprites out by scale
948 //
949         best = 0;               // shut up the compiler warning
950         vsprsortedhead.next = vsprsortedhead.prev = &vsprsortedhead;
951         for (i=0 ; i<count ; i++)
952         {
953                 bestscale = MAXINT;
954                 for (ds=unsorted.next ; ds!= &unsorted ; ds=ds->next)
955                 {
956                         if (ds->scale < bestscale)
957                         {
958                                 bestscale = ds->scale;
959                                 best = ds;
960                         }
961                 }
962                 best->next->prev = best->prev;
963                 best->prev->next = best->next;
964                 best->next = &vsprsortedhead;
965                 best->prev = vsprsortedhead.prev;
966                 vsprsortedhead.prev->next = best;
967                 vsprsortedhead.prev = best;
968         }
969 }
970
971
972 #ifndef RENDER3D
973
974 /*
975 ========================
976 =
977 = R_DrawSprite
978 =
979 ========================
980 */
981
982 void R_DrawSprite (vissprite_t *spr)
983 {
984         drawseg_t               *ds;
985         short                   clipbot[SCREENWIDTH], cliptop[SCREENWIDTH];
986         int                             x, r1, r2;
987         fixed_t                 scale, lowscale;
988         int                             silhouette;
989
990         for (x = spr->x1 ; x<=spr->x2 ; x++)
991                 clipbot[x] = cliptop[x] = -2;
992
993 //
994 // scan drawsegs from end to start for obscuring segs
995 // the first drawseg that has a greater scale is the clip seg
996 //
997         for (ds=ds_p-1 ; ds >= drawsegs ; ds--)
998         {
999                 //
1000                 // determine if the drawseg obscures the sprite
1001                 //
1002                 if (ds->x1 > spr->x2 || ds->x2 < spr->x1 ||
1003                 (!ds->silhouette && !ds->maskedtexturecol) )
1004                         continue;                       // doesn't cover sprite
1005
1006                 r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1;
1007                 r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2;
1008                 if (ds->scale1 > ds->scale2)
1009                 {
1010                         lowscale = ds->scale2;
1011                         scale = ds->scale1;
1012                 }
1013                 else
1014                 {
1015                         lowscale = ds->scale1;
1016                         scale = ds->scale2;
1017                 }
1018
1019                 if (scale < spr->scale || ( lowscale < spr->scale
1020                 && !R_PointOnSegSide (spr->gx, spr->gy, ds->curline) ) )
1021                 {
1022                         if (ds->maskedtexturecol)       // masked mid texture
1023                                 R_RenderMaskedSegRange (ds, r1, r2);
1024                         continue;                       // seg is behind sprite
1025                 }
1026
1027 //
1028 // clip this piece of the sprite
1029 //
1030                 silhouette = ds->silhouette;
1031                 if (spr->gz >= ds->bsilheight)
1032                         silhouette &= ~SIL_BOTTOM;
1033                 if (spr->gzt <= ds->tsilheight)
1034                         silhouette &= ~SIL_TOP;
1035
1036                 if (silhouette == 1)
1037                 {       // bottom sil
1038                         for (x=r1 ; x<=r2 ; x++)
1039                                 if (clipbot[x] == -2)
1040                                         clipbot[x] = ds->sprbottomclip[x];
1041                 }
1042                 else if (silhouette == 2)
1043                 {       // top sil
1044                         for (x=r1 ; x<=r2 ; x++)
1045                                 if (cliptop[x] == -2)
1046                                         cliptop[x] = ds->sprtopclip[x];
1047                 }
1048                 else if (silhouette == 3)
1049                 {       // both
1050                         for (x=r1 ; x<=r2 ; x++)
1051                         {
1052                                 if (clipbot[x] == -2)
1053                                         clipbot[x] = ds->sprbottomclip[x];
1054                                 if (cliptop[x] == -2)
1055                                         cliptop[x] = ds->sprtopclip[x];
1056                         }
1057                 }
1058
1059         }
1060
1061 //
1062 // all clipping has been performed, so draw the sprite
1063 //
1064
1065 // check for unclipped columns
1066         for (x = spr->x1 ; x<=spr->x2 ; x++)
1067         {
1068                 if (clipbot[x] == -2)
1069                         clipbot[x] = viewheight;
1070                 if (cliptop[x] == -2)
1071                         cliptop[x] = -1;
1072         }
1073
1074         mfloorclip = clipbot;
1075         mceilingclip = cliptop;
1076         R_DrawVisSprite (spr, spr->x1, spr->x2);
1077 }
1078 #endif
1079
1080
1081 /*
1082 ========================
1083 =
1084 = R_DrawMasked
1085 =
1086 ========================
1087 */
1088
1089 void R_DrawMasked (void)
1090 {
1091         vissprite_t             *spr;
1092 #ifdef RENDER3D
1093     extern boolean willRenderSprites;
1094
1095     if( willRenderSprites )
1096     {
1097         R_SortVisSprites();
1098
1099         if( vissprite_p > vissprites )
1100         {
1101             // draw all vissprites back to front
1102             glDepthMask(GL_FALSE);
1103
1104             for( spr = vsprsortedhead.next; spr != &vsprsortedhead;
1105                  spr = spr->next )
1106             {
1107                 //R_DrawSprite (spr);
1108                 R_RenderSprite(spr);
1109             }
1110
1111             glDepthMask(GL_TRUE);
1112         }
1113     }
1114 #else
1115         drawseg_t               *ds;
1116
1117         R_SortVisSprites ();
1118
1119         if (vissprite_p > vissprites)
1120         {
1121         // draw all vissprites back to front
1122
1123                 for (spr = vsprsortedhead.next ; spr != &vsprsortedhead
1124                 ; spr=spr->next)
1125                         R_DrawSprite (spr);
1126         }
1127
1128 //
1129 // render any remaining masked mid textures
1130 //
1131         for (ds=ds_p-1 ; ds >= drawsegs ; ds--)
1132                 if (ds->maskedtexturecol)
1133                         R_RenderMaskedSegRange (ds, ds->x1, ds->x2);
1134
1135 //
1136 // draw the psprites on top of everything
1137 //
1138 // Added for the sideviewing with an external device
1139         if (viewangleoffset <= 1024<<ANGLETOFINESHIFT || viewangleoffset >=
1140                         -1024<<ANGLETOFINESHIFT)
1141         {       // don't draw on side views
1142                 R_DrawPlayerSprites ();
1143         }
1144
1145 //      if (!viewangleoffset)           // don't draw on side views
1146 //              R_DrawPlayerSprites ();
1147 #endif
1148 }