From 0b1e81023f78ab8b52294ba387a35986ef4fdac0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 25 Mar 2021 23:16:32 +0100 Subject: [PATCH] - added sprite collection loop to ProcessSector and did a bit of cleanup on the TSprite code. --- source/build/include/build.h | 32 ++++++++++-------- source/build/src/engine.cpp | 16 --------- source/build/src/engine_priv.h | 1 - source/build/src/polymost.cpp | 1 + .../core/rendering/scene/hw_bunchdrawer.cpp | 33 +++++++++++++++++-- source/core/rendering/scene/hw_bunchdrawer.h | 2 ++ source/games/blood/src/animatesprite.cpp | 8 ++--- source/games/sw/src/draw.cpp | 28 ---------------- 8 files changed, 57 insertions(+), 64 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 0196754cc..f28aea79c 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -161,25 +161,33 @@ extern sectortype sectorbackup[MAXSECTORS]; extern walltype wallbackup[MAXWALLS]; -static inline tspriteptr_t renderMakeTSpriteFromSprite(tspriteptr_t const tspr, uint16_t const spritenum) +inline tspriteptr_t renderAddNewTSprite() { - auto const spr = &sprite[spritenum]; - - *tspr = *spr; - - tspr->clipdist = 0; - tspr->owner = spritenum; - + auto tspr = &tsprite[spritesortcnt++]; + *tspr = {}; return tspr; } -static inline tspriteptr_t renderAddTSpriteFromSprite(uint16_t const spritenum) +inline tspriteptr_t renderAddTSpriteFromSprite(uint16_t const spritenum) { - return renderMakeTSpriteFromSprite(&tsprite[spritesortcnt++], spritenum); + auto tspr = &tsprite[spritesortcnt++]; + auto const spr = &sprite[spritenum]; + *tspr = *spr; + tspr->clipdist = 0; + tspr->owner = spritenum; + return tspr; +} + +// returns: 0=continue sprite collecting; +// 1=break out of sprite collecting; +inline int32_t renderAddTsprite(int16_t z, int16_t sectnum) +{ + if (spritesortcnt >= MAXSPRITESONSCREEN) return 1; + renderAddTSpriteFromSprite(z); + return 0; } -EXTERN tspriteptr_t tspriteptr[MAXSPRITESONSCREEN + 1]; EXTERN int32_t xdim, ydim; EXTERN int32_t yxaspect, viewingrange; @@ -192,8 +200,6 @@ EXTERN int32_t randomseed; EXTERN uint8_t paletteloaded; -EXTERN int32_t maxspritesonscreen; - enum { PALETTE_MAIN = 1<<0, PALETTE_SHADE = 1<<1, diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 42a0e4432..0de1ad172 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -172,20 +172,6 @@ char inpreparemirror = 0; // Internal Engine Functions // -// returns: 0=continue sprite collecting; -// 1=break out of sprite collecting; -int32_t renderAddTsprite(int16_t z, int16_t sectnum) -{ - auto const spr = (uspriteptr_t)&sprite[z]; - if (spritesortcnt >= maxspritesonscreen) - return 1; - - renderAddTSpriteFromSprite(z); - - - return 0; -} - // // animateoffs (internal) @@ -703,8 +689,6 @@ int32_t engineInit(void) g_visibility = 512; parallaxvisibility = 512; - maxspritesonscreen = MAXSPRITESONSCREEN; - GPalette.Init(MAXPALOOKUPS + 1); // one slot for each translation, plus a separate one for the base palettes. gi->loadPalette(); diff --git a/source/build/src/engine_priv.h b/source/build/src/engine_priv.h index e0008dfd8..6c094d2be 100644 --- a/source/build/src/engine_priv.h +++ b/source/build/src/engine_priv.h @@ -14,7 +14,6 @@ #define ENGINE_PRIV_H extern int32_t globalpal, globalfloorpal; -extern tspriteptr_t tspriteptr[MAXSPRITESONSCREEN + 1]; extern int32_t xdimen, xdimenscale, xdimscale, ydimen; extern float fxdimen; extern int32_t globalposx, globalposy, globalposz; diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp index 5a6435ec7..22eb3bdc1 100644 --- a/source/build/src/polymost.cpp +++ b/source/build/src/polymost.cpp @@ -61,6 +61,7 @@ static int16_t bunchfirst[MAXWALLSB], bunchlast[MAXWALLSB]; static int16_t numscans, numbunches; static int16_t maskwall[MAXWALLSB], maskwallcnt; static int16_t sectorborder[256]; +static tspriteptr_t tspriteptr[MAXSPRITESONSCREEN + 1]; diff --git a/source/core/rendering/scene/hw_bunchdrawer.cpp b/source/core/rendering/scene/hw_bunchdrawer.cpp index 9ab483aba..f49f70170 100644 --- a/source/core/rendering/scene/hw_bunchdrawer.cpp +++ b/source/core/rendering/scene/hw_bunchdrawer.cpp @@ -53,8 +53,13 @@ void BunchDrawer::Init(HWDrawInfo *_di, Clipper* c, vec2_t& view) clipper = c; viewx = view.x * (1/ 16.f); viewy = view.y * -(1/ 16.f); + iview = view; StartScene(); clipper->SetViewpoint(view); + + gcosang = bamang(di->Viewpoint.RotAngle).fcos(); + gsinang = bamang(di->Viewpoint.RotAngle).fsin(); + for (int i = 0; i < numwalls; i++) { // Precalculate the clip angles to avoid doing this repeatedly during level traversal. @@ -419,16 +424,40 @@ void BunchDrawer::ProcessSector(int sectnum) if (gotsector[sectnum]) return; gotsector.Set(sectnum); - Bsp.Clock(); - auto sect = §or[sectnum]; bool inbunch; binangle startangle; + SetupSprite.Clock(); + + int z; + SectIterator it(sectnum); + while ((z = it.NextIndex()) >= 0) + { + auto const spr = (uspriteptr_t)&sprite[z]; + + if ((spr->cstat & CSTAT_SPRITE_INVISIBLE) || spr->xrepeat == 0 || spr->yrepeat == 0) // skip invisible sprites + continue; + + int sx = spr->x - iview.x, sy = spr->y - int(iview.y); + + if ((spr->cstat & CSTAT_SPRITE_ALIGNMENT_MASK) || (hw_models && tile2model[spr->picnum].modelid >= 0) || ((sx * gcosang) + (sy * gsinang) > 0)) // is it behind the camera? (fixme!) + { + if ((spr->cstat & (CSTAT_SPRITE_ONE_SIDED | CSTAT_SPRITE_ALIGNMENT_MASK)) != (CSTAT_SPRITE_ONE_SIDED | CSTAT_SPRITE_ALIGNMENT_WALL + 16) || + (r_voxels && tiletovox[spr->picnum] >= 0 && voxmodels[tiletovox[spr->picnum]]) || + DMulScale(bcos(spr->ang), -sx, bsin(spr->ang), -sy, 6) > 0) + if (renderAddTsprite(z, sectnum)) + break; + } + } + SetupSprite.Unclock(); + + SetupFlat.Clock(); HWFlat flat; flat.ProcessSector(di, §or[sectnum]); SetupFlat.Unclock(); + Bsp.Clock(); //Todo: process subsectors inbunch = false; diff --git a/source/core/rendering/scene/hw_bunchdrawer.h b/source/core/rendering/scene/hw_bunchdrawer.h index a43f98b88..371eae9cc 100644 --- a/source/core/rendering/scene/hw_bunchdrawer.h +++ b/source/core/rendering/scene/hw_bunchdrawer.h @@ -24,6 +24,8 @@ class BunchDrawer TArray Bunches; TArray CompareData; double viewx, viewy; + vec2_t iview; + float gcosang, gsinang; FixedBitArray gotsector; private: diff --git a/source/games/blood/src/animatesprite.cpp b/source/games/blood/src/animatesprite.cpp index 536ae1d5d..322a64ad0 100644 --- a/source/games/blood/src/animatesprite.cpp +++ b/source/games/blood/src/animatesprite.cpp @@ -126,7 +126,7 @@ static tspritetype *viewAddEffect(int nTSprite, VIEW_EFFECT nViewEffect) { assert(nViewEffect >= 0 && nViewEffect < kViewEffectMax); auto pTSprite = &tsprite[nTSprite]; - if (gDetail < effectDetail[nViewEffect] || nTSprite >= maxspritesonscreen) return NULL; + if (gDetail < effectDetail[nViewEffect] || nTSprite >= MAXSPRITESONSCREEN) return NULL; switch (nViewEffect) { case VIEW_EFFECT_18: @@ -220,7 +220,7 @@ static tspritetype *viewAddEffect(int nTSprite, VIEW_EFFECT nViewEffect) { nAng = (nAng+1024)&2047; } - for (int i = 0; i < 5 && spritesortcnt < maxspritesonscreen; i++) + for (int i = 0; i < 5 && spritesortcnt < MAXSPRITESONSCREEN; i++) { int nSector = pTSprite->sectnum; auto pNSprite = viewInsertTSprite(nSector, 32767, NULL); @@ -433,7 +433,7 @@ void viewProcessSprites(int32_t cX, int32_t cY, int32_t cZ, int32_t cA, int32_t { // shift before interpolating to increase precision. int myclock = (PlayClock<<3) + MulScale(4<<3, smoothratio, 16); - assert(spritesortcnt <= maxspritesonscreen); + assert(spritesortcnt <= MAXSPRITESONSCREEN); gCameraAng = cA; int nViewSprites = spritesortcnt; for (int nTSprite = spritesortcnt-1; nTSprite >= 0; nTSprite--) @@ -633,7 +633,7 @@ void viewProcessSprites(int32_t cX, int32_t cY, int32_t cZ, int32_t cA, int32_t pTSprite->xrepeat = pTSprite->yrepeat = 0; } } - if (spritesortcnt >= maxspritesonscreen) continue; + if (spritesortcnt >= MAXSPRITESONSCREEN) continue; if (pTXSprite && pTXSprite->burnTime > 0) { pTSprite->shade = ClipRange(pTSprite->shade-16-QRandom(8), -128, 127); diff --git a/source/games/sw/src/draw.cpp b/source/games/sw/src/draw.cpp index 38a0ae41a..a2c38e6d7 100644 --- a/source/games/sw/src/draw.cpp +++ b/source/games/sw/src/draw.cpp @@ -1094,34 +1094,6 @@ void PrintSpriteInfo(PLAYERp pp) } -void SpriteSortList2D(int tx, int ty) -{ - SPRITEp sp; - int i; - int dist,a,b,c; - - spritesortcnt = 0; - for (i = 0; i < MAXSPRITES; i++) - { - if (sprite[i].statnum < MAXSTATUS) - { - sp = &sprite[i]; - - if (!TEST(sp->cstat, CSTAT_SPRITE_INVISIBLE) && - (sp->xrepeat > 0) && (sp->yrepeat > 0) && - (spritesortcnt < MAXSPRITESONSCREEN)) - { - DISTANCE(tx,ty,sp->x,sp->y,dist,a,b,c); - - if (dist < 22000) - { - renderAddTSpriteFromSprite(i); - } - } - } - } -} - void DrawCrosshair(PLAYERp pp) { extern bool CameraTestMode;