Skip to content

Commit

Permalink
Refactor a bit xrParticles
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Nov 3, 2017
1 parent 252a735 commit 906d998
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 95 deletions.
9 changes: 4 additions & 5 deletions src/xrParticles/particle_actions_collection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "stdafx.h"
#pragma hdrstop

#include "particle_actions_collection.h"
#include "particle_effect.h"
Expand Down Expand Up @@ -631,7 +630,7 @@ void PABounce::Execute(ParticleEffect* effect, const float dt, float& tm_max)
if (position.Within(pnext))
{
// See if we were inside on previous timestep.
BOOL pinside = position.Within(m.pos);
const bool pinside = position.Within(m.pos);

// Normal to surface. This works for a sphere. Isn't
// computed quite right, should extrapolate particle
Expand All @@ -640,10 +639,10 @@ void PABounce::Execute(ParticleEffect* effect, const float dt, float& tm_max)
n.normalize_safe();

// Compute tangential and normal components of velocity
float nmag = m.vel * n;
const float nmag = m.vel * n;

pVector vn(n * nmag); // Normal Vn = (V.N)N
pVector vt = m.vel - vn; // Tangent Vt = V - Vn
const pVector vn(n * nmag); // Normal Vn = (V.N)N
const pVector vt = m.vel - vn; // Tangent Vt = V - Vn

if (pinside)
{
Expand Down
8 changes: 4 additions & 4 deletions src/xrParticles/particle_actions_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct PARTICLES_API PABounce : public ParticleAction

struct PARTICLES_API PACopyVertexB : public ParticleAction
{
BOOL copy_pos; // True to copy pos to posB.
bool copy_pos; // True to copy pos to posB.

_METHODS;
};
Expand Down Expand Up @@ -106,7 +106,7 @@ struct PARTICLES_API PAJet : public ParticleAction
struct PARTICLES_API PAKillOld : public ParticleAction
{
float age_limit; // Exact age at which to kill particles.
BOOL kill_less_than; // True to kill particles less than limit.
bool kill_less_than; // True to kill particles less than limit.

_METHODS;
};
Expand Down Expand Up @@ -191,7 +191,7 @@ struct PARTICLES_API PAScatter : public ParticleAction

struct PARTICLES_API PASink : public ParticleAction
{
BOOL kill_inside; // True to dispose of particles *inside* domain
bool kill_inside; // True to dispose of particles *inside* domain
pDomain positionL; // Disposal region (in local space)
pDomain position; // Disposal region

Expand All @@ -200,7 +200,7 @@ struct PARTICLES_API PASink : public ParticleAction

struct PARTICLES_API PASinkVelocity : public ParticleAction
{
BOOL kill_inside; // True to dispose of particles with vel *inside* domain
bool kill_inside; // True to dispose of particles with vel *inside* domain
pDomain velocityL; // Disposal region (in local space)
pDomain velocity; // Disposal region

Expand Down
91 changes: 90 additions & 1 deletion src/xrParticles/particle_effect.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
#include "stdafx.h"
#pragma hdrstop

#include "particle_effect.h"

namespace PAPI
{
ParticleEffect::ParticleEffect(int mp)
{
owner = nullptr;
param = 0;
b_cb = nullptr;
d_cb = nullptr;
p_count = 0;
max_particles = mp;
particles_allocated = max_particles;

particles = xr_alloc<Particle>(max_particles);
//Msg("Allocated %u bytes (%u particles) with base address 0x%p", max_particles * sizeof(Particle), max_particles, particles);
}

ParticleEffect::~ParticleEffect() { xr_free(particles); }

int ParticleEffect::Resize(u32 max_count)
{
// Reducing max.
if (particles_allocated >= max_count)
{
max_particles = max_count;

// May have to kill particles.
if (p_count > max_particles)
p_count = max_particles;

return max_count;
}

// Allocate particles.
Particle* new_particles = xr_alloc<Particle>(max_count);
if (!new_particles)
{
// ERROR - Not enough memory. Just give all we've got.
max_particles = particles_allocated;
return max_particles;
}

//Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);

CopyMemory(new_particles, particles, p_count*sizeof(Particle));
xr_free(particles);

particles = new_particles;

max_particles = max_count;
particles_allocated = max_count;
return max_count;
}

void ParticleEffect::Remove(int i)
{
if (0 == p_count)
return;
Particle& m = particles[i];

if (d_cb)
d_cb(owner, param, m, i);

m = particles[--p_count]; // íå ìåíÿòü ïðàâèëî óäàëåíèÿ !!! (dependence ParticleGroup)
//Msg("pDel() : %u", p_count);
}

bool ParticleEffect::Add(const pVector& pos, const pVector& posB, const pVector& size, const pVector& rot,
const pVector& vel, u32 color, const float age /*= 0.0f*/, u16 frame /*= 0*/,
u16 flags /*= 0*/)
{
if (p_count >= max_particles)
return false;
Particle& P = particles[p_count];
P.pos = pos;
P.posB = posB;
P.size = size;
P.rot.x = rot.x;
P.vel = vel;
P.color = color;
P.age = age;
P.frame = frame;
P.flags.assign(flags);
if (b_cb)
b_cb(owner, param, P, p_count);
p_count++;
//Msg("pAdd() : %u", p_count);
return true;
}
} // namespace PAPI
92 changes: 7 additions & 85 deletions src/xrParticles/particle_effect.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once
#ifndef particle_effectH
#define particle_effectH

namespace PAPI
{
// A effect of particles - Info and an array of Particles
struct ParticleEffect
struct PARTICLES_API ParticleEffect
{
u32 p_count; // Number of particles currently existing.
u32 max_particles; // Max particles allowed in effect.
Expand All @@ -16,91 +14,15 @@ struct ParticleEffect
void* owner;
u32 param;

ParticleEffect(int mp)
{
owner = nullptr;
param = 0;
b_cb = nullptr;
d_cb = nullptr;
p_count = 0;
max_particles = mp;
particles_allocated = max_particles;
ParticleEffect(int mp);

particles = xr_alloc<Particle>(max_particles);
//Msg("Allocated %u bytes (%u particles) with base address 0x%p", max_particles * sizeof(Particle), max_particles, particles);
}
~ParticleEffect();

~ParticleEffect() { xr_free(particles); }
int Resize(u32 max_count);

int Resize(u32 max_count)
{
// Reducing max.
if (particles_allocated >= max_count)
{
max_particles = max_count;

// May have to kill particles.
if (p_count > max_particles)
p_count = max_particles;

return max_count;
}

// Allocate particles.
Particle* new_particles = xr_alloc<Particle>(max_count);
if (!new_particles)
{
// ERROR - Not enough memory. Just give all we've got.
max_particles = particles_allocated;
return max_particles;
}

//Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);

CopyMemory(new_particles, particles, p_count * sizeof(Particle));
xr_free(particles);

particles = new_particles;

max_particles = max_count;
particles_allocated = max_count;
return max_count;
}

void Remove(int i)
{
if (0 == p_count)
return;
Particle& m = particles[i];
if (d_cb)
d_cb(owner, param, m, i);
m = particles[--p_count]; // не менять правило удаления !!! (dependence ParticleGroup)
//Msg("pDel() : %u", p_count);
}
void Remove(int i);

bool Add(const pVector& pos, const pVector& posB, const pVector& size, const pVector& rot, const pVector& vel,
u32 color, const float age = 0.0f, u16 frame = 0, u16 flags = 0)
{
if (p_count >= max_particles)
return false;
Particle& P = particles[p_count];
P.pos = pos;
P.posB = posB;
P.size = size;
P.rot.x = rot.x;
P.vel = vel;
P.color = color;
P.age = age;
P.frame = frame;
P.flags.assign(flags);
if (b_cb)
b_cb(owner, param, P, p_count);
p_count++;
//Msg("pAdd() : %u", p_count);
return true;
}
u32 color, const float age = 0.0f, u16 frame = 0, u16 flags = 0);
};
};

//---------------------------------------------------------------------------
#endif
} // namespace PAPI

0 comments on commit 906d998

Please sign in to comment.