Wolf3D-Mac/RefSprite.c

1 line
8.8 KiB
C
Raw Normal View History

#include "WolfDef.h" #include <string.h> Word *src1,*src2,*dest; /* Used by the sort */ /********************************** Merges src1/size1 and src2/size2 to dest Both Size1 and Size2 MUST be non-zero **********************************/ void Merge(Word Size1, Word Size2) { Word *XDest,*XSrc1,*XSrc2; /* merge two parts of the unsorted array to the sorted array */ XDest = dest; dest = &XDest[Size1+Size2]; XSrc1 = src1; src1 = &XSrc1[Size1]; XSrc2 = src2; src2 = &XSrc2[Size2]; if (XSrc1[0] < XSrc2[0]) { /* Which sort to use? */ mergefrom1: do { XDest[0] = XSrc1[0]; /* Copy one entry */ ++XDest; ++XSrc1; if (!--Size1) { /* Any more? */ do { /* Dump the rest */ XDest[0] = XSrc2[0]; /* Copy the rest of data */ ++XDest; ++XSrc2; } while (--Size2); return; } } while (XSrc1[0] < XSrc2[0]); } do { XDest[0] = XSrc2[0]; ++XDest; ++XSrc2; if (!--Size2) { do { XDest[0] = XSrc1[0]; ++XDest; ++XSrc1; } while (--Size1); return; } } while (XSrc1[0] >= XSrc2[0]); goto mergefrom1; } /********************************** Sorts the events from xevents[0] to xevent_p firstevent will be set to the first sorted event (either xevents[0] or sortbuffer[0]) **********************************/ void SortEvents(void) { Word count; /* Number of members to sort */ Word size; /* Entry size to sort with */ Word sort; /* Sort count */ Word remaining; /* Temp merge count */ Word *sorted,*unsorted,*temp; count = numvisspr; /* How many entries are there? */ if (count<2) { firstevent = xevents; /* Just return the 0 or 1 entries */ return; /* Leave now */ } size = 1; /* source size (<<1 / loop)*/ sort = 1; /* iteration number (+1 / loop)*/ sorted = xevents; unsorted = sortbuffer; do { remaining = count>>sort; /* How many times to try */ /* pointers incremented by the merge */ src1 = sorted; /* Sorted array */ src2 = &sorted[remaining<<(sort-1)]; /* Half point */ dest = unsorted; /* Dest array */ /* merge paired blocks*/ if (remaining) { /* Any to sort? */ do { Merge(size,size); /* All groups equal size */ } while (--remaining); } /* copy or merge the leftovers */ remaining = count&((size<<1)-1); /* Create mask (1 bit higher) */ if (remaining > size) { /* one complete block and one fragment */ src1 = &src2[size]; Merge(remaining-size,size); } else if (remaining) { /* just a single sorted fragment */ memcpy(dest,src2,remaining*sizeof(Word)); /* Copy it */ } /* get ready to sort back to the other array */ size <<= 1; /* Double the entry size */ ++sort; /* Increase the shift size */ temp = sorted; /* Swap the pointers */ sorted = unsorted; unsorted = temp; } while (size<count); firstevent = sorted; } /********************************** Draw a single scaled sprite x1 = Left edge, x2 = Right edge, rs_vseg = record for sprite **********************************/ void RenderSprite(Word x1,Word x2,vissprite_t *VisPtr) { Word column; Word scaler; scaler = VisPtr->clipscale; /* Get the size of the sprite */ column = 0; /* Start at the first column */ if ((int) x1 > VisPtr->x1) { /* Clip the left? */ column = (x1-VisPtr->x1)*VisPtr->columnstep; /* Start here instead */ } /* calculate and draw each column */ do { if (xscale[x1] <= scaler) { /* Visible? */ IO_ScaleMaskedColumn(x1,scaler,VisPtr->pos,column>>FRACBITS); } column+=VisPtr->columnstep; /* Next column (Fraction) */ } while (++x1<=x2); } /********************************** Add a sprite entry to the render list **********************************/ void AddSprite (thing_t *thing,Word actornum) { fixed_t tx; /* New X coord */ fixed_t tz; /* New z coord (Size) */ Word scale; /* Scaled size */ int px; /* Center X coord */ unsigned short *patch; /* Pointer to sprite data */ int x1, x2; /* Left,Right x */ Word width; /* Width of sprite */ fixed_t trx,try; /* x,y from the camera */ vissprite_t *VisPtr; /* Local pointer to visible sprite