- use a TArray to store the particles and remove all 16 bit global variables.

This means one less exit function to deal with - and these days 16 bit variables are a pointless attempt at saving space.
This commit is contained in:
Christoph Oelckers 2018-12-15 10:04:49 +01:00
parent 3d0fb6cf90
commit cd25b4be4f
4 changed files with 18 additions and 33 deletions

View File

@ -59,10 +59,9 @@ FRandom pr_railtrail("RailTrail");
#define FADEFROMTTL(a) (1.f/(a)) #define FADEFROMTTL(a) (1.f/(a))
// [RH] particle globals // [RH] particle globals
uint16_t NumParticles; uint32_t ActiveParticles;
uint16_t ActiveParticles; uint32_t InactiveParticles;
uint16_t InactiveParticles; TArray<particle_t> Particles;
particle_t *Particles;
TArray<uint16_t> ParticlesInSubsec; TArray<uint16_t> ParticlesInSubsec;
static int grey1, grey2, grey3, grey4, red, green, blue, yellow, black, static int grey1, grey2, grey3, grey4, red, green, blue, yellow, black,
@ -105,13 +104,13 @@ static const struct ColorList {
inline particle_t *NewParticle (void) inline particle_t *NewParticle (void)
{ {
particle_t *result = NULL; particle_t *result = nullptr;
if (InactiveParticles != NO_PARTICLE) if (InactiveParticles != NO_PARTICLE)
{ {
result = Particles + InactiveParticles; result = &Particles[InactiveParticles];
InactiveParticles = result->tnext; InactiveParticles = result->tnext;
result->tnext = ActiveParticles; result->tnext = ActiveParticles;
ActiveParticles = uint16_t(result - Particles); ActiveParticles = uint32_t(result - Particles.Data());
} }
return result; return result;
} }
@ -120,7 +119,6 @@ inline particle_t *NewParticle (void)
// [RH] Particle functions // [RH] Particle functions
// //
void P_InitParticles (); void P_InitParticles ();
void P_DeinitParticles ();
// [BC] Allow the maximum number of particles to be specified by a cvar (so people // [BC] Allow the maximum number of particles to be specified by a cvar (so people
// with lots of nice hardware can have lots of particles!). // with lots of nice hardware can have lots of particles!).
@ -135,7 +133,6 @@ CUSTOM_CVAR( Int, r_maxparticles, 4000, CVAR_ARCHIVE )
if ( gamestate != GS_STARTUP ) if ( gamestate != GS_STARTUP )
{ {
P_DeinitParticles( );
P_InitParticles( ); P_InitParticles( );
} }
} }
@ -152,33 +149,21 @@ void P_InitParticles ()
num = r_maxparticles; num = r_maxparticles;
// This should be good, but eh... // This should be good, but eh...
NumParticles = (uint16_t)clamp<int>(num, 100, 65535); int NumParticles = clamp<int>(num, 100, 65535);
P_DeinitParticles(); Particles.Resize(NumParticles);
Particles = new particle_t[NumParticles];
P_ClearParticles (); P_ClearParticles ();
atterm (P_DeinitParticles);
}
void P_DeinitParticles()
{
if (Particles != NULL)
{
delete[] Particles;
Particles = NULL;
}
} }
void P_ClearParticles () void P_ClearParticles ()
{ {
int i; int i = 0;
memset (Particles.Data(), 0, Particles.Size() * sizeof(particle_t));
memset (Particles, 0, NumParticles * sizeof(particle_t));
ActiveParticles = NO_PARTICLE; ActiveParticles = NO_PARTICLE;
InactiveParticles = 0; InactiveParticles = 0;
for (i = 0; i < NumParticles-1; i++) for (auto &p : Particles)
Particles[i].tnext = i + 1; p.tnext = ++i;
Particles[i].tnext = NO_PARTICLE; Particles.Last().tnext = NO_PARTICLE;
} }
// Group particles by subsectors. Because particles are always // Group particles by subsectors. Because particles are always
@ -255,7 +240,7 @@ void P_ThinkParticles ()
prev = NULL; prev = NULL;
while (i != NO_PARTICLE) while (i != NO_PARTICLE)
{ {
particle = Particles + i; particle = &Particles[i];
i = particle->tnext; i = particle->tnext;
if (!particle->notimefreeze && ((bglobal.freeze) || (level.flags2 & LEVEL2_FROZEN))) if (!particle->notimefreeze && ((bglobal.freeze) || (level.flags2 & LEVEL2_FROZEN)))
{ {
@ -274,7 +259,7 @@ void P_ThinkParticles ()
else else
ActiveParticles = i; ActiveParticles = i;
particle->tnext = InactiveParticles; particle->tnext = InactiveParticles;
InactiveParticles = (int)(particle - Particles); InactiveParticles = (int)(particle - Particles.Data());
continue; continue;
} }

View File

@ -62,7 +62,7 @@ struct particle_t
uint16_t snext; uint16_t snext;
}; };
extern particle_t *Particles; extern TArray<particle_t> Particles;
extern TArray<uint16_t> ParticlesInSubsec; extern TArray<uint16_t> ParticlesInSubsec;
const uint16_t NO_PARTICLE = 0xffff; const uint16_t NO_PARTICLE = 0xffff;

View File

@ -189,7 +189,7 @@ void RenderPolyScene::RenderSubsector(PolyRenderThread *thread, subsector_t *sub
int subsectorIndex = sub->Index(); int subsectorIndex = sub->Index();
for (int i = ParticlesInSubsec[subsectorIndex]; i != NO_PARTICLE; i = Particles[i].snext) for (int i = ParticlesInSubsec[subsectorIndex]; i != NO_PARTICLE; i = Particles[i].snext)
{ {
particle_t *particle = Particles + i; particle_t *particle = &Particles[i];
thread->TranslucentObjects.push_back(thread->FrameMemory->NewObject<PolyTranslucentParticle>(particle, sub, subsectorDepth, CurrentViewpoint->StencilValue)); thread->TranslucentObjects.push_back(thread->FrameMemory->NewObject<PolyTranslucentParticle>(particle, sub, subsectorDepth, CurrentViewpoint->StencilValue));
} }
} }

View File

@ -616,7 +616,7 @@ namespace swrenderer
int shade = LightVisibility::LightLevelToShade((floorlightlevel + ceilinglightlevel) / 2 + LightVisibility::ActualExtraLight(foggy, Thread->Viewport.get()), foggy); int shade = LightVisibility::LightLevelToShade((floorlightlevel + ceilinglightlevel) / 2 + LightVisibility::ActualExtraLight(foggy, Thread->Viewport.get()), foggy);
for (int i = ParticlesInSubsec[sub->Index()]; i != NO_PARTICLE; i = Particles[i].snext) for (int i = ParticlesInSubsec[sub->Index()]; i != NO_PARTICLE; i = Particles[i].snext)
{ {
RenderParticle::Project(Thread, Particles + i, sub->sector, shade, FakeSide, foggy); RenderParticle::Project(Thread, &Particles[i], sub->sector, shade, FakeSide, foggy);
} }
} }