From cd25b4be4f4667456f68aa2f3f80b09cf9903f6c Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 15 Dec 2018 10:04:49 +0100 Subject: [PATCH] - 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. --- src/p_effect.cpp | 45 +++++++++----------------- src/p_effect.h | 2 +- src/polyrenderer/scene/poly_scene.cpp | 2 +- src/swrenderer/scene/r_opaque_pass.cpp | 2 +- 4 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/p_effect.cpp b/src/p_effect.cpp index aeb5b0ec88..da43890c5d 100644 --- a/src/p_effect.cpp +++ b/src/p_effect.cpp @@ -59,10 +59,9 @@ FRandom pr_railtrail("RailTrail"); #define FADEFROMTTL(a) (1.f/(a)) // [RH] particle globals -uint16_t NumParticles; -uint16_t ActiveParticles; -uint16_t InactiveParticles; -particle_t *Particles; +uint32_t ActiveParticles; +uint32_t InactiveParticles; +TArray Particles; TArray ParticlesInSubsec; static int grey1, grey2, grey3, grey4, red, green, blue, yellow, black, @@ -105,13 +104,13 @@ static const struct ColorList { inline particle_t *NewParticle (void) { - particle_t *result = NULL; + particle_t *result = nullptr; if (InactiveParticles != NO_PARTICLE) { - result = Particles + InactiveParticles; + result = &Particles[InactiveParticles]; InactiveParticles = result->tnext; result->tnext = ActiveParticles; - ActiveParticles = uint16_t(result - Particles); + ActiveParticles = uint32_t(result - Particles.Data()); } return result; } @@ -120,7 +119,6 @@ inline particle_t *NewParticle (void) // [RH] Particle functions // void P_InitParticles (); -void P_DeinitParticles (); // [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!). @@ -135,7 +133,6 @@ CUSTOM_CVAR( Int, r_maxparticles, 4000, CVAR_ARCHIVE ) if ( gamestate != GS_STARTUP ) { - P_DeinitParticles( ); P_InitParticles( ); } } @@ -152,33 +149,21 @@ void P_InitParticles () num = r_maxparticles; // This should be good, but eh... - NumParticles = (uint16_t)clamp(num, 100, 65535); + int NumParticles = clamp(num, 100, 65535); - P_DeinitParticles(); - Particles = new particle_t[NumParticles]; + Particles.Resize(NumParticles); P_ClearParticles (); - atterm (P_DeinitParticles); -} - -void P_DeinitParticles() -{ - if (Particles != NULL) - { - delete[] Particles; - Particles = NULL; - } } void P_ClearParticles () { - int i; - - memset (Particles, 0, NumParticles * sizeof(particle_t)); + int i = 0; + memset (Particles.Data(), 0, Particles.Size() * sizeof(particle_t)); ActiveParticles = NO_PARTICLE; InactiveParticles = 0; - for (i = 0; i < NumParticles-1; i++) - Particles[i].tnext = i + 1; - Particles[i].tnext = NO_PARTICLE; + for (auto &p : Particles) + p.tnext = ++i; + Particles.Last().tnext = NO_PARTICLE; } // Group particles by subsectors. Because particles are always @@ -255,7 +240,7 @@ void P_ThinkParticles () prev = NULL; while (i != NO_PARTICLE) { - particle = Particles + i; + particle = &Particles[i]; i = particle->tnext; if (!particle->notimefreeze && ((bglobal.freeze) || (level.flags2 & LEVEL2_FROZEN))) { @@ -274,7 +259,7 @@ void P_ThinkParticles () else ActiveParticles = i; particle->tnext = InactiveParticles; - InactiveParticles = (int)(particle - Particles); + InactiveParticles = (int)(particle - Particles.Data()); continue; } diff --git a/src/p_effect.h b/src/p_effect.h index 4620bfa5a9..00689118ac 100644 --- a/src/p_effect.h +++ b/src/p_effect.h @@ -62,7 +62,7 @@ struct particle_t uint16_t snext; }; -extern particle_t *Particles; +extern TArray Particles; extern TArray ParticlesInSubsec; const uint16_t NO_PARTICLE = 0xffff; diff --git a/src/polyrenderer/scene/poly_scene.cpp b/src/polyrenderer/scene/poly_scene.cpp index 2f5e367023..f92c73762e 100644 --- a/src/polyrenderer/scene/poly_scene.cpp +++ b/src/polyrenderer/scene/poly_scene.cpp @@ -189,7 +189,7 @@ void RenderPolyScene::RenderSubsector(PolyRenderThread *thread, subsector_t *sub int subsectorIndex = sub->Index(); 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(particle, sub, subsectorDepth, CurrentViewpoint->StencilValue)); } } diff --git a/src/swrenderer/scene/r_opaque_pass.cpp b/src/swrenderer/scene/r_opaque_pass.cpp index 1b03361ef1..cff29cbb43 100644 --- a/src/swrenderer/scene/r_opaque_pass.cpp +++ b/src/swrenderer/scene/r_opaque_pass.cpp @@ -616,7 +616,7 @@ namespace swrenderer 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) { - RenderParticle::Project(Thread, Particles + i, sub->sector, shade, FakeSide, foggy); + RenderParticle::Project(Thread, &Particles[i], sub->sector, shade, FakeSide, foggy); } }