r_things.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Refresh of things, i.e. objects represented by sprites.
  31. *
  32. *-----------------------------------------------------------------------------*/
  33. #include "doomstat.h"
  34. #include "w_wad.h"
  35. #include "r_main.h"
  36. #include "r_bsp.h"
  37. #include "r_segs.h"
  38. #include "r_draw.h"
  39. #include "r_things.h"
  40. #include "r_fps.h"
  41. #include "v_video.h"
  42. #include "lprintf.h"
  43. #define MINZ (FRACUNIT*4)
  44. #define BASEYCENTER 100
  45. typedef struct {
  46. int x1;
  47. int x2;
  48. int column;
  49. int topclip;
  50. int bottomclip;
  51. } maskdraw_t;
  52. //
  53. // Sprite rotation 0 is facing the viewer,
  54. // rotation 1 is one angle turn CLOCKWISE around the axis.
  55. // This is not the same as the angle,
  56. // which increases counter clockwise (protractor).
  57. // There was a lot of stuff grabbed wrong, so I changed it...
  58. //
  59. fixed_t pspritescale;
  60. fixed_t pspriteiscale;
  61. // proff 11/06/98: Added for high-res
  62. fixed_t pspriteyscale;
  63. // constant arrays
  64. // used for psprite clipping and initializing clipping
  65. int negonearray[MAX_SCREENWIDTH]; // killough 2/8/98: // dropoff overflow
  66. int screenheightarray[MAX_SCREENWIDTH]; // change to MAX_* // dropoff overflow
  67. //
  68. // INITIALIZATION FUNCTIONS
  69. //
  70. // variables used to look up and range check thing_t sprites patches
  71. spritedef_t *sprites;
  72. int numsprites;
  73. #define MAX_SPRITE_FRAMES 29 /* Macroized -- killough 1/25/98 */
  74. static spriteframe_t sprtemp[MAX_SPRITE_FRAMES];
  75. static int maxframe;
  76. //
  77. // R_InstallSpriteLump
  78. // Local function for R_InitSprites.
  79. //
  80. static void R_InstallSpriteLump(int lump, unsigned frame,
  81. unsigned rotation, boolean flipped)
  82. {
  83. if (frame >= MAX_SPRITE_FRAMES || rotation > 8)
  84. I_Error("R_InstallSpriteLump: Bad frame characters in lump %i", lump);
  85. if ((int) frame > maxframe)
  86. maxframe = frame;
  87. if (rotation == 0)
  88. { // the lump should be used for all rotations
  89. int r;
  90. for (r=0 ; r<8 ; r++)
  91. if (sprtemp[frame].lump[r]==-1)
  92. {
  93. sprtemp[frame].lump[r] = lump - firstspritelump;
  94. sprtemp[frame].flip[r] = (byte) flipped;
  95. sprtemp[frame].rotate = false; //jff 4/24/98 if any subbed, rotless
  96. }
  97. return;
  98. }
  99. // the lump is only used for one rotation
  100. if (sprtemp[frame].lump[--rotation] == -1)
  101. {
  102. sprtemp[frame].lump[rotation] = lump - firstspritelump;
  103. sprtemp[frame].flip[rotation] = (byte) flipped;
  104. sprtemp[frame].rotate = true; //jff 4/24/98 only change if rot used
  105. }
  106. }
  107. //
  108. // R_InitSpriteDefs
  109. // Pass a null terminated list of sprite names
  110. // (4 chars exactly) to be used.
  111. //
  112. // Builds the sprite rotation matrixes to account
  113. // for horizontally flipped sprites.
  114. //
  115. // Will report an error if the lumps are inconsistent.
  116. // Only called at startup.
  117. //
  118. // Sprite lump names are 4 characters for the actor,
  119. // a letter for the frame, and a number for the rotation.
  120. //
  121. // A sprite that is flippable will have an additional
  122. // letter/number appended.
  123. //
  124. // The rotation character can be 0 to signify no rotations.
  125. //
  126. // 1/25/98, 1/31/98 killough : Rewritten for performance
  127. //
  128. // Empirically verified to have excellent hash
  129. // properties across standard Doom sprites:
  130. #define R_SpriteNameHash(s) ((unsigned)((s)[0]-((s)[1]*3-(s)[3]*2-(s)[2])*2))
  131. static void R_InitSpriteDefs(const char * const * namelist)
  132. {
  133. size_t numentries = lastspritelump-firstspritelump+1;
  134. struct { int index, next; } *hash;
  135. int i;
  136. if (!numentries || !*namelist)
  137. return;
  138. // count the number of sprite names
  139. for (i=0; namelist[i]; i++)
  140. ;
  141. numsprites = i;
  142. sprites = Z_Malloc(numsprites *sizeof(*sprites), PU_STATIC, NULL);
  143. memset( sprites, 0, numsprites *sizeof(*sprites) ); // JDC: Z_Malloc isn't zero filled
  144. // Create hash table based on just the first four letters of each sprite
  145. // killough 1/31/98
  146. hash = malloc(sizeof(*hash)*numentries); // allocate hash table
  147. for (i=0; (size_t)i<numentries; i++) // initialize hash table as empty
  148. hash[i].index = -1;
  149. for (i=0; (size_t)i<numentries; i++) // Prepend each sprite to hash chain
  150. { // prepend so that later ones win
  151. int j = R_SpriteNameHash(lumpinfo[i+firstspritelump].name) % numentries;
  152. hash[i].next = hash[j].index;
  153. hash[j].index = i;
  154. }
  155. // scan all the lump names for each of the names,
  156. // noting the highest frame letter.
  157. for (i=0 ; i<numsprites ; i++)
  158. {
  159. const char *spritename = namelist[i];
  160. int j = hash[R_SpriteNameHash(spritename) % numentries].index;
  161. if (j >= 0)
  162. {
  163. memset(sprtemp, -1, sizeof(sprtemp));
  164. maxframe = -1;
  165. do
  166. {
  167. register lumpinfo_t *lump = lumpinfo + j + firstspritelump;
  168. // Fast portable comparison -- killough
  169. // (using int pointer cast is nonportable):
  170. if (!((lump->name[0] ^ spritename[0]) |
  171. (lump->name[1] ^ spritename[1]) |
  172. (lump->name[2] ^ spritename[2]) |
  173. (lump->name[3] ^ spritename[3])))
  174. {
  175. R_InstallSpriteLump(j+firstspritelump,
  176. lump->name[4] - 'A',
  177. lump->name[5] - '0',
  178. false);
  179. if (lump->name[6])
  180. R_InstallSpriteLump(j+firstspritelump,
  181. lump->name[6] - 'A',
  182. lump->name[7] - '0',
  183. true);
  184. }
  185. }
  186. while ((j = hash[j].next) >= 0);
  187. // check the frames that were found for completeness
  188. if ((sprites[i].numframes = ++maxframe)) // killough 1/31/98
  189. {
  190. int frame;
  191. for (frame = 0; frame < maxframe; frame++)
  192. switch ((int) sprtemp[frame].rotate)
  193. {
  194. case -1:
  195. // no rotations were found for that frame at all
  196. I_Error ("R_InitSprites: No patches found "
  197. "for %.8s frame %c", namelist[i], frame+'A');
  198. break;
  199. case 0:
  200. // only the first rotation is needed
  201. break;
  202. case 1:
  203. // must have all 8 frames
  204. {
  205. int rotation;
  206. for (rotation=0 ; rotation<8 ; rotation++)
  207. if (sprtemp[frame].lump[rotation] == -1)
  208. I_Error ("R_InitSprites: Sprite %.8s frame %c "
  209. "is missing rotations",
  210. namelist[i], frame+'A');
  211. break;
  212. }
  213. }
  214. // allocate space for the frames present and copy sprtemp to it
  215. sprites[i].spriteframes =
  216. Z_Malloc (maxframe * sizeof(spriteframe_t), PU_STATIC, NULL);
  217. memcpy (sprites[i].spriteframes, sprtemp,
  218. maxframe*sizeof(spriteframe_t));
  219. }
  220. }
  221. }
  222. free(hash); // free hash table
  223. }
  224. //
  225. // GAME FUNCTIONS
  226. //
  227. static vissprite_t *vissprites, **vissprite_ptrs; // killough
  228. static size_t num_vissprite, num_vissprite_alloc, num_vissprite_ptrs;
  229. //
  230. // R_InitSprites
  231. // Called at program start.
  232. //
  233. void R_InitSprites(const char * const *namelist)
  234. {
  235. int i;
  236. for (i=0; i<MAX_SCREENWIDTH; i++) // killough 2/8/98
  237. negonearray[i] = -1;
  238. R_InitSpriteDefs(namelist);
  239. }
  240. //
  241. // R_ClearSprites
  242. // Called at frame start.
  243. //
  244. void R_ClearSprites (void)
  245. {
  246. num_vissprite = 0; // killough
  247. }
  248. //
  249. // R_NewVisSprite
  250. //
  251. static vissprite_t *R_NewVisSprite(void)
  252. {
  253. if (num_vissprite >= num_vissprite_alloc) // killough
  254. {
  255. size_t num_vissprite_alloc_prev = num_vissprite_alloc;
  256. num_vissprite_alloc = num_vissprite_alloc ? num_vissprite_alloc*2 : 128;
  257. vissprites = realloc(vissprites,num_vissprite_alloc*sizeof(*vissprites));
  258. //e6y: set all fields to zero
  259. memset(vissprites + num_vissprite_alloc_prev, 0,
  260. (num_vissprite_alloc - num_vissprite_alloc_prev)*sizeof(*vissprites));
  261. }
  262. return vissprites + num_vissprite++;
  263. }
  264. //
  265. // R_DrawMaskedColumn
  266. // Used for sprites and masked mid textures.
  267. // Masked means: partly transparent, i.e. stored
  268. // in posts/runs of opaque pixels.
  269. //
  270. int *mfloorclip; // dropoff overflow
  271. int *mceilingclip; // dropoff overflow
  272. fixed_t spryscale;
  273. fixed_t sprtopscreen;
  274. void R_DrawMaskedColumn(
  275. const rpatch_t *patch,
  276. R_DrawColumn_f colfunc,
  277. draw_column_vars_t *dcvars,
  278. const rcolumn_t *column,
  279. const rcolumn_t *prevcolumn,
  280. const rcolumn_t *nextcolumn
  281. )
  282. {
  283. int i;
  284. int topscreen;
  285. int bottomscreen;
  286. fixed_t basetexturemid = dcvars->texturemid;
  287. dcvars->texheight = patch->height; // killough 11/98
  288. for (i=0; i<column->numPosts; i++) {
  289. const rpost_t *post = &column->posts[i];
  290. // calculate unclipped screen coordinates for post
  291. topscreen = sprtopscreen + spryscale*post->topdelta;
  292. bottomscreen = topscreen + spryscale*post->length;
  293. dcvars->yl = (topscreen+FRACUNIT-1)>>FRACBITS;
  294. dcvars->yh = (bottomscreen-1)>>FRACBITS;
  295. if (dcvars->yh >= mfloorclip[dcvars->x])
  296. dcvars->yh = mfloorclip[dcvars->x]-1;
  297. if (dcvars->yl <= mceilingclip[dcvars->x])
  298. dcvars->yl = mceilingclip[dcvars->x]+1;
  299. // killough 3/2/98, 3/27/98: Failsafe against overflow/crash:
  300. if (dcvars->yl <= dcvars->yh && dcvars->yh < viewheight)
  301. {
  302. dcvars->source = column->pixels + post->topdelta;
  303. dcvars->prevsource = prevcolumn->pixels + post->topdelta;
  304. dcvars->nextsource = nextcolumn->pixels + post->topdelta;
  305. dcvars->texturemid = basetexturemid - (post->topdelta<<FRACBITS);
  306. dcvars->edgeslope = post->slope;
  307. // Drawn by either R_DrawColumn
  308. // or (SHADOW) R_DrawFuzzColumn.
  309. dcvars->drawingmasked = 1; // POPE
  310. colfunc (dcvars);
  311. dcvars->drawingmasked = 0; // POPE
  312. }
  313. }
  314. dcvars->texturemid = basetexturemid;
  315. }
  316. //
  317. // R_DrawVisSprite
  318. // mfloorclip and mceilingclip should also be set.
  319. //
  320. // CPhipps - new wad lump handling, *'s to const*'s
  321. static void R_DrawVisSprite(vissprite_t *vis, int x1, int x2)
  322. {
  323. int texturecolumn;
  324. fixed_t frac;
  325. const rpatch_t *patch = R_CachePatchNum(vis->patch+firstspritelump);
  326. R_DrawColumn_f colfunc;
  327. draw_column_vars_t dcvars;
  328. enum draw_filter_type_e filter;
  329. enum draw_filter_type_e filterz;
  330. R_SetDefaultDrawColumnVars(&dcvars);
  331. if (vis->isplayersprite) {
  332. dcvars.edgetype = drawvars.patch_edges;
  333. filter = drawvars.filterpatch;
  334. filterz = RDRAW_FILTER_POINT;
  335. } else {
  336. dcvars.edgetype = drawvars.sprite_edges;
  337. filter = drawvars.filtersprite;
  338. filterz = drawvars.filterz;
  339. }
  340. dcvars.colormap = vis->colormap;
  341. dcvars.nextcolormap = dcvars.colormap; // for filtering -- POPE
  342. // killough 4/11/98: rearrange and handle translucent sprites
  343. // mixed with translucent/non-translucenct 2s normals
  344. if (!dcvars.colormap) // NULL colormap = shadow draw
  345. colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_FUZZ, filter, filterz); // killough 3/14/98
  346. else
  347. if (vis->mobjflags & MF_TRANSLATION)
  348. {
  349. colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_TRANSLATED, filter, filterz);
  350. dcvars.translation = translationtables - 256 +
  351. ((vis->mobjflags & MF_TRANSLATION) >> (MF_TRANSSHIFT-8) );
  352. }
  353. else
  354. if (vis->mobjflags & MF_TRANSLUCENT && general_translucency) // phares
  355. {
  356. colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_TRANSLUCENT, filter, filterz);
  357. tranmap = main_tranmap; // killough 4/11/98
  358. }
  359. else
  360. colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_STANDARD, filter, filterz); // killough 3/14/98, 4/11/98
  361. // proff 11/06/98: Changed for high-res
  362. dcvars.iscale = FixedDiv (FRACUNIT, vis->scale);
  363. dcvars.texturemid = vis->texturemid;
  364. frac = vis->startfrac;
  365. if (filter == RDRAW_FILTER_LINEAR)
  366. frac -= (FRACUNIT>>1);
  367. spryscale = vis->scale;
  368. sprtopscreen = centeryfrac - FixedMul(dcvars.texturemid,spryscale);
  369. for (dcvars.x=vis->x1 ; dcvars.x<=vis->x2 ; dcvars.x++, frac += vis->xiscale)
  370. {
  371. texturecolumn = frac>>FRACBITS;
  372. dcvars.texu = frac;
  373. R_DrawMaskedColumn(
  374. patch,
  375. colfunc,
  376. &dcvars,
  377. R_GetPatchColumnClamped(patch, texturecolumn),
  378. R_GetPatchColumnClamped(patch, texturecolumn-1),
  379. R_GetPatchColumnClamped(patch, texturecolumn+1)
  380. );
  381. }
  382. R_UnlockPatchNum(vis->patch+firstspritelump); // cph - release lump
  383. }
  384. //
  385. // R_ProjectSprite
  386. // Generates a vissprite for a thing if it might be visible.
  387. //
  388. static void R_ProjectSprite (mobj_t* thing, int lightlevel)
  389. {
  390. fixed_t gzt; // killough 3/27/98
  391. fixed_t tx;
  392. fixed_t xscale;
  393. int x1;
  394. int x2;
  395. spritedef_t *sprdef;
  396. spriteframe_t *sprframe;
  397. int lump;
  398. boolean flip;
  399. vissprite_t *vis;
  400. fixed_t iscale;
  401. int heightsec; // killough 3/27/98
  402. // transform the origin point
  403. fixed_t tr_x, tr_y;
  404. fixed_t fx, fy, fz;
  405. fixed_t gxt, gyt;
  406. fixed_t tz;
  407. int width;
  408. if (movement_smooth)
  409. {
  410. fx = thing->PrevX + FixedMul (tic_vars.frac, thing->x - thing->PrevX);
  411. fy = thing->PrevY + FixedMul (tic_vars.frac, thing->y - thing->PrevY);
  412. fz = thing->PrevZ + FixedMul (tic_vars.frac, thing->z - thing->PrevZ);
  413. }
  414. else
  415. {
  416. fx = thing->x;
  417. fy = thing->y;
  418. fz = thing->z;
  419. }
  420. tr_x = fx - viewx;
  421. tr_y = fy - viewy;
  422. gxt = FixedMul(tr_x,viewcos);
  423. gyt = -FixedMul(tr_y,viewsin);
  424. tz = gxt-gyt;
  425. // thing is behind view plane?
  426. if (tz < MINZ)
  427. return;
  428. xscale = FixedDiv(projection, tz);
  429. gxt = -FixedMul(tr_x,viewsin);
  430. gyt = FixedMul(tr_y,viewcos);
  431. tx = -(gyt+gxt);
  432. // too far off the side?
  433. if (D_abs(tx)>(tz<<2))
  434. return;
  435. // decide which patch to use for sprite relative to player
  436. #ifdef RANGECHECK
  437. if ((unsigned) thing->sprite >= (unsigned)numsprites)
  438. I_Error ("R_ProjectSprite: Invalid sprite number %i", thing->sprite);
  439. #endif
  440. sprdef = &sprites[thing->sprite];
  441. #ifdef RANGECHECK
  442. if ((thing->frame&FF_FRAMEMASK) >= sprdef->numframes)
  443. I_Error ("R_ProjectSprite: Invalid sprite frame %i : %i", thing->sprite,
  444. thing->frame);
  445. #endif
  446. if (!sprdef->spriteframes)
  447. I_Error ("R_ProjectSprite: Missing spriteframes %i : %i", thing->sprite,
  448. thing->frame);
  449. sprframe = &sprdef->spriteframes[thing->frame & FF_FRAMEMASK];
  450. if (sprframe->rotate)
  451. {
  452. // choose a different rotation based on player view
  453. angle_t ang = R_PointToAngle(fx, fy);
  454. unsigned rot = (ang-thing->angle+(unsigned)(ANG45/2)*9)>>29;
  455. lump = sprframe->lump[rot];
  456. flip = (boolean) sprframe->flip[rot];
  457. }
  458. else
  459. {
  460. // use single rotation for all views
  461. lump = sprframe->lump[0];
  462. flip = (boolean) sprframe->flip[0];
  463. }
  464. {
  465. const rpatch_t* patch = R_CachePatchNum(lump+firstspritelump);
  466. /* calculate edges of the shape
  467. * cph 2003/08/1 - fraggle points out that this offset must be flipped
  468. * if the sprite is flipped; e.g. FreeDoom imp is messed up by this. */
  469. if (flip) {
  470. tx -= (patch->width - patch->leftoffset) << FRACBITS;
  471. } else {
  472. tx -= patch->leftoffset << FRACBITS;
  473. }
  474. x1 = (centerxfrac + FixedMul(tx,xscale)) >> FRACBITS;
  475. tx += patch->width<<FRACBITS;
  476. x2 = ((centerxfrac + FixedMul (tx,xscale) ) >> FRACBITS) - 1;
  477. gzt = fz + (patch->topoffset << FRACBITS);
  478. width = patch->width;
  479. R_UnlockPatchNum(lump+firstspritelump);
  480. }
  481. // off the side?
  482. if (x1 > viewwidth || x2 < 0)
  483. return;
  484. // killough 4/9/98: clip things which are out of view due to height
  485. // e6y: fix of hanging decoration disappearing in Batman Doom MAP02
  486. // centeryfrac -> viewheightfrac
  487. if (fz > viewz + FixedDiv(viewheightfrac, xscale) ||
  488. gzt < viewz - FixedDiv(viewheightfrac-viewheight, xscale))
  489. return;
  490. // killough 3/27/98: exclude things totally separated
  491. // from the viewer, by either water or fake ceilings
  492. // killough 4/11/98: improve sprite clipping for underwater/fake ceilings
  493. heightsec = thing->subsector->sector->heightsec;
  494. if (heightsec != -1) // only clip things which are in special sectors
  495. {
  496. int phs = viewplayer->mo->subsector->sector->heightsec;
  497. if (phs != -1 && viewz < sectors[phs].floorheight ?
  498. fz >= sectors[heightsec].floorheight :
  499. gzt < sectors[heightsec].floorheight)
  500. return;
  501. if (phs != -1 && viewz > sectors[phs].ceilingheight ?
  502. gzt < sectors[heightsec].ceilingheight &&
  503. viewz >= sectors[heightsec].ceilingheight :
  504. fz >= sectors[heightsec].ceilingheight)
  505. return;
  506. }
  507. // store information in a vissprite
  508. vis = R_NewVisSprite ();
  509. #ifdef GL_DOOM
  510. if (V_GetMode() == VID_MODEGL)
  511. {
  512. // proff 11/99: add sprite for OpenGL
  513. vis->thing = thing;
  514. vis->flip = flip;
  515. vis->scale = FixedDiv(projectiony, tz);
  516. vis->patch = lump;
  517. gld_AddSprite(vis);
  518. return;
  519. }
  520. #endif
  521. // killough 3/27/98: save sector for special clipping later
  522. vis->heightsec = heightsec;
  523. vis->mobjflags = thing->flags;
  524. // proff 11/06/98: Changed for high-res
  525. vis->scale = FixedDiv(projectiony, tz);
  526. vis->gx = fx;
  527. vis->gy = fy;
  528. vis->gz = fz;
  529. vis->gzt = gzt; // killough 3/27/98
  530. vis->texturemid = vis->gzt - viewz;
  531. vis->x1 = x1 < 0 ? 0 : x1;
  532. vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
  533. iscale = FixedDiv (FRACUNIT, xscale);
  534. if (flip)
  535. {
  536. vis->startfrac = (width<<FRACBITS)-1;
  537. vis->xiscale = -iscale;
  538. }
  539. else
  540. {
  541. vis->startfrac = 0;
  542. vis->xiscale = iscale;
  543. }
  544. if (vis->x1 > x1)
  545. vis->startfrac += vis->xiscale*(vis->x1-x1);
  546. vis->patch = lump;
  547. // get light level
  548. if (thing->flags & MF_SHADOW)
  549. vis->colormap = NULL; // shadow draw
  550. else if (fixedcolormap)
  551. vis->colormap = fixedcolormap; // fixed map
  552. else if (thing->frame & FF_FULLBRIGHT)
  553. vis->colormap = fullcolormap; // full bright // killough 3/20/98
  554. else
  555. { // diminished light
  556. vis->colormap = R_ColourMap(lightlevel,xscale);
  557. }
  558. }
  559. //
  560. // R_AddSprites
  561. // During BSP traversal, this adds sprites by sector.
  562. //
  563. // killough 9/18/98: add lightlevel as parameter, fixing underwater lighting
  564. void R_AddSprites(subsector_t* subsec, int lightlevel)
  565. {
  566. sector_t* sec=subsec->sector;
  567. mobj_t *thing;
  568. // BSP is traversed by subsector.
  569. // A sector might have been split into several
  570. // subsectors during BSP building.
  571. // Thus we check whether its already added.
  572. if (sec->validcount == validcount)
  573. return;
  574. // Well, now it will be done.
  575. sec->validcount = validcount;
  576. // Handle all things in sector.
  577. for (thing = sec->thinglist; thing; thing = thing->snext)
  578. R_ProjectSprite(thing, lightlevel);
  579. }
  580. //
  581. // R_DrawPSprite
  582. //
  583. static void R_DrawPSprite (pspdef_t *psp, int lightlevel)
  584. {
  585. int x1, x2;
  586. spritedef_t *sprdef;
  587. spriteframe_t *sprframe;
  588. int lump;
  589. boolean flip;
  590. vissprite_t *vis;
  591. vissprite_t avis;
  592. int width;
  593. fixed_t topoffset;
  594. avis.isplayersprite = true;
  595. // decide which patch to use
  596. #ifdef RANGECHECK
  597. if ( (unsigned)psp->state->sprite >= (unsigned)numsprites)
  598. I_Error ("R_ProjectSprite: Invalid sprite number %i", psp->state->sprite);
  599. #endif
  600. sprdef = &sprites[psp->state->sprite];
  601. #ifdef RANGECHECK
  602. if ( (psp->state->frame & FF_FRAMEMASK) >= sprdef->numframes)
  603. I_Error ("R_ProjectSprite: Invalid sprite frame %i : %li",
  604. psp->state->sprite, psp->state->frame);
  605. #endif
  606. sprframe = &sprdef->spriteframes[psp->state->frame & FF_FRAMEMASK];
  607. lump = sprframe->lump[0];
  608. flip = (boolean) sprframe->flip[0];
  609. {
  610. const rpatch_t* patch = R_CachePatchNum(lump+firstspritelump);
  611. // calculate edges of the shape
  612. fixed_t tx;
  613. tx = psp->sx-160*FRACUNIT;
  614. tx -= patch->leftoffset<<FRACBITS;
  615. x1 = (centerxfrac + FixedMul (tx,pspritescale))>>FRACBITS;
  616. tx += patch->width<<FRACBITS;
  617. x2 = ((centerxfrac + FixedMul (tx, pspritescale) ) >>FRACBITS) - 1;
  618. width = patch->width;
  619. topoffset = patch->topoffset<<FRACBITS;
  620. R_UnlockPatchNum(lump+firstspritelump);
  621. }
  622. // off the side
  623. if (x2 < 0 || x1 > viewwidth)
  624. return;
  625. // store information in a vissprite
  626. vis = &avis;
  627. vis->mobjflags = 0;
  628. // killough 12/98: fix psprite positioning problem
  629. vis->texturemid = (BASEYCENTER<<FRACBITS) /* + FRACUNIT/2 */ -
  630. (psp->sy-topoffset);
  631. vis->x1 = x1 < 0 ? 0 : x1;
  632. vis->x2 = x2 >= viewwidth ? viewwidth-1 : x2;
  633. // proff 11/06/98: Added for high-res
  634. vis->scale = pspriteyscale;
  635. if (flip)
  636. {
  637. vis->xiscale = -pspriteiscale;
  638. vis->startfrac = (width<<FRACBITS)-1;
  639. }
  640. else
  641. {
  642. vis->xiscale = pspriteiscale;
  643. vis->startfrac = 0;
  644. }
  645. if (vis->x1 > x1)
  646. vis->startfrac += vis->xiscale*(vis->x1-x1);
  647. vis->patch = lump;
  648. if (viewplayer->powers[pw_invisibility] > 4*32
  649. || viewplayer->powers[pw_invisibility] & 8)
  650. vis->colormap = NULL; // shadow draw
  651. else if (fixedcolormap)
  652. vis->colormap = fixedcolormap; // fixed color
  653. else if (psp->state->frame & FF_FULLBRIGHT)
  654. vis->colormap = fullcolormap; // full bright // killough 3/20/98
  655. else
  656. // add a fudge factor to better match the original game
  657. vis->colormap = R_ColourMap(lightlevel,
  658. FixedMul(pspritescale, 0x2b000)); // local light
  659. // proff 11/99: don't use software stuff in OpenGL
  660. if (V_GetMode() != VID_MODEGL)
  661. {
  662. R_DrawVisSprite(vis, vis->x1, vis->x2);
  663. }
  664. #ifdef GL_DOOM
  665. else
  666. {
  667. int finallightlevel;
  668. sector_t tmpsec;
  669. int floorlightlevel, ceilinglightlevel;
  670. if ((vis->colormap==fixedcolormap) || (vis->colormap==fullcolormap))
  671. finallightlevel=255;
  672. else
  673. {
  674. // finallightlevel = (viewplayer->mo->subsector->sector->lightlevel) + (extralight << LIGHTSEGSHIFT);
  675. R_FakeFlat( viewplayer->mo->subsector->sector, &tmpsec,
  676. &floorlightlevel, &ceilinglightlevel, false);
  677. finallightlevel = ((floorlightlevel+ceilinglightlevel) >> 1) + (extralight << LIGHTSEGSHIFT);
  678. if (finallightlevel < 0)
  679. finallightlevel = 0;
  680. else if (finallightlevel >= 255)
  681. finallightlevel = 255;
  682. }
  683. gld_DrawWeapon(lump,vis,finallightlevel);
  684. }
  685. #endif
  686. }
  687. //
  688. // R_DrawPlayerSprites
  689. //
  690. void R_DrawPlayerSprites(void)
  691. {
  692. int i, lightlevel = viewplayer->mo->subsector->sector->lightlevel;
  693. pspdef_t *psp;
  694. // clip to screen bounds
  695. mfloorclip = screenheightarray;
  696. mceilingclip = negonearray;
  697. // add all active psprites
  698. for (i=0, psp=viewplayer->psprites; i<NUMPSPRITES; i++,psp++)
  699. if (psp->state)
  700. R_DrawPSprite (psp, lightlevel);
  701. }
  702. //
  703. // R_SortVisSprites
  704. //
  705. // Rewritten by Lee Killough to avoid using unnecessary
  706. // linked lists, and to use faster sorting algorithm.
  707. //
  708. #ifdef DJGPP
  709. // killough 9/22/98: inlined memcpy of pointer arrays
  710. // CPhipps - added memory as modified
  711. #define bcopyp(d, s, n) asm(" cld; rep; movsl;" :: "D"(d), "S"(s), "c"(n) : "%cc", "%esi", "%edi", "%ecx", "memory")
  712. #else
  713. #define bcopyp(d, s, n) memcpy(d, s, (n) * sizeof(void *))
  714. #endif
  715. // killough 9/2/98: merge sort
  716. static void msort(vissprite_t **s, vissprite_t **t, int n)
  717. {
  718. if (n >= 16)
  719. {
  720. int n1 = n/2, n2 = n - n1;
  721. vissprite_t **s1 = s, **s2 = s + n1, **d = t;
  722. msort(s1, t, n1);
  723. msort(s2, t, n2);
  724. while ((*s1)->scale > (*s2)->scale ?
  725. (*d++ = *s1++, --n1) : (*d++ = *s2++, --n2));
  726. if (n2)
  727. bcopyp(d, s2, n2);
  728. else
  729. bcopyp(d, s1, n1);
  730. bcopyp(s, t, n);
  731. }
  732. else
  733. {
  734. int i;
  735. for (i = 1; i < n; i++)
  736. {
  737. vissprite_t *temp = s[i];
  738. if (s[i-1]->scale < temp->scale)
  739. {
  740. int j = i;
  741. while ((s[j] = s[j-1])->scale < temp->scale && --j);
  742. s[j] = temp;
  743. }
  744. }
  745. }
  746. }
  747. void R_SortVisSprites (void)
  748. {
  749. if (num_vissprite)
  750. {
  751. int i = num_vissprite;
  752. // If we need to allocate more pointers for the vissprites,
  753. // allocate as many as were allocated for sprites -- killough
  754. // killough 9/22/98: allocate twice as many
  755. if (num_vissprite_ptrs < num_vissprite*2)
  756. {
  757. free(vissprite_ptrs); // better than realloc -- no preserving needed
  758. vissprite_ptrs = malloc((num_vissprite_ptrs = num_vissprite_alloc*2)
  759. * sizeof *vissprite_ptrs);
  760. }
  761. while (--i>=0)
  762. vissprite_ptrs[i] = vissprites+i;
  763. // killough 9/22/98: replace qsort with merge sort, since the keys
  764. // are roughly in order to begin with, due to BSP rendering.
  765. msort(vissprite_ptrs, vissprite_ptrs + num_vissprite, num_vissprite);
  766. }
  767. }
  768. //
  769. // R_DrawSprite
  770. //
  771. static void R_DrawSprite (vissprite_t* spr)
  772. {
  773. drawseg_t *ds;
  774. int clipbot[MAX_SCREENWIDTH]; // killough 2/8/98: // dropoff overflow
  775. int cliptop[MAX_SCREENWIDTH]; // change to MAX_* // dropoff overflow
  776. int x;
  777. int r1;
  778. int r2;
  779. fixed_t scale;
  780. fixed_t lowscale;
  781. for (x = spr->x1 ; x<=spr->x2 ; x++)
  782. clipbot[x] = cliptop[x] = -2;
  783. // Scan drawsegs from end to start for obscuring segs.
  784. // The first drawseg that has a greater scale is the clip seg.
  785. // Modified by Lee Killough:
  786. // (pointer check was originally nonportable
  787. // and buggy, by going past LEFT end of array):
  788. // for (ds=ds_p-1 ; ds >= drawsegs ; ds--) old buggy code
  789. for (ds=ds_p ; ds-- > drawsegs ; ) // new -- killough
  790. { // determine if the drawseg obscures the sprite
  791. if (ds->x1 > spr->x2 || ds->x2 < spr->x1 ||
  792. (!ds->silhouette && !ds->maskedtexturecol))
  793. continue; // does not cover sprite
  794. r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1;
  795. r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2;
  796. if (ds->scale1 > ds->scale2)
  797. {
  798. lowscale = ds->scale2;
  799. scale = ds->scale1;
  800. }
  801. else
  802. {
  803. lowscale = ds->scale1;
  804. scale = ds->scale2;
  805. }
  806. if (scale < spr->scale || (lowscale < spr->scale &&
  807. !R_PointOnSegSide (spr->gx, spr->gy, ds->curline)))
  808. {
  809. if (ds->maskedtexturecol) // masked mid texture?
  810. R_RenderMaskedSegRange(ds, r1, r2);
  811. continue; // seg is behind sprite
  812. }
  813. // clip this piece of the sprite
  814. // killough 3/27/98: optimized and made much shorter
  815. if (ds->silhouette&SIL_BOTTOM && spr->gz < ds->bsilheight) //bottom sil
  816. for (x=r1 ; x<=r2 ; x++)
  817. if (clipbot[x] == -2)
  818. clipbot[x] = ds->sprbottomclip[x];
  819. if (ds->silhouette&SIL_TOP && spr->gzt > ds->tsilheight) // top sil
  820. for (x=r1 ; x<=r2 ; x++)
  821. if (cliptop[x] == -2)
  822. cliptop[x] = ds->sprtopclip[x];
  823. }
  824. // killough 3/27/98:
  825. // Clip the sprite against deep water and/or fake ceilings.
  826. // killough 4/9/98: optimize by adding mh
  827. // killough 4/11/98: improve sprite clipping for underwater/fake ceilings
  828. // killough 11/98: fix disappearing sprites
  829. if (spr->heightsec != -1) // only things in specially marked sectors
  830. {
  831. fixed_t h,mh;
  832. int phs = viewplayer->mo->subsector->sector->heightsec;
  833. if ((mh = sectors[spr->heightsec].floorheight) > spr->gz &&
  834. (h = centeryfrac - FixedMul(mh-=viewz, spr->scale)) >= 0 &&
  835. (h >>= FRACBITS) < viewheight) {
  836. if (mh <= 0 || (phs != -1 && viewz > sectors[phs].floorheight))
  837. { // clip bottom
  838. for (x=spr->x1 ; x<=spr->x2 ; x++)
  839. if (clipbot[x] == -2 || h < clipbot[x])
  840. clipbot[x] = h;
  841. }
  842. else // clip top
  843. if (phs != -1 && viewz <= sectors[phs].floorheight) // killough 11/98
  844. for (x=spr->x1 ; x<=spr->x2 ; x++)
  845. if (cliptop[x] == -2 || h > cliptop[x])
  846. cliptop[x] = h;
  847. }
  848. if ((mh = sectors[spr->heightsec].ceilingheight) < spr->gzt &&
  849. (h = centeryfrac - FixedMul(mh-viewz, spr->scale)) >= 0 &&
  850. (h >>= FRACBITS) < viewheight) {
  851. if (phs != -1 && viewz >= sectors[phs].ceilingheight)
  852. { // clip bottom
  853. for (x=spr->x1 ; x<=spr->x2 ; x++)
  854. if (clipbot[x] == -2 || h < clipbot[x])
  855. clipbot[x] = h;
  856. }
  857. else // clip top
  858. for (x=spr->x1 ; x<=spr->x2 ; x++)
  859. if (cliptop[x] == -2 || h > cliptop[x])
  860. cliptop[x] = h;
  861. }
  862. }
  863. // killough 3/27/98: end special clipping for deep water / fake ceilings
  864. // all clipping has been performed, so draw the sprite
  865. // check for unclipped columns
  866. for (x = spr->x1 ; x<=spr->x2 ; x++) {
  867. if (clipbot[x] == -2)
  868. clipbot[x] = viewheight;
  869. if (cliptop[x] == -2)
  870. cliptop[x] = -1;
  871. }
  872. mfloorclip = clipbot;
  873. mceilingclip = cliptop;
  874. R_DrawVisSprite (spr, spr->x1, spr->x2);
  875. }
  876. //
  877. // R_DrawMasked
  878. //
  879. void R_DrawMasked(void)
  880. {
  881. int i;
  882. drawseg_t *ds;
  883. R_SortVisSprites();
  884. // draw all vissprites back to front
  885. rendered_vissprites = num_vissprite;
  886. for (i = num_vissprite ;--i>=0; )
  887. R_DrawSprite(vissprite_ptrs[i]); // killough
  888. // render any remaining masked mid textures
  889. // Modified by Lee Killough:
  890. // (pointer check was originally nonportable
  891. // and buggy, by going past LEFT end of array):
  892. // for (ds=ds_p-1 ; ds >= drawsegs ; ds--) old buggy code
  893. for (ds=ds_p ; ds-- > drawsegs ; ) // new -- killough
  894. if (ds->maskedtexturecol)
  895. R_RenderMaskedSegRange(ds, ds->x1, ds->x2);
  896. // draw the psprites on top of everything
  897. // but does not draw on side views
  898. if (!viewangleoffset)
  899. R_DrawPlayerSprites ();
  900. }