R_THINGS.C 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // R_things.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "DoomDef.h"
  5. #include "R_local.h"
  6. void R_DrawColumn (void);
  7. void R_DrawFuzzColumn (void);
  8. typedef struct
  9. {
  10. int x1, x2;
  11. int column;
  12. int topclip;
  13. int bottomclip;
  14. } maskdraw_t;
  15. /*
  16. Sprite rotation 0 is facing the viewer, rotation 1 is one angle turn CLOCKWISE around the axis.
  17. This is not the same as the angle, which increases counter clockwise
  18. (protractor). There was a lot of stuff grabbed wrong, so I changed it...
  19. */
  20. fixed_t pspritescale, pspriteiscale;
  21. lighttable_t **spritelights;
  22. // constant arrays used for psprite clipping and initializing clipping
  23. short negonearray[SCREENWIDTH];
  24. short screenheightarray[SCREENWIDTH];
  25. /*
  26. ===============================================================================
  27. INITIALIZATION FUNCTIONS
  28. ===============================================================================
  29. */
  30. // variables used to look up and range check thing_t sprites patches
  31. spritedef_t *sprites;
  32. int numsprites;
  33. spriteframe_t sprtemp[26];
  34. int maxframe;
  35. char *spritename;
  36. /*
  37. =================
  38. =
  39. = R_InstallSpriteLump
  40. =
  41. = Local function for R_InitSprites
  42. =================
  43. */
  44. void R_InstallSpriteLump (int lump, unsigned frame, unsigned rotation, boolean flipped)
  45. {
  46. int r;
  47. if (frame >= 26 || rotation > 8)
  48. I_Error ("R_InstallSpriteLump: Bad frame characters in lump %i", lump);
  49. if ((int)frame > maxframe)
  50. maxframe = frame;
  51. if (rotation == 0)
  52. {
  53. // the lump should be used for all rotations
  54. if (sprtemp[frame].rotate == false)
  55. I_Error ("R_InitSprites: Sprite %s frame %c has multip rot=0 lump"
  56. , spritename, 'A'+frame);
  57. if (sprtemp[frame].rotate == true)
  58. I_Error ("R_InitSprites: Sprite %s frame %c has rotations and a rot=0 lump"
  59. , spritename, 'A'+frame);
  60. sprtemp[frame].rotate = false;
  61. for (r=0 ; r<8 ; r++)
  62. {
  63. sprtemp[frame].lump[r] = lump - firstspritelump;
  64. sprtemp[frame].flip[r] = (byte)flipped;
  65. }
  66. return;
  67. }
  68. // the lump is only used for one rotation
  69. if (sprtemp[frame].rotate == false)
  70. I_Error ("R_InitSprites: Sprite %s frame %c has rotations and a rot=0 lump"
  71. , spritename, 'A'+frame);
  72. sprtemp[frame].rotate = true;
  73. rotation--; // make 0 based
  74. if (sprtemp[frame].lump[rotation] != -1)
  75. I_Error ("R_InitSprites: Sprite %s : %c : %c has two lumps mapped to it"
  76. ,spritename, 'A'+frame, '1'+rotation);
  77. sprtemp[frame].lump[rotation] = lump - firstspritelump;
  78. sprtemp[frame].flip[rotation] = (byte)flipped;
  79. }
  80. /*
  81. =================
  82. =
  83. = R_InitSpriteDefs
  84. =
  85. = Pass a null terminated list of sprite names (4 chars exactly) to be used
  86. = Builds the sprite rotation matrixes to account for horizontally flipped
  87. = sprites. Will report an error if the lumps are inconsistant
  88. = Only called at startup
  89. =
  90. = Sprite lump names are 4 characters for the actor, a letter for the frame,
  91. = and a number for the rotation, A sprite that is flippable will have an
  92. = additional letter/number appended. The rotation character can be 0 to
  93. = signify no rotations
  94. =================
  95. */
  96. void R_InitSpriteDefs (char **namelist)
  97. {
  98. char **check;
  99. int i, l, intname, frame, rotation;
  100. int start, end;
  101. // count the number of sprite names
  102. check = namelist;
  103. while (*check != NULL)
  104. check++;
  105. numsprites = check-namelist;
  106. if (!numsprites)
  107. return;
  108. sprites = Z_Malloc(numsprites *sizeof(*sprites), PU_STATIC, NULL);
  109. start = firstspritelump-1;
  110. end = lastspritelump+1;
  111. // scan all the lump names for each of the names, noting the highest
  112. // frame letter
  113. // Just compare 4 characters as ints
  114. for (i=0 ; i<numsprites ; i++)
  115. {
  116. spritename = namelist[i];
  117. memset (sprtemp,-1, sizeof(sprtemp));
  118. maxframe = -1;
  119. intname = *(int *)namelist[i];
  120. //
  121. // scan the lumps, filling in the frames for whatever is found
  122. //
  123. for (l=start+1 ; l<end ; l++)
  124. if (*(int *)lumpinfo[l].name == intname)
  125. {
  126. frame = lumpinfo[l].name[4] - 'A';
  127. rotation = lumpinfo[l].name[5] - '0';
  128. R_InstallSpriteLump (l, frame, rotation, false);
  129. if (lumpinfo[l].name[6])
  130. {
  131. frame = lumpinfo[l].name[6] - 'A';
  132. rotation = lumpinfo[l].name[7] - '0';
  133. R_InstallSpriteLump (l, frame, rotation, true);
  134. }
  135. }
  136. //
  137. // check the frames that were found for completeness
  138. //
  139. if (maxframe == -1)
  140. {
  141. //continue;
  142. sprites[i].numframes = 0;
  143. if (shareware)
  144. continue;
  145. I_Error ("R_InitSprites: No lumps found for sprite %s"
  146. ,namelist[i]);
  147. }
  148. maxframe++;
  149. for (frame = 0 ; frame < maxframe ; frame++)
  150. {
  151. switch ((int)sprtemp[frame].rotate)
  152. {
  153. case -1: // no rotations were found for that frame at all
  154. I_Error ("R_InitSprites: No patches found for %s frame %c"
  155. , namelist[i], frame+'A');
  156. case 0: // only the first rotation is needed
  157. break;
  158. case 1: // must have all 8 frames
  159. for (rotation=0 ; rotation<8 ; rotation++)
  160. if (sprtemp[frame].lump[rotation] == -1)
  161. I_Error ("R_InitSprites: Sprite %s frame %c is missing rotations"
  162. , namelist[i], frame+'A');
  163. }
  164. }
  165. //
  166. // allocate space for the frames present and copy sprtemp to it
  167. //
  168. sprites[i].numframes = maxframe;
  169. sprites[i].spriteframes =
  170. Z_Malloc (maxframe * sizeof(spriteframe_t), PU_STATIC, NULL);
  171. memcpy (sprites[i].spriteframes, sprtemp, maxframe*sizeof(spriteframe_t));
  172. }
  173. }
  174. /*
  175. ===============================================================================
  176. GAME FUNCTIONS
  177. ===============================================================================
  178. */
  179. vissprite_t vissprites[MAXVISSPRITES], *vissprite_p;
  180. int newvissprite;
  181. /*
  182. ===================
  183. =
  184. = R_InitSprites
  185. =
  186. = Called at program start
  187. ===================
  188. */
  189. void R_InitSprites (char **namelist)
  190. {
  191. int i;
  192. for (i=0 ; i<SCREENWIDTH ; i++)
  193. {
  194. negonearray[i] = -1;
  195. }
  196. R_InitSpriteDefs (namelist);
  197. }
  198. /*
  199. ===================
  200. =
  201. = R_ClearSprites
  202. =
  203. = Called at frame start
  204. ===================
  205. */
  206. void R_ClearSprites (void)
  207. {
  208. vissprite_p = vissprites;
  209. }
  210. /*
  211. ===================
  212. =
  213. = R_NewVisSprite
  214. =
  215. ===================
  216. */
  217. vissprite_t overflowsprite;
  218. vissprite_t *R_NewVisSprite (void)
  219. {
  220. if (vissprite_p == &vissprites[MAXVISSPRITES])
  221. return &overflowsprite;
  222. vissprite_p++;
  223. return vissprite_p-1;
  224. }
  225. /*
  226. ================
  227. =
  228. = R_DrawMaskedColumn
  229. =
  230. = Used for sprites and masked mid textures
  231. ================
  232. */
  233. short *mfloorclip;
  234. short *mceilingclip;
  235. fixed_t spryscale;
  236. fixed_t sprtopscreen;
  237. fixed_t sprbotscreen;
  238. void R_DrawMaskedColumn (column_t *column, signed int baseclip)
  239. {
  240. int topscreen, bottomscreen;
  241. fixed_t basetexturemid;
  242. basetexturemid = dc_texturemid;
  243. for ( ; column->topdelta != 0xff ; )
  244. {
  245. // calculate unclipped screen coordinates for post
  246. topscreen = sprtopscreen + spryscale*column->topdelta;
  247. bottomscreen = topscreen + spryscale*column->length;
  248. dc_yl = (topscreen+FRACUNIT-1)>>FRACBITS;
  249. dc_yh = (bottomscreen-1)>>FRACBITS;
  250. if (dc_yh >= mfloorclip[dc_x])
  251. dc_yh = mfloorclip[dc_x]-1;
  252. if (dc_yl <= mceilingclip[dc_x])
  253. dc_yl = mceilingclip[dc_x]+1;
  254. if(dc_yh >= baseclip && baseclip != -1)
  255. dc_yh = baseclip;
  256. if (dc_yl <= dc_yh)
  257. {
  258. dc_source = (byte *)column + 3;
  259. dc_texturemid = basetexturemid - (column->topdelta<<FRACBITS);
  260. // dc_source = (byte *)column + 3 - column->topdelta;
  261. colfunc (); // either R_DrawColumn or R_DrawFuzzColumn
  262. }
  263. column = (column_t *)( (byte *)column + column->length + 4);
  264. }
  265. dc_texturemid = basetexturemid;
  266. }
  267. /*
  268. ================
  269. =
  270. = R_DrawVisSprite
  271. =
  272. = mfloorclip and mceilingclip should also be set
  273. ================
  274. */
  275. void R_DrawVisSprite (vissprite_t *vis, int x1, int x2)
  276. {
  277. column_t *column;
  278. int texturecolumn;
  279. fixed_t frac;
  280. patch_t *patch;
  281. fixed_t baseclip;
  282. patch = W_CacheLumpNum(vis->patch+firstspritelump, PU_CACHE);
  283. dc_colormap = vis->colormap;
  284. // if(!dc_colormap)
  285. // colfunc = fuzzcolfunc; // NULL colormap = shadow draw
  286. if(vis->mobjflags&MF_SHADOW)
  287. {
  288. if(vis->mobjflags&MF_TRANSLATION)
  289. {
  290. colfunc = R_DrawTranslatedFuzzColumn;
  291. dc_translation = translationtables - 256 +
  292. ((vis->mobjflags & MF_TRANSLATION) >> (MF_TRANSSHIFT-8));
  293. }
  294. else
  295. { // Draw using shadow column function
  296. colfunc = fuzzcolfunc;
  297. }
  298. }
  299. else if(vis->mobjflags&MF_TRANSLATION)
  300. {
  301. // Draw using translated column function
  302. colfunc = R_DrawTranslatedColumn;
  303. dc_translation = translationtables - 256 +
  304. ( (vis->mobjflags & MF_TRANSLATION) >> (MF_TRANSSHIFT-8) );
  305. }
  306. dc_iscale = abs(vis->xiscale)>>detailshift;
  307. dc_texturemid = vis->texturemid;
  308. frac = vis->startfrac;
  309. spryscale = vis->scale;
  310. sprtopscreen = centeryfrac - FixedMul(dc_texturemid,spryscale);
  311. // check to see if weapon is a vissprite
  312. if(vis->psprite)
  313. {
  314. dc_texturemid += FixedMul(((centery-viewheight/2)<<FRACBITS),
  315. vis->xiscale);
  316. sprtopscreen += (viewheight/2-centery)<<FRACBITS;
  317. }
  318. if(vis->footclip && !vis->psprite)
  319. {
  320. sprbotscreen = sprtopscreen+FixedMul(patch->height<<FRACBITS,
  321. spryscale);
  322. baseclip = (sprbotscreen-FixedMul(vis->footclip<<FRACBITS,
  323. spryscale))>>FRACBITS;
  324. }
  325. else
  326. {
  327. baseclip = -1;
  328. }
  329. for (dc_x=vis->x1 ; dc_x<=vis->x2 ; dc_x++, frac += vis->xiscale)
  330. {
  331. texturecolumn = frac>>FRACBITS;
  332. #ifdef RANGECHECK
  333. if (texturecolumn < 0 || texturecolumn >= SHORT(patch->width))
  334. I_Error ("R_DrawSpriteRange: bad texturecolumn");
  335. #endif
  336. column = (column_t *) ((byte *)patch +
  337. LONG(patch->columnofs[texturecolumn]));
  338. R_DrawMaskedColumn (column, baseclip);
  339. }
  340. colfunc = basecolfunc;
  341. }
  342. /*
  343. ===================
  344. =
  345. = R_ProjectSprite
  346. =
  347. = Generates a vissprite for a thing if it might be visible
  348. =
  349. ===================
  350. */
  351. void R_ProjectSprite (mobj_t *thing)
  352. {
  353. fixed_t trx,try;
  354. fixed_t gxt,gyt;
  355. fixed_t tx,tz;
  356. fixed_t xscale;
  357. int x1, x2;
  358. spritedef_t *sprdef;
  359. spriteframe_t *sprframe;
  360. int lump;
  361. unsigned rot;
  362. boolean flip;
  363. int index;
  364. vissprite_t *vis;
  365. angle_t ang;
  366. fixed_t iscale;
  367. if(thing->flags2&MF2_DONTDRAW)
  368. { // Never make a vissprite when MF2_DONTDRAW is flagged.
  369. return;
  370. }
  371. //
  372. // transform the origin point
  373. //
  374. trx = thing->x - viewx;
  375. try = thing->y - viewy;
  376. gxt = FixedMul(trx,viewcos);
  377. gyt = -FixedMul(try,viewsin);
  378. tz = gxt-gyt;
  379. if (tz < MINZ)
  380. return; // thing is behind view plane
  381. xscale = FixedDiv(projection, tz);
  382. gxt = -FixedMul(trx,viewsin);
  383. gyt = FixedMul(try,viewcos);
  384. tx = -(gyt+gxt);
  385. if (abs(tx)>(tz<<2))
  386. return; // too far off the side
  387. //
  388. // decide which patch to use for sprite reletive to player
  389. //
  390. #ifdef RANGECHECK
  391. if ((unsigned)thing->sprite >= numsprites)
  392. I_Error ("R_ProjectSprite: invalid sprite number %i ",thing->sprite);
  393. #endif
  394. sprdef = &sprites[thing->sprite];
  395. #ifdef RANGECHECK
  396. if ( (thing->frame&FF_FRAMEMASK) >= sprdef->numframes )
  397. I_Error ("R_ProjectSprite: invalid sprite frame %i : %i "
  398. ,thing->sprite, thing->frame);
  399. #endif
  400. sprframe = &sprdef->spriteframes[ thing->frame & FF_FRAMEMASK];
  401. if (sprframe->rotate)
  402. { // choose a different rotation based on player view
  403. ang = R_PointToAngle (thing->x, thing->y);
  404. rot = (ang-thing->angle+(unsigned)(ANG45/2)*9)>>29;
  405. lump = sprframe->lump[rot];
  406. flip = (boolean)sprframe->flip[rot];
  407. }
  408. else
  409. { // use single rotation for all views
  410. lump = sprframe->lump[0];
  411. flip = (boolean)sprframe->flip[0];
  412. }
  413. //
  414. // calculate edges of the shape
  415. //
  416. tx -= spriteoffset[lump];
  417. x1 = (centerxfrac + FixedMul (tx,xscale) ) >>FRACBITS;
  418. if (x1 > viewwidth)
  419. return; // off the right side
  420. tx += spritewidth[lump];
  421. x2 = ((centerxfrac + FixedMul (tx,xscale) ) >>FRACBITS) - 1;
  422. if (x2 < 0)
  423. return; // off the left side
  424. //
  425. // store information in a vissprite
  426. //
  427. vis = R_NewVisSprite ();
  428. vis->mobjflags = thing->flags;
  429. vis->psprite = false;
  430. vis->scale = xscale<<detailshift;
  431. vis->gx = thing->x;
  432. vis->gy = thing->y;
  433. vis->gz = thing->z;
  434. vis->gzt = thing->z + spritetopoffset[lump];
  435. // foot clipping
  436. if(thing->flags2&MF2_FEETARECLIPPED
  437. && thing->z <= thing->subsector->sector->floorheight)
  438. {
  439. vis->footclip = 10;
  440. }
  441. else vis->footclip = 0;
  442. vis->texturemid = vis->gzt - viewz - (vis->footclip<<FRACBITS);
  443. vis->x1 = x1 < 0 ? 0 : x1;
  444. vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
  445. iscale = FixedDiv (FRACUNIT, xscale);
  446. if (flip)
  447. {
  448. vis->startfrac = spritewidth[lump]-1;
  449. vis->xiscale = -iscale;
  450. }
  451. else
  452. {
  453. vis->startfrac = 0;
  454. vis->xiscale = iscale;
  455. }
  456. if (vis->x1 > x1)
  457. vis->startfrac += vis->xiscale*(vis->x1-x1);
  458. vis->patch = lump;
  459. //
  460. // get light level
  461. //
  462. // if (thing->flags & MF_SHADOW)
  463. // vis->colormap = NULL; // shadow draw
  464. // else ...
  465. if (fixedcolormap)
  466. vis->colormap = fixedcolormap; // fixed map
  467. else if (thing->frame & FF_FULLBRIGHT)
  468. vis->colormap = colormaps; // full bright
  469. else
  470. { // diminished light
  471. index = xscale>>(LIGHTSCALESHIFT-detailshift);
  472. if (index >= MAXLIGHTSCALE)
  473. index = MAXLIGHTSCALE-1;
  474. vis->colormap = spritelights[index];
  475. }
  476. }
  477. /*
  478. ========================
  479. =
  480. = R_AddSprites
  481. =
  482. ========================
  483. */
  484. void R_AddSprites (sector_t *sec)
  485. {
  486. mobj_t *thing;
  487. int lightnum;
  488. if (sec->validcount == validcount)
  489. return; // already added
  490. sec->validcount = validcount;
  491. lightnum = (sec->lightlevel >> LIGHTSEGSHIFT)+extralight;
  492. if (lightnum < 0)
  493. spritelights = scalelight[0];
  494. else if (lightnum >= LIGHTLEVELS)
  495. spritelights = scalelight[LIGHTLEVELS-1];
  496. else
  497. spritelights = scalelight[lightnum];
  498. for (thing = sec->thinglist ; thing ; thing = thing->snext)
  499. R_ProjectSprite (thing);
  500. }
  501. /*
  502. ========================
  503. =
  504. = R_DrawPSprite
  505. =
  506. ========================
  507. */
  508. int PSpriteSY[NUMWEAPONS] =
  509. {
  510. 0, // staff
  511. 5*FRACUNIT, // goldwand
  512. 15*FRACUNIT, // crossbow
  513. 15*FRACUNIT, // blaster
  514. 15*FRACUNIT, // skullrod
  515. 15*FRACUNIT, // phoenix rod
  516. 15*FRACUNIT, // mace
  517. 15*FRACUNIT, // gauntlets
  518. 15*FRACUNIT // beak
  519. };
  520. void R_DrawPSprite (pspdef_t *psp)
  521. {
  522. fixed_t tx;
  523. int x1, x2;
  524. spritedef_t *sprdef;
  525. spriteframe_t *sprframe;
  526. int lump;
  527. boolean flip;
  528. vissprite_t *vis, avis;
  529. int tempangle;
  530. //
  531. // decide which patch to use
  532. //
  533. #ifdef RANGECHECK
  534. if ( (unsigned)psp->state->sprite >= numsprites)
  535. I_Error ("R_ProjectSprite: invalid sprite number %i "
  536. , psp->state->sprite);
  537. #endif
  538. sprdef = &sprites[psp->state->sprite];
  539. #ifdef RANGECHECK
  540. if ( (psp->state->frame & FF_FRAMEMASK) >= sprdef->numframes)
  541. I_Error ("R_ProjectSprite: invalid sprite frame %i : %i "
  542. , psp->state->sprite, psp->state->frame);
  543. #endif
  544. sprframe = &sprdef->spriteframes[ psp->state->frame & FF_FRAMEMASK ];
  545. lump = sprframe->lump[0];
  546. flip = (boolean)sprframe->flip[0];
  547. //
  548. // calculate edges of the shape
  549. //
  550. tx = psp->sx-160*FRACUNIT;
  551. tx -= spriteoffset[lump];
  552. if(viewangleoffset)
  553. {
  554. tempangle = ((centerxfrac/1024)*(viewangleoffset>>ANGLETOFINESHIFT));
  555. }
  556. else
  557. {
  558. tempangle = 0;
  559. }
  560. x1 = (centerxfrac + FixedMul (tx,pspritescale)+tempangle ) >>FRACBITS;
  561. if (x1 > viewwidth)
  562. return; // off the right side
  563. tx += spritewidth[lump];
  564. x2 = ((centerxfrac + FixedMul (tx, pspritescale)+tempangle ) >>FRACBITS) - 1;
  565. if (x2 < 0)
  566. return; // off the left side
  567. //
  568. // store information in a vissprite
  569. //
  570. vis = &avis;
  571. vis->mobjflags = 0;
  572. vis->psprite = true;
  573. vis->texturemid = (BASEYCENTER<<FRACBITS)+FRACUNIT/2-(psp->sy-spritetopoffset[lump]);
  574. if(viewheight == SCREENHEIGHT)
  575. {
  576. vis->texturemid -= PSpriteSY[players[consoleplayer].readyweapon];
  577. }
  578. vis->x1 = x1 < 0 ? 0 : x1;
  579. vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
  580. vis->scale = pspritescale<<detailshift;
  581. if (flip)
  582. {
  583. vis->xiscale = -pspriteiscale;
  584. vis->startfrac = spritewidth[lump]-1;
  585. }
  586. else
  587. {
  588. vis->xiscale = pspriteiscale;
  589. vis->startfrac = 0;
  590. }
  591. if (vis->x1 > x1)
  592. vis->startfrac += vis->xiscale*(vis->x1-x1);
  593. vis->patch = lump;
  594. if(viewplayer->powers[pw_invisibility] > 4*32 ||
  595. viewplayer->powers[pw_invisibility] & 8)
  596. {
  597. // Invisibility
  598. vis->colormap = spritelights[MAXLIGHTSCALE-1];
  599. vis->mobjflags |= MF_SHADOW;
  600. }
  601. else if(fixedcolormap)
  602. {
  603. // Fixed color
  604. vis->colormap = fixedcolormap;
  605. }
  606. else if(psp->state->frame & FF_FULLBRIGHT)
  607. {
  608. // Full bright
  609. vis->colormap = colormaps;
  610. }
  611. else
  612. {
  613. // local light
  614. vis->colormap = spritelights[MAXLIGHTSCALE-1];
  615. }
  616. R_DrawVisSprite(vis, vis->x1, vis->x2);
  617. }
  618. /*
  619. ========================
  620. =
  621. = R_DrawPlayerSprites
  622. =
  623. ========================
  624. */
  625. void R_DrawPlayerSprites (void)
  626. {
  627. int i, lightnum;
  628. pspdef_t *psp;
  629. //
  630. // get light level
  631. //
  632. lightnum = (viewplayer->mo->subsector->sector->lightlevel >> LIGHTSEGSHIFT)
  633. +extralight;
  634. if (lightnum < 0)
  635. spritelights = scalelight[0];
  636. else if (lightnum >= LIGHTLEVELS)
  637. spritelights = scalelight[LIGHTLEVELS-1];
  638. else
  639. spritelights = scalelight[lightnum];
  640. //
  641. // clip to screen bounds
  642. //
  643. mfloorclip = screenheightarray;
  644. mceilingclip = negonearray;
  645. //
  646. // add all active psprites
  647. //
  648. for (i=0, psp=viewplayer->psprites ; i<NUMPSPRITES ; i++,psp++)
  649. if (psp->state)
  650. R_DrawPSprite (psp);
  651. }
  652. /*
  653. ========================
  654. =
  655. = R_SortVisSprites
  656. =
  657. ========================
  658. */
  659. vissprite_t vsprsortedhead;
  660. void R_SortVisSprites (void)
  661. {
  662. int i, count;
  663. vissprite_t *ds, *best;
  664. vissprite_t unsorted;
  665. fixed_t bestscale;
  666. count = vissprite_p - vissprites;
  667. unsorted.next = unsorted.prev = &unsorted;
  668. if (!count)
  669. return;
  670. for (ds=vissprites ; ds<vissprite_p ; ds++)
  671. {
  672. ds->next = ds+1;
  673. ds->prev = ds-1;
  674. }
  675. vissprites[0].prev = &unsorted;
  676. unsorted.next = &vissprites[0];
  677. (vissprite_p-1)->next = &unsorted;
  678. unsorted.prev = vissprite_p-1;
  679. //
  680. // pull the vissprites out by scale
  681. //
  682. best = 0; // shut up the compiler warning
  683. vsprsortedhead.next = vsprsortedhead.prev = &vsprsortedhead;
  684. for (i=0 ; i<count ; i++)
  685. {
  686. bestscale = MAXINT;
  687. for (ds=unsorted.next ; ds!= &unsorted ; ds=ds->next)
  688. {
  689. if (ds->scale < bestscale)
  690. {
  691. bestscale = ds->scale;
  692. best = ds;
  693. }
  694. }
  695. best->next->prev = best->prev;
  696. best->prev->next = best->next;
  697. best->next = &vsprsortedhead;
  698. best->prev = vsprsortedhead.prev;
  699. vsprsortedhead.prev->next = best;
  700. vsprsortedhead.prev = best;
  701. }
  702. }
  703. /*
  704. ========================
  705. =
  706. = R_DrawSprite
  707. =
  708. ========================
  709. */
  710. void R_DrawSprite (vissprite_t *spr)
  711. {
  712. drawseg_t *ds;
  713. short clipbot[SCREENWIDTH], cliptop[SCREENWIDTH];
  714. int x, r1, r2;
  715. fixed_t scale, lowscale;
  716. int silhouette;
  717. for (x = spr->x1 ; x<=spr->x2 ; x++)
  718. clipbot[x] = cliptop[x] = -2;
  719. //
  720. // scan drawsegs from end to start for obscuring segs
  721. // the first drawseg that has a greater scale is the clip seg
  722. //
  723. for (ds=ds_p-1 ; ds >= drawsegs ; ds--)
  724. {
  725. //
  726. // determine if the drawseg obscures the sprite
  727. //
  728. if (ds->x1 > spr->x2 || ds->x2 < spr->x1 ||
  729. (!ds->silhouette && !ds->maskedtexturecol) )
  730. continue; // doesn't cover sprite
  731. r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1;
  732. r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2;
  733. if (ds->scale1 > ds->scale2)
  734. {
  735. lowscale = ds->scale2;
  736. scale = ds->scale1;
  737. }
  738. else
  739. {
  740. lowscale = ds->scale1;
  741. scale = ds->scale2;
  742. }
  743. if (scale < spr->scale || ( lowscale < spr->scale
  744. && !R_PointOnSegSide (spr->gx, spr->gy, ds->curline) ) )
  745. {
  746. if (ds->maskedtexturecol) // masked mid texture
  747. R_RenderMaskedSegRange (ds, r1, r2);
  748. continue; // seg is behind sprite
  749. }
  750. //
  751. // clip this piece of the sprite
  752. //
  753. silhouette = ds->silhouette;
  754. if (spr->gz >= ds->bsilheight)
  755. silhouette &= ~SIL_BOTTOM;
  756. if (spr->gzt <= ds->tsilheight)
  757. silhouette &= ~SIL_TOP;
  758. if (silhouette == 1)
  759. { // bottom sil
  760. for (x=r1 ; x<=r2 ; x++)
  761. if (clipbot[x] == -2)
  762. clipbot[x] = ds->sprbottomclip[x];
  763. }
  764. else if (silhouette == 2)
  765. { // top sil
  766. for (x=r1 ; x<=r2 ; x++)
  767. if (cliptop[x] == -2)
  768. cliptop[x] = ds->sprtopclip[x];
  769. }
  770. else if (silhouette == 3)
  771. { // both
  772. for (x=r1 ; x<=r2 ; x++)
  773. {
  774. if (clipbot[x] == -2)
  775. clipbot[x] = ds->sprbottomclip[x];
  776. if (cliptop[x] == -2)
  777. cliptop[x] = ds->sprtopclip[x];
  778. }
  779. }
  780. }
  781. //
  782. // all clipping has been performed, so draw the sprite
  783. //
  784. // check for unclipped columns
  785. for (x = spr->x1 ; x<=spr->x2 ; x++)
  786. {
  787. if (clipbot[x] == -2)
  788. clipbot[x] = viewheight;
  789. if (cliptop[x] == -2)
  790. cliptop[x] = -1;
  791. }
  792. mfloorclip = clipbot;
  793. mceilingclip = cliptop;
  794. R_DrawVisSprite (spr, spr->x1, spr->x2);
  795. }
  796. /*
  797. ========================
  798. =
  799. = R_DrawMasked
  800. =
  801. ========================
  802. */
  803. void R_DrawMasked (void)
  804. {
  805. vissprite_t *spr;
  806. drawseg_t *ds;
  807. R_SortVisSprites ();
  808. if (vissprite_p > vissprites)
  809. {
  810. // draw all vissprites back to front
  811. for (spr = vsprsortedhead.next ; spr != &vsprsortedhead
  812. ; spr=spr->next)
  813. R_DrawSprite (spr);
  814. }
  815. //
  816. // render any remaining masked mid textures
  817. //
  818. for (ds=ds_p-1 ; ds >= drawsegs ; ds--)
  819. if (ds->maskedtexturecol)
  820. R_RenderMaskedSegRange (ds, ds->x1, ds->x2);
  821. //
  822. // draw the psprites on top of everything
  823. //
  824. // Added for the sideviewing with an external device
  825. if (viewangleoffset <= 1024<<ANGLETOFINESHIFT || viewangleoffset >=
  826. -1024<<ANGLETOFINESHIFT)
  827. { // don't draw on side views
  828. R_DrawPlayerSprites ();
  829. }
  830. // if (!viewangleoffset) // don't draw on side views
  831. // R_DrawPlayerSprites ();
  832. }