From a5c3e296e9546b5acabfc71cd5ffba64657ed7d9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 22 Feb 2020 23:39:30 +1100 Subject: [PATCH] reduce custom particles memory usage by 2kb on 32 bit, 9kb on 64 bit --- src/Particle.c | 59 ++++++++++++++++++++++++++++---------------------- src/Particle.h | 10 ++++----- src/Program.c | 4 ++-- src/Protocol.c | 34 ++++++++++++++--------------- 4 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/Particle.c b/src/Particle.c index db253e12c..428d5b297 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -298,28 +298,31 @@ static void Terrain_Tick(double delta) { *#########################################################################################################################*/ struct CustomParticle { struct Particle base; - struct CustomParticleProperty* prop; + int effectId; float totalLifespan; }; -struct CustomParticleProperty Particles_CustomEffects[256]; +struct CustomParticleEffect Particles_CustomEffects[256]; static struct CustomParticle custom_particles[PARTICLES_MAX]; static int custom_count; static cc_bool CustomParticle_Tick(struct CustomParticle* p, double delta) { + struct CustomParticleEffect* e = &Particles_CustomEffects[p->effectId]; particle_hitTerrain = false; - return Particle_PhysicsTick(&p->base, p->prop->gravity, false, delta) - || (particle_hitTerrain && p->prop->expireUponTouchingGround); + + return Particle_PhysicsTick(&p->base, e->gravity, false, delta) + || (particle_hitTerrain && e->expireUponTouchingGround); } static void CustomParticle_Render(struct CustomParticle* p, float t, VertexP3fT2fC4b* vertices) { + struct CustomParticleEffect* e = &Particles_CustomEffects[p->effectId]; Vec3 pos; Vec2 size; PackedCol col; - TextureRec rec = p->prop->rec; + TextureRec rec = e->rec; int x, y, z; - float frame_time = p->totalLifespan / p->prop->frameCount; + float frame_time = p->totalLifespan / e->frameCount; float inverted_lifetime = Math_AbsF(p->base.lifetime - p->totalLifespan); int curFrame = Math_Floor(inverted_lifetime / frame_time); float shiftU = curFrame * (rec.U2 - rec.U1); @@ -331,8 +334,8 @@ static void CustomParticle_Render(struct CustomParticle* p, float t, VertexP3fT2 size.X = p->base.size; size.Y = size.X; x = Math_Floor(pos.X); y = Math_Floor(pos.Y); z = Math_Floor(pos.Z); - col = p->prop->fullBright ? PACKEDCOL_WHITE : (World_Contains(x, y, z) ? Lighting_Col(x, y, z) : Env.SunCol); - col = PackedCol_Tint(col, p->prop->tint); + col = e->fullBright ? PACKEDCOL_WHITE : (World_Contains(x, y, z) ? Lighting_Col(x, y, z) : Env.SunCol); + col = PackedCol_Tint(col, e->tintCol); Particle_DoRender(&size, &pos, &rec, col, vertices); } @@ -502,24 +505,28 @@ void Particles_RainSnowEffect(float x, float y, float z) { void Particles_CustomEffect(int effectID, float x, float y, float z, float originX, float originY, float originZ) { struct CustomParticle* p; - struct CustomParticleProperty* prop = &Particles_CustomEffects[effectID]; - int i; - int count = prop->particleCount; + struct CustomParticleEffect* e = &Particles_CustomEffects[effectID]; + int i, count = e->particleCount; + Vec3 offset; + float d; for (i = 0; i < count; i++) { if (custom_count == PARTICLES_MAX) Custom_RemoveAt(0); p = &custom_particles[custom_count++]; - p->prop = prop; + p->effectId = effectID; - Vec3 offset = { Random_Float(&rnd) - 0.5f, Random_Float(&rnd) - 0.5f, Random_Float(&rnd) - 0.5f }; + offset.X = Random_Float(&rnd) - 0.5f; + offset.Y = Random_Float(&rnd) - 0.5f; + offset.Z = Random_Float(&rnd) - 0.5f; Vec3_Normalize(&offset, &offset); - float d = Random_Float(&rnd); - d = Math_Exp(Math_Log(d) / 3.0); - Vec3_Mul1By(&offset, d); - Vec3_Mul1By(&offset, p->prop->spread); - p->base.lastPos.X = x + (offset.X); - p->base.lastPos.Y = y + (offset.Y); - p->base.lastPos.Z = z + (offset.Z); + + d = Random_Float(&rnd); + d = Math_Exp(Math_Log(d) / 3.0); /* d^1/3 for better distribution */ + d *= e->spread; + + p->base.lastPos.X = x + offset.X * d; + p->base.lastPos.Y = y + offset.Y * d; + p->base.lastPos.Z = z + offset.Z * d; Vec3 origin = { originX, originY, originZ }; if (Vec3_Equals(&origin, &p->base.lastPos)) { @@ -531,16 +538,16 @@ void Particles_CustomEffect(int effectID, float x, float y, float z, float origi Vec3 diff; Vec3_Sub(&diff, &p->base.lastPos, &origin); Vec3_Normalize(&diff, &diff); - p->base.velocity.X = diff.X * p->prop->speed; - p->base.velocity.Y = diff.Y * p->prop->speed; - p->base.velocity.Z = diff.Z * p->prop->speed; + p->base.velocity.X = diff.X * e->speed; + p->base.velocity.Y = diff.Y * e->speed; + p->base.velocity.Z = diff.Z * e->speed; } - p->base.nextPos = p->base.lastPos; - p->base.lifetime = p->prop->baseLifetime + ( (p->prop->baseLifetime * p->prop->lifetimeVariation) * ((Random_Float(&rnd) - 0.5f) * 2)); + p->base.nextPos = p->base.lastPos; + p->base.lifetime = e->baseLifetime + (e->baseLifetime * e->lifetimeVariation) * ((Random_Float(&rnd) - 0.5f) * 2); p->totalLifespan = p->base.lifetime; - p->base.size = p->prop->size + ( (p->prop->size * p->prop->sizeVariation) * ((Random_Float(&rnd) - 0.5f) * 2) ) ; + p->base.size = e->size + (e->size * e->sizeVariation) * ((Random_Float(&rnd) - 0.5f) * 2); } } diff --git a/src/Particle.h b/src/Particle.h index e6d2c0fab..1d5dad4e8 100644 --- a/src/Particle.h +++ b/src/Particle.h @@ -19,9 +19,11 @@ struct Particle { struct CustomParticleEffect { TextureRec rec; - PackedCol tint; - int frameCount; - int particleCount; + PackedCol tintCol; + cc_uint8 frameCount; + cc_uint8 particleCount; + cc_bool expireUponTouchingGround; + cc_bool fullBright; float size; float sizeVariation; float spread; //how far from the spawnpoint their location can vary @@ -29,8 +31,6 @@ struct CustomParticleEffect { float gravity; float baseLifetime; //how long (in seconds) the particle lives for float lifetimeVariation; - cc_bool expireUponTouchingGround; - cc_bool fullBright; }; extern struct CustomParticleEffect Particles_CustomEffects[256]; diff --git a/src/Program.c b/src/Program.c index ee48a7a6e..1554d92da 100644 --- a/src/Program.c +++ b/src/Program.c @@ -107,9 +107,9 @@ static int Program_Run(int argc, char** argv) { int argsCount = Platform_GetCommandLineArgs(argc, argv, args); #ifdef _MSC_VER /* NOTE: Make sure to comment this out before pushing a commit */ - String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); + //String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); //String rawArgs = String_FromConst("UnknownShadow200"); - argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); + //argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); #endif if (argsCount == 0) { diff --git a/src/Protocol.c b/src/Protocol.c index 7929dc31a..69f55cfe8 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1341,28 +1341,28 @@ static void CPE_VelocityControl(cc_uint8* data) { } static void CPE_DefineEffect(cc_uint8* data) { - struct CustomParticleProperty* effect = &Particles_CustomEffects[data[0]]; + struct CustomParticleEffect* e = &Particles_CustomEffects[data[0]]; /* e.g. bounds of 0,0, 15,15 gives an 8x8 icon in the default 128x128 particles.png */ - effect->rec.U1 = data[1] / 256.0f; - effect->rec.V1 = data[2] / 256.0f; - effect->rec.U2 = (data[3] + 1) / 256.0f; - effect->rec.V2 = (data[4] + 1) / 256.0f; + e->rec.U1 = data[1] / 256.0f; + e->rec.V1 = data[2] / 256.0f; + e->rec.U2 = (data[3] + 1) / 256.0f; + e->rec.V2 = (data[4] + 1) / 256.0f; - effect->tint = PackedCol_Make(data[5], data[6], data[7], 255); - effect->frameCount = data[8]; - effect->particleCount = data[9]; - effect->size = data[10] / 32.0f; /* 32 units per block */ + e->tintCol = PackedCol_Make(data[5], data[6], data[7], 255); + e->frameCount = data[8]; + e->particleCount = data[9]; + e->size = data[10] / 32.0f; /* 32 units per block */ - effect->sizeVariation = (int)Stream_GetU32_BE(data + 11) / 10000.0f; - effect->spread = (int)Stream_GetU32_BE(data + 15) / 32.0f; /* TODO: should be 10000? */ - effect->speed = (int)Stream_GetU32_BE(data + 19) / 10000.0f; - effect->gravity = (int)Stream_GetU32_BE(data + 23) / 10000.0f; - effect->baseLifetime = (int)Stream_GetU32_BE(data + 27) / 10000.0f; - effect->lifetimeVariation = (int)Stream_GetU32_BE(data + 31) / 10000.0f; + e->sizeVariation = (int)Stream_GetU32_BE(data + 11) / 10000.0f; + e->spread = (int)Stream_GetU32_BE(data + 15) / 32.0f; /* TODO: should be 10000? */ + e->speed = (int)Stream_GetU32_BE(data + 19) / 10000.0f; + e->gravity = (int)Stream_GetU32_BE(data + 23) / 10000.0f; + e->baseLifetime = (int)Stream_GetU32_BE(data + 27) / 10000.0f; + e->lifetimeVariation = (int)Stream_GetU32_BE(data + 31) / 10000.0f; - effect->expireUponTouchingGround = data[35]; - effect->fullBright = data[36]; + e->expireUponTouchingGround = data[35]; + e->fullBright = data[36]; } static void CPE_SpawnEffect(cc_uint8* data) {